use static variables for all preference keys

This commit is contained in:
dkanada 2020-06-19 07:07:23 +09:00
commit 690dfb0c10
7 changed files with 28 additions and 27 deletions

View file

@ -35,7 +35,7 @@ public class MusicLibraryPagerAdapter extends FragmentPagerAdapter {
public MusicLibraryPagerAdapter(@NonNull final Context context, final FragmentManager fragmentManager) {
super(fragmentManager);
mContext = context;
setCategoryInfos(PreferenceUtil.getInstance(context).getLibraryCategories());
setCategoryInfos(PreferenceUtil.getInstance(context).getCategories());
}
public void setCategoryInfos(@NonNull List<CategoryInfo> categoryInfos) {

View file

@ -31,9 +31,9 @@ public class LibraryPreferenceDialog extends DialogFragment {
List<CategoryInfo> categoryInfos;
if (savedInstanceState != null) {
categoryInfos = savedInstanceState.getParcelableArrayList(PreferenceUtil.LIBRARY_CATEGORIES);
categoryInfos = savedInstanceState.getParcelableArrayList(PreferenceUtil.CATEGORIES);
} else {
categoryInfos = PreferenceUtil.getInstance(getContext()).getLibraryCategories();
categoryInfos = PreferenceUtil.getInstance(getContext()).getCategories();
}
adapter = new CategoryInfoAdapter(categoryInfos);
@ -51,7 +51,7 @@ public class LibraryPreferenceDialog extends DialogFragment {
.negativeText(android.R.string.cancel)
.neutralText(R.string.reset_action)
.autoDismiss(false)
.onNeutral((dialog, action) -> adapter.setCategoryInfos(PreferenceUtil.getInstance(getContext()).getDefaultLibraryCategories()))
.onNeutral((dialog, action) -> adapter.setCategoryInfos(PreferenceUtil.getInstance(getContext()).getDefaultCategories()))
.onNegative((dialog, action) -> dismiss())
.onPositive((dialog, action) -> {
updateCategories(adapter.getCategoryInfos());
@ -63,13 +63,13 @@ public class LibraryPreferenceDialog extends DialogFragment {
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelableArrayList(PreferenceUtil.LIBRARY_CATEGORIES, new ArrayList<>(adapter.getCategoryInfos()));
outState.putParcelableArrayList(PreferenceUtil.CATEGORIES, new ArrayList<>(adapter.getCategoryInfos()));
}
private void updateCategories(List<CategoryInfo> categories) {
if (getSelected(categories) == 0) return;
PreferenceUtil.getInstance(getContext()).setLibraryCategories(categories);
PreferenceUtil.getInstance(getContext()).setCategories(categories);
}
private int getSelected(List<CategoryInfo> categories) {

View file

@ -75,6 +75,7 @@ public class SettingsActivity extends AbsBaseActivity implements ColorChooserDia
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
new DynamicShortcutManager(this).updateDynamicShortcuts();
}
recreate();
}
@ -106,9 +107,7 @@ public class SettingsActivity extends AbsBaseActivity implements ColorChooserDia
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
preference.setSummary(
index >= 0
? listPreference.getEntries()[index]
: null);
index >= 0 ? listPreference.getEntries()[index] : null);
} else {
preference.setSummary(stringValue);
}
@ -132,6 +131,7 @@ public class SettingsActivity extends AbsBaseActivity implements ColorChooserDia
} else if (preference instanceof LibraryPreference) {
return LibraryPreferenceDialog.newInstance();
}
return super.onCreatePreferenceDialog(preference);
}
@ -150,7 +150,7 @@ public class SettingsActivity extends AbsBaseActivity implements ColorChooserDia
}
private void invalidateSettings() {
final Preference generalTheme = findPreference("general_theme");
final Preference generalTheme = findPreference(PreferenceUtil.GENERAL_THEME);
setSummary(generalTheme);
generalTheme.setOnPreferenceChangeListener((preference, o) -> {
String themeName = (String) o;
@ -193,7 +193,7 @@ public class SettingsActivity extends AbsBaseActivity implements ColorChooserDia
return true;
});
TwoStatePreference colorNavBar = (TwoStatePreference) findPreference("should_color_navigation_bar");
TwoStatePreference colorNavBar = (TwoStatePreference) findPreference(PreferenceUtil.COLORED_NAVIGATION_BAR);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
colorNavBar.setVisible(false);
} else {
@ -207,7 +207,7 @@ public class SettingsActivity extends AbsBaseActivity implements ColorChooserDia
});
}
final TwoStatePreference classicNotification = (TwoStatePreference) findPreference("classic_notification");
final TwoStatePreference classicNotification = (TwoStatePreference) findPreference(PreferenceUtil.CLASSIC_NOTIFICATION);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
classicNotification.setVisible(false);
} else {
@ -219,7 +219,7 @@ public class SettingsActivity extends AbsBaseActivity implements ColorChooserDia
});
}
final TwoStatePreference coloredNotification = (TwoStatePreference) findPreference("colored_notification");
final TwoStatePreference coloredNotification = (TwoStatePreference) findPreference(PreferenceUtil.COLORED_NOTIFICATION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
coloredNotification.setEnabled(PreferenceUtil.getInstance(getActivity()).getClassicNotification());
} else {
@ -258,14 +258,14 @@ public class SettingsActivity extends AbsBaseActivity implements ColorChooserDia
break;
case PreferenceUtil.CLASSIC_NOTIFICATION:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
findPreference("colored_notification").setEnabled(sharedPreferences.getBoolean(key, false));
findPreference(PreferenceUtil.COLORED_NOTIFICATION).setEnabled(sharedPreferences.getBoolean(key, false));
}
break;
}
}
private void updateNowPlayingScreenSummary() {
findPreference("now_playing_screen_id").setSummary(PreferenceUtil.getInstance(getActivity()).getNowPlayingScreen().titleRes);
findPreference(PreferenceUtil.NOW_PLAYING_SCREEN).setSummary(PreferenceUtil.getInstance(getActivity()).getNowPlayingScreen().titleRes);
}
}
}

