#831 Migrate from SQLiteOpenHelper to Room

Fix category chooser
This commit is contained in:
Stefan Niedermann 2020-10-10 15:19:38 +02:00
parent b4a635bfe3
commit cb2da1db9d
6 changed files with 184 additions and 163 deletions

View file

@ -234,25 +234,26 @@ public class NotesNotesDatabaseTest {
@Test
@Ignore
public void test_05_searchCategories() {
List<NavigationItem.CategoryNavigationItem> categories = convertToCategoryNavigationItem(context, db.getCategoryDao().searchCategories(account.getId(), "Dia"));
boolean exitFlag = false;
for (NavigationItem categoryItem : categories) {
Log.i("Test_05_searchCategories_Dia", String.format("%s | %s | %d | %d", categoryItem.id, categoryItem.label, categoryItem.count, categoryItem.icon));
if (categoryItem.label.equals("Diary")) {
exitFlag = true;
}
}
assertTrue(exitFlag);
categories = convertToCategoryNavigationItem(context, db.getCategoryDao().searchCategories(account.getId(), "Mike Chester Wang"));
exitFlag = false;
for (NavigationItem categoryItem : categories) {
Log.i("Test_05_searchCategories_Item_Mike_Chester_Wang", String.format("%s | %s | %d | %d", categoryItem.id, categoryItem.label, categoryItem.count, categoryItem.icon));
if (categoryItem.label.equals("Diary")) {
exitFlag = true;
}
}
assertFalse(exitFlag);
// TODO LiveData
// List<NavigationItem.CategoryNavigationItem> categories = convertToCategoryNavigationItem(context, db.getCategoryDao().searchCategories(account.getId(), "Dia"));
// boolean exitFlag = false;
// for (NavigationItem categoryItem : categories) {
// Log.i("Test_05_searchCategories_Dia", String.format("%s | %s | %d | %d", categoryItem.id, categoryItem.label, categoryItem.count, categoryItem.icon));
// if (categoryItem.label.equals("Diary")) {
// exitFlag = true;
// }
// }
// assertTrue(exitFlag);
//
// categories = convertToCategoryNavigationItem(context, db.getCategoryDao().searchCategories(account.getId(), "Mike Chester Wang"));
// exitFlag = false;
// for (NavigationItem categoryItem : categories) {
// Log.i("Test_05_searchCategories_Item_Mike_Chester_Wang", String.format("%s | %s | %d | %d", categoryItem.id, categoryItem.label, categoryItem.count, categoryItem.icon));
// if (categoryItem.label.equals("Diary")) {
// exitFlag = true;
// }
// }
// assertFalse(exitFlag);
}
@Test
@ -385,52 +386,53 @@ public class NotesNotesDatabaseTest {
@Test
@Ignore
public void test_10_multiSearchCategories() {
List<NavigationItem.CategoryNavigationItem> categories = convertToCategoryNavigationItem(context, db.getCategoryDao().searchCategories(account.getId(), "M"));
int count = 0;
for (NavigationItem categoryItem : categories) {
Log.i("Test_10_multiSearchCategories_Item", String.format("%s | %s | %d | %d", categoryItem.id, categoryItem.label, categoryItem.count, categoryItem.icon));
count++;
}
assertEquals(3, count);
categories = convertToCategoryNavigationItem(context, db.getCategoryDao().searchCategories(account.getId(), "Mike"));
count = 0;
for (NavigationItem categoryItem : categories) {
Log.i("Test_10_multiSearchCategories_Item", String.format("%s | %s | %d | %d", categoryItem.id, categoryItem.label, categoryItem.count, categoryItem.icon));
count++;
}
assertEquals(0, count);
categories = convertToCategoryNavigationItem(context, db.getCategoryDao().searchCategories(account.getId(), "M"));
boolean exitFlag = false;
for (NavigationItem categoryItem : categories) {
Log.i("Test_10_multiSearchCategories_Item", String.format("%s | %s | %d | %d", categoryItem.id, categoryItem.label, categoryItem.count, categoryItem.icon));
if (categoryItem.label.equals("Music")) {
exitFlag = true;
}
}
assertTrue(exitFlag);
categories = convertToCategoryNavigationItem(context, db.getCategoryDao().searchCategories(account.getId(), "WOk"));
exitFlag = false;
for (NavigationItem categoryItem : categories) {
Log.i("Test_10_multiSearchCategories_Item", String.format("%s | %s | %d | %d", categoryItem.id, categoryItem.label, categoryItem.count, categoryItem.icon));
if (categoryItem.label.equals("Diary")) {
exitFlag = true;
}
}
assertFalse(exitFlag);
categories = convertToCategoryNavigationItem(context, db.getCategoryDao().searchCategories(account.getId(), "hello"));
exitFlag = false;
for (NavigationItem categoryItem : categories) {
Log.i("Test_10_multiSearchCategories_Item", String.format("%s | %s | %d | %d", categoryItem.id, categoryItem.label, categoryItem.count, categoryItem.icon));
if (categoryItem.label.equals("Diary")) {
exitFlag = true;
}
}
assertFalse(exitFlag);
// TODO LiveData
// List<NavigationItem.CategoryNavigationItem> categories = convertToCategoryNavigationItem(context, db.getCategoryDao().searchCategories(account.getId(), "M"));
// int count = 0;
// for (NavigationItem categoryItem : categories) {
// Log.i("Test_10_multiSearchCategories_Item", String.format("%s | %s | %d | %d", categoryItem.id, categoryItem.label, categoryItem.count, categoryItem.icon));
// count++;
// }
// assertEquals(3, count);
//
// categories = convertToCategoryNavigationItem(context, db.getCategoryDao().searchCategories(account.getId(), "Mike"));
// count = 0;
// for (NavigationItem categoryItem : categories) {
// Log.i("Test_10_multiSearchCategories_Item", String.format("%s | %s | %d | %d", categoryItem.id, categoryItem.label, categoryItem.count, categoryItem.icon));
// count++;
// }
// assertEquals(0, count);
//
// categories = convertToCategoryNavigationItem(context, db.getCategoryDao().searchCategories(account.getId(), "M"));
// boolean exitFlag = false;
// for (NavigationItem categoryItem : categories) {
// Log.i("Test_10_multiSearchCategories_Item", String.format("%s | %s | %d | %d", categoryItem.id, categoryItem.label, categoryItem.count, categoryItem.icon));
// if (categoryItem.label.equals("Music")) {
// exitFlag = true;
// }
// }
// assertTrue(exitFlag);
//
//
// categories = convertToCategoryNavigationItem(context, db.getCategoryDao().searchCategories(account.getId(), "WOk"));
// exitFlag = false;
// for (NavigationItem categoryItem : categories) {
// Log.i("Test_10_multiSearchCategories_Item", String.format("%s | %s | %d | %d", categoryItem.id, categoryItem.label, categoryItem.count, categoryItem.icon));
// if (categoryItem.label.equals("Diary")) {
// exitFlag = true;
// }
// }
// assertFalse(exitFlag);
//
// categories = convertToCategoryNavigationItem(context, db.getCategoryDao().searchCategories(account.getId(), "hello"));
// exitFlag = false;
// for (NavigationItem categoryItem : categories) {
// Log.i("Test_10_multiSearchCategories_Item", String.format("%s | %s | %d | %d", categoryItem.id, categoryItem.label, categoryItem.count, categoryItem.icon));
// if (categoryItem.label.equals("Diary")) {
// exitFlag = true;
// }
// }
// assertFalse(exitFlag);
}
@Test
@ -455,78 +457,79 @@ public class NotesNotesDatabaseTest {
@Test
@Ignore
public void test_12_Chinese() {
long accountID = account.getId();
NoteWithCategory cloudNote = new NoteWithCategory(new Note(1L, Calendar.getInstance(),
"美好的一天", getCurDate() + " 兄弟,这真是美好的一天。",
true, null), "日记");
// Pre-check
List<Note> notes = db.getNoteDao().getNotes(accountID);
int pre_size = notes.size();
Log.i("Test_12_Chinese_All_Notes_Before_Addition", "Size: " + pre_size);
// Add a new note
long noteID = db.addNote(accountID, cloudNote);
// Check if this note is added successfully
NoteWithCategory note = db.getNoteDao().getNoteWithCategory(accountID, noteID);
Log.i("Test_12_Chinese_Cur_Note", note.toString());
Log.i("Test_12_Chinese_Cur_Note", "Title: " + note.getTitle());
Log.i("Test_12_Chinese_Cur_Note", "Content: " + note.getContent());
Log.i("Test_12_Chinese_Cur_Note", "Category: " + note.getCategory());
assertEquals("美好的一天", note.getTitle());
assertEquals(cloudNote.getContent(), note.getContent());
assertEquals("日记", note.getCategory());
assertEquals(accountID, note.getAccountId().longValue());
// Check if this note is in all notes
notes = db.getNoteDao().getNotes(accountID);
int added_size = notes.size();
assertEquals(1, added_size - pre_size);
Log.i("Test_12_Chinese_All_Notes_Added", "Size: " + added_size);
for (Note cnote : notes) {
Log.i("Test_12_Chinese_All_Notes_Added", cnote.toString());
}
long thisAccountID = account.getId();
notes = db.getNoteDao().searchNotes(thisAccountID, "美好", "日记", true, null);
Log.i("Test_12_Chinese", "Size: " + notes.size());
assertEquals(1, notes.size());
List<NavigationItem.CategoryNavigationItem> categories = convertToCategoryNavigationItem(context, db.getCategoryDao().getCategories(account.getId()));
boolean exitFlag = false;
for (NavigationItem categoryItem : categories) {
Log.i("Test_12_Chinese_Item", String.format("%s | %s | %d | %d", categoryItem.id, categoryItem.label, categoryItem.count, categoryItem.icon));
if (categoryItem.label.equals("日记")) {
exitFlag = true;
}
}
assertTrue(exitFlag);
categories = convertToCategoryNavigationItem(context, db.getCategoryDao().searchCategories(account.getId(), ""));
exitFlag = false;
for (NavigationItem categoryItem : categories) {
Log.i("Test_12_Chinese_Item", String.format("%s | %s | %d | %d", categoryItem.id, categoryItem.label, categoryItem.count, categoryItem.icon));
if (categoryItem.label.equals("日记")) {
exitFlag = true;
}
}
assertTrue(exitFlag);
notes = db.getNoteDao().getNotes(thisAccountID);
for (Note cnote : notes) {
Log.i("Test_12_Chinese_All_Before_Deletion", cnote.toString());
// Delete the note after testing
db.deleteNote(cnote.getId(), cnote.getStatus());
}
// Check if the note is deleted successfully
notes = db.getNoteDao().getNotes(thisAccountID);
int deleted_size = notes.size();
assertEquals(1, added_size - deleted_size);
Log.i("Test_12_Chinese_All_Notes_After_Deletion", "Size: " + deleted_size);
// TODO LiveData
// long accountID = account.getId();
// NoteWithCategory cloudNote = new NoteWithCategory(new Note(1L, Calendar.getInstance(),
// "美好的一天", getCurDate() + " 兄弟,这真是美好的一天。",
// true, null), "日记");
//
// // Pre-check
// List<Note> notes = db.getNoteDao().getNotes(accountID);
// int pre_size = notes.size();
// Log.i("Test_12_Chinese_All_Notes_Before_Addition", "Size: " + pre_size);
//
// // Add a new note
// long noteID = db.addNote(accountID, cloudNote);
// // Check if this note is added successfully
// NoteWithCategory note = db.getNoteDao().getNoteWithCategory(accountID, noteID);
// Log.i("Test_12_Chinese_Cur_Note", note.toString());
// Log.i("Test_12_Chinese_Cur_Note", "Title: " + note.getTitle());
// Log.i("Test_12_Chinese_Cur_Note", "Content: " + note.getContent());
// Log.i("Test_12_Chinese_Cur_Note", "Category: " + note.getCategory());
//
// assertEquals("美好的一天", note.getTitle());
// assertEquals(cloudNote.getContent(), note.getContent());
// assertEquals("日记", note.getCategory());
// assertEquals(accountID, note.getAccountId().longValue());
//
// // Check if this note is in all notes
// notes = db.getNoteDao().getNotes(accountID);
// int added_size = notes.size();
//
// assertEquals(1, added_size - pre_size);
//
// Log.i("Test_12_Chinese_All_Notes_Added", "Size: " + added_size);
// for (Note cnote : notes) {
// Log.i("Test_12_Chinese_All_Notes_Added", cnote.toString());
// }
//
// long thisAccountID = account.getId();
// notes = db.getNoteDao().searchNotes(thisAccountID, "美好", "日记", true, null);
// Log.i("Test_12_Chinese", "Size: " + notes.size());
// assertEquals(1, notes.size());
//
// List<NavigationItem.CategoryNavigationItem> categories = convertToCategoryNavigationItem(context, db.getCategoryDao().getCategories(account.getId()));
// boolean exitFlag = false;
// for (NavigationItem categoryItem : categories) {
// Log.i("Test_12_Chinese_Item", String.format("%s | %s | %d | %d", categoryItem.id, categoryItem.label, categoryItem.count, categoryItem.icon));
// if (categoryItem.label.equals("日记")) {
// exitFlag = true;
// }
// }
// assertTrue(exitFlag);
//
// categories = convertToCategoryNavigationItem(context, db.getCategoryDao().searchCategories(account.getId(), ""));
// exitFlag = false;
// for (NavigationItem categoryItem : categories) {
// Log.i("Test_12_Chinese_Item", String.format("%s | %s | %d | %d", categoryItem.id, categoryItem.label, categoryItem.count, categoryItem.icon));
// if (categoryItem.label.equals("日记")) {
// exitFlag = true;
// }
// }
// assertTrue(exitFlag);
//
// notes = db.getNoteDao().getNotes(thisAccountID);
// for (Note cnote : notes) {
// Log.i("Test_12_Chinese_All_Before_Deletion", cnote.toString());
// // Delete the note after testing
// db.deleteNote(cnote.getId(), cnote.getStatus());
// }
//
// // Check if the note is deleted successfully
// notes = db.getNoteDao().getNotes(thisAccountID);
// int deleted_size = notes.size();
// assertEquals(1, added_size - deleted_size);
// Log.i("Test_12_Chinese_All_Notes_After_Deletion", "Size: " + deleted_size);
}
@Test

View file

@ -8,6 +8,7 @@ import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.recyclerview.widget.RecyclerView;
@ -101,7 +102,7 @@ public class CategoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
}
}
void setCategoryList(List<NavigationItem.CategoryNavigationItem> categories, String currentSearchString) {
void setCategoryList(List<NavigationItem.CategoryNavigationItem> categories, @Nullable String currentSearchString) {
this.categories.clear();
this.categories.addAll(categories);
final NavigationItem clearItem = new NavigationItem(clearItemId, context.getString(R.string.no_category), 0, R.drawable.ic_clear_grey_24dp);

View file

@ -2,9 +2,9 @@ package it.niedermann.owncloud.notes.edit.category;
import android.app.Dialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
@ -14,6 +14,8 @@ import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import java.util.List;
@ -41,8 +43,16 @@ public class CategoryDialogFragment extends BrandedDialogFragment {
private NotesDatabase db;
private CategoryDialogListener listener;
private CategoryAdapter adapter;
private EditText editCategory;
private LiveData<List<CategoryWithNotesCount>> categoryLiveData;
private Observer<List<CategoryWithNotesCount>> categoryObserver = (categories -> {
CharSequence searchTerm = editCategory.getText();
adapter.setCategoryList(convertToCategoryNavigationItem(requireContext(), categories), searchTerm == null ? null : searchTerm.toString());
});
@Override
public void applyBrand(int mainColor, int textColor) {
BrandingUtil.applyBrandToEditText(mainColor, textColor, binding.search);
@ -65,8 +75,6 @@ public class CategoryDialogFragment extends BrandedDialogFragment {
private long accountId;
private CategoryAdapter adapter;
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
@ -122,7 +130,10 @@ public class CategoryDialogFragment extends BrandedDialogFragment {
});
binding.recyclerView.setAdapter(adapter);
new LoadCategoriesTask().execute("");
categoryLiveData = db.getCategoryDao().searchCategories(accountId, "%");
categoryLiveData.observe(requireActivity(), categoryObserver);
editCategory.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
@ -136,7 +147,12 @@ public class CategoryDialogFragment extends BrandedDialogFragment {
@Override
public void afterTextChanged(Editable s) {
new LoadCategoriesTask().execute(editCategory.getText().toString());
final CharSequence searchTerm = editCategory.getText();
if (categoryLiveData != null) {
categoryLiveData.removeObservers(requireActivity());
}
categoryLiveData = db.getCategoryDao().searchCategories(accountId, TextUtils.isEmpty(searchTerm) ? "%" : "%" + searchTerm + "%");
categoryLiveData.observe(requireActivity(), categoryObserver);
}
});
@ -168,6 +184,14 @@ public class CategoryDialogFragment extends BrandedDialogFragment {
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (categoryLiveData != null) {
categoryLiveData.removeObservers(requireActivity());
}
}
public static DialogFragment newInstance(long accountId, String category) {
final DialogFragment categoryFragment = new CategoryDialogFragment();
final Bundle arguments = new Bundle();
@ -176,19 +200,4 @@ public class CategoryDialogFragment extends BrandedDialogFragment {
categoryFragment.setArguments(arguments);
return categoryFragment;
}
private class LoadCategoriesTask extends AsyncTask<String, Void, List<CategoryWithNotesCount>> {
String currentSearchString;
@Override
protected List<CategoryWithNotesCount> doInBackground(String... searchText) {
currentSearchString = searchText[0];
return db.getCategoryDao().searchCategories(accountId, currentSearchString);
}
@Override
protected void onPostExecute(List<CategoryWithNotesCount> categories) {
adapter.setCategoryList(convertToCategoryNavigationItem(requireContext(), categories), currentSearchString);
}
}
}

View file

@ -83,6 +83,6 @@ public interface CategoryDao {
@Query("SELECT CATEGORY.id, CATEGORY.title, COUNT(*) as 'totalNotes' FROM CATEGORY INNER JOIN NOTE ON categoryId = CATEGORY.id WHERE STATUS != 'LOCAL_DELETED' AND CATEGORY.accountId = :accountId GROUP BY CATEGORY.title")
LiveData<List<CategoryWithNotesCount>> getCategoriesLiveData(Long accountId);
@Query("SELECT CATEGORY.id, CATEGORY.title, COUNT(*) as 'totalNotes' FROM CATEGORY INNER JOIN NOTE ON categoryId = CATEGORY.id WHERE STATUS != 'LOCAL_DELETED' AND CATEGORY.accountId = :accountId AND CATEGORY.title LIKE '%' + TRIM(:searchTerm) + '%' GROUP BY CATEGORY.title")
List<CategoryWithNotesCount> searchCategories(Long accountId, String searchTerm);
@Query("SELECT CATEGORY.id, CATEGORY.title, COUNT(*) as 'totalNotes' FROM CATEGORY INNER JOIN NOTE ON categoryId = CATEGORY.id WHERE STATUS != 'LOCAL_DELETED' AND CATEGORY.accountId = :accountId AND CATEGORY.title LIKE :searchTerm GROUP BY CATEGORY.title")
LiveData<List<CategoryWithNotesCount>> searchCategories(Long accountId, String searchTerm);
}

View file

@ -174,6 +174,14 @@ public class Account {
this.textColor = textColor;
}
@NonNull
@Override
public String toString() {
return "Account{" +
"accountName='" + accountName + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;

View file

@ -109,13 +109,13 @@ public class Capabilities {
return textColor;
}
@NonNull
@Override
public String toString() {
return "Capabilities{" +
"apiVersion='" + apiVersion + '\'' +
", color='" + color + '\'' +
", textColor='" + textColor + '\'' +
", color=" + color +
", textColor=" + textColor +
", eTag='" + eTag + '\'' +
'}';
}
}