Update AppIconOptionsController.m

This commit is contained in:
aricloverEXTRA 2026-02-24 15:45:53 -06:00 committed by GitHub
parent f099017820
commit 972ab19a8c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -12,25 +12,6 @@ static NSString *BundlePath(void) {
return @"/Library/Application Support/uYouEnhanced";
}
static NSString *GetPrefsPath(void) {
// Use standard preferences directory
return [NSString stringWithFormat:@"/var/mobile/Library/Preferences/%@.plist", kPrefDomain];
}
static BOOL EnsurePrefsDirectoryExists(void) {
NSFileManager *fm = [NSFileManager defaultManager];
NSString *prefsDir = @"/var/mobile/Library/Preferences";
NSError *error = nil;
if (![fm fileExistsAtPath:prefsDir]) {
if (![fm createDirectoryAtPath:prefsDir withIntermediateDirectories:YES attributes:nil error:&error]) {
NSLog(@"[uYouEnhanced] Failed to create preferences directory: %@", error);
return NO;
}
}
return YES;
}
@interface AppIconOptionsController ()
@property (strong, nonatomic) UITableView *tableView;
@ -87,11 +68,6 @@ static BOOL EnsurePrefsDirectoryExists(void) {
UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithCustomView:self.backButton];
self.navigationItem.leftBarButtonItem = customBackButton;
[self loadAppIcons];
[self loadSavedIconPreference];
}
- (void)loadAppIcons {
NSMutableSet<NSString *> *iconNames = [NSMutableSet set];
NSFileManager *fm = [NSFileManager defaultManager];
@ -100,14 +76,63 @@ static BOOL EnsurePrefsDirectoryExists(void) {
if (bundle) {
NSString *appIconsDir = [bundle.bundlePath stringByAppendingPathComponent:@"AppIcons"];
[self scanIconsInDirectory:appIconsDir fileManager:fm iconNames:iconNames];
BOOL isDir = NO;
if ([fm fileExistsAtPath:appIconsDir isDirectory:&isDir] && isDir) {
NSArray *contents = [fm contentsOfDirectoryAtPath:appIconsDir error:nil] ?: @[];
for (NSString *entry in contents) {
NSString *full = [appIconsDir stringByAppendingPathComponent:entry];
BOOL entryIsDir = NO;
if ([fm fileExistsAtPath:full isDirectory:&entryIsDir]) {
if (entryIsDir) {
[iconNames addObject:entry];
} else {
NSString *ext = entry.pathExtension.lowercaseString;
if ([ext isEqualToString:@"png"]) {
NSString *name = [entry stringByDeletingPathExtension];
if (name.length > 0) {
[iconNames addObject:name];
}
}
}
}
}
}
}
NSString *supportBase = @"/Library/Application Support/uYouEnhanced/AppIcons";
[self scanIconsInDirectory:supportBase fileManager:fm iconNames:iconNames];
BOOL supportIsDir = NO;
if ([fm fileExistsAtPath:supportBase isDirectory:&supportIsDir] && supportIsDir) {
NSArray *contents = [fm contentsOfDirectoryAtPath:supportBase error:nil] ?: @[];
for (NSString *entry in contents) {
NSString *full = [supportBase stringByAppendingPathComponent:entry];
BOOL isDir = NO;
if ([fm fileExistsAtPath:full isDirectory:&isDir]) {
if (isDir) {
[iconNames addObject:entry];
} else {
NSString *ext = entry.pathExtension.lowercaseString;
if ([ext isEqualToString:@"png"]) {
NSString *name = [entry stringByDeletingPathExtension];
if (name.length > 0) {
[iconNames addObject:name];
}
}
}
}
}
}
self.appIcons = [[iconNames allObjects] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSDictionary *prefs = [NSDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"/var/mobile/Library/Preferences/%@.plist", kPrefDomain]] ?: @{};
NSString *savedIcon = prefs[kPrefIconName];
if (savedIcon) {
NSInteger idx = [self.appIcons indexOfObject:savedIcon];
if (idx != NSNotFound) self.selectedIconIndex = idx;
}
if (self.appIcons.count == 0) {
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectInset(self.view.bounds, 20, 20)];
lbl.text = @"No custom icons found. Place PNGs or icon folders in uYouPlus.bundle/AppIcons/ or /Library/Application Support/uYouEnhanced/AppIcons/";
@ -118,53 +143,6 @@ static BOOL EnsurePrefsDirectoryExists(void) {
}
}
- (void)scanIconsInDirectory:(NSString *)dirPath fileManager:(NSFileManager *)fm iconNames:(NSMutableSet *)iconNames {
BOOL isDir = NO;
if (![fm fileExistsAtPath:dirPath isDirectory:&isDir] || !isDir) {
return;
}
NSError *error = nil;
NSArray *contents = [fm contentsOfDirectoryAtPath:dirPath error:&error];
if (error) {
NSLog(@"[uYouEnhanced] Error scanning directory %@: %@", dirPath, error);
return;
}
for (NSString *entry in contents) {
NSString *full = [dirPath stringByAppendingPathComponent:entry];
BOOL entryIsDir = NO;
if ([fm fileExistsAtPath:full isDirectory:&entryIsDir]) {
if (entryIsDir) {
[iconNames addObject:entry];
} else {
NSString *ext = entry.pathExtension.lowercaseString;
if ([ext isEqualToString:@"png"]) {
NSString *name = [entry stringByDeletingPathExtension];
if (name.length > 0) {
[iconNames addObject:name];
}
}
}
}
}
}
- (void)loadSavedIconPreference {
NSString *prefsPath = GetPrefsPath();
NSDictionary *prefs = [NSDictionary dictionaryWithContentsOfFile:prefsPath];
NSString *savedIcon = prefs[kPrefIconName];
if (savedIcon) {
NSInteger idx = [self.appIcons indexOfObject:savedIcon];
if (idx != NSNotFound) {
self.selectedIconIndex = idx;
}
} else {
self.selectedIconIndex = -1;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.appIcons.count + 1;
}
@ -190,21 +168,7 @@ static BOOL EnsurePrefsDirectoryExists(void) {
cell.textLabel.text = iconName;
cell.detailTextLabel.text = @"Tap to apply this icon";
UIImage *preview = [self loadIconPreviewForName:iconName];
if (preview) {
cell.imageView.image = preview;
cell.imageView.layer.cornerRadius = 12.0;
cell.imageView.clipsToBounds = YES;
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
}
cell.accessoryType = ((indexPath.row - 1) == self.selectedIconIndex) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
- (UIImage *)loadIconPreviewForName:(NSString *)iconName {
UIImage *preview = nil;
NSArray<NSString *> *candidates = @[
@"AppIcon60x60@3x.png",
@"AppIcon60x60@2x.png",
@ -218,130 +182,114 @@ static BOOL EnsurePrefsDirectoryExists(void) {
NSString *supportBase = @"/Library/Application Support/uYouEnhanced/AppIcons";
NSFileManager *fm = [NSFileManager defaultManager];
BOOL found = NO;
if (bundle) {
UIImage *image = [self loadIconFromDirectory:[bundle.bundlePath stringByAppendingPathComponent:[NSString stringWithFormat:@"AppIcons/%@", iconName]]
candidates:candidates
fileManager:fm];
if (image) return image;
}
UIImage *image = [self loadIconFromDirectory:[supportBase stringByAppendingPathComponent:iconName]
candidates:candidates
fileManager:fm];
return image;
}
- (UIImage *)loadIconFromDirectory:(NSString *)dir candidates:(NSArray *)candidates fileManager:(NSFileManager *)fm {
BOOL isDir = NO;
if (![fm fileExistsAtPath:dir isDirectory:&isDir]) {
return nil;
}
if (isDir) {
for (NSString *candidate in candidates) {
NSString *imagePath = [dir stringByAppendingPathComponent:candidate];
if ([fm fileExistsAtPath:imagePath]) {
return [UIImage imageWithContentsOfFile:imagePath];
NSString *dir = [bundle.bundlePath stringByAppendingPathComponent:[NSString stringWithFormat:@"AppIcons/%@", iconName]];
BOOL isDir = NO;
if ([fm fileExistsAtPath:dir isDirectory:&isDir] && isDir) {
for (NSString *c in candidates) {
NSString *imagePath = [dir stringByAppendingPathComponent:c];
if ([fm fileExistsAtPath:imagePath]) {
preview = [UIImage imageWithContentsOfFile:imagePath];
found = YES;
break;
}
}
if (!found) {
NSArray *files = [fm contentsOfDirectoryAtPath:dir error:nil];
for (NSString *file in files) {
NSString *ext = file.pathExtension.lowercaseString;
if ([ext isEqualToString:@"png"]) {
NSString *path = [dir stringByAppendingPathComponent:file];
preview = [UIImage imageWithContentsOfFile:path];
found = YES;
break;
}
}
}
} else {
NSString *pngPath = [[bundle.bundlePath stringByAppendingPathComponent:@"AppIcons"] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", iconName]];
if ([fm fileExistsAtPath:pngPath]) {
preview = [UIImage imageWithContentsOfFile:pngPath];
found = YES;
}
}
}
NSError *error = nil;
NSArray *files = [fm contentsOfDirectoryAtPath:dir error:&error];
for (NSString *file in files) {
if ([file.pathExtension.lowercaseString isEqualToString:@"png"]) {
NSString *path = [dir stringByAppendingPathComponent:file];
UIImage *image = [UIImage imageWithContentsOfFile:path];
if (image) return image;
if (!found) {
NSString *dir = [supportBase stringByAppendingPathComponent:iconName];
BOOL isDir = NO;
if ([fm fileExistsAtPath:dir isDirectory:&isDir] && isDir) {
for (NSString *c in candidates) {
NSString *imagePath = [dir stringByAppendingPathComponent:c];
if ([fm fileExistsAtPath:imagePath]) {
preview = [UIImage imageWithContentsOfFile:imagePath];
found = YES;
break;
}
}
if (!found) {
NSArray *files = [fm contentsOfDirectoryAtPath:dir error:nil];
for (NSString *file in files) {
NSString *ext = file.pathExtension.lowercaseString;
if ([ext isEqualToString:@"png"]) {
NSString *path = [dir stringByAppendingPathComponent:file];
preview = [UIImage imageWithContentsOfFile:path];
found = YES;
break;
}
}
}
} else {
NSString *pngPath = [supportBase stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", iconName]];
if ([fm fileExistsAtPath:pngPath]) {
preview = [UIImage imageWithContentsOfFile:pngPath];
found = YES;
}
}
} else {
NSString *pngPath = [NSString stringWithFormat:@"%@.png", dir];
if ([fm fileExistsAtPath:pngPath]) {
return [UIImage imageWithContentsOfFile:pngPath];
}
}
return nil;
cell.imageView.image = preview;
cell.imageView.layer.cornerRadius = 12.0;
cell.imageView.clipsToBounds = YES;
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
cell.accessoryType = ((indexPath.row - 1) == self.selectedIconIndex) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
- (void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tv deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.row == 0) {
[self resetIconPreference];
return;
}
NSString *iconName = self.appIcons[indexPath.row - 1];
[self setIconPreference:iconName];
}
- (void)resetIconPreference {
NSString *prefsPath = GetPrefsPath();
NSString *prefsPath = [NSString stringWithFormat:@"/var/mobile/Library/Preferences/%@.plist", kPrefDomain];
NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:prefsPath] ?: [NSMutableDictionary dictionary];
self.selectedIconIndex = -1;
prefs[kPrefEnableIconOverride] = @NO;
[prefs removeObjectForKey:kPrefIconName];
if ([self savePreferences:prefs toPath:prefsPath]) {
if (indexPath.row == 0) {
self.selectedIconIndex = -1;
prefs[kPrefEnableIconOverride] = @NO;
[prefs writeToFile:prefsPath atomically:YES];
notify_post([kPrefNotifyName UTF8String]);
[self.tableView reloadData];
[self showAlertWithTitle:@"Requested" message:@"Icon reset requested."];
} else {
[self showAlertWithTitle:@"Error" message:@"Failed to save preference"];
}
}
- (void)setIconPreference:(NSString *)iconName {
NSString *prefsPath = GetPrefsPath();
NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:prefsPath] ?: [NSMutableDictionary dictionary];
NSInteger idx = [self.appIcons indexOfObject:iconName];
if (idx == NSNotFound) {
[self showAlertWithTitle:@"Error" message:@"Selected icon not found"];
return;
}
self.selectedIconIndex = idx;
self.selectedIconIndex = indexPath.row - 1;
NSString *iconName = self.appIcons[self.selectedIconIndex];
prefs[kPrefEnableIconOverride] = @YES;
prefs[kPrefIconName] = iconName;
if ([self savePreferences:prefs toPath:prefsPath]) {
notify_post([kPrefNotifyName UTF8String]);
[self.tableView reloadData];
[self showAlertWithTitle:@"Requested" message:@"Icon change requested."];
} else {
BOOL ok = [prefs writeToFile:prefsPath atomically:YES];
if (!ok) {
[self showAlertWithTitle:@"Error" message:@"Failed to save preference"];
}
}
- (BOOL)savePreferences:(NSDictionary *)prefs toPath:(NSString *)path {
// Ensure preferences directory exists
if (!EnsurePrefsDirectoryExists()) {
NSLog(@"[uYouEnhanced] Could not ensure preferences directory exists");
return NO;
return;
}
NSFileManager *fm = [NSFileManager defaultManager];
// Write preferences atomically
BOOL success = [prefs writeToFile:path atomically:YES];
if (!success) {
NSLog(@"[uYouEnhanced] Failed to write preferences to %@", path);
return NO;
}
// Verify file was actually written
if (![fm fileExistsAtPath:path]) {
NSLog(@"[uYouEnhanced] Preferences file does not exist after write: %@", path);
return NO;
}
NSLog(@"[uYouEnhanced] Successfully saved preferences to %@", path);
return YES;
notify_post([kPrefNotifyName UTF8String]);
[self.tableView reloadData];
[self showAlertWithTitle:@"Requested" message:@"Icon change requested."];
}
- (void)showAlertWithTitle:(NSString *)title message:(NSString *)message {