Search in sources :

Example 1 with CategoryOptions

use of it.niedermann.owncloud.notes.persistence.entity.CategoryOptions in project nextcloud-notes by stefan-niedermann.

the class NotesRepository method modifyCategoryOrder.

/**
 * Modifies the sorting method for one category, the category can be normal category or
 * one of "All notes", "Favorite", and "Uncategorized".
 * If category is one of these three, sorting method will be modified in android.content.SharedPreference.
 * The user can determine use which sorting method to show the notes for a category.
 * When the user changes the sorting method, this method should be called.
 *
 * @param accountId        The user accountID
 * @param selectedCategory The category to be modified
 * @param sortingMethod    The sorting method in {@link CategorySortingMethod} enum format
 */
@AnyThread
public void modifyCategoryOrder(long accountId, @NonNull NavigationCategory selectedCategory, @NonNull CategorySortingMethod sortingMethod) {
    executor.submit(() -> {
        final var ctx = context.getApplicationContext();
        final var sp = PreferenceManager.getDefaultSharedPreferences(ctx).edit();
        int orderIndex = sortingMethod.getId();
        switch(selectedCategory.getType()) {
            case FAVORITES:
                {
                    sp.putInt(ctx.getString(R.string.action_sorting_method) + ' ' + ctx.getString(R.string.label_favorites), orderIndex);
                    break;
                }
            case UNCATEGORIZED:
                {
                    sp.putInt(ctx.getString(R.string.action_sorting_method) + ' ' + ctx.getString(R.string.action_uncategorized), orderIndex);
                    break;
                }
            case RECENT:
                {
                    sp.putInt(ctx.getString(R.string.action_sorting_method) + ' ' + ctx.getString(R.string.label_all_notes), orderIndex);
                    break;
                }
            case DEFAULT_CATEGORY:
            default:
                {
                    final String category = selectedCategory.getCategory();
                    if (category != null) {
                        if (db.getCategoryOptionsDao().modifyCategoryOrder(accountId, category, sortingMethod) == 0) {
                            // Nothing updated means we didn't have this yet
                            final var categoryOptions = new CategoryOptions();
                            categoryOptions.setAccountId(accountId);
                            categoryOptions.setCategory(category);
                            categoryOptions.setSortingMethod(sortingMethod);
                            db.getCategoryOptionsDao().addCategoryOptions(categoryOptions);
                        }
                    } else {
                        throw new IllegalStateException("Tried to modify category order for " + ENavigationCategoryType.DEFAULT_CATEGORY + "but category is null.");
                    }
                    break;
                }
        }
        sp.apply();
    });
}
Also used : CategoryOptions(it.niedermann.owncloud.notes.persistence.entity.CategoryOptions) AnyThread(androidx.annotation.AnyThread)

Aggregations

AnyThread (androidx.annotation.AnyThread)1 CategoryOptions (it.niedermann.owncloud.notes.persistence.entity.CategoryOptions)1