From cb5a4d0c9b3b340928695fcb1d10b164a6dcef27 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Wed, 3 May 2023 11:50:22 +0400 Subject: [PATCH] feat(youtube/settings): add reset button to edit preference dialog (#383) --- .../integrations/settings/SettingsEnum.java | 4 +- .../settings/SharedPrefCategory.java | 29 +++++++- .../ResettableEditTextPreference.java | 66 +++++++++++++++++++ 3 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/app/revanced/integrations/settingsmenu/ResettableEditTextPreference.java diff --git a/app/src/main/java/app/revanced/integrations/settings/SettingsEnum.java b/app/src/main/java/app/revanced/integrations/settings/SettingsEnum.java index d5aff79a..1506c3f6 100644 --- a/app/src/main/java/app/revanced/integrations/settings/SettingsEnum.java +++ b/app/src/main/java/app/revanced/integrations/settings/SettingsEnum.java @@ -124,11 +124,11 @@ public enum SettingsEnum { parents(ENABLE_SWIPE_BRIGHTNESS, ENABLE_SWIPE_VOLUME)), ENABLE_SWIPE_HAPTIC_FEEDBACK("revanced_enable_swipe_haptic_feedback", BOOLEAN, TRUE, parents(ENABLE_SWIPE_BRIGHTNESS, ENABLE_SWIPE_VOLUME)), - SWIPE_MAGNITUDE_THRESHOLD("revanced_swipe_magnitude_threshold", FLOAT, 30f, + SWIPE_MAGNITUDE_THRESHOLD("revanced_swipe_magnitude_threshold", FLOAT, 30f, // edit: why is this a float and not an Integer? parents(ENABLE_SWIPE_BRIGHTNESS, ENABLE_SWIPE_VOLUME)), SWIPE_OVERLAY_BACKGROUND_ALPHA("revanced_swipe_overlay_background_alpha", INTEGER, 127, parents(ENABLE_SWIPE_BRIGHTNESS, ENABLE_SWIPE_VOLUME)), - SWIPE_OVERLAY_TEXT_SIZE("revanced_swipe_overlay_text_size", FLOAT, 22f, + SWIPE_OVERLAY_TEXT_SIZE("revanced_swipe_overlay_text_size", FLOAT, 22f, // edit: why is this a float and not an Integer? parents(ENABLE_SWIPE_BRIGHTNESS, ENABLE_SWIPE_VOLUME)), SWIPE_OVERLAY_TIMEOUT("revanced_swipe_overlay_timeout", LONG, 500L, parents(ENABLE_SWIPE_BRIGHTNESS, ENABLE_SWIPE_VOLUME)), diff --git a/app/src/main/java/app/revanced/integrations/settings/SharedPrefCategory.java b/app/src/main/java/app/revanced/integrations/settings/SharedPrefCategory.java index 4bf180ed..bded5469 100644 --- a/app/src/main/java/app/revanced/integrations/settings/SharedPrefCategory.java +++ b/app/src/main/java/app/revanced/integrations/settings/SharedPrefCategory.java @@ -8,6 +8,7 @@ import androidx.annotation.Nullable; import java.util.Objects; +import app.revanced.integrations.utils.LogHelper; import app.revanced.integrations.utils.ReVancedUtils; /** @@ -35,6 +36,11 @@ public enum SharedPrefCategory { preferences = Objects.requireNonNull(ReVancedUtils.getContext()).getSharedPreferences(prefName, Context.MODE_PRIVATE); } + private void removeConflictingPreferenceKeyValue(@NonNull String key) { + LogHelper.printException(() -> "Found conflicting preference: " + key); + preferences.edit().remove(key).apply(); + } + private void saveObjectAsString(@NonNull String key, @Nullable Object value) { preferences.edit().putString(key, (value == null ? null : value.toString())).apply(); } @@ -91,7 +97,14 @@ public enum SharedPrefCategory { } return _default; } catch (ClassCastException ex) { - return preferences.getInt(key, _default); // old data, previously stored as primitive + try { + // Old data previously stored as primitive. + return preferences.getInt(key, _default); + } catch (ClassCastException ex2) { + // Value stored is a completely different type (should never happen). + removeConflictingPreferenceKeyValue(key); + return _default; + } } } @@ -104,7 +117,12 @@ public enum SharedPrefCategory { } return _default; } catch (ClassCastException ex) { - return preferences.getLong(key, _default); + try { + return preferences.getLong(key, _default); + } catch (ClassCastException ex2) { + removeConflictingPreferenceKeyValue(key); + return _default; + } } } @@ -117,7 +135,12 @@ public enum SharedPrefCategory { } return _default; } catch (ClassCastException ex) { - return preferences.getFloat(key, _default); + try { + return preferences.getFloat(key, _default); + } catch (ClassCastException ex2) { + removeConflictingPreferenceKeyValue(key); + return _default; + } } } diff --git a/app/src/main/java/app/revanced/integrations/settingsmenu/ResettableEditTextPreference.java b/app/src/main/java/app/revanced/integrations/settingsmenu/ResettableEditTextPreference.java new file mode 100644 index 00000000..fc4c7f7e --- /dev/null +++ b/app/src/main/java/app/revanced/integrations/settingsmenu/ResettableEditTextPreference.java @@ -0,0 +1,66 @@ +package app.revanced.integrations.settingsmenu; + +import static app.revanced.integrations.utils.StringRef.str; + +import android.app.AlertDialog; +import android.content.Context; +import android.os.Bundle; +import android.preference.EditTextPreference; +import android.util.AttributeSet; +import android.widget.Button; +import android.widget.EditText; + +import java.util.Objects; + +import app.revanced.integrations.settings.SettingsEnum; +import app.revanced.integrations.utils.LogHelper; + +public class ResettableEditTextPreference extends EditTextPreference { + public ResettableEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + } + + public ResettableEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + public ResettableEditTextPreference(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public ResettableEditTextPreference(Context context) { + super(context); + } + + @Override + protected void onPrepareDialogBuilder(AlertDialog.Builder builder) { + super.onPrepareDialogBuilder(builder); + SettingsEnum setting = SettingsEnum.settingFromPath(getKey()); + if (setting != null) { + builder.setNeutralButton(str("revanced_settings_reset"), null); + } + } + + @Override + protected void showDialog(Bundle state) { + super.showDialog(state); + + // Override the button click listener to prevent dismissing the dialog. + Button button = ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_NEUTRAL); + if (button == null) { + return; + } + button.setOnClickListener(v -> { + try { + SettingsEnum setting = Objects.requireNonNull(SettingsEnum.settingFromPath(getKey())); + setting.saveValue(setting.defaultValue); + String defaultStringValue = setting.defaultValue.toString(); + EditText editText = getEditText(); + editText.setText(defaultStringValue); + editText.setSelection(defaultStringValue.length()); // move cursor to end of text + } catch (Exception ex) { + LogHelper.printException(() -> "reset failure", ex); + } + }); + } +}