Add sorting method to special categories

This commit is contained in:
IF-ACT 2020-05-23 21:15:17 +08:00
parent 87b3fb03ba
commit fdf0adb5b8
3 changed files with 75 additions and 40 deletions

View file

@ -618,16 +618,11 @@ public class NotesListViewActivity extends LockedActivity implements ItemAdapter
return;
}
MenuItem sortMethod = currentMenu.findItem(R.id.sorting_method);
if (navigationSelection.category != null && !navigationSelection.category.isEmpty()) {
sortMethod.setVisible(true);
CategorySortingMethod method = db.getCategoryOrderByTitle(localAccount.getId(), navigationSelection.category);
if (method == CategorySortingMethod.SORT_LEXICOGRAPHICAL_ASC) {
sortMethod.setIcon(R.drawable.alphabetical_asc);
} else {
sortMethod.setIcon(R.drawable.modification_desc);
}
CategorySortingMethod method = db.getCategoryOrderByTitle(localAccount.getId(), navigationSelection);
if (method == CategorySortingMethod.SORT_LEXICOGRAPHICAL_ASC) {
sortMethod.setIcon(R.drawable.alphabetical_asc);
} else {
sortMethod.setVisible(false);
sortMethod.setIcon(R.drawable.modification_desc);
}
}
@ -643,14 +638,14 @@ public class NotesListViewActivity extends LockedActivity implements ItemAdapter
if (item.getItemId() == R.id.sorting_method) {
Log.d("onOptionsItemSelected", navigationSelection.category + localAccount.getId());
method = db.getCategoryOrderByTitle(localAccount.getId(), navigationSelection.category);
method = db.getCategoryOrderByTitle(localAccount.getId(), navigationSelection);
if (method == CategorySortingMethod.SORT_LEXICOGRAPHICAL_ASC) {
method = CategorySortingMethod.SORT_MODIFIED_DESC;
} else {
method = CategorySortingMethod.SORT_LEXICOGRAPHICAL_ASC;
}
db.modifyCategoryOrderByTitle(localAccount.getId(), navigationSelection.category, method);
db.modifyCategoryOrderByTitle(localAccount.getId(), navigationSelection, method);
refreshLists();
updateSortMethodIcon();
return true;
@ -667,11 +662,7 @@ public class NotesListViewActivity extends LockedActivity implements ItemAdapter
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
currentMenu = menu;
if (localAccount != null) {
updateSortMethodIcon();
} else {
menu.findItem(R.id.sorting_method).setVisible(false);
}
updateSortMethodIcon();
return super.onPrepareOptionsMenu(menu);
}

View file

