mirror of
https://github.com/arichornlover/uYouEnhanced.git
synced 2026-01-11 22:40:19 +00:00
Update uYouPlusSettings.xm
This commit is contained in:
parent
91bf3debc6
commit
2915b02dd2
1 changed files with 22 additions and 72 deletions
|
|
@ -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<NSURL *> * _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<NSURL *> *)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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue