Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store category sorting for recent, favorites and uncategorized per account #1218

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
32369d5
Remove markdown syntax from title.
AlpAcA0072 May 10, 2021
970f4ab
Remove markdown syntax "# " from title. A simple version.
AlpAcA0072 May 11, 2021
60023e4
Remove markdown syntax "# " from title. A simple version.
AlpAcA0072 May 11, 2021
05c51c9
Remove markdown syntax "# " from title. A simple version.
AlpAcA0072 May 11, 2021
29e398c
Merge branch 'master' into master
AlpAcA0072 May 12, 2021
f33b67a
Remove markdown syntax "# " from title up to 6 times. A simple version.
AlpAcA0072 May 12, 2021
551aa3c
Merge remote-tracking branch 'origin/master'
AlpAcA0072 May 12, 2021
62d9863
[Store category sorting for recent, favorites and uncategorized per a…
AlpAcA0072 May 15, 2021
2f53701
[Store category sorting for recent, favorites and uncategorized per a…
AlpAcA0072 May 15, 2021
869f783
[Store category sorting for recent, favorites and uncategorized per a…
AlpAcA0072 May 16, 2021
ac6e07c
[Store category sorting for recent, favorites and uncategorized per a…
AlpAcA0072 May 16, 2021
7593f81
[Store category sorting for recent, favorites and uncategorized per a…
AlpAcA0072 May 16, 2021
acf797b
Merge branch 'master' into master
AlpAcA0072 May 16, 2021
995ae3a
Merge branch 'master' into master
AlpAcA0072 May 16, 2021
9e15412
[Store category sorting for recent, favorites and uncategorized per a…
AlpAcA0072 May 16, 2021
fea5839
[Store category sorting for recent, favorites and uncategorized per a…
AlpAcA0072 May 16, 2021
b5c8b4c
Merge branch 'master' into master
AlpAcA0072 May 16, 2021
c8f37eb
Merge branch 'master' into master
AlpAcA0072 May 17, 2021
b3fcd8e
[Store category sorting for recent, favorites and uncategorized per a…
AlpAcA0072 May 18, 2021
63a0110
Merge remote-tracking branch 'origin/master'
AlpAcA0072 May 18, 2021
5d8b44b
[Store category sorting for recent, favorites and uncategorized per a…
AlpAcA0072 May 18, 2021
9d626fa
[Store category sorting for recent, favorites and uncategorized per a…
AlpAcA0072 May 18, 2021
11bc09b
[Store category sorting for recent, favorites and uncategorized per a…
AlpAcA0072 May 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import it.niedermann.owncloud.notes.persistence.migration.Migration_20_21;
import it.niedermann.owncloud.notes.persistence.migration.Migration_21_22;
import it.niedermann.owncloud.notes.persistence.migration.Migration_22_23;
import it.niedermann.owncloud.notes.persistence.migration.Migration_23_24;
import it.niedermann.owncloud.notes.persistence.migration.Migration_9_10;

@Database(
Expand All @@ -43,7 +44,7 @@
CategoryOptions.class,
SingleNoteWidgetData.class,
NotesListWidgetData.class
}, version = 23
}, version = 24
)
@TypeConverters({Converters.class})
public abstract class NotesDatabase extends RoomDatabase {
Expand Down Expand Up @@ -78,7 +79,8 @@ private static NotesDatabase create(final Context context) {
new Migration_19_20(context),
new Migration_20_21(),
new Migration_21_22(context),
new Migration_22_23()
new Migration_22_23(),
new Migration_23_24(context)
)
.fallbackToDestructiveMigrationOnDowngrade()
.fallbackToDestructiveMigration()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ public List<Account> getAccounts() {
public void deleteAccount(@NonNull Account account) {
try {
ApiProvider.invalidateAPICache(AccountImporter.getSingleSignOnAccount(context, account.getAccountName()));
final SharedPreferences.Editor sp = PreferenceManager.getDefaultSharedPreferences(this.context).edit();
sp.remove(this.context.getString(R.string.action_sorting_method) + ' ' + this.context.getString(R.string.label_favorites) + account.getId());
sp.remove(this.context.getString(R.string.action_sorting_method) + ' ' + this.context.getString(R.string.action_uncategorized) + account.getId());
sp.remove(this.context.getString(R.string.action_sorting_method) + ' ' + this.context.getString(R.string.label_all_notes) + account.getId());
sp.apply();
} catch (NextcloudFilesAppAccountNotFoundException e) {
e.printStackTrace();
ApiProvider.invalidateAPICache();
Expand Down Expand Up @@ -608,18 +613,17 @@ public void modifyCategoryOrder(long accountId, @NonNull NavigationCategory sele
final Context ctx = context.getApplicationContext();
final SharedPreferences.Editor 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);
sp.putInt(ctx.getString(R.string.action_sorting_method) + ' ' + ctx.getString(R.string.label_favorites) + accountId, orderIndex);
break;
}
case UNCATEGORIZED: {
sp.putInt(ctx.getString(R.string.action_sorting_method) + ' ' + ctx.getString(R.string.action_uncategorized), orderIndex);
sp.putInt(ctx.getString(R.string.action_sorting_method) + ' ' + ctx.getString(R.string.action_uncategorized) + accountId, orderIndex);
break;
}
case RECENT: {
sp.putInt(ctx.getString(R.string.action_sorting_method) + ' ' + ctx.getString(R.string.label_all_notes), orderIndex);
sp.putInt(ctx.getString(R.string.action_sorting_method) + ' ' + ctx.getString(R.string.label_all_notes) + accountId, orderIndex);
break;
}
case DEFAULT_CATEGORY:
Expand Down Expand Up @@ -661,19 +665,19 @@ public void modifyCategoryOrder(long accountId, @NonNull NavigationCategory sele
public LiveData<CategorySortingMethod> getCategoryOrder(@NonNull NavigationCategory selectedCategory) {
final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
String prefKey;

final String accountId = String.valueOf(selectedCategory.getAccountId());
switch (selectedCategory.getType()) {
// TODO make this account specific
case RECENT: {
prefKey = context.getString(R.string.action_sorting_method) + ' ' + context.getString(R.string.label_all_notes);
prefKey = context.getString(R.string.action_sorting_method) + ' ' + context.getString(R.string.label_all_notes) + accountId;
break;
}
case FAVORITES: {
prefKey = context.getString(R.string.action_sorting_method) + ' ' + context.getString(R.string.label_favorites);
prefKey = context.getString(R.string.action_sorting_method) + ' ' + context.getString(R.string.label_favorites) + accountId;
break;
}
case UNCATEGORIZED: {
prefKey = context.getString(R.string.action_sorting_method) + ' ' + context.getString(R.string.action_uncategorized);
prefKey = context.getString(R.string.action_sorting_method) + ' ' + context.getString(R.string.action_uncategorized) + accountId;
break;
}
case DEFAULT_CATEGORY:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package it.niedermann.owncloud.notes.persistence.migration;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;

import androidx.annotation.NonNull;
import androidx.preference.PreferenceManager;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

import it.niedermann.owncloud.notes.R;

/**
* Add account ID to sharedPreferences and thus make meta category account aware.
* https://github.com/stefan-niedermann/nextcloud-notes/issues/1169
*/
public class Migration_23_24 extends Migration {
@NonNull
private final Context context;

public Migration_23_24(@NonNull Context context) {
super(23, 24);
this.context = context;
}

@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
final Cursor cursor = database.query("SELECT id FROM ACCOUNT", null);
final int COLUMN_POSITION_ID = cursor.getColumnIndex("id");

while (cursor.moveToNext()) {
long accountId = cursor.getLong(COLUMN_POSITION_ID);
resetSharedPreferences(sharedPreferences, editor, context.getString(R.string.meta_category_uncategorized), accountId);
resetSharedPreferences(sharedPreferences, editor, context.getString(R.string.meta_category_favorites), accountId);
resetSharedPreferences(sharedPreferences, editor, context.getString(R.string.meta_category_all_notes), accountId);
}

final String categorySorting = context.getString(R.string.category_sorting);

editor.remove(categorySorting + ' ' + context.getString(R.string.meta_category_uncategorized));
editor.remove(categorySorting + ' ' + context.getString(R.string.meta_category_favorites));
editor.remove(categorySorting + ' ' + context.getString(R.string.meta_category_all_notes));
editor.apply();
cursor.close();
}

private void resetSharedPreferences(SharedPreferences sharedPreferences, SharedPreferences.Editor editor, String label, long accountId) {
final String categorySorting = context.getString(R.string.category_sorting);
final String key = categorySorting + ' ' + label;
if (sharedPreferences.contains(key)) {
int sortingMethod = sharedPreferences.getInt(key, 0);
editor.putInt(key + accountId, sortingMethod);
}
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
<string name="menu_preview">Preview</string>
<string name="menu_share">Share</string>

<string name="category_sorting" translatable="false">Sorting method</string>
<string name="meta_category_uncategorized" translatable="false">Uncategorized</string>
<string name="meta_category_favorites" translatable="false">Favorites</string>
<string name="meta_category_all_notes" translatable="false">All notes</string>

<string name="search_in_category">Search in %1$s</string>
<string name="search_in_all">Search all notes</string>

Expand Down