View file

@ -99,9 +99,9 @@ public class LibraryFragment extends AbsMainActivityFragment implements CabHolde
@Override
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
if (PreferenceUtil.LIBRARY_CATEGORIES.equals(key)) {
if (PreferenceUtil.CATEGORIES.equals(key)) {
Fragment current = getCurrentFragment();
pagerAdapter.setCategoryInfos(PreferenceUtil.getInstance(getActivity()).getLibraryCategories());
pagerAdapter.setCategoryInfos(PreferenceUtil.getInstance(getActivity()).getCategories());
pager.setOffscreenPageLimit(pagerAdapter.getCount() - 1);
int position = pagerAdapter.getItemPosition(current);
if (position < 0) position = 0;

View file

@ -21,7 +21,7 @@ import java.util.ArrayList;
import java.util.List;
public final class PreferenceUtil {
public static final String LIBRARY_CATEGORIES = "library_categories";
public static final String CATEGORIES = "library_categories";
public static final String REMEMBER_LAST_TAB = "remember_last_tab";
public static final String LAST_TAB = "last_tab";
@ -51,6 +51,7 @@ public final class PreferenceUtil {
public static final String CLASSIC_NOTIFICATION = "classic_notification";
public static final String GENERAL_THEME = "general_theme";
public static final String COLORED_NAVIGATION_BAR = "should_color_navigation_bar";
public static final String COLORED_SHORTCUTS = "colored_shortcuts";
public static final String TRANSCODE_CODEC = "transcode_codec";
@ -366,8 +367,8 @@ public final class PreferenceUtil {
editor.apply();
}
public List<CategoryInfo> getLibraryCategories() {
String data = mPreferences.getString(LIBRARY_CATEGORIES, null);
public List<CategoryInfo> getCategories() {
String data = mPreferences.getString(CATEGORIES, null);
if (data != null) {
Gson gson = new Gson();
Type collectionType = new TypeToken<List<CategoryInfo>>() {
@ -380,19 +381,19 @@ public final class PreferenceUtil {
}
}
return getDefaultLibraryCategories();
return getDefaultCategories();
}
public void setLibraryCategories(List<CategoryInfo> categories) {
public void setCategories(List<CategoryInfo> categories) {
Gson gson = new Gson();
Type collectionType = new TypeToken<List<CategoryInfo>>() {}.getType();
final SharedPreferences.Editor editor = mPreferences.edit();
editor.putString(LIBRARY_CATEGORIES, gson.toJson(categories, collectionType));
editor.putString(CATEGORIES, gson.toJson(categories, collectionType));
editor.apply();
}
public List<CategoryInfo> getDefaultLibraryCategories() {
public List<CategoryInfo> getDefaultCategories() {
List<CategoryInfo> defaultCategories = new ArrayList<>(5);
defaultCategories.add(new CategoryInfo(CategoryInfo.Category.SONGS, true));
defaultCategories.add(new CategoryInfo(CategoryInfo.Category.ALBUMS, true));

View file

@ -96,7 +96,7 @@
<string name="pref_header_lockscreen">Lockscreen</string>
<string name="pref_header_notification">Notification</string>
<string name="library_categories">Library Categories</string>
<string name="library_categories">Categories</string>
<string name="primary_color">Primary Color</string>
<string name="accent_color">Accent Color</string>
<string name="colored_footers">Colored footers</string>
@ -128,7 +128,7 @@
<string name="pref_summary_remember_queue">Save the queue when closing the app so it can persist across sessions.</string>
<string name="pref_summary_colored_app_shortcuts">Colors the app shortcuts in the primary color.</string>
<string name="pref_summary_remember_last_tab">Go to the last opened tab on launch.</string>
<string name="pref_summary_library_categories">Configure visibility and order of library categories.</string>
<string name="pref_summary_library_categories">Configure visibility and order of display categories.</string>
<string name="delete_action">Delete</string>
<string name="remove_action">Remove</string>

View file

@ -6,7 +6,7 @@
<com.dkanada.gramophone.preferences.NowPlayingScreenPreference
app:iconSpaceReserved="false"
android:key="now_playing_screen_id"
android:key="now_playing_screen"
android:title="@string/pref_title_now_playing_screen_appearance" />
</com.kabouzeid.appthemehelper.common.prefs.supportv7.ATEPreferenceCategory>