mirror of
https://github.com/arichornlover/uYouEnhanced.git
synced 2026-04-21 11:52:00 +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
|
// Import Settings functionality
|
||||||
UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"public.text"] inMode:UIDocumentPickerModeImport];
|
UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"public.text"] inMode:UIDocumentPickerModeImport];
|
||||||
documentPicker.allowsMultipleSelection = NO;
|
documentPicker.allowsMultipleSelection = NO;
|
||||||
__weak typeof(self) weakSelf = self;
|
documentPicker.delegate = 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;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
[settingsViewController presentViewController:documentPicker animated:YES completion:nil];
|
[settingsViewController presentViewController:documentPicker animated:YES completion:nil];
|
||||||
return YES;
|
return YES;
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -1646,51 +1619,28 @@ NSString *cacheDescription = [NSString stringWithFormat:@"%@", GetCacheSize()];
|
||||||
// File Manager (Import Settings .txt)
|
// File Manager (Import Settings .txt)
|
||||||
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
|
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
|
||||||
if (urls.count > 0) {
|
if (urls.count > 0) {
|
||||||
NSURL *fileURL = urls.firstObject;
|
NSURL *url = urls.firstObject;
|
||||||
NSString *fileContents = [NSString stringWithContentsOfURL:fileURL encoding:NSUTF8StringEncoding error:nil];
|
NSError *error = nil;
|
||||||
if (fileContents.length > 0) {
|
NSString *settingsString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
|
||||||
UIAlertController *confirmImportAlert = [UIAlertController alertControllerWithTitle:@"Confirm Import"
|
if (error) {
|
||||||
message:@"Do you want to import the settings from the selected file?"
|
NSLog(@"Error reading file: %@", error.localizedDescription);
|
||||||
preferredStyle:UIAlertControllerStyleAlert];
|
UIAlertController *errorAlert = [UIAlertController alertControllerWithTitle:@"Error" message:@"Failed to read the settings file." preferredStyle:UIAlertControllerStyleAlert];
|
||||||
[confirmImportAlert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
|
[errorAlert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
|
||||||
[confirmImportAlert addAction:[UIAlertAction actionWithTitle:@"Import" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
|
[settingsViewController presentViewController:errorAlert animated:YES completion:nil];
|
||||||
NSDictionary *currentSettings = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
|
return;
|
||||||
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];
|
|
||||||
}
|
}
|
||||||
|
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