#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.SectionViewHolder;
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.NoteClickListener;
@ -160,7 +161,7 @@ public class ItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> i
case TYPE_NOTE_WITH_EXCERPT:
case TYPE_NOTE_WITHOUT_EXCERPT:
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;
}
}
@ -224,9 +225,9 @@ public class ItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> i
throw new IllegalArgumentException("Item at position " + position + " must not be null");
}
if (getItem(position).isSection()) return TYPE_SECTION;
Note note = (Note) getItem(position);
if (TextUtils.isEmpty(note.getExcerpt())) {
if (TextUtils.isEmpty(note.getCategory())) {
NoteWithCategory note = (NoteWithCategory) getItem(position);
if (TextUtils.isEmpty(note.getNote().getExcerpt())) {
if (TextUtils.isEmpty(note.getNote().getCategory())) {
return TYPE_NOTE_ONLY_TITLE;
} else {
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.branding.BrandingUtil;
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.NoteClickListener;
@ -49,7 +50,7 @@ public abstract class NoteViewHolder extends RecyclerView.ViewHolder {
}
@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.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.main.items.NoteViewHolder;
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 static android.view.View.GONE;
@ -39,8 +40,9 @@ public class NoteViewGridHolder extends NoteViewHolder {
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) {
super.bind(note, showCategory, mainColor, textColor, searchQuery);
public void bind(@NonNull NoteWithCategory noteWithCategory, boolean showCategory, int mainColor, int textColor, @Nullable CharSequence searchQuery) {
super.bind(noteWithCategory, showCategory, mainColor, textColor, searchQuery);
Note note = noteWithCategory.getNote();
@NonNull final Context context = itemView.getContext();
bindCategory(context, binding.noteCategory, showCategory, note.getCategory(), 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.main.items.NoteViewHolder;
import it.niedermann.owncloud.notes.persistence.entity.Note;
import it.niedermann.owncloud.notes.persistence.entity.NoteWithCategory;
import it.niedermann.owncloud.notes.shared.model.NoteClickListener;
public class NoteViewGridHolderOnlyTitle extends NoteViewHolder {
@ -32,12 +33,12 @@ public class NoteViewGridHolderOnlyTitle extends NoteViewHolder {
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);
@NonNull final Context context = itemView.getContext();
bindStatus(binding.noteStatus, note.getStatus(), mainColor);
bindFavorite(binding.noteFavorite, note.getFavorite());
bindSearchableContent(context, binding.noteTitle, searchQuery, note.getTitle(), mainColor);
bindStatus(binding.noteStatus, note.getNote().getStatus(), mainColor);
bindFavorite(binding.noteFavorite, note.getNote().getFavorite());
bindSearchableContent(context, binding.noteTitle, searchQuery, note.getNote().getTitle(), mainColor);
}
@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.main.items.NoteViewHolder;
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.NoteClickListener;
@ -28,8 +29,9 @@ public class NoteViewHolderWithExcerpt extends NoteViewHolder {
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) {
super.bind(note, showCategory, mainColor, textColor, searchQuery);
public void bind(@NonNull NoteWithCategory noteWithCategory, boolean showCategory, int mainColor, int textColor, @Nullable CharSequence searchQuery) {
super.bind(noteWithCategory, showCategory, mainColor, textColor, searchQuery);
Note note = noteWithCategory.getNote();
@NonNull final Context context = itemView.getContext();
binding.noteSwipeable.setAlpha(DBStatus.LOCAL_DELETED.equals(note.getStatus()) ? 0.5f : 1.0f);
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.main.items.NoteViewHolder;
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.NoteClickListener;
@ -28,8 +29,9 @@ public class NoteViewHolderWithoutExcerpt extends NoteViewHolder {
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) {
super.bind(note, showCategory, mainColor, textColor, searchQuery);
public void bind(@NonNull NoteWithCategory noteWithCategory, boolean showCategory, int mainColor, int textColor, @Nullable CharSequence searchQuery) {
super.bind(noteWithCategory, showCategory, mainColor, textColor, searchQuery);
Note note = noteWithCategory.getNote();
@NonNull final Context context = itemView.getContext();
binding.noteSwipeable.setAlpha(DBStatus.LOCAL_DELETED.equals(note.getStatus()) ? 0.5f : 1.0f);
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.main.items.section.SectionItem;
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.CategorySortingMethod;
import it.niedermann.owncloud.notes.shared.model.Item;
@ -38,7 +39,7 @@ public class LoadNotesListTask extends AsyncTask<Void, Void, List<Item>> {
@Override
protected List<Item> doInBackground(Void... voids) {
List<Note> noteList;
List<NoteWithCategory> noteList;
NotesDatabase db = NotesDatabase.getInstance(context);
CategorySortingMethod sortingMethod = db.getCategoryOrder(accountId, category);
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
@WorkerThread
private List<Item> fillListByCategory(@NonNull List<Note> noteList) {
private List<Item> fillListByCategory(@NonNull List<NoteWithCategory> noteList) {
List<Item> itemList = new ArrayList<>();
String currentCategory = category.category;
for (Note note : noteList) {
for (NoteWithCategory note : noteList) {
if (currentCategory != null && !currentCategory.equals(note.getCategory())) {
itemList.add(new SectionItem(NoteUtil.extendCategory(note.getCategory())));
}
@ -72,17 +73,17 @@ public class LoadNotesListTask extends AsyncTask<Void, Void, List<Item>> {
@NonNull
@WorkerThread
private List<Item> fillListByTime(@NonNull List<Note> noteList) {
private List<Item> fillListByTime(@NonNull List<NoteWithCategory> noteList) {
List<Item> itemList = new ArrayList<>();
Timeslotter timeslotter = new Timeslotter();
String lastTimeslot = null;
for (int i = 0; i < noteList.size(); i++) {
Note currentNote = noteList.get(i);
String timeslot = timeslotter.getTimeslot(currentNote);
NoteWithCategory currentNote = noteList.get(i);
String timeslot = timeslotter.getTimeslot(currentNote.getNote());
if (i > 0 && !timeslot.equals(lastTimeslot)) {
itemList.add(new SectionItem(timeslot));
}
itemList.add(currentNote);
itemList.add(currentNote.getNote());
lastTimeslot = timeslot;
}
@ -91,12 +92,12 @@ public class LoadNotesListTask extends AsyncTask<Void, Void, List<Item>> {
@NonNull
@WorkerThread
private List<Item> fillListByInitials(@NonNull List<Note> noteList) {
private List<Item> fillListByInitials(@NonNull List<NoteWithCategory> noteList) {
List<Item> itemList = new ArrayList<>();
String lastInitials = null;
for (int i = 0; i < noteList.size(); i++) {
Note currentNote = noteList.get(i);
String initials = currentNote.getTitle().substring(0, 1).toUpperCase();
NoteWithCategory currentNote = noteList.get(i);
String initials = currentNote.getNote().getTitle().substring(0, 1).toUpperCase();
if (!initials.matches("[A-Z\\u00C0-\\u00DF]")) {
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 ( " +
"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")
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")
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 {
@Embedded
private Note note;
@Embedded(prefix = "category_")
private Category category;
private String category;
public Note getNote() {
return note;
@ -18,11 +17,11 @@ public class NoteWithCategory implements Item {
this.note = note;
}
public Category getCategory() {
public String getCategory() {
return category;
}
public void setCategory(Category category) {
public void setCategory(String category) {
this.category = category;
}
}