AppIconOptionsController is now using UITableView

I used some of the things from https://github.com/arichornlover/YouTube-Reborn-v5/blob/main/Controllers/StartupPageOptionsController.m to make this work
This commit is contained in:
arichornlover 2024-04-13 18:28:20 -05:00 committed by GitHub
parent 3028d73913
commit c9b4cacc3e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,11 +1,11 @@
#import "AppIconOptionsController.h"
@interface AppIconOptionsController () <UICollectionViewDataSource, UICollectionViewDelegate>
@interface AppIconOptionsController () <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) UIImageView *iconPreview;
@property (strong, nonatomic) NSArray<NSString *> *appIcons;
@property (strong, nonatomic) NSString *selectedIconFile;
@property (assign, nonatomic) NSInteger selectedIconIndex;
@end
@ -14,12 +14,12 @@
- (void)viewDidLoad {
[super viewDidLoad];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
[self.view addSubview:self.collectionView];
self.selectedIconIndex = 0;
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.view addSubview:self.tableView];
UIButton *defaultButton = [UIButton buttonWithType:UIButtonTypeSystem];
defaultButton.frame = CGRectMake(20, 100, 100, 40);
@ -28,11 +28,11 @@
[self.view addSubview:defaultButton];
UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeSystem];
saveButton.frame = CGRectMake(150, 100, 100, 40);
saveButton.frame = CGRectMake(20, 100, 100, 40);
[saveButton setTitle:@"Save" forState:UIControlStateNormal];
[saveButton addTarget:self action:@selector(saveIcon) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:saveButton];
self.iconPreview = [[UIImageView alloc] initWithFrame:CGRectMake(20, 150, 60, 60)];
self.iconPreview.layer.cornerRadius = 10.0;
self.iconPreview.clipsToBounds = YES;
@ -42,72 +42,53 @@
NSBundle *bundle = [NSBundle bundleWithPath:path];
self.appIcons = [bundle pathsForResourcesOfType:@"png" inDirectory:@"AppIcons"];
if ([UIApplication sharedApplication].supportsAlternateIcons) {
} else {
if (![UIApplication sharedApplication].supportsAlternateIcons) {
NSLog(@"Alternate icons are not supported on this device.");
}
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.appIcons.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
UIImage *appIconImage = [UIImage imageWithContentsOfFile:self.appIcons[indexPath.row]];
UIImage *resizedIconImage = [self resizedImageWithImage:appIconImage];
UIImageView *imageView = [[UIImageView alloc] initWithImage:resizedIconImage];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.frame = cell.contentView.bounds;
imageView.layer.cornerRadius = 10.0;
imageView.clipsToBounds = YES;
[cell.contentView addSubview:imageView];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
cell.textLabel.text = [self.appIcons[indexPath.row] lastPathComponent];
if (indexPath.row == self.selectedIconIndex) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
self.selectedIconFile = self.appIcons[indexPath.row];
UIImage *selectedIconImage = [UIImage imageWithContentsOfFile:self.selectedIconFile];
UIImage *resizedSelectedIconImage = [self resizedImageWithImage:selectedIconImage];
self.iconPreview.image = resizedSelectedIconImage;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *previousSelectedCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectedIconIndex inSection:0]];
previousSelectedCell.accessoryType = UITableViewCellAccessoryNone;
self.selectedIconIndex = indexPath.row;
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
- (void)setDefaultIcon {
self.iconPreview.image = nil;
self.selectedIconFile = nil;
UIImage *selectedIconImage = [UIImage imageWithContentsOfFile:self.appIcons[self.selectedIconIndex]];
self.iconPreview.image = [self resizedImageWithImage:selectedIconImage];
}
- (void)saveIcon {
if (self.selectedIconFile) {
[[UIApplication sharedApplication] setAlternateIconName:@"logo_youtube_color60x60" completionHandler:^(NSError * _Nullable error){
if (error) {
NSLog(@"Error setting alternate icon: %@", error.localizedDescription);
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Error" message:@"Failed to set alternate icon" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:okAction];
[self presentViewController:alert animated:YES completion:nil];
});
} else {
NSLog(@"Alternate icon set successfully");
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Success" message:@"Alternate icon set successfully" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:okAction];
[self presentViewController:alert animated:YES completion:nil];
});
}
}];
} else {
NSLog(@"No icon selected to save");
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"No Icon Selected" message:@"Please select an icon before saving" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:okAction];
[self presentViewController:alert animated:YES completion:nil];
}
NSString *selectedIcon = self.appIcons[self.selectedIconIndex];
[[UIApplication sharedApplication] setAlternateIconName:selectedIcon completionHandler:^(NSError * _Nullable error){
if (error) {
NSLog(@"Error setting alternate icon: %@", error.localizedDescription);
[self showAlertWithTitle:@"Error" message:@"Failed to set alternate icon"];
} else {
NSLog(@"Alternate icon set successfully");
[self showAlertWithTitle:@"Success" message:@"Alternate icon set successfully"];
}
}];
}
- (UIImage *)resizedImageWithImage:(UIImage *)image {
@ -120,4 +101,13 @@
return resizedImage;
}
- (void)showAlertWithTitle:(NSString *)title message:(NSString *)message {
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:okAction];
[self presentViewController:alert animated:YES completion:nil];
});
}
@end