refactor: use an interface for import/export callback

This commit is contained in:
LisoUseInAIKyrios 2024-02-11 21:56:10 +04:00
parent c5a6e271ba
commit 0615c13198
5 changed files with 67 additions and 24 deletions

View File

@ -7,7 +7,7 @@ import app.revanced.integrations.shared.Logger;
import app.revanced.integrations.shared.StringRef; import app.revanced.integrations.shared.StringRef;
import app.revanced.integrations.shared.Utils; import app.revanced.integrations.shared.Utils;
import app.revanced.integrations.shared.settings.preference.SharedPrefCategory; import app.revanced.integrations.shared.settings.preference.SharedPrefCategory;
import app.revanced.integrations.youtube.sponsorblock.SponsorBlockSettings;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -62,6 +62,21 @@ public abstract class Setting<T> {
}; };
} }
/**
* Callback for importing/exporting settings.
*/
public interface ImportExportCallback {
/**
* Called after all settings have been imported.
*/
void settingsImported(@Nullable Context context);
/**
* Called after all settings have been exported.
*/
void settingsExported(@Nullable Context context);
}
/** /**
* All settings that were instantiated. * All settings that were instantiated.
* When a new setting is created, it is automatically added to this list. * When a new setting is created, it is automatically added to this list.
@ -78,6 +93,15 @@ public abstract class Setting<T> {
*/ */
public static final SharedPrefCategory preferences = new SharedPrefCategory("revanced_prefs"); public static final SharedPrefCategory preferences = new SharedPrefCategory("revanced_prefs");
private static final List<ImportExportCallback> importExportCallbacks = new ArrayList<>();
/**
* Adds a callback for {@link #importFromJSON(Context, String)} and {@link #exportToJson(Context)}.
*/
public static void addImportExportCallback(@NonNull ImportExportCallback callback) {
importExportCallbacks.add(Objects.requireNonNull(callback));
}
@Nullable @Nullable
public static Setting<?> getSettingFromPath(@NonNull String str) { public static Setting<?> getSettingFromPath(@NonNull String str) {
return PATH_TO_SETTINGS.get(str); return PATH_TO_SETTINGS.get(str);
@ -357,7 +381,10 @@ public abstract class Setting<T> {
setting.writeToJSON(json, importExportKey); setting.writeToJSON(json, importExportKey);
} }
} }
SponsorBlockSettings.showExportWarningIfNeeded(alertDialogContext);
for (ImportExportCallback callback : importExportCallbacks) {
callback.settingsExported(alertDialogContext);
}
if (json.length() == 0) { if (json.length() == 0) {
return ""; return "";
@ -377,7 +404,7 @@ public abstract class Setting<T> {
/** /**
* @return if any settings that require a reboot were changed. * @return if any settings that require a reboot were changed.
*/ */
public static boolean importFromJSON(@NonNull String settingsJsonString) { public static boolean importFromJSON(@Nullable Context context, @NonNull String settingsJsonString) {
try { try {
if (!settingsJsonString.matches("[\\s\\S]*\\{")) { if (!settingsJsonString.matches("[\\s\\S]*\\{")) {
settingsJsonString = '{' + settingsJsonString + '}'; // Restore outer JSON braces settingsJsonString = '{' + settingsJsonString + '}'; // Restore outer JSON braces
@ -403,12 +430,9 @@ public abstract class Setting<T> {
} }
} }
// SB Enum categories are saved using StringSettings. for (ImportExportCallback callback : importExportCallbacks) {
// Which means they need to reload again if changed by other code (such as here). callback.settingsImported(context);
// This call could be removed by creating a custom Setting class that manages the }
// "String <-> Enum" logic or by adding an event hook of when settings are imported.
// But for now this is simple and works.
SponsorBlockSettings.updateFromImportedSettings();
Utils.showToastLong(numberOfSettingsImported == 0 Utils.showToastLong(numberOfSettingsImported == 0
? str("revanced_settings_import_reset") ? str("revanced_settings_import_reset")

View File

@ -83,7 +83,7 @@ public class ImportExportPreference extends EditTextPreference implements Prefer
return; return;
} }
AbstractPreferenceFragment.settingImportInProgress = true; AbstractPreferenceFragment.settingImportInProgress = true;
final boolean rebootNeeded = Setting.importFromJSON(replacementSettings); final boolean rebootNeeded = Setting.importFromJSON(getContext(), replacementSettings);
if (rebootNeeded) { if (rebootNeeded) {
AbstractPreferenceFragment.showRestartDialog(getContext()); AbstractPreferenceFragment.showRestartDialog(getContext());
} }

View File

@ -15,15 +15,14 @@ import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import app.revanced.integrations.shared.Logger; import app.revanced.integrations.shared.settings.BaseSettings;
import app.revanced.integrations.shared.settings.BooleanSetting; import app.revanced.integrations.shared.settings.BooleanSetting;
import app.revanced.integrations.shared.settings.FloatSetting; import app.revanced.integrations.shared.settings.FloatSetting;
import app.revanced.integrations.shared.settings.IntegerSetting; import app.revanced.integrations.shared.settings.IntegerSetting;
import app.revanced.integrations.shared.settings.LongSetting; import app.revanced.integrations.shared.settings.LongSetting;
import app.revanced.integrations.shared.settings.Setting; import app.revanced.integrations.shared.settings.Setting;
import app.revanced.integrations.shared.settings.preference.SharedPrefCategory;
import app.revanced.integrations.shared.settings.BaseSettings;
import app.revanced.integrations.shared.settings.StringSetting; import app.revanced.integrations.shared.settings.StringSetting;
import app.revanced.integrations.shared.settings.preference.SharedPrefCategory;
import app.revanced.integrations.youtube.sponsorblock.SponsorBlockSettings; import app.revanced.integrations.youtube.sponsorblock.SponsorBlockSettings;
public class Settings extends BaseSettings { public class Settings extends BaseSettings {
@ -339,12 +338,18 @@ public class Settings extends BaseSettings {
} }
} }
// Do _not_ delete this SB private user id migration property until sometime in 2024. // Do _not_ delete this SB private user id migration property until sometime in 2024.
// This is the only setting that cannot be reconfigured if lost, // This is the only setting that cannot be reconfigured if lost,
// and more time should be given for users who rarely upgrade. // and more time should be given for users who rarely upgrade.
migrateOldSettingToNew(DEPRECATED_SB_UUID_OLD_MIGRATION_SETTING, SB_PRIVATE_USER_ID); migrateOldSettingToNew(DEPRECATED_SB_UUID_OLD_MIGRATION_SETTING, SB_PRIVATE_USER_ID);
// endregion // endregion
// region SB import/export callbacks
Setting.addImportExportCallback(SponsorBlockSettings.SB_IMPORT_EXPORT_CALLBACK);
// endregion
} }
} }

View File

@ -1,6 +1,7 @@
package app.revanced.integrations.youtube.sponsorblock; package app.revanced.integrations.youtube.sponsorblock;
import static app.revanced.integrations.shared.StringRef.str; import static app.revanced.integrations.shared.StringRef.str;
import static app.revanced.integrations.shared.settings.Setting.ImportExportCallback;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
@ -159,9 +160,18 @@ public class SponsorBlockSettings {
} }
/** /**
* Export the categories using flatten json (no embedded dictionaries or arrays). * Updates internal data based on {@link Setting} values.
*/ */
public static void showExportWarningIfNeeded(@Nullable Context dialogContext) { private static void updateFromImportedSettings() {
// SB Enum categories are saved using StringSettings.
// Which means they need to reload again if changed by other code.
SegmentCategory.loadAllCategoriesFromSettings();
}
/**
* Shows a warning, if a private SB user id exists.
*/
private static void showExportWarningIfNeeded(@Nullable Context dialogContext) {
Utils.verifyOnMainThread(); Utils.verifyOnMainThread();
initialize(); initialize();
@ -178,6 +188,17 @@ public class SponsorBlockSettings {
} }
} }
public static final ImportExportCallback SB_IMPORT_EXPORT_CALLBACK = new ImportExportCallback() {
@Override
public void settingsImported(@Nullable Context context) {
updateFromImportedSettings();
}
@Override
public void settingsExported(@Nullable Context context) {
showExportWarningIfNeeded(context);
}
};
public static boolean isValidSBUserId(@NonNull String userId) { public static boolean isValidSBUserId(@NonNull String userId) {
return !userId.isEmpty() && userId.length() >= SB_PRIVATE_USER_ID_MINIMUM_LENGTH; return !userId.isEmpty() && userId.length() >= SB_PRIVATE_USER_ID_MINIMUM_LENGTH;
} }
@ -234,11 +255,4 @@ public class SponsorBlockSettings {
SegmentCategory.updateEnabledCategories(); SegmentCategory.updateEnabledCategories();
} }
/**
* Updates internal data based on {@link Setting} values.
*/
public static void updateFromImportedSettings() {
SegmentCategory.loadAllCategoriesFromSettings();
}
} }

View File

@ -106,7 +106,7 @@ public enum SegmentCategory {
} }
/** /**
* Must be called if behavior of any category is changed * Must be called if behavior of any category is changed.
*/ */
public static void updateEnabledCategories() { public static void updateEnabledCategories() {
Utils.verifyOnMainThread(); Utils.verifyOnMainThread();