More changes to AppIconOptionsController.m

Also added stuff to let the average user knows what the code is gonna do.
This commit is contained in:
aricloverEXTRA 2025-09-21 15:07:53 -05:00 committed by GitHub
parent f3ab3f7cbf
commit 9ce69f4b4e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,93 +1,172 @@
#import "AppIconOptionsController.h" #import "AppIconOptionsController.h"
#import <YouTubeHeader/YTAssetLoader.h> #import "uYouPlus.h"
#import <notify.h> #import <notify.h>
@interface AppIconOptionsController () <UITableViewDataSource, UITableViewDelegate> @interface AppIconOptionsController () <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) UITableView *tableView; @property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSArray<NSString *> *appIcons; @property (strong, nonatomic) NSArray<NSString *> *appIcons; // list of icon folder names
@property (assign, nonatomic) NSInteger selectedIconIndex; @property (assign, nonatomic) NSInteger selectedIconIndex;
@end @end
@implementation AppIconOptionsController // Preference keys (must match SpringBoard-side tweak)
static NSString *const kPrefDomain = @"com.arichornlover.uYouEnhanced"; static NSString *const kPrefDomain = @"com.arichornlover.uYouEnhanced";
static NSString *const kPrefEnableIconOverride = @"appIconCustomization_enabled"; static NSString *const kPrefEnableIconOverride = @"appIconCustomization_enabled";
static NSString *const kPrefIconName = @"customAppIcon_name"; static NSString *const kPrefIconName = @"customAppIcon_name";
static NSString *const kPrefNotifyName = @"com.arichornlover.uYouEnhanced.prefschanged"; static NSString *const kPrefNotifyName = @"com.arichornlover.uYouEnhanced.prefschanged";
// Helper: path to the bundled resource bundle inside the tweak / preferences app
static NSString *BundlePath(void) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"uYouPlus" ofType:@"bundle"];
if (path) return path;
// fallback to the tweak support folder if running outside the bundle (development)
return @"/Library/Application Support/uYouEnhanced";
}
@implementation AppIconOptionsController
- (void)viewDidLoad { - (void)viewDidLoad {
[super viewDidLoad]; [super viewDidLoad];
self.title = @"Change App Icon"; self.title = @"Change App Icon";
self.selectedIconIndex = -1; self.selectedIconIndex = -1;
self.view.backgroundColor = [UIColor systemBackgroundColor];
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.tableView.dataSource = self; self.tableView.dataSource = self;
self.tableView.delegate = self; self.tableView.delegate = self;
[self.view addSubview:self.tableView]; [self.view addSubview:self.tableView];
NSString *path = [[NSBundle mainBundle] pathForResource:@"uYouPlus" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:path]; // Back button
NSArray *iconDirs = [bundle pathsForResourcesOfType:nil inDirectory:@"AppIcons"]; self.backButton = [UIButton buttonWithType:UIButtonTypeSystem];
NSMutableSet *iconNames = [NSMutableSet set]; [self.backButton setTitle:@"Back" forState:UIControlStateNormal];
for (NSString *p in iconDirs) { [self.backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
NSString *rel = [p stringByReplacingOccurrencesOfString:[bundle bundlePath] withString:@""]; UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithCustomView:self.backButton];
NSRange r = [p rangeOfString:@"AppIcons/"]; self.navigationItem.leftBarButtonItem = customBackButton;
if (r.location != NSNotFound) {
NSString *sub = [p substringFromIndex:r.location + r.length]; // Discover available icon folders
NSArray *comp = [sub componentsSeparatedByString:@"/"]; NSMutableSet<NSString *> *iconNames = [NSMutableSet set];
if (comp.count > 0 && comp[0].length > 0) [iconNames addObject:comp[0]];
// 1) Try bundled resources inside the tweak bundle
NSString *bundlePath = BundlePath();
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
if (bundle) {
// Look for directories under AppIcons/
NSString *appIconsDir = [bundle pathForResource:@"AppIcons" ofType:nil];
if (!appIconsDir) {
// If pathForResource didn't return the dir, try bundle path + "/AppIcons"
appIconsDir = [bundle.bundlePath stringByAppendingPathComponent:@"AppIcons"];
}
BOOL isDir = NO;
if ([[NSFileManager defaultManager] fileExistsAtPath:appIconsDir isDirectory:&isDir] && isDir) {
NSArray<NSString *> *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:appIconsDir error:nil] ?: @[];
for (NSString *entry in contents) {
NSString *full = [appIconsDir stringByAppendingPathComponent:entry];
BOOL entryIsDir = NO;
if ([[NSFileManager defaultManager] fileExistsAtPath:full isDirectory:&entryIsDir] && entryIsDir) {
if (entry && [entry isKindOfClass:[NSString class]] && entry.length > 0) {
[iconNames addObject:entry];
}
}
}
} else {
// If bundle returned resource file paths instead (older APIs), attempt to parse paths
NSArray *iconDirs = [bundle pathsForResourcesOfType:nil inDirectory:@"AppIcons"];
for (NSString *p in iconDirs) {
if (![p isKindOfClass:[NSString class]]) continue;
NSRange r = [p rangeOfString:@"AppIcons/"];
if (r.location != NSNotFound) {
NSString *sub = [p substringFromIndex:(r.location + r.length)];
NSArray<NSString *> *comp = [sub componentsSeparatedByString:@"/"];
NSString *first = (comp.count > 0 && [comp[0] isKindOfClass:[NSString class]]) ? comp[0] : nil;
if (first && first.length > 0) {
[iconNames addObject:first];
}
}
}
} }
} }
// 2) Also check installed support folder (/Library/Application Support/...) where package assets may be placed
NSString *supportBase = @"/Library/Application Support/uYouEnhanced/Icons"; NSString *supportBase = @"/Library/Application Support/uYouEnhanced/Icons";
NSFileManager *fm = [NSFileManager defaultManager]; NSFileManager *fm = [NSFileManager defaultManager];
if ([fm fileExistsAtPath:supportBase]) { BOOL supportIsDir = NO;
NSArray *dirs = [fm contentsOfDirectoryAtPath:supportBase error:nil]; if ([fm fileExistsAtPath:supportBase isDirectory:&supportIsDir] && supportIsDir) {
NSArray<NSString *> *dirs = [fm contentsOfDirectoryAtPath:supportBase error:nil] ?: @[];
for (NSString *d in dirs) { for (NSString *d in dirs) {
if (d.length > 0) [iconNames addObject:d]; if ([d isKindOfClass:[NSString class]] && d.length > 0) {
NSString *full = [supportBase stringByAppendingPathComponent:d];
BOOL isDir = NO;
if ([fm fileExistsAtPath:full isDirectory:&isDir] && isDir) {
[iconNames addObject:d];
}
}
} }
} }
// Build sorted list
self.appIcons = [[iconNames allObjects] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; self.appIcons = [[iconNames allObjects] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
// Load saved selection if present
NSDictionary *prefs = [NSDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"/var/mobile/Library/Preferences/%@.plist", kPrefDomain]] ?: @{}; NSDictionary *prefs = [NSDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"/var/mobile/Library/Preferences/%@.plist", kPrefDomain]] ?: @{};
NSString *savedIcon = prefs[kPrefIconName]; NSString *savedIcon = prefs[kPrefIconName];
if (savedIcon) { if (savedIcon && [savedIcon isKindOfClass:[NSString class]]) {
NSInteger idx = [self.appIcons indexOfObject:savedIcon]; NSInteger idx = [self.appIcons indexOfObject:savedIcon];
if (idx != NSNotFound) self.selectedIconIndex = idx; if (idx != NSNotFound) self.selectedIconIndex = idx;
} }
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
self.navigationItem.leftBarButtonItem = back; if (self.appIcons.count == 0) {
// Friendly message if no icons found
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectInset(self.view.bounds, 20, 20)];
lbl.text = @"No custom icons found. Place icon folders in the tweak bundle under AppIcons/ or in /Library/Application Support/uYouEnhanced/Icons/";
lbl.numberOfLines = 0;
lbl.textAlignment = NSTextAlignmentCenter;
lbl.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:lbl];
}
} }
#pragma mark - UITableViewDataSource #pragma mark - Table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// +1 for Reset option
return self.appIcons.count + 1; return self.appIcons.count + 1;
} }
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 80.0; return 80.0;
} }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellId = @"AppIconCell"; static NSString *CellId = @"AppIconCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellId]; UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellId];
if (!cell) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellId]; if (!cell) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellId];
cell.selectionStyle = UITableViewCellSelectionStyleDefault; cell.selectionStyle = UITableViewCellSelectionStyleDefault;
if (indexPath.row == 0) { if (indexPath.row == 0) {
cell.textLabel.text = @"Reset to default"; cell.textLabel.text = @"Reset to default";
cell.detailTextLabel.text = @"Restore the original app icon";
cell.imageView.image = nil; cell.imageView.image = nil;
cell.accessoryType = UITableViewCellAccessoryNone; cell.accessoryType = (self.selectedIconIndex == -1) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
} else { } else {
NSString *iconName = self.appIcons[indexPath.row - 1]; NSString *iconName = self.appIcons[indexPath.row - 1];
cell.textLabel.text = iconName; cell.textLabel.text = iconName;
cell.detailTextLabel.text = @"Tap to request SpringBoard to apply this icon";
// Attempt preview: check bundle then support folder
UIImage *preview = nil; UIImage *preview = nil;
NSString *bundlePreviewPath = [[NSBundle mainBundle] pathForResource:@"uYouPlus" ofType:@"bundle"]; // candidate file names
NSBundle *bundle = [NSBundle bundleWithPath:bundlePreviewPath]; NSArray<NSString *> *candidates = @[@"AppIcon60x60@3x.png",@"AppIcon60x60@2x.png",@"Icon@3x.png",@"Icon@2x.png",@"Icon.png"];
NSArray *candidates = @[ // bundle first
[NSString stringWithFormat:@"AppIcons/%@/AppIcon60x60@3x.png", iconName], NSString *bundlePath = BundlePath();
[NSString stringWithFormat:@"AppIcons/%@/AppIcon60x60@2x.png", iconName], NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
[NSString stringWithFormat:@"AppIcons/%@/Icon@3x.png", iconName],
[NSString stringWithFormat:@"AppIcons/%@/Icon@2x.png", iconName],
[NSString stringWithFormat:@"AppIcons/%@/Icon.png", iconName]
];
for (NSString *c in candidates) { for (NSString *c in candidates) {
NSString *full = [bundle pathForResource:c ofType:nil]; if (![c isKindOfClass:[NSString class]]) continue;
if (!full) full = [@"/Library/Application Support/uYouEnhanced/Icons" stringByAppendingPathComponent:[iconName stringByAppendingPathComponent:[c lastPathComponent]]]; NSString *rel = [NSString stringWithFormat:@"AppIcons/%@/%@", iconName, c];
NSString *full = [bundle pathForResource:rel ofType:nil];
if (!full) {
full = [@"/Library/Application Support/uYouEnhanced/Icons" stringByAppendingPathComponent:[iconName stringByAppendingPathComponent:c]];
}
if ([[NSFileManager defaultManager] fileExistsAtPath:full]) { if ([[NSFileManager defaultManager] fileExistsAtPath:full]) {
preview = [UIImage imageWithContentsOfFile:full]; preview = [UIImage imageWithContentsOfFile:full];
break; break;
@ -96,47 +175,61 @@ static NSString *const kPrefNotifyName = @"com.arichornlover.uYouEnhanced.prefsc
cell.imageView.image = preview; cell.imageView.image = preview;
cell.imageView.layer.cornerRadius = 12.0; cell.imageView.layer.cornerRadius = 12.0;
cell.imageView.clipsToBounds = YES; cell.imageView.clipsToBounds = YES;
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
cell.accessoryType = ((indexPath.row - 1) == self.selectedIconIndex) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; cell.accessoryType = ((indexPath.row - 1) == self.selectedIconIndex) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
} }
return cell; return cell;
} }
#pragma mark - UITableViewDelegate #pragma mark - Table Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES]; - (void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tv deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.row == 0) { if (indexPath.row == 0) {
// Reset: disable override and notify SpringBoard
self.selectedIconIndex = -1; self.selectedIconIndex = -1;
NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:[NSString stringWithFormat:@"/var/mobile/Library/Preferences/%@.plist", kPrefDomain]] ?: [NSMutableDictionary dictionary]; NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:[NSString stringWithFormat:@"/var/mobile/Library/Preferences/%@.plist", kPrefDomain]] ?: [NSMutableDictionary dictionary];
prefs[kPrefEnableIconOverride] = @NO; prefs[kPrefEnableIconOverride] = @NO;
[prefs writeToFile:[NSString stringWithFormat:@"/var/mobile/Library/Preferences/%@.plist", kPrefDomain] atomically:YES]; [prefs writeToFile:[NSString stringWithFormat:@"/var/mobile/Library/Preferences/%@.plist", kPrefDomain] atomically:YES];
notify_post([kPrefNotifyName UTF8String]); notify_post([kPrefNotifyName UTF8String]);
[self.tableView reloadData]; [self.tableView reloadData];
[self showAlertWithTitle:@"Success" message:@"Icon reset requested. SpringBoard should update."]; [self showAlertWithTitle:@"Requested" message:@"Icon reset requested. SpringBoard will attempt to update."];
return; return;
} }
self.selectedIconIndex = indexPath.row - 1; self.selectedIconIndex = indexPath.row - 1;
NSString *iconName = self.appIcons[self.selectedIconIndex]; NSString *iconName = self.appIcons[self.selectedIconIndex];
// Persist preference
NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:[NSString stringWithFormat:@"/var/mobile/Library/Preferences/%@.plist", kPrefDomain]] ?: [NSMutableDictionary dictionary]; NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:[NSString stringWithFormat:@"/var/mobile/Library/Preferences/%@.plist", kPrefDomain]] ?: [NSMutableDictionary dictionary];
prefs[kPrefEnableIconOverride] = @YES; prefs[kPrefEnableIconOverride] = @YES;
prefs[kPrefIconName] = iconName; prefs[kPrefIconName] = iconName;
BOOL wrote = [prefs writeToFile:[NSString stringWithFormat:@"/var/mobile/Library/Preferences/%@.plist", kPrefDomain] atomically:YES]; BOOL ok = [prefs writeToFile:[NSString stringWithFormat:@"/var/mobile/Library/Preferences/%@.plist", kPrefDomain] atomically:YES];
if (!wrote) { if (!ok) {
[self showAlertWithTitle:@"Error" message:@"Failed to save preference"]; [self showAlertWithTitle:@"Error" message:@"Failed to save preference"];
return; return;
} }
// Notify SpringBoard to apply change
notify_post([kPrefNotifyName UTF8String]); notify_post([kPrefNotifyName UTF8String]);
[self.tableView reloadData]; [self.tableView reloadData];
[self showAlertWithTitle:@"Success" message:@"Requested icon change. SpringBoard should update."]; [self showAlertWithTitle:@"Requested" message:@"Icon change requested. SpringBoard will attempt to update."];
} }
#pragma mark - UI #pragma mark - Utilities
- (void)showAlertWithTitle:(NSString *)title message:(NSString *)message { - (void)showAlertWithTitle:(NSString *)title message:(NSString *)message {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]; UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:okAction]; [alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil]; [self presentViewController:alert animated:YES completion:nil];
} }
- (void)back { - (void)back {
[self.navigationController popViewControllerAnimated:YES]; [self.navigationController popViewControllerAnimated:YES];
} }
@end @end