From bd3ef6b4fff568519f6e2dfd530c303d42d98dc7 Mon Sep 17 00:00:00 2001 From: "aricloverGitHub (INACTIVE)" <78001398+arichornlover@users.noreply.github.com> Date: Thu, 12 Dec 2024 19:25:58 -0600 Subject: [PATCH] =?UTF-8?q?Add=20failsafe=20to=20=E2=80=9CImport=20Setting?= =?UTF-8?q?s=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/uYouPlusSettings.xm | 63 ++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 21 deletions(-) diff --git a/Sources/uYouPlusSettings.xm b/Sources/uYouPlusSettings.xm index 89689d9026..e95575f645 100644 --- a/Sources/uYouPlusSettings.xm +++ b/Sources/uYouPlusSettings.xm @@ -188,7 +188,7 @@ extern NSBundle *uYouPlusBundle(); ]; [sectionItems addObject:developers]; -# pragma mark - Copy and Paste Settings +# pragma mark - Copy/Import and Paste/Export Settings YTSettingsSectionItem *copySettings = [%c(YTSettingsSectionItem) itemWithTitle:IS_ENABLED(kReplaceCopyandPasteButtons) ? LOC(@"EXPORT_SETTINGS") : LOC(@"COPY_SETTINGS") titleDescription:IS_ENABLED(kReplaceCopyandPasteButtons) ? LOC(@"EXPORT_SETTINGS_DESC") : LOC(@"COPY_SETTINGS_DESC") @@ -208,6 +208,7 @@ extern NSBundle *uYouPlusBundle(); [settingsString writeToURL:tempFileURL atomically:YES encoding:NSUTF8StringEncoding error:nil]; UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithURL:tempFileURL inMode:UIDocumentPickerModeExportToService]; documentPicker.allowsMultipleSelection = NO; + documentPicker.delegate = self; [settingsViewController presentViewController:documentPicker animated:YES completion:nil]; } else { // Copy Settings functionality (DEFAULT - Copies to Clipboard) @@ -1621,18 +1622,47 @@ NSString *cacheDescription = [NSString stringWithFormat:@"%@", GetCacheSize()]; NSURL *fileURL = urls.firstObject; NSString *fileContents = [NSString stringWithContentsOfURL:fileURL encoding:NSUTF8StringEncoding error:nil]; if (fileContents.length > 0) { - NSArray *lines = [fileContents componentsSeparatedByString:@"\n"]; - for (NSString *line in lines) { - NSArray *components = [line componentsSeparatedByString:@": "]; - if (components.count == 2) { - NSString *key = components[0]; - NSString *value = components[1]; - [[NSUserDefaults standardUserDefaults] setObject:value forKey:key]; + UIAlertController *confirmImportAlert = [UIAlertController alertControllerWithTitle:@"Confirm Import" + message:@"Do you want to import the settings from the selected file?" + preferredStyle:UIAlertControllerStyleAlert]; + [confirmImportAlert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]]; + [confirmImportAlert addAction:[UIAlertAction actionWithTitle:@"Import" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { + NSDictionary *currentSettings = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]; + NSData *backupData = [NSKeyedArchiver archivedDataWithRootObject:currentSettings]; + BOOL success = YES; + NSArray *lines = [fileContents componentsSeparatedByString:@"\n"]; + for (NSString *line in lines) { + NSArray *components = [line componentsSeparatedByString:@": "]; + if (components.count == 2) { + NSString *key = components[0]; + NSString *value = components[1]; + @try { + [[NSUserDefaults standardUserDefaults] setObject:value forKey:key]; + } + @catch (NSException *exception) { + success = NO; + break; + } + } } - } - YTSettingsViewController *settingsViewController = [self valueForKey:@"_settingsViewControllerDelegate"]; - [settingsViewController reloadData]; - [[%c(GOOHUDManagerInternal) sharedInstance] showMessageMainThread:[%c(YTHUDMessage) messageWithText:@"Settings applied."]]; + if (success) { + YTSettingsViewController *settingsViewController = [self valueForKey:@"_settingsViewControllerDelegate"]; + [settingsViewController reloadData]; + [[%c(GOOHUDManagerInternal) sharedInstance] showMessageMainThread:[%c(YTHUDMessage) messageWithText:@"Settings applied"]]; + SHOW_RELAUNCH_YT_SNACKBAR; + } else { + NSDictionary *backupSettings = [NSKeyedUnarchiver unarchiveObjectWithData:backupData]; + for (NSString *key in backupSettings) { + [[NSUserDefaults standardUserDefaults] setObject:backupSettings[key] forKey:key]; + } + UIAlertController *errorAlert = [UIAlertController alertControllerWithTitle:@"Error" + message:@"Failed to import settings. Reverted to previous settings." + preferredStyle:UIAlertControllerStyleAlert]; + [errorAlert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]]; + [settingsViewController presentViewController:errorAlert animated:YES completion:nil]; + } + }]]; + [settingsViewController presentViewController:confirmImportAlert animated:YES completion:nil]; } } } @@ -1641,15 +1671,6 @@ NSString *cacheDescription = [NSString stringWithFormat:@"%@", GetCacheSize()]; NSLog(@"Document picker was cancelled"); } -// Update text without the need of restarting the app. -- (void)updateCopyAndPasteText { - NSString *newTitle = IS_ENABLED(kReplaceCopyandPasteButtons) ? LOC(@"IMPORT_SETTINGS") : LOC(@"PASTE_SETTINGS"); - NSString *newTitleDescription = IS_ENABLED(kReplaceCopyandPasteButtons) ? LOC(@"IMPORT_SETTINGS_DESC") : LOC(@"PASTE_SETTINGS_DESC"); - [pasteSettings setTitle:newTitle]; - [pasteSettings setTitleDescription:newTitleDescription]; - [settingsViewController reloadData]; -} - // - (void)updateSectionForCategory:(NSUInteger)category withEntry:(id)entry { if (category == uYouPlusSection) {