fix: Handle custom preferences (#586)

* fix: Handle custom preferences
This commit is contained in:
oSumAtrIX 2024-03-16 16:53:53 +01:00 committed by GitHub
parent 6f1ac5d073
commit ad477e4859
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 62 additions and 30 deletions

View File

@ -152,19 +152,15 @@ public abstract class AbstractPreferenceFragment extends PreferenceFragment {
} }
/** /**
* Updates a UI Preference with the {@link Setting} that backs it. * Handles syncing a UI Preference with the {@link Setting} that backs it.
* If needed, subclasses can override this to handle additional UI Preference types. * If needed, subclasses can override this to handle additional UI Preference types.
* *
* @param syncSetting If the UI should be synced {@link Setting} <-> Preference
* @param applySettingToPreference If true, then apply {@link Setting} -> Preference. * @param applySettingToPreference If true, then apply {@link Setting} -> Preference.
* If false, then apply {@link Setting} <- Preference. * If false, then apply {@link Setting} <- Preference.
*/ */
protected void updatePreference(@NonNull Preference pref, @NonNull Setting<?> setting, protected void syncSettingWithPreference(@NonNull Preference pref,
boolean syncSetting, boolean applySettingToPreference) { @NonNull Setting<?> setting,
if (!syncSetting && applySettingToPreference) { boolean applySettingToPreference) {
throw new IllegalArgumentException();
}
if (syncSetting) {
if (pref instanceof SwitchPreference) { if (pref instanceof SwitchPreference) {
SwitchPreference switchPref = (SwitchPreference) pref; SwitchPreference switchPref = (SwitchPreference) pref;
BooleanSetting boolSetting = (BooleanSetting) setting; BooleanSetting boolSetting = (BooleanSetting) setting;
@ -190,9 +186,26 @@ public abstract class AbstractPreferenceFragment extends PreferenceFragment {
updateListPreferenceSummary(listPref, setting); updateListPreferenceSummary(listPref, setting);
} else { } else {
Logger.printException(() -> "Setting cannot be handled: " + pref.getClass() + ": " + pref); Logger.printException(() -> "Setting cannot be handled: " + pref.getClass() + ": " + pref);
return;
} }
} }
/**
* Updates a UI Preference with the {@link Setting} that backs it.
*
* @param syncSetting If the UI should be synced {@link Setting} <-> Preference
* @param applySettingToPreference If true, then apply {@link Setting} -> Preference.
* If false, then apply {@link Setting} <- Preference.
*/
private void updatePreference(@NonNull Preference pref, @NonNull Setting<?> setting,
boolean syncSetting, boolean applySettingToPreference) {
if (!syncSetting && applySettingToPreference) {
throw new IllegalArgumentException();
}
if (syncSetting) {
syncSettingWithPreference(pref, setting, applySettingToPreference);
}
updatePreferenceAvailability(pref, setting); updatePreferenceAvailability(pref, setting);
} }

View File

@ -1,11 +1,15 @@
package app.revanced.integrations.tiktok.settings.preference; package app.revanced.integrations.tiktok.settings.preference;
import android.preference.Preference;
import android.preference.PreferenceScreen; import android.preference.PreferenceScreen;
import androidx.annotation.NonNull;
import app.revanced.integrations.shared.settings.Setting;
import app.revanced.integrations.shared.settings.preference.AbstractPreferenceFragment; import app.revanced.integrations.shared.settings.preference.AbstractPreferenceFragment;
import app.revanced.integrations.tiktok.settings.preference.categories.DownloadsPreferenceCategory; import app.revanced.integrations.tiktok.settings.preference.categories.DownloadsPreferenceCategory;
import app.revanced.integrations.tiktok.settings.preference.categories.FeedFilterPreferenceCategory; import app.revanced.integrations.tiktok.settings.preference.categories.FeedFilterPreferenceCategory;
import app.revanced.integrations.tiktok.settings.preference.categories.IntegrationsPreferenceCategory; import app.revanced.integrations.tiktok.settings.preference.categories.IntegrationsPreferenceCategory;
import app.revanced.integrations.tiktok.settings.preference.categories.SimSpoofPreferenceCategory; import app.revanced.integrations.tiktok.settings.preference.categories.SimSpoofPreferenceCategory;
import org.jetbrains.annotations.NotNull;
/** /**
* Preference fragment for ReVanced settings * Preference fragment for ReVanced settings
@ -13,6 +17,21 @@ import app.revanced.integrations.tiktok.settings.preference.categories.SimSpoofP
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public class ReVancedPreferenceFragment extends AbstractPreferenceFragment { public class ReVancedPreferenceFragment extends AbstractPreferenceFragment {
@Override
protected void syncSettingWithPreference(@NonNull @NotNull Preference pref,
@NonNull @NotNull Setting<?> setting,
boolean applySettingToPreference) {
if (pref instanceof RangeValuePreference) {
RangeValuePreference rangeValuePref = (RangeValuePreference) pref;
Setting.privateSetValueFromString(setting, rangeValuePref.getValue());
} else if (pref instanceof DownloadPathPreference) {
DownloadPathPreference downloadPathPref = (DownloadPathPreference) pref;
Setting.privateSetValueFromString(setting, downloadPathPref.getValue());
} else {
super.syncSettingWithPreference(pref, setting, applySettingToPreference);
}
}
@Override @Override
protected void initialize() { protected void initialize() {
final var context = getContext(); final var context = getContext();