Skip to content

Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.

C

feat: add backup settings

Vojtěch Škopekdaaf445de51e
diff --git a/tweak/Sources/Features/About/About.h b/tweak/Sources/Features/About/About.h
index c5dd03cd..08124ad4 100644
--- a/tweak/Sources/Features/About/About.h
+++ b/tweak/Sources/Features/About/About.h
@@ -23,4 +23,9 @@ SGModRow *SGSigningWarningRow(void); // nil while the signature is soun
void SGCheckSigningOnce(void);
void SGShowSigningFixIfPending(void); // the sheet the tour held back, if any
+// Backup.m: the settings out to a JSON file through the share sheet, and back in from one, replacing
+// what is set and restarting.
+void SGExportSettings(void);
+void SGImportSettings(void);
+
UIViewController *SGAboutPage(void); // the Mod page
diff --git a/tweak/Sources/Features/About/AboutSettings.m b/tweak/Sources/Features/About/AboutSettings.m
index 57bc9fbd..1ce3ddcd 100644
--- a/tweak/Sources/Features/About/AboutSettings.m
+++ b/tweak/Sources/Features/About/AboutSettings.m
@@ -51,6 +51,10 @@ UIViewController *SGAboutPage(void) {
withSymbol(SGLinkRow(@"GitHub", @"Source, releases and issues", SGRepoURL), @"chevron.left.forwardslash.chevron.right"),
withSymbol(SGActionRow(@"Welcome tour", @"The pages from the first launch, again", ^{ SGShowOnboarding(); }), @"map"),
]),
+ SGSection(nil, @[
+ withSymbol(SGActionRow(@"Export settings", @"Every switch and choice to a file to keep or share", ^{ SGExportSettings(); }), @"square.and.arrow.up"),
+ withSymbol(SGActionRow(@"Import settings", @"Replace yours with a file's, then a restart", ^{ SGImportSettings(); }), @"square.and.arrow.down"),
+ ]),
SGSection(nil, @[reset]),
] footer:nil];
}
diff --git a/tweak/Sources/Features/About/Backup.m b/tweak/Sources/Features/About/Backup.m
new file mode 100644
index 00000000..bc50e414
--- /dev/null
+++ b/tweak/Sources/Features/About/Backup.m
@@ -0,0 +1,105 @@
+#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
+#import "Core/SGCore.h"
+#import "Settings/SGPageStyle.h"
+#import "About.h"
+
+// Every key under the prefix travels, the way the reset sweeps them, so a new switch needs nothing
+// here. What is left out belongs to this install rather than to its user's choices.
+static BOOL isSetting(NSString *key) {
+ if (![key hasPrefix:@"spotifyglass."]) return NO;
+ for (NSString *local in @[@"spotifyglass.adblock.counts", @"spotifyglass.privacy.counts", @"spotifyglass.update.",
+ @"spotifyglass.signing.", @"spotifyglass.onboarding.", @"spotifyglass.navbar.stock"]) {
+ if ([key hasPrefix:local]) return NO;
+ }
+ return YES;
+}
+
+static NSDictionary *storedDefaults(void) {
+ return [NSUserDefaults.standardUserDefaults persistentDomainForName:NSBundle.mainBundle.bundleIdentifier] ?: @{};
+}
+
+static void showAlert(NSString *title, NSString *message, UIAlertAction *confirm) {
+ UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
+ if (confirm) [alert addAction:confirm];
+ [alert addAction:[UIAlertAction actionWithTitle:confirm ? @"Cancel" : @"OK" style:UIAlertActionStyleCancel handler:nil]];
+ [SGTopController() presentViewController:alert animated:YES completion:nil];
+}
+
+void SGExportSettings(void) {
+ NSMutableDictionary *settings = [NSMutableDictionary dictionary];
+ [storedDefaults() enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL *stop) {
+ if (isSetting(key) && [NSJSONSerialization isValidJSONObject:@[value]]) settings[key] = value;
+ }];
+ NSDictionary *file = @{
+ @"mod": @(SG_VERSION),
+ @"spotify": [NSBundle.mainBundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"] ?: @"unknown",
+ @"settings": settings,
+ };
+ NSData *data = [NSJSONSerialization dataWithJSONObject:file options:NSJSONWritingPrettyPrinted | NSJSONWritingSortedKeys error:nil];
+ NSURL *url = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"spotifyglass-settings.json"]];
+ [data writeToURL:url atomically:YES];
+
+ UIViewController *top = SGTopController();
+ UIActivityViewController *sheet = [[UIActivityViewController alloc] initWithActivityItems:@[url] applicationActivities:nil];
+ sheet.popoverPresentationController.sourceView = top.view;
+ [top presentViewController:sheet animated:YES completion:nil];
+}
+
+static Class kindOf(id value) {
+ for (Class kind in @[NSNumber.class, NSString.class, NSArray.class, NSDictionary.class]) {
+ if ([value isKindOfClass:kind]) return kind;
+ }
+ return Nil;
+}
+
+// A key already stored keeps its type: the hooks send boolValue to whatever they find at launch, and
+// an array there would crash Spotify before the reset row could be reached.
+static void importSettings(NSDictionary *settings) {
+ NSUserDefaults *store = NSUserDefaults.standardUserDefaults;
+ NSDictionary *stored = storedDefaults();
+ for (NSString *key in stored) {
+ if (isSetting(key)) [store removeObjectForKey:key];
+ }
+ NSUInteger skipped = 0;
+ for (NSString *key in settings) {
+ id value = settings[key];
+ id current = stored[key];
+ BOOL fits = isSetting(key)
+ && [NSPropertyListSerialization propertyList:value isValidForFormat:NSPropertyListBinaryFormat_v1_0]
+ && (!current || kindOf(current) == kindOf(value));
+ if (fits) [store setObject:value forKey:key];
+ else skipped++;
+ }
+ SGLog(@"import: set %lu keys, skipped %lu", (unsigned long)(settings.count - skipped), (unsigned long)skipped);
+ SGRestartSpotify();
+}
+
+@interface SGSettingsPicker : NSObject <UIDocumentPickerDelegate>
+@end
+
+@implementation SGSettingsPicker
+
+- (void)documentPicker:(UIDocumentPickerViewController *)picker didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
+ NSData *data = [NSData dataWithContentsOfURL:urls.firstObject];
+ id file = data ? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil;
+ NSDictionary *settings = [file isKindOfClass:NSDictionary.class] ? file[@"settings"] : nil;
+ if (![settings isKindOfClass:NSDictionary.class]) {
+ showAlert(@"Not a settings file", @"Pick a file made by Export settings.", nil);
+ return;
+ }
+ NSString *mod = [file[@"mod"] isKindOfClass:NSString.class] ? file[@"mod"] : @"unknown";
+ NSString *message = [NSString stringWithFormat:@"Your settings are replaced by the file's, exported from version %@. Anything it does not have goes back to its default, and Spotify restarts.", mod];
+ showAlert(@"Import settings?", message, [UIAlertAction actionWithTitle:@"Import and restart" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
+ importSettings(settings);
+ }]);
+}
+
+@end
+
+void SGImportSettings(void) {
+ static SGSettingsPicker *delegate;
+ if (!delegate) delegate = [SGSettingsPicker new];
+ UIDocumentPickerViewController *picker = [[UIDocumentPickerViewController alloc] initForOpeningContentTypes:@[UTTypeJSON] asCopy:YES];
+ picker.delegate = delegate;
+ [SGTopController() presentViewController:picker animated:YES completion:nil];
+}