From 2915b02dd2505dea6bd51da016326328ef2ab5d7 Mon Sep 17 00:00:00 2001 From: "aricloverGitHub (INACTIVE)" <78001398+arichornlover@users.noreply.github.com> Date: Wed, 18 Dec 2024 01:05:54 -0600 Subject: [PATCH] Update uYouPlusSettings.xm --- Sources/uYouPlusSettings.xm | 94 +++++++++---------------------------- 1 file changed, 22 insertions(+), 72 deletions(-) diff --git a/Sources/uYouPlusSettings.xm b/Sources/uYouPlusSettings.xm index 34d3141..4b71744 100644 --- a/Sources/uYouPlusSettings.xm +++ b/Sources/uYouPlusSettings.xm @@ -250,34 +250,7 @@ extern NSBundle *uYouPlusBundle(); // Import Settings functionality UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"public.text"] inMode:UIDocumentPickerModeImport]; documentPicker.allowsMultipleSelection = NO; - __weak typeof(self) weakSelf = self; - documentPicker.didPickDocumentHandler = ^(NSArray * _Nonnull urls) { - __strong typeof(weakSelf) strongSelf = weakSelf; - if (strongSelf && 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; - } - }; + documentPicker.delegate = self; [settingsViewController presentViewController:documentPicker animated:YES completion:nil]; return YES; } else { @@ -1646,51 +1619,28 @@ NSString *cacheDescription = [NSString stringWithFormat:@"%@", GetCacheSize()]; // File Manager (Import Settings .txt) - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray *)urls { if (urls.count > 0) { - NSURL *fileURL = urls.firstObject; - NSString *fileContents = [NSString stringWithContentsOfURL:fileURL encoding:NSUTF8StringEncoding error:nil]; - if (fileContents.length > 0) { - 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; - } - } - } - if (success) { - YTSettingsViewController *settingsViewController = [self valueForKey:@"_settingsViewControllerDelegate"]; - [settingsViewController reloadData]; - } 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]]; - YTSettingsViewController *settingsViewController = [self valueForKey:@"_settingsViewControllerDelegate"]; - [settingsViewController presentViewController:errorAlert animated:YES completion:nil]; - } - }]]; - YTSettingsViewController *settingsViewController = [self valueForKey:@"_settingsViewControllerDelegate"]; - [settingsViewController presentViewController:confirmImportAlert animated:YES completion:nil]; + 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; } }