#831 Migrate from SQLiteOpenHelper to Room

This commit is contained in:
Stefan Niedermann 2020-10-08 10:13:38 +02:00
parent 31586c7ddd
commit a6cecbe2c2
9 changed files with 39 additions and 30 deletions

View file

@ -32,6 +32,7 @@ import it.niedermann.owncloud.notes.main.items.list.NoteViewHolderWithoutExcerpt
import it.niedermann.owncloud.notes.main.items.section.SectionItem; import it.niedermann.owncloud.notes.main.items.section.SectionItem;
import it.niedermann.owncloud.notes.main.items.section.SectionViewHolder; import it.niedermann.owncloud.notes.main.items.section.SectionViewHolder;
import it.niedermann.owncloud.notes.persistence.entity.Note; import it.niedermann.owncloud.notes.persistence.entity.Note;
import it.niedermann.owncloud.notes.persistence.entity.NoteWithCategory;
import it.niedermann.owncloud.notes.shared.model.Item; import it.niedermann.owncloud.notes.shared.model.Item;
import it.niedermann.owncloud.notes.shared.model.NoteClickListener; import it.niedermann.owncloud.notes.shared.model.NoteClickListener;
@ -160,7 +161,7 @@ public class ItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> i
case TYPE_NOTE_WITH_EXCERPT: case TYPE_NOTE_WITH_EXCERPT:
case TYPE_NOTE_WITHOUT_EXCERPT: case TYPE_NOTE_WITHOUT_EXCERPT:
case TYPE_NOTE_ONLY_TITLE: { case TYPE_NOTE_ONLY_TITLE: {
((NoteViewHolder) holder).bind((Note) itemList.get(position), showCategory, mainColor, textColor, searchQuery); ((NoteViewHolder) holder).bind((NoteWithCategory) itemList.get(position), showCategory, mainColor, textColor, searchQuery);
break; break;
} }
} }
@ -224,9 +225,9 @@ public class ItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> i
throw new IllegalArgumentException("Item at position " + position + " must not be null"); throw new IllegalArgumentException("Item at position " + position + " must not be null");
} }
if (getItem(position).isSection()) return TYPE_SECTION; if (getItem(position).isSection()) return TYPE_SECTION;
Note note = (Note) getItem(position); NoteWithCategory note = (NoteWithCategory) getItem(position);
if (TextUtils.isEmpty(note.getExcerpt())) { if (TextUtils.isEmpty(note.getNote().getExcerpt())) {
if (TextUtils.isEmpty(note.getCategory())) { if (TextUtils.isEmpty(note.getNote().getCategory())) {
return TYPE_NOTE_ONLY_TITLE; return TYPE_NOTE_ONLY_TITLE;
} else { } else {
return TYPE_NOTE_WITHOUT_EXCERPT; return TYPE_NOTE_WITHOUT_EXCERPT;

View file

@ -30,6 +30,7 @@ import it.niedermann.owncloud.notes.NotesApplication;
import it.niedermann.owncloud.notes.R; import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.branding.BrandingUtil; import it.niedermann.owncloud.notes.branding.BrandingUtil;
import it.niedermann.owncloud.notes.persistence.entity.Note; import it.niedermann.owncloud.notes.persistence.entity.Note;
import it.niedermann.owncloud.notes.persistence.entity.NoteWithCategory;
import it.niedermann.owncloud.notes.shared.model.DBStatus; import it.niedermann.owncloud.notes.shared.model.DBStatus;
import it.niedermann.owncloud.notes.shared.model.NoteClickListener; import it.niedermann.owncloud.notes.shared.model.NoteClickListener;
@ -49,7 +50,7 @@ public abstract class NoteViewHolder extends RecyclerView.ViewHolder {
} }
@CallSuper @CallSuper
public void bind(@NonNull Note note, boolean showCategory, int mainColor, int textColor, @Nullable CharSequence searchQuery) { public void bind(@NonNull NoteWithCategory note, boolean showCategory, int mainColor, int textColor, @Nullable CharSequence searchQuery) {
itemView.setOnClickListener((view) -> noteClickListener.onNoteClick(getAdapterPosition(), view)); itemView.setOnClickListener((view) -> noteClickListener.onNoteClick(getAdapterPosition(), view));
itemView.setOnLongClickListener((view) -> noteClickListener.onNoteLongClick(getAdapterPosition(), view)); itemView.setOnLongClickListener((view) -> noteClickListener.onNoteLongClick(getAdapterPosition(), view));
} }

View file

@ -13,6 +13,7 @@ import androidx.annotation.Px;
import it.niedermann.owncloud.notes.databinding.ItemNotesListNoteItemGridBinding; import it.niedermann.owncloud.notes.databinding.ItemNotesListNoteItemGridBinding;
import it.niedermann.owncloud.notes.main.items.NoteViewHolder; import it.niedermann.owncloud.notes.main.items.NoteViewHolder;
import it.niedermann.owncloud.notes.persistence.entity.Note; import it.niedermann.owncloud.notes.persistence.entity.Note;
import it.niedermann.owncloud.notes.persistence.entity.NoteWithCategory;
import it.niedermann.owncloud.notes.shared.model.NoteClickListener; import it.niedermann.owncloud.notes.shared.model.NoteClickListener;
import static android.view.View.GONE; import static android.view.View.GONE;
@ -39,8 +40,9 @@ public class NoteViewGridHolder extends NoteViewHolder {
throw new UnsupportedOperationException(NoteViewGridHolder.class.getSimpleName() + " does not support swiping"); throw new UnsupportedOperationException(NoteViewGridHolder.class.getSimpleName() + " does not support swiping");
} }
public void bind(@NonNull Note note, boolean showCategory, int mainColor, int textColor, @Nullable CharSequence searchQuery) { public void bind(@NonNull NoteWithCategory noteWithCategory, boolean showCategory, int mainColor, int textColor, @Nullable CharSequence searchQuery) {
super.bind(note, showCategory, mainColor, textColor, searchQuery); super.bind(noteWithCategory, showCategory, mainColor, textColor, searchQuery);
Note note = noteWithCategory.getNote();
@NonNull final Context context = itemView.getContext(); @NonNull final Context context = itemView.getContext();
bindCategory(context, binding.noteCategory, showCategory, note.getCategory(), mainColor); bindCategory(context, binding.noteCategory, showCategory, note.getCategory(), mainColor);
bindStatus(binding.noteStatus, note.getStatus(), mainColor); bindStatus(binding.noteStatus, note.getStatus(), mainColor);

View file

@ -12,6 +12,7 @@ import androidx.annotation.Px;
import it.niedermann.owncloud.notes.databinding.ItemNotesListNoteItemGridOnlyTitleBinding; import it.niedermann.owncloud.notes.databinding.ItemNotesListNoteItemGridOnlyTitleBinding;
import it.niedermann.owncloud.notes.main.items.NoteViewHolder; import it.niedermann.owncloud.notes.main.items.NoteViewHolder;
import it.niedermann.owncloud.notes.persistence.entity.Note; import it.niedermann.owncloud.notes.persistence.entity.Note;
import it.niedermann.owncloud.notes.persistence.entity.NoteWithCategory;
import it.niedermann.owncloud.notes.shared.model.NoteClickListener; import it.niedermann.owncloud.notes.shared.model.NoteClickListener;
public class NoteViewGridHolderOnlyTitle extends NoteViewHolder { public class NoteViewGridHolderOnlyTitle extends NoteViewHolder {
@ -32,12 +33,12 @@ public class NoteViewGridHolderOnlyTitle extends NoteViewHolder {
throw new UnsupportedOperationException(NoteViewGridHolderOnlyTitle.class.getSimpleName() + " does not support swiping"); throw new UnsupportedOperationException(NoteViewGridHolderOnlyTitle.class.getSimpleName() + " does not support swiping");
} }
public void bind(@NonNull Note note, boolean showCategory, int mainColor, int textColor, @Nullable CharSequence searchQuery) { public void bind(@NonNull NoteWithCategory note, boolean showCategory, int mainColor, int textColor, @Nullable CharSequence searchQuery) {
super.bind(note, showCategory, mainColor, textColor, searchQuery); super.bind(note, showCategory, mainColor, textColor, searchQuery);
@NonNull final Context context = itemView.getContext(); @NonNull final Context context = itemView.getContext();
bindStatus(binding.noteStatus, note.getStatus(), mainColor); bindStatus(binding.noteStatus, note.getNote().getStatus(), mainColor);
bindFavorite(binding.noteFavorite, note.getFavorite()); bindFavorite(binding.noteFavorite, note.getNote().getFavorite());
bindSearchableContent(context, binding.noteTitle, searchQuery, note.getTitle(), mainColor); bindSearchableContent(context, binding.noteTitle, searchQuery, note.getNote().getTitle(), mainColor);
} }
@Nullable @Nullable

View file

@ -10,6 +10,7 @@ import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.databinding.ItemNotesListNoteItemWithExcerptBinding; import it.niedermann.owncloud.notes.databinding.ItemNotesListNoteItemWithExcerptBinding;
import it.niedermann.owncloud.notes.main.items.NoteViewHolder; import it.niedermann.owncloud.notes.main.items.NoteViewHolder;
import it.niedermann.owncloud.notes.persistence.entity.Note; import it.niedermann.owncloud.notes.persistence.entity.Note;
import it.niedermann.owncloud.notes.persistence.entity.NoteWithCategory;
import it.niedermann.owncloud.notes.shared.model.DBStatus; import it.niedermann.owncloud.notes.shared.model.DBStatus;
import it.niedermann.owncloud.notes.shared.model.NoteClickListener; import it.niedermann.owncloud.notes.shared.model.NoteClickListener;
@ -28,8 +29,9 @@ public class NoteViewHolderWithExcerpt extends NoteViewHolder {
binding.noteSwipeFrame.setBackgroundResource(left ? R.color.bg_warning : R.color.bg_attention); binding.noteSwipeFrame.setBackgroundResource(left ? R.color.bg_warning : R.color.bg_attention);
} }
public void bind(@NonNull Note note, boolean showCategory, int mainColor, int textColor, @Nullable CharSequence searchQuery) { public void bind(@NonNull NoteWithCategory noteWithCategory, boolean showCategory, int mainColor, int textColor, @Nullable CharSequence searchQuery) {
super.bind(note, showCategory, mainColor, textColor, searchQuery); super.bind(noteWithCategory, showCategory, mainColor, textColor, searchQuery);
Note note = noteWithCategory.getNote();
@NonNull final Context context = itemView.getContext(); @NonNull final Context context = itemView.getContext();
binding.noteSwipeable.setAlpha(DBStatus.LOCAL_DELETED.equals(note.getStatus()) ? 0.5f : 1.0f); binding.noteSwipeable.setAlpha(DBStatus.LOCAL_DELETED.equals(note.getStatus()) ? 0.5f : 1.0f);
bindCategory(context, binding.noteCategory, showCategory, note.getCategory(), mainColor); bindCategory(context, binding.noteCategory, showCategory, note.getCategory(), mainColor);

View file

@ -10,6 +10,7 @@ import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.databinding.ItemNotesListNoteItemWithoutExcerptBinding; import it.niedermann.owncloud.notes.databinding.ItemNotesListNoteItemWithoutExcerptBinding;
import it.niedermann.owncloud.notes.main.items.NoteViewHolder; import it.niedermann.owncloud.notes.main.items.NoteViewHolder;
import it.niedermann.owncloud.notes.persistence.entity.Note; import it.niedermann.owncloud.notes.persistence.entity.Note;
import it.niedermann.owncloud.notes.persistence.entity.NoteWithCategory;
import it.niedermann.owncloud.notes.shared.model.DBStatus; import it.niedermann.owncloud.notes.shared.model.DBStatus;
import it.niedermann.owncloud.notes.shared.model.NoteClickListener; import it.niedermann.owncloud.notes.shared.model.NoteClickListener;
@ -28,8 +29,9 @@ public class NoteViewHolderWithoutExcerpt extends NoteViewHolder {
binding.noteSwipeFrame.setBackgroundResource(left ? R.color.bg_warning : R.color.bg_attention); binding.noteSwipeFrame.setBackgroundResource(left ? R.color.bg_warning : R.color.bg_attention);
} }
public void bind(@NonNull Note note, boolean showCategory, int mainColor, int textColor, @Nullable CharSequence searchQuery) { public void bind(@NonNull NoteWithCategory noteWithCategory, boolean showCategory, int mainColor, int textColor, @Nullable CharSequence searchQuery) {
super.bind(note, showCategory, mainColor, textColor, searchQuery); super.bind(noteWithCategory, showCategory, mainColor, textColor, searchQuery);
Note note = noteWithCategory.getNote();
@NonNull final Context context = itemView.getContext(); @NonNull final Context context = itemView.getContext();
binding.noteSwipeable.setAlpha(DBStatus.LOCAL_DELETED.equals(note.getStatus()) ? 0.5f : 1.0f); binding.noteSwipeable.setAlpha(DBStatus.LOCAL_DELETED.equals(note.getStatus()) ? 0.5f : 1.0f);
bindCategory(context, binding.noteCategory, showCategory, note.getCategory(), mainColor); bindCategory(context, binding.noteCategory, showCategory, note.getCategory(), mainColor);

View file

@ -15,6 +15,7 @@ import java.util.List;
import it.niedermann.owncloud.notes.R; import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.main.items.section.SectionItem; import it.niedermann.owncloud.notes.main.items.section.SectionItem;
import it.niedermann.owncloud.notes.persistence.entity.Note; import it.niedermann.owncloud.notes.persistence.entity.Note;
import it.niedermann.owncloud.notes.persistence.entity.NoteWithCategory;
import it.niedermann.owncloud.notes.shared.model.OldCategory; import it.niedermann.owncloud.notes.shared.model.OldCategory;
import it.niedermann.owncloud.notes.shared.model.CategorySortingMethod; import it.niedermann.owncloud.notes.shared.model.CategorySortingMethod;
import it.niedermann.owncloud.notes.shared.model.Item; import it.niedermann.owncloud.notes.shared.model.Item;
@ -38,7 +39,7 @@ public class LoadNotesListTask extends AsyncTask<Void, Void, List<Item>> {
@Override @Override
protected List<Item> doInBackground(Void... voids) { protected List<Item> doInBackground(Void... voids) {
List<Note> noteList; List<NoteWithCategory> noteList;
NotesDatabase db = NotesDatabase.getInstance(context); NotesDatabase db = NotesDatabase.getInstance(context);
CategorySortingMethod sortingMethod = db.getCategoryOrder(accountId, category); CategorySortingMethod sortingMethod = db.getCategoryOrder(accountId, category);
noteList = db.getNoteDao().searchNotesSubcategory(accountId, searchQuery == null ? "%" : "%" + searchQuery + "%", category.category, Boolean.TRUE.equals(category.favorite), sortingMethod); noteList = db.getNoteDao().searchNotesSubcategory(accountId, searchQuery == null ? "%" : "%" + searchQuery + "%", category.category, Boolean.TRUE.equals(category.favorite), sortingMethod);
@ -56,10 +57,10 @@ public class LoadNotesListTask extends AsyncTask<Void, Void, List<Item>> {
@NonNull @NonNull
@WorkerThread @WorkerThread
private List<Item> fillListByCategory(@NonNull List<Note> noteList) { private List<Item> fillListByCategory(@NonNull List<NoteWithCategory> noteList) {
List<Item> itemList = new ArrayList<>(); List<Item> itemList = new ArrayList<>();
String currentCategory = category.category; String currentCategory = category.category;
for (Note note : noteList) { for (NoteWithCategory note : noteList) {
if (currentCategory != null && !currentCategory.equals(note.getCategory())) { if (currentCategory != null && !currentCategory.equals(note.getCategory())) {
itemList.add(new SectionItem(NoteUtil.extendCategory(note.getCategory()))); itemList.add(new SectionItem(NoteUtil.extendCategory(note.getCategory())));
} }
@ -72,17 +73,17 @@ public class LoadNotesListTask extends AsyncTask<Void, Void, List<Item>> {
@NonNull @NonNull
@WorkerThread @WorkerThread
private List<Item> fillListByTime(@NonNull List<Note> noteList) { private List<Item> fillListByTime(@NonNull List<NoteWithCategory> noteList) {
List<Item> itemList = new ArrayList<>(); List<Item> itemList = new ArrayList<>();
Timeslotter timeslotter = new Timeslotter(); Timeslotter timeslotter = new Timeslotter();
String lastTimeslot = null; String lastTimeslot = null;
for (int i = 0; i < noteList.size(); i++) { for (int i = 0; i < noteList.size(); i++) {
Note currentNote = noteList.get(i); NoteWithCategory currentNote = noteList.get(i);
String timeslot = timeslotter.getTimeslot(currentNote); String timeslot = timeslotter.getTimeslot(currentNote.getNote());
if (i > 0 && !timeslot.equals(lastTimeslot)) { if (i > 0 && !timeslot.equals(lastTimeslot)) {
itemList.add(new SectionItem(timeslot)); itemList.add(new SectionItem(timeslot));
} }
itemList.add(currentNote); itemList.add(currentNote.getNote());
lastTimeslot = timeslot; lastTimeslot = timeslot;
} }
@ -91,12 +92,12 @@ public class LoadNotesListTask extends AsyncTask<Void, Void, List<Item>> {
@NonNull @NonNull
@WorkerThread @WorkerThread
private List<Item> fillListByInitials(@NonNull List<Note> noteList) { private List<Item> fillListByInitials(@NonNull List<NoteWithCategory> noteList) {
List<Item> itemList = new ArrayList<>(); List<Item> itemList = new ArrayList<>();
String lastInitials = null; String lastInitials = null;
for (int i = 0; i < noteList.size(); i++) { for (int i = 0; i < noteList.size(); i++) {
Note currentNote = noteList.get(i); NoteWithCategory currentNote = noteList.get(i);
String initials = currentNote.getTitle().substring(0, 1).toUpperCase(); String initials = currentNote.getNote().getTitle().substring(0, 1).toUpperCase();
if (!initials.matches("[A-Z\\u00C0-\\u00DF]")) { if (!initials.matches("[A-Z\\u00C0-\\u00DF]")) {
initials = initials.matches("[\\u0250-\\uFFFF]") ? context.getString(R.string.simple_other) : "#"; initials = initials.matches("[\\u0250-\\uFFFF]") ? context.getString(R.string.simple_other) : "#";
} }

View file

@ -100,7 +100,7 @@ public interface NoteDao {
@Query("SELECT *, CATEGORY.title as 'category' FROM NOTE INNER JOIN CATEGORY ON categoryId = CATEGORY.id WHERE NOTE.accountId = :accountId AND status != 'LOCAL_DELETED' AND ( " + @Query("SELECT *, CATEGORY.title as 'category' FROM NOTE INNER JOIN CATEGORY ON categoryId = CATEGORY.id WHERE NOTE.accountId = :accountId AND status != 'LOCAL_DELETED' AND ( " +
"NOTE.title LIKE :query OR content LIKE :query OR CATEGORY.title LIKE :query) AND (CATEGORY.title = :category OR CATEGORY.title LIKE :category + '/%' " + "NOTE.title LIKE :query OR content LIKE :query OR CATEGORY.title LIKE :query) AND (CATEGORY.title = :category OR CATEGORY.title LIKE :category + '/%' " +
") AND favorite = :favorite ORDER BY categoryId, favorite DESC, :sortingMethod") ") AND favorite = :favorite ORDER BY categoryId, favorite DESC, :sortingMethod")
List<Note> searchNotesSubcategory(long accountId, String query, String category, Boolean favorite, CategorySortingMethod sortingMethod); List<NoteWithCategory> searchNotesSubcategory(long accountId, String query, String category, Boolean favorite, CategorySortingMethod sortingMethod);
@Query("UPDATE NOTE SET remoteId = :remoteId WHERE id = :id") @Query("UPDATE NOTE SET remoteId = :remoteId WHERE id = :id")
void updateRemoteId(long id, long remoteId); void updateRemoteId(long id, long remoteId);

View file

@ -7,8 +7,7 @@ import it.niedermann.owncloud.notes.shared.model.Item;
public class NoteWithCategory implements Item { public class NoteWithCategory implements Item {
@Embedded @Embedded
private Note note; private Note note;
@Embedded(prefix = "category_") private String category;
private Category category;
public Note getNote() { public Note getNote() {
return note; return note;
@ -18,11 +17,11 @@ public class NoteWithCategory implements Item {
this.note = note; this.note = note;
} }
public Category getCategory() { public String getCategory() {
return category; return category;
} }
public void setCategory(Category category) { public void setCategory(String category) {
this.category = category; this.category = category;
} }
} }