@ -52,11 +52,7 @@ public class LoadNotesListTask extends AsyncTask<Void, Void, List<Item>> {
protected List<Item> doInBackground(Void... voids) {
List<DBNote> noteList;
NotesDatabase db = NotesDatabase.getInstance(context);
if (category.category != null && !category.category.isEmpty()) {
noteList = db.searchNotes(accountId, searchQuery, category.category, category.favorite, db.getCategoryOrderByTitle(accountId, category.category));
} else {
noteList = db.searchNotes(accountId, searchQuery, category.category, category.favorite);
}
noteList = db.searchNotes(accountId, searchQuery, category.category, category.favorite, db.getCategoryOrderByTitle(accountId, category));
if (category.category == null) {
return fillListByTime(noteList);

View file

@ -3,6 +3,7 @@ package it.niedermann.owncloud.notes.persistence;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.content.res.Resources;
@ -19,6 +20,7 @@ import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;
import androidx.preference.PreferenceManager;
import com.nextcloud.android.sso.AccountImporter;
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException;
@ -43,6 +45,7 @@ import it.niedermann.owncloud.notes.android.appwidget.NoteListWidget;
import it.niedermann.owncloud.notes.android.appwidget.SingleNoteWidget;
import it.niedermann.owncloud.notes.model.ApiVersion;
import it.niedermann.owncloud.notes.model.Capabilities;
import it.niedermann.owncloud.notes.model.Category;
import it.niedermann.owncloud.notes.model.CloudNote;
import it.niedermann.owncloud.notes.model.DBNote;
import it.niedermann.owncloud.notes.model.DBStatus;
@ -978,26 +981,48 @@ public class NotesDatabase extends AbstractNotesDatabase {
}
/**
* This function is used to get the sorting method of a category by title.
* This function is used to get the sorting method of a category.
* The sorting method of the category can be used to decide
* to use which sorting method to show the notes for each categories.
*
* @param accountId The user accountID
* @param categoryTitle The category title
* @param category The category
* @return The sorting method in CategorySortingMethod enum format
*/
public CategorySortingMethod getCategoryOrderByTitle(long accountId, String categoryTitle) {
public CategorySortingMethod getCategoryOrderByTitle(long accountId, @NonNull Category category) {
validateAccountId(accountId);
long categoryId = getCategoryIdByTitle(accountId, categoryTitle);
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(table_category, new String[]{key_category_sorting_method},
key_category_id + " = ?", new String[]{String.valueOf(categoryId)},
null, null, null);
final Context ctx = getContext().getApplicationContext();
final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(ctx);
int orderIndex = 0;
while (cursor.moveToNext()) {
orderIndex = cursor.getInt(0);
if (category.category == null) {
if (category.favorite != null && category.favorite) {
// Favorite
orderIndex = sp.getInt(ctx.getString(R.string.action_sorting_method) +
' ' + ctx.getString(R.string.label_favorites),
0);
} else {
// All notes
orderIndex = sp.getInt(ctx.getString(R.string.action_sorting_method) +
' ' + ctx.getString(R.string.label_all_notes),
0);
}
} else if (category.category.isEmpty()) {
// Uncategorized
orderIndex = sp.getInt(ctx.getString(R.string.action_sorting_method) +
' ' + ctx.getString(R.string.action_uncategorized),
0);
} else {
long categoryId = getCategoryIdByTitle(accountId, category.category);
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(table_category, new String[]{key_category_sorting_method},
key_category_id + " = ?", new String[]{String.valueOf(categoryId)},
null, null, null);
while (cursor.moveToNext()) {
orderIndex = cursor.getInt(0);
}
}
return CategorySortingMethod.getCSM(orderIndex);
@ -1009,21 +1034,44 @@ public class NotesDatabase extends AbstractNotesDatabase {
* When the user changes the sorting method, this method should be called.
*
* @param accountId The user accountID
* @param categoryTitle The category title
* @param category The category
* @param sortingMethod The sorting method in CategorySortingMethod enum format
*/
public void modifyCategoryOrderByTitle(
long accountId, String categoryTitle, CategorySortingMethod sortingMethod) {
long accountId, @NonNull Category category, CategorySortingMethod sortingMethod) {
validateAccountId(accountId);
long categoryId = getCategoryIdByTitle(accountId, categoryTitle);
final Context ctx = getContext().getApplicationContext();
final SharedPreferences.Editor sp = PreferenceManager.getDefaultSharedPreferences(ctx).edit();
int orderIndex = sortingMethod.getCSMID();
if (category.category == null) {
if (category.favorite != null && category.favorite) {
// Favorite
sp.putInt(ctx.getString(R.string.action_sorting_method) +
' ' + ctx.getString(R.string.label_favorites),
orderIndex);
} else {
// All notes
sp.putInt(ctx.getString(R.string.action_sorting_method) +
' ' + ctx.getString(R.string.label_all_notes),
orderIndex);
}
} else if (category.category.isEmpty()) {
// Uncategorized
sp.putInt(ctx.getString(R.string.action_sorting_method) +
' ' + ctx.getString(R.string.action_uncategorized),
orderIndex);
} else {
long categoryId = getCategoryIdByTitle(accountId, category.category);
SQLiteDatabase db = getWritableDatabase();
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(key_category_sorting_method, sortingMethod.getCSMID());
db.update(table_category, values,
key_category_id + " = ?", new String[]{String.valueOf(categoryId)});
ContentValues values = new ContentValues();
values.put(key_category_sorting_method, sortingMethod.getCSMID());
db.update(table_category, values,
key_category_id + " = ?", new String[]{String.valueOf(categoryId)});
}
sp.apply();
}
}