-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathTDAlternateIconViewController.m
More file actions
161 lines (133 loc) · 5.45 KB
/
TDAlternateIconViewController.m
File metadata and controls
161 lines (133 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#import "TDAlternateIconViewController.h"
#import "TDAlternateIconCell.h"
#import <dlfcn.h>
#import <rootless.h>
#import <objc/runtime.h>
#import "Localize.h"
static UIImage *alternateIconImage(NSString *iconName) {
NSString *fileName = [NSString stringWithFormat:@"%@60x60@2x.png", iconName];
NSString *path = [[[NSBundle mainBundle] bundleURL] URLByAppendingPathComponent:fileName].path;
UIImage *image = [UIImage imageWithContentsOfFile:path];
if (!image) {
NSLog(@"[td] Failed to load alternate icon image: %@", path);
image = [UIImage systemImageNamed:@"questionmark"];
}
return image;
}
@implementation TDAlternateIconViewController {
NSDictionary *_alternateIconsDictionary;
BOOL _hasSnowBoardFixInstalled;
}
- (void)viewDidLoad {
[super viewDidLoad];
// self.title = @"Alternate Icons";
self.title = [Localize localizedStringForKey:@"ALTERNATE_ICONS"];
self.view.backgroundColor = [UIColor systemBackgroundColor];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.tableFooterView = [UIView new];
_alternateIconsDictionary = [self alternateIconInfo];
// the space is not a typo; its's to load earlier than snowboard's stub: AAASnowBoardStub.dylib
_hasSnowBoardFixInstalled = dlopen(ROOT_PATH("/Library/MobileSubstrate/DynamicLibraries/ SnowBoardFix.dylib"), RTLD_LAZY);
}
- (NSArray<NSString *> *)alternateIconKeys {
return @[ @"Default", @"Original", @"ClearDark", @"ClearLight", @"Dark", @"TintedDark", @"TintedLight" ];
}
- (NSDictionary *)alternateIconInfo {
static dispatch_once_t onceToken;
static NSDictionary *iconsDict = nil;
dispatch_once(&onceToken, ^{
iconsDict = @{
@"Default": @{
@"name": @"Default",
@"image": alternateIconImage(@"AppIcon")
},
@"Original": @{
@"name": @"Original",
@"image": alternateIconImage(@"Original")
},
@"ClearDark": @{
@"name": @"Clear Dark",
@"image": alternateIconImage(@"ClearDark")
},
@"ClearLight": @{
@"name": @"Clear Light",
@"image": alternateIconImage(@"ClearLight")
},
@"Dark": @{
@"name": @"Dark",
@"image": alternateIconImage(@"Dark")
},
@"TintedDark": @{
@"name": @"Tinted Dark (Purple)",
@"image": alternateIconImage(@"TintedDark")
},
@"TintedLight": @{
@"name": @"Tinted Light (Purple)",
@"image": alternateIconImage(@"TintedLight")
},
};
});
return iconsDict;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.alternateIconKeys.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 75.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TDAlternateIconCell *cell = [tableView dequeueReusableCellWithIdentifier:@"IconCell"];
if (!cell) {
cell = [[TDAlternateIconCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"IconCell"];
}
NSString *iconKey = self.alternateIconKeys[indexPath.row];
NSDictionary *iconData = _alternateIconsDictionary[iconKey];
cell.iconNameLabel.text = iconData[@"name"];
cell.iconImageView.image = iconData[@"image"];
if (([UIApplication sharedApplication].alternateIconName == nil && [iconKey isEqualToString:@"Default"]) ||
[[UIApplication sharedApplication].alternateIconName isEqualToString:iconKey]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (![UIApplication sharedApplication].supportsAlternateIcons) {
NSLog(@"[td] Alternate icons not supported on this device.");
return;
}
NSString *iconKey = self.alternateIconKeys[indexPath.row];
NSString *currentIconName = [UIApplication sharedApplication].alternateIconName;
if ((currentIconName == nil && [iconKey isEqualToString:@"Default"]) ||
[currentIconName isEqualToString:iconKey]) {
return;
}
NSString *old = currentIconName ?: @"Default";
NSString *alternateIconName = [iconKey isEqualToString:@"Default"] ? nil : iconKey;
NSLog(@"Setting alternate icon: %@ -> %@", old, alternateIconName ?: @"Default");
[[UIApplication sharedApplication] setAlternateIconName:alternateIconName completionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"[td] Error setting alternate icon: %@", error);
return;
}
if (!_hasSnowBoardFixInstalled) {
CFNotificationCenterPostNotification(
CFNotificationCenterGetDarwinNotifyCenter(),
CFSTR("com.spark.snowboard.refresh"),
NULL,
NULL,
true
);
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}];
}
@end