Repository navigation
Expand file tree
/
Copy pathUtils.m
More file actions
320 lines (250 loc) · 12.5 KB
/
Copy pathUtils.m
File metadata and controls
320 lines (250 loc) · 12.5 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#import "Utils.h"
@implementation SCIUtils
+ (BOOL)getBoolPref:(NSString *)key {
if (![key length] || [[NSUserDefaults standardUserDefaults] objectForKey:key] == nil) return false;
return [[NSUserDefaults standardUserDefaults] boolForKey:key];
}
+ (double)getDoublePref:(NSString *)key {
if (![key length] || [[NSUserDefaults standardUserDefaults] objectForKey:key] == nil) return 0;
return [[NSUserDefaults standardUserDefaults] doubleForKey:key];
}
+ (NSString *)getStringPref:(NSString *)key {
if (![key length] || [[NSUserDefaults standardUserDefaults] objectForKey:key] == nil) return @"";
return [[NSUserDefaults standardUserDefaults] stringForKey:key];
}
+ (_Bool)liquidGlassEnabledBool:(_Bool)fallback {
BOOL setting = [SCIUtils getBoolPref:@"liquid_glass_surfaces"];
return setting ? true : fallback;
}
+ (void)cleanCache {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableArray<NSError *> *deletionErrors = [NSMutableArray array];
// Temp folder
// * disabled bc app crashed trying to delete certain files inside it
// todo: remove the above disclaimer if this new code doesn't cause crashing
NSArray *tempFolderContents = [fileManager contentsOfDirectoryAtURL:[NSURL fileURLWithPath:NSTemporaryDirectory()] includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsHiddenFiles error:nil];
for (NSURL *fileURL in tempFolderContents) {
NSError *cacheItemDeletionError;
[fileManager removeItemAtURL:fileURL error:&cacheItemDeletionError];
if (cacheItemDeletionError) [deletionErrors addObject:cacheItemDeletionError];
}
// Analytics folder
NSString *analyticsFolder = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"Application Support/com.burbn.instagram/analytics"];
NSArray *analyticsFolderContents = [fileManager contentsOfDirectoryAtURL:[[NSURL alloc] initFileURLWithPath:analyticsFolder] includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsHiddenFiles error:nil];
for (NSURL *fileURL in analyticsFolderContents) {
NSError *cacheItemDeletionError;
[fileManager removeItemAtURL:fileURL error:&cacheItemDeletionError];
if (cacheItemDeletionError) [deletionErrors addObject:cacheItemDeletionError];
}
// Caches folder
NSString *cachesFolder = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"Caches"];
NSArray *cachesFolderContents = [fileManager contentsOfDirectoryAtURL:[[NSURL alloc] initFileURLWithPath:cachesFolder] includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsHiddenFiles error:nil];
for (NSURL *fileURL in cachesFolderContents) {
NSError *cacheItemDeletionError;
[fileManager removeItemAtURL:fileURL error:&cacheItemDeletionError];
if (cacheItemDeletionError) [deletionErrors addObject:cacheItemDeletionError];
}
// Log errors
if (deletionErrors.count > 1) {
for (NSError *error in deletionErrors) {
NSLog(@"[SCInsta] File Deletion Error: %@", error);
}
}
}
// Displaying View Controllers
+ (void)showQuickLookVC:(NSArray<id> *)items {
QLPreviewController *previewController = [[QLPreviewController alloc] init];
QuickLookDelegate *quickLookDelegate = [[QuickLookDelegate alloc] initWithPreviewItemURLs:items];
previewController.dataSource = quickLookDelegate;
[topMostController() presentViewController:previewController animated:true completion:nil];
}
+ (void)showShareVC:(id)item {
UIActivityViewController *acVC = [[UIActivityViewController alloc] initWithActivityItems:@[item] applicationActivities:nil];
if (is_iPad()) {
acVC.popoverPresentationController.sourceView = topMostController().view;
acVC.popoverPresentationController.sourceRect = CGRectMake(topMostController().view.bounds.size.width / 2.0, topMostController().view.bounds.size.height / 2.0, 1.0, 1.0);
}
[topMostController() presentViewController:acVC animated:true completion:nil];
}
+ (void)showSettingsVC:(UIWindow *)window {
UIViewController *rootController = [window rootViewController];
SCISettingsViewController *settingsViewController = [SCISettingsViewController new];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:settingsViewController];
[rootController presentViewController:navigationController animated:YES completion:nil];
}
// Colours
+ (UIColor *)SCIColor_Primary {
return [UIColor colorWithRed:0/255.0 green:152/255.0 blue:254/255.0 alpha:1];
};
// Errors
+ (NSError *)errorWithDescription:(NSString *)errorDesc {
return [self errorWithDescription:errorDesc code:1];
}
+ (NSError *)errorWithDescription:(NSString *)errorDesc code:(NSInteger)errorCode {
NSError *error = [ NSError errorWithDomain:@"com.socuul.scinsta" code:errorCode userInfo:@{ NSLocalizedDescriptionKey: errorDesc } ];
return error;
}
+ (JGProgressHUD *)showErrorHUDWithDescription:(NSString *)errorDesc {
return [self showErrorHUDWithDescription:errorDesc dismissAfterDelay:4.0];
}
+ (JGProgressHUD *)showErrorHUDWithDescription:(NSString *)errorDesc dismissAfterDelay:(CGFloat)dismissDelay {
JGProgressHUD *hud = [[JGProgressHUD alloc] init];
hud.textLabel.text = errorDesc;
hud.indicatorView = [[JGProgressHUDErrorIndicatorView alloc] init];
[hud showInView:topMostController().view];
[hud dismissAfterDelay:4.0];
return hud;
}
// Media
+ (NSURL *)getPhotoUrl:(IGPhoto *)photo {
if (!photo) return nil;
// Get highest quality photo link
NSURL *photoUrl = [photo imageURLForWidth:100000.00];
return photoUrl;
}
+ (NSURL *)getPhotoUrlForMedia:(IGMedia *)media {
if (!media) return nil;
IGPhoto *photo = media.photo;
return [SCIUtils getPhotoUrl:photo];
}
+ (NSURL *)getVideoUrl:(IGVideo *)video {
if (!video) return nil;
// The past (pre v398)
if ([video respondsToSelector:@selector(sortedVideoURLsBySize)]) {
NSArray<NSDictionary *> *sorted = [video sortedVideoURLsBySize];
NSString *urlString = sorted.firstObject[@"url"];
return urlString.length ? [NSURL URLWithString:urlString] : nil;
}
// The present (post v398)
if ([video respondsToSelector:@selector(allVideoURLs)]) {
return [[video allVideoURLs] anyObject];
}
return nil;
}
+ (NSURL *)getVideoUrlForMedia:(IGMedia *)media {
if (!media) return nil;
IGVideo *video = media.video;
if (!video) return nil;
return [SCIUtils getVideoUrl:video];
}
// View Controllers
+ (UIViewController *)viewControllerForView:(UIView *)view {
NSString *viewDelegate = @"viewDelegate";
if ([view respondsToSelector:NSSelectorFromString(viewDelegate)]) {
return [view valueForKey:viewDelegate];
}
return nil;
}
+ (UIViewController *)viewControllerForAncestralView:(UIView *)view {
NSString *_viewControllerForAncestor = @"_viewControllerForAncestor";
if ([view respondsToSelector:NSSelectorFromString(_viewControllerForAncestor)]) {
return [view valueForKey:_viewControllerForAncestor];
}
return nil;
}
+ (UIViewController *)nearestViewControllerForView:(UIView *)view {
return [self viewControllerForView:view] ?: [self viewControllerForAncestralView:view];
}
// Functions
+ (NSString *)IGVersionString {
return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
};
+ (BOOL)isNotch {
return [[[UIApplication sharedApplication] keyWindow] safeAreaInsets].bottom > 0;
};
+ (BOOL)existingLongPressGestureRecognizerForView:(UIView *)view {
NSArray *allRecognizers = view.gestureRecognizers;
for (UIGestureRecognizer *recognizer in allRecognizers) {
if ([[recognizer class] isSubclassOfClass:[UILongPressGestureRecognizer class]]) {
return YES;
}
}
return NO;
}
// Alerts
+ (BOOL)showConfirmation:(void(^)(void))okHandler title:(NSString *)title {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:title message:@"Are you sure?" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
okHandler();
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"No!" style:UIAlertActionStyleCancel handler:nil]];
[topMostController() presentViewController:alert animated:YES completion:nil];
return nil;
};
+ (BOOL)showConfirmation:(void(^)(void))okHandler cancelHandler:(void(^)(void))cancelHandler title:(NSString *)title {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:title message:@"Are you sure?" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
okHandler();
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"No!" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
if (cancelHandler != nil) {
cancelHandler();
}
}]];
[topMostController() presentViewController:alert animated:YES completion:nil];
return nil;
};
+ (BOOL)showConfirmation:(void(^)(void))okHandler {
return [self showConfirmation:okHandler title:nil];
};
+ (BOOL)showConfirmation:(void(^)(void))okHandler cancelHandler:(void(^)(void))cancelHandler {
return [self showConfirmation:okHandler cancelHandler:cancelHandler title:nil];
}
+ (void)showRestartConfirmation {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Restart required" message:@"You must restart the app to apply this change" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Restart" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
exit(0);
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"Later" style:UIAlertActionStyleCancel handler:nil]];
[topMostController() presentViewController:alert animated:YES completion:nil];
};
// Toasts
+ (void)showToastForDuration:(double)duration title:(NSString *)title {
[SCIUtils showToastForDuration:duration title:title subtitle:nil];
}
+ (void)showToastForDuration:(double)duration title:(NSString *)title subtitle:(NSString *)subtitle {
// Root VC
Class rootVCClass = NSClassFromString(@"IGRootViewController");
UIViewController *topMostVC = topMostController();
if (![topMostVC isKindOfClass:rootVCClass]) return;
IGRootViewController *rootVC = (IGRootViewController *)topMostVC;
// Presenter
IGActionableConfirmationToastPresenter *toastPresenter = [rootVC toastPresenter];
if (toastPresenter == nil) return;
// View Model
Class modelClass = NSClassFromString(@"IGActionableConfirmationToastViewModel");
IGActionableConfirmationToastViewModel *model = [modelClass new];
[model setValue:title forKey:@"text_annotatedTitleText"];
[model setValue:subtitle forKey:@"text_annotatedSubtitleText"];
// Show new toast, after clearing existing one
[toastPresenter hideAlert];
[toastPresenter showAlertWithViewModel:model isAnimated:true animationDuration:duration presentationPriority:0 tapActionBlock:nil presentedHandler:nil dismissedHandler:nil];
}
// Math
+ (NSUInteger)decimalPlacesInDouble:(double)value {
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMaximumFractionDigits:15]; // Allow enough digits for double precision
[formatter setMinimumFractionDigits:0];
[formatter setDecimalSeparator:@"."]; // Force dot for internal logic, then respect locale for final display if needed
NSString *stringValue = [formatter stringFromNumber:@(value)];
// Find decimal separator
NSRange decimalRange = [stringValue rangeOfString:formatter.decimalSeparator];
if (decimalRange.location == NSNotFound) {
return 0;
} else {
return stringValue.length - (decimalRange.location + decimalRange.length);
}
}
// Ivars
+ (id)getIvarForObj:(id)obj name:(const char *)name {
Ivar ivar = class_getInstanceVariable(object_getClass(obj), name);
if (!ivar) return nil;
return object_getIvar(obj, ivar);
}
+ (void)setIvarForObj:(id)obj name:(const char *)name value:(id)value {
Ivar ivar = class_getInstanceVariable(object_getClass(obj), name);
if (!ivar) return;
object_setIvarWithStrongDefault(obj, ivar, value);
}
@end