Improve AppIconOptionsController Menu

Some of the improvements were suggested by Balackburn. Thanks!
This commit is contained in:
arichornlover
2024-04-16 01:49:12 -05:00
committed by GitHub
parent 7058a72305
commit c2ae647567

View File

@@ -3,10 +3,9 @@
@interface AppIconOptionsController () <UITableViewDataSource, UITableViewDelegate> @interface AppIconOptionsController () <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) UITableView *tableView; @property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) UIImageView *iconPreview;
@property (strong, nonatomic) NSArray<NSString *> *appIcons; @property (strong, nonatomic) NSArray<NSString *> *appIcons;
@property (assign, nonatomic) NSInteger selectedIconIndex; @property (assign, nonatomic) NSInteger selectedIconIndex;
@property (assign, nonatomic) NSInteger defaultIconIndex; @property (strong, nonatomic) UIImageView *backButton;
@end @end
@@ -14,25 +13,34 @@
- (void)viewDidLoad { - (void)viewDidLoad {
[super viewDidLoad]; [super viewDidLoad];
self.selectedIconIndex = 0; self.title = @"Change App Icon";
self.defaultIconIndex = 0; [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"YTSans-Bold" size:17], NSForegroundColorAttributeName: [UIColor whiteColor]}];
self.selectedIconIndex = -1;
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
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];
UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStylePlain target:self action:@selector(close)]; UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
self.navigationItem.leftBarButtonItem = closeButton; UIImage *backImage = [UIImage imageNamed:@"yt_outline_chevron_left_ios_24pt" inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil];
if (!backImage) {
backButton.image = [UIImage systemImageNamed:@"chevron.backward"];
} else {
backButton.image = backImage;
}
[backButton setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: [UIFont fontWithName:@"YTSans-Medium" size:17]} forState:UIControlStateNormal];
self.navigationItem.leftBarButtonItem = backButton;
UIBarButtonItem *resetButton = [[UIBarButtonItem alloc] initWithTitle:@"Reset" style:UIBarButtonItemStylePlain target:self action:@selector(resetIcon)];
[resetButton setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: [UIFont fontWithName:@"YTSans-Medium" size:17]} forState:UIControlStateNormal];
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(saveIcon)]; UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(saveIcon)];
self.navigationItem.rightBarButtonItem = saveButton; [saveButton setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: [UIFont fontWithName:@"YTSans-Medium" size:17]} forState:UIControlStateNormal];
self.iconPreview = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width - 80, self.view.bounds.size.height - 80, 60, 60)]; self.navigationItem.rightBarButtonItems = @[saveButton, resetButton];
self.iconPreview.layer.cornerRadius = 10.0;
self.iconPreview.clipsToBounds = YES;
[self.view addSubview:self.iconPreview];
NSString *path = [[NSBundle mainBundle] pathForResource:@"YTLitePlus" ofType:@"bundle"]; NSString *path = [[NSBundle mainBundle] pathForResource:@"YTLitePlus" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:path]; NSBundle *bundle = [NSBundle bundleWithPath:path];
@@ -47,6 +55,10 @@
return self.appIcons.count; return self.appIcons.count;
} }
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 60.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) { if (!cell) {
@@ -55,41 +67,44 @@
NSString *iconPath = self.appIcons[indexPath.row]; NSString *iconPath = self.appIcons[indexPath.row];
cell.textLabel.text = [iconPath.lastPathComponent stringByDeletingPathExtension]; cell.textLabel.text = [iconPath.lastPathComponent stringByDeletingPathExtension];
UIImage *iconImage = [UIImage imageWithContentsOfFile:iconPath];
cell.imageView.image = [self resizedImageWithImage:iconImage];
UIImage *iconImage = [UIImage imageWithContentsOfFile:iconPath];
cell.imageView.image = iconImage;
cell.imageView.layer.cornerRadius = 10.0;
cell.imageView.clipsToBounds = YES;
if (indexPath.row == self.selectedIconIndex) { if (indexPath.row == self.selectedIconIndex) {
cell.accessoryType = UITableViewCellAccessoryCheckmark; cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else { } else {
cell.accessoryType = UITableViewCellAccessoryNone; cell.accessoryType = UITableViewCellAccessoryNone;
} }
if (indexPath.row == self.defaultIconIndex) {
cell.textLabel.text = @"Default";
}
return cell; return cell;
} }
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES]; [tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *previousSelectedCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectedIconIndex inSection:0]];
previousSelectedCell.accessoryType = UITableViewCellAccessoryNone;
self.selectedIconIndex = indexPath.row; self.selectedIconIndex = indexPath.row;
[self.tableView reloadData];
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; }
selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
- (void)resetIcon {
NSString *selectedIconPath = self.appIcons[self.selectedIconIndex]; [[UIApplication sharedApplication] setAlternateIconName:nil completionHandler:^(NSError * _Nullable error) {
UIImage *selectedIconImage = [UIImage imageWithContentsOfFile:selectedIconPath]; if (error) {
self.iconPreview.image = [self resizedImageWithImage:selectedIconImage]; NSLog(@"Error resetting icon: %@", error.localizedDescription);
[self showAlertWithTitle:@"Error" message:@"Failed to reset icon"];
} else {
NSLog(@"Icon reset successfully");
[self showAlertWithTitle:@"Success" message:@"Icon reset successfully"];
[self.tableView reloadData];
}
}];
} }
- (void)saveIcon { - (void)saveIcon {
NSString *selectedIconPath = self.appIcons[self.selectedIconIndex]; NSString *selectedIconPath = self.selectedIconIndex >= 0 ? self.appIcons[self.selectedIconIndex] : nil;
[[UIApplication sharedApplication] setAlternateIconName:selectedIconPath completionHandler:^(NSError * _Nullable error) { [[UIApplication sharedApplication] setAlternateIconName:selectedIconPath completionHandler:^(NSError * _Nullable error) {
if (error) { if (error) {
NSLog(@"Error setting alternate icon: %@", error.localizedDescription); NSLog(@"Error setting alternate icon: %@", error.localizedDescription);
@@ -101,16 +116,6 @@
}]; }];
} }
- (UIImage *)resizedImageWithImage:(UIImage *)image {
CGFloat scale = [UIScreen mainScreen].scale;
CGSize newSize = CGSizeMake(image.size.width / scale, image.size.height / scale);
UIGraphicsBeginImageContextWithOptions(newSize, NO, scale);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resizedImage;
}
- (void)showAlertWithTitle:(NSString *)title message:(NSString *)message { - (void)showAlertWithTitle:(NSString *)title message:(NSString *)message {
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
@@ -120,8 +125,8 @@
}); });
} }
- (void)close { - (void)back {
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil]; [self.navigationController popViewControllerAnimated:YES];
} }
@end @end