Update AppIconOptionsController.m

This commit is contained in:
arichornlover 2024-06-12 17:31:24 -05:00 committed by GitHub
parent 53eb25f260
commit c258c339fe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4,7 +4,7 @@
@interface AppIconOptionsController () <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSArray<NSString *> *appIcons;
@property (strong, nonatomic) NSArray<NSString *> *appIconFolders;
@property (assign, nonatomic) NSInteger selectedIconIndex;
@end
@ -18,48 +18,29 @@
[self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"YTSans-Bold" size:22], NSForegroundColorAttributeName: [UIColor whiteColor]}];
self.selectedIconIndex = -1;
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.view addSubview:self.tableView];
self.appIcons = @[@"White", @"YTLitePlus", @"Blue", @"Outline", @"2012", @"2013", @"2007", @"Black", @"Oreo", @"uYou", @"2012_Cyan", @"uYouPlus"];
self.appIconFolders = @[@"2013", @"2017_Gold", @"Gold", @"Shorts", @"White", @"YTLitePlus", @"Blue", @"Outline", @"2012", @"2007", @"Black", @"Oreo", @"uYou", @"2012_Cyan", @"uYouPlus"];
self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
NSBundle *backIcon = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"uYouPlus" ofType:@"bundle"]];
UIImage *backImage = [UIImage imageNamed:@"Back.png" inBundle:backIcon compatibleWithTraitCollection:nil];
backImage = [self resizeImage:backImage newSize:CGSizeMake(24, 24)];
backImage = [backImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[self.backButton setTintColor:[UIColor whiteColor]];
[self.backButton setImage:backImage forState:UIControlStateNormal];
[self.backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[self.backButton setFrame:CGRectMake(0, 0, 24, 24)];
UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithCustomView:self.backButton];
self.navigationItem.leftBarButtonItem = customBackButton;
UIColor *buttonColor = [UIColor colorWithRed:203.0/255.0 green:22.0/255.0 blue:51.0/255.0 alpha:1.0];
UIImage *resetImage = [UIImage systemImageNamed:@"arrow.clockwise.circle.fill"];
UIBarButtonItem *resetButton = [[UIBarButtonItem alloc] initWithImage:resetImage style:UIBarButtonItemStylePlain target:self action:@selector(resetIcon)];
resetButton.tintColor = buttonColor;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[self createBackImage] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
UIImage *saveImage = [UIImage systemImageNamed:@"square.and.arrow.up.fill"];
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithImage:saveImage style:UIBarButtonItemStylePlain target:self action:@selector(saveIcon)];
saveButton.tintColor = buttonColor;
self.navigationItem.rightBarButtonItems = @[[self createBarButtonItemWithSystemImage:@"arrow.clockwise.circle.fill" action:@selector(resetIcon)], [self createBarButtonItemWithSystemImage:@"square.and.arrow.up.fill" action:@selector(saveIcon)];
self.navigationItem.rightBarButtonItems = @[saveButton, resetButton];
NSString *path = [[NSBundle mainBundle] pathForResource:@"uYouPlus" ofType:@"bundle"];
NSString *path = [[NSBundle mainBundle] pathForResource:@"AppIcons" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:path];
self.appIcons = [bundle pathsForResourcesOfType:@"png" inDirectory:@"AppIcons"];
self.appIconFolders = [bundle pathsForResourcesOfType:nil inDirectory:@"AppIcons"];
if (![UIApplication sharedApplication].supportsAlternateIcons) {
NSLog(@"Alternate icons are not supported on this device.");
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.appIcons.count;
return self.appIconFolders.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
@ -71,12 +52,12 @@
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
NSString *iconName = self.appIcons[indexPath.row];
cell.textLabel.text = iconName;
UIImage *iconImage = [UIImage imageNamed:iconName];
cell.imageView.image = [self createRoundedImage:iconImage size:CGSizeMake(40, 40)];
NSString *iconFolder = self.appIconFolders[indexPath.row];
cell.textLabel.text = iconFolder.lastPathComponent;
UIImage *iconImage = [self appIconPreviewForFolder:iconFolder];
cell.imageView.image = iconImage;
if (indexPath.row == self.selectedIconIndex) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
@ -89,7 +70,7 @@
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
self.selectedIconIndex = indexPath.row;
[self.tableView reloadData];
}
@ -113,25 +94,19 @@
return;
}
NSString *iconName = self.appIcons[self.selectedIconIndex];
NSString *path = [[NSBundle mainBundle] pathForResource:@"uYouPlus" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:path];
NSString *imagePath = [bundle pathForResource:iconName ofType:@"png"];
UIImage *iconImage = [UIImage imageWithContentsOfFile:imagePath];
NSString *iconFolder = self.appIconFolders[self.selectedIconIndex];
UIImage *iconImage = [self appIconPreviewForFolder:iconFolder];
if (!iconImage) {
NSLog(@"Failed to load custom icon image");
return;
}
UIImage *roundedIconImage = [self createRoundedImage:iconImage size:CGSizeMake(120, 120)]; // Adjust size as needed
NSData *imageData = UIImagePNGRepresentation(roundedIconImage);
NSString *newIconPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@_custom", iconName] ofType:@"png"];
NSData *imageData = UIImagePNGRepresentation(iconImage);
NSString *newIconPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@_custom", iconFolder.lastPathComponent] ofType:@"png"];
[imageData writeToFile:newIconPath atomically:YES];
[[UIApplication sharedApplication] setAlternateIconName:[NSString stringWithFormat:@"%@_custom", iconName] completionHandler:^(NSError * _Nullable error) {
[[UIApplication sharedApplication] setAlternateIconName:[NSString stringWithFormat:@"%@_custom", iconFolder.lastPathComponent] completionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"Error setting alternate icon: %@", error.localizedDescription);
[self showAlertWithTitle:@"Error" message:@"Failed to set alternate icon"];
@ -143,6 +118,16 @@
}];
}
- (UIImage *)appIconPreviewForFolder:(NSString *)folder {
NSString *icon2xPath = [folder stringByAppendingPathComponent:@"@2.png"];
NSString *icon3xPath = [folder stringByAppendingPathComponent:@"@3.png"];
UIImage *icon2xImage = [UIImage imageNamed:icon2xPath];
UIImage *roundedIconImage = [self createRoundedImage:icon2xImage size:CGSizeMake(120, 120)];
return roundedIconImage;
}
- (UIImage *)createRoundedImage:(UIImage *)image size:(CGSize)size {
UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, size.width, size.height) cornerRadius:size.width * 0.1]; // Adjust the corner radius as needed
@ -150,17 +135,38 @@
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return roundedImage;
}
- (UIImage *)resizeImage:(UIImage *)image newSize:(CGSize)newSize { // Back Button
- (UIImage *)createBackImage {
NSString *path = [[NSBundle mainBundle] pathForResource:@"uYouPlus" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:path];
UIImage *backImage = [UIImage imageNamed:@"Back.png" inBundle:bundle compatibleWithTraitCollection:nil];
backImage = [self resizeImage:backImage newSize:CGSizeMake(24, 24)];
backImage = [backImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setTintColor:[UIColor whiteColor]];
[backButton setImage:backImage forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[backButton setFrame:CGRectMake(0, 0, 24, 24)];
return [backButton imageForState:UIControlStateNormal];
}
- (UIBarButtonItem *)createBarButtonItemWithSystemImage:(NSString *)imageName action:(SEL)action {
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage systemImageNamed:imageName] style:UIBarButtonItemStylePlain target:self action:action];
UIColor *buttonColor = [UIColor colorWithRed:203.0/255.0 green:22.0/255.0 blue:51.0/255.0 alpha:1.0];
barButtonItem.tintColor = buttonColor;
return barButtonItem;
}
- (UIImage *)resizeImage:(UIImage *)image newSize:(CGSize)newSize {
UIGraphicsBeginImageContextWithOptions(newSize, NO, [UIScreen mainScreen].scale);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
}
- (void)showAlertWithTitle:(NSString *)title message:(NSString *)message {
dispatch_async(dispatch_get_main_queue(), ^{