From 9950cc220061d06fd1ca1066eb10287d3aca4040 Mon Sep 17 00:00:00 2001 From: "aricloverGitHub (INACTIVE)" <78001398+arichornlover@users.noreply.github.com> Date: Tue, 17 Dec 2024 21:02:43 -0600 Subject: [PATCH] Parse Settings (import settings functionality) - uYouPlusSettings.xm --- Sources/uYouPlusSettings.xm | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/Sources/uYouPlusSettings.xm b/Sources/uYouPlusSettings.xm index e691f75..aa04bfa 100644 --- a/Sources/uYouPlusSettings.xm +++ b/Sources/uYouPlusSettings.xm @@ -188,7 +188,7 @@ extern NSBundle *uYouPlusBundle(); ]; [sectionItems addObject:developers]; -# pragma mark - Copy/Import and Paste/Export Settings +# pragma mark - Copy/Export and Paste/Import 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") @@ -250,6 +250,32 @@ extern NSBundle *uYouPlusBundle(); // Import Settings functionality UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"public.text"] inMode:UIDocumentPickerModeImport]; documentPicker.allowsMultipleSelection = NO; + documentPicker.didPickDocumentHandler = ^(NSArray * _Nonnull urls) { + if (urls.count > 0) { + NSURL *url = urls.firstObject; + NSError *error = nil; + NSString *settingsString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error]; + if (error) { + NSLog(@"Error reading file: %@", error.localizedDescription); + UIAlertController *errorAlert = [UIAlertController alertControllerWithTitle:@"Error" message:@"Failed to read the settings file." preferredStyle:UIAlertControllerStyleAlert]; + [errorAlert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]]; + [settingsViewController presentViewController:errorAlert animated:YES completion:nil]; + return; + } + NSArray *lines = [settingsString 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]; + } + } + [settingsViewController reloadData]; + [[%c(GOOHUDManagerInternal) sharedInstance] showMessageMainThread:[%c(YTHUDMessage) messageWithText:@"Settings imported"]]; + SHOW_RELAUNCH_YT_SNACKBAR; + } + }; [settingsViewController presentViewController:documentPicker animated:YES completion:nil]; return YES; } else {