From f0e9b046b1db65d6f3dd90d6802a6968eb8130b8 Mon Sep 17 00:00:00 2001 From: dkanada Date: Sun, 30 May 2021 19:18:27 +0900 Subject: [PATCH] fix issue with category preference --- .../gramophone/util/PreferenceUtil.java | 44 +++++++++++++------ 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/dkanada/gramophone/util/PreferenceUtil.java b/app/src/main/java/com/dkanada/gramophone/util/PreferenceUtil.java index 1c72a017..531c798e 100644 --- a/app/src/main/java/com/dkanada/gramophone/util/PreferenceUtil.java +++ b/app/src/main/java/com/dkanada/gramophone/util/PreferenceUtil.java @@ -6,8 +6,6 @@ import android.content.SharedPreferences; import android.os.Build; import android.preference.PreferenceManager; -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; import com.dkanada.gramophone.model.Theme; import com.dkanada.gramophone.R; import com.dkanada.gramophone.model.SortMethod; @@ -17,9 +15,7 @@ import com.dkanada.gramophone.model.Codec; import com.dkanada.gramophone.interfaces.base.PreferenceMigration; import com.dkanada.gramophone.fragments.player.NowPlayingScreen; -import java.lang.reflect.Type; import java.util.Arrays; -import java.util.Collections; import java.util.List; import java.util.Set; import java.util.stream.Collectors; @@ -105,7 +101,21 @@ public final class PreferenceUtil { } }; - private static final List migrations = Collections.singletonList(Migration1); + private static final PreferenceMigration Migration2 = new PreferenceMigration(1, 2) { + @Override + public void migrate(SharedPreferences preferences) { + String defaultCategories = Arrays.stream(Category.values()) + .map(Enum::toString) + .collect(Collectors.joining(".")); + + preferences.edit().putString(CATEGORIES, defaultCategories).commit(); + } + }; + + private static final List migrations = Arrays.asList( + Migration1, + Migration2 + ); private static PreferenceUtil instance; @@ -426,19 +436,27 @@ public final class PreferenceUtil { } public List getCategories() { - String data = mPreferences.getString(CATEGORIES, null); - if (data != null) { - return new Gson().fromJson(data, new TypeToken>(){}.getType()); - } + String defaultValues = Arrays.stream(Category.values()).map(Category::toString).collect(Collectors.joining(".")); + String values = mPreferences.getString(CATEGORIES, defaultValues); - return Arrays.stream(Category.values()).peek(category -> category.select = true).collect(Collectors.toList()); + return Arrays.stream(values.split("\\.")).map(category -> { + Category item = Category.valueOf(category.toUpperCase()); + + // this is kind of a hack but avoids any annoying json serialization + // lowercase enum means the category is not enabled on the main activity + item.select = Character.isUpperCase(category.charAt(0)); + + return item; + }).collect(Collectors.toList()); } + @SuppressWarnings("SimplifyStreamApiCallChains") public void setCategories(List categories) { - Gson gson = new Gson(); - Type type = new TypeToken>(){}.getType(); + List values = categories.stream().map(category -> { + return category.select ? category.toString() : category.toString().toLowerCase(); + }).collect(Collectors.toList()); - mPreferences.edit().putString(CATEGORIES, gson.toJson(categories, type)).apply(); + mPreferences.edit().putString(CATEGORIES, values.stream().collect(Collectors.joining("."))).apply(); } public List getDirectPlayCodecs() {