Skip to content

Commit

Permalink
[Store category sorting for recent, favorites and uncategorized per a…
Browse files Browse the repository at this point in the history
…ccount](nextcloud#1169)
  • Loading branch information
AlpAcA0072 committed May 18, 2021
1 parent 63a0110 commit 5d8b44b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
CategoryOptions.class,
SingleNoteWidgetData.class,
NotesListWidgetData.class
}, version = 23
}, version = 24
)
@TypeConverters({Converters.class})
public abstract class NotesDatabase extends RoomDatabase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.util.Log;

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

import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.persistence.SyncWorker;
import it.niedermann.owncloud.notes.shared.model.CategorySortingMethod;

/**
* Add account ID to sharedPreferences and thus make meta category account aware.
Expand All @@ -31,24 +28,26 @@ public Migration_23_24(@NonNull Context context) {
public void migrate(@NonNull SupportSQLiteDatabase database) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
String tmpTableNotes = String.format("tmp_%s", "NOTES");
Cursor tmpNotesCursor = database.query("SELECT * FROM " + tmpTableNotes, null);
while (tmpNotesCursor.moveToNext()) {
int accountId = tmpNotesCursor.getInt(2);
Log.e("###", accountId + "");
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, R.string.action_uncategorized, accountId);
resetSharedPreferences(sharedPreferences, editor, R.string.label_favorites, accountId);
resetSharedPreferences(sharedPreferences, editor, R.string.label_all_notes, accountId);
}
editor.apply();
cursor.close();
}


private void resetSharedPreferences(SharedPreferences sharedPreferences, SharedPreferences.Editor editor, int label, int accountId) {
if (sharedPreferences.contains(context.getString(label) + ' ' + context.getString(label))) {
int sortingMethod = sharedPreferences.getInt(context.getString(R.string.action_sorting_method) + ' ' + context.getString(label), 0);
editor.remove(context.getString(R.string.action_sorting_method) + ' ' + context.getString(label));
editor.putInt(context.getString(R.string.action_sorting_method) + ' ' + context.getString(label) + accountId, sortingMethod);
private void resetSharedPreferences(SharedPreferences sharedPreferences, SharedPreferences.Editor editor, int label, long accountId) {
final String key = context.getString(R.string.action_sorting_method) + ' ' + context.getString(label);
if (sharedPreferences.contains(key)) {
int sortingMethod = sharedPreferences.getInt(key, 0);
editor.remove(key);
editor.putInt(key + accountId, sortingMethod);
}
}
}

0 comments on commit 5d8b44b

Please sign in to comment.