-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathSettings.x
More file actions
669 lines (602 loc) · 32 KB
/
Copy pathSettings.x
File metadata and controls
669 lines (602 loc) · 32 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
#import <PSHeader/Misc.h>
#import <YouTubeHeader/UIDevice+YouTube.h>
#import <YouTubeHeader/GOOHUDManagerInternal.h>
#import <YouTubeHeader/YTAlertView.h>
#import <YouTubeHeader/YTCommonUtils.h>
#import <YouTubeHeader/YTSearchableSettingsViewController.h>
#import <YouTubeHeader/YTSettingsGroupData.h>
#import <YouTubeHeader/YTSettingsPickerViewController.h>
#import <YouTubeHeader/YTSettingsSectionItem.h>
#import <YouTubeHeader/YTSettingsSectionItemManager.h>
#import <YouTubeHeader/YTSettingsViewController.h>
#import <YouTubeHeader/YTUIUtils.h>
#import <YouTubeHeader/YTVersionUtils.h>
#import <pthread.h>
#define Prefix @"YTABC"
#define EnabledKey @"EnabledYTABC"
#define GroupedKey @"GroupedYTABC"
#define INCLUDED_CLASSES @"Included classes: YTGlobalConfig, YTColdConfig, YTHotConfig"
#define EXCLUDED_METHODS @"Excluded settings: android*, amsterdam*, kidsClient*, musicClient*, musicOfflineClient* and unplugged*"
#define _LOC(b, x) [b localizedStringForKey:x value:nil table:nil]
#define LOC(x) _LOC(tweakBundle, x)
static const NSInteger YTABCSection = 404;
static const NSUInteger EstimatedCategoryCount = 26;
static const NSUInteger EstimatedCategoryDivisor = 10;
static const NSUInteger LongMethodNameThreshold = 26;
static NSString * const KeyFormatString = @"%@.%@";
static NSString * const FullKeyFormatString = @"%@.%@.%@";
@interface YTSettingsSectionItemManager (YTABConfig)
- (void)updateYTABCSectionWithEntry:(id)entry;
@end
extern NSMutableDictionary <NSString *, NSMutableDictionary <NSString *, NSNumber *> *> *cache;
NSUserDefaults *defaults;
NSSet <NSString *> *allKeysSet;
BOOL allKeysNeedsUpdate = YES;
NSMutableDictionary <NSString *, NSString *> *keyCache;
NSSortDescriptor *titleSortDescriptor;
NSRegularExpression *importRegex;
NSMutableDictionary <NSString *, NSString *> *categoryCache; // Memoize category results
NSUInteger prefixLength; // Cache prefix length
pthread_mutex_t cacheMutex;
BOOL tweakEnabled() {
return [defaults boolForKey:EnabledKey];
}
BOOL groupedSettings() {
return [defaults boolForKey:GroupedKey];
}
NSBundle *YTABCBundle() {
static NSBundle *bundle = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *tweakBundlePath = [[NSBundle mainBundle] pathForResource:@"YTABC" ofType:@"bundle"];
bundle = [NSBundle bundleWithPath:tweakBundlePath ?: PS_ROOT_PATH_NS(@"/Library/Application Support/" Prefix ".bundle")];
});
return bundle;
}
NSString *getKey(NSString *method, NSString *classKey) {
NSString *cacheKey = [NSString stringWithFormat:KeyFormatString, classKey, method];
pthread_mutex_lock(&cacheMutex);
NSString *fullKey = keyCache[cacheKey];
if (!fullKey) {
fullKey = [NSString stringWithFormat:FullKeyFormatString, Prefix, classKey, method];
keyCache[cacheKey] = fullKey;
}
pthread_mutex_unlock(&cacheMutex);
return fullKey;
}
static NSString *getCacheKey(NSString *method, NSString *classKey) {
return [NSString stringWithFormat:KeyFormatString, classKey, method];
}
BOOL getValue(NSString *methodKey) {
if (!methodKey) return NO;
pthread_mutex_lock(&cacheMutex);
BOOL contains = [allKeysSet containsObject:methodKey];
BOOL result = NO;
if (!contains) {
NSString *keyPath = [methodKey substringFromIndex:prefixLength + 1];
id value = [cache valueForKeyPath:keyPath];
result = value ? [value boolValue] : NO;
} else {
result = [defaults boolForKey:methodKey];
}
pthread_mutex_unlock(&cacheMutex);
return result;
}
static void setValue(NSString *method, NSString *classKey, BOOL value) {
pthread_mutex_lock(&cacheMutex);
[cache setValue:@(value) forKeyPath:getCacheKey(method, classKey)];
pthread_mutex_unlock(&cacheMutex);
[defaults setBool:value forKey:getKey(method, classKey)];
allKeysNeedsUpdate = YES;
}
static void setValueFromImport(NSString *settingKey, BOOL value) {
pthread_mutex_lock(&cacheMutex);
[cache setValue:@(value) forKeyPath:settingKey];
pthread_mutex_unlock(&cacheMutex);
[defaults setBool:value forKey:[NSString stringWithFormat:KeyFormatString, Prefix, settingKey]];
allKeysNeedsUpdate = YES;
}
void updateAllKeys() {
pthread_mutex_lock(&cacheMutex);
if (allKeysNeedsUpdate) {
NSArray *keys = [defaults dictionaryRepresentation].allKeys;
allKeysSet = [NSSet setWithArray:keys];
allKeysNeedsUpdate = NO;
}
pthread_mutex_unlock(&cacheMutex);
}
static void clearCaches() {
pthread_mutex_lock(&cacheMutex);
[keyCache removeAllObjects];
[categoryCache removeAllObjects];
pthread_mutex_unlock(&cacheMutex);
}
%group Search
%hook YTSettingsViewController
- (void)loadWithModel:(id)model fromView:(UIView *)view {
%orig;
@try {
if ([[self valueForKey:@"_detailsCategoryID"] integerValue] == YTABCSection)
[self setValue:@(YES) forKey:@"_shouldShowSearchBar"];
} @catch (id ex) {}
}
- (void)setSectionControllers {
%orig;
@try {
if (![[self valueForKey:@"_shouldShowSearchBar"] boolValue]) return;
YTSettingsSectionController *settingsSectionController = [self settingsSectionControllers][[self valueForKey:@"_detailsCategoryID"]];
if (settingsSectionController == nil) return;
YTSearchableSettingsViewController *searchableVC = [self valueForKey:@"_searchableSettingsViewController"];
[searchableVC storeCollectionViewSections:@[settingsSectionController]];
} @catch (id ex) {}
}
%end
%end
%hook YTSettingsGroupData
- (NSArray <NSNumber *> *)orderedCategories {
if (self.type != 1 || class_getClassMethod(objc_getClass("YTSettingsGroupData"), @selector(tweaks)))
return %orig;
NSArray *categories = %orig;
NSMutableArray *mutableCategories = categories.mutableCopy;
[mutableCategories insertObject:@(YTABCSection) atIndex:0];
return mutableCategories.copy;
}
%end
%hook YTAppSettingsPresentationData
+ (NSArray <NSNumber *> *)settingsCategoryOrder {
NSArray <NSNumber *> *order = %orig;
NSMutableArray <NSNumber *> *mutableOrder = [order mutableCopy];
[mutableOrder insertObject:@(YTABCSection) atIndex:0];
return mutableOrder.copy;
}
%end
static NSString *getCategory(char c, NSString *method) {
// Check cache first
NSString *cachedCategory = categoryCache[method];
if (cachedCategory) return cachedCategory;
NSString *category = nil;
if (c == 'e') {
if ([method hasPrefix:@"elements"]) category = @"elements";
else if ([method hasPrefix:@"enable"]) category = @"enable";
}
else if (c == 'i') {
if ([method hasPrefix:@"ios"]) category = @"ios";
else if ([method hasPrefix:@"is"]) category = @"is";
}
else if (c == 's') {
if ([method hasPrefix:@"shorts"]) category = @"shorts";
else if ([method hasPrefix:@"should"]) category = @"should";
}
if (!category) {
unichar uc = (unichar)c;
category = [NSString stringWithCharacters:&uc length:1];
}
// Cache the result
categoryCache[method] = category;
return category;
}
%hook YTSettingsSectionItemManager
%new(v@:@)
- (void)updateYTABCSectionWithEntry:(id)entry {
NSMutableArray *sectionItems = [NSMutableArray array];
int totalSettings = 0;
NSBundle *tweakBundle = YTABCBundle();
BOOL isPhone = ![%c(YTCommonUtils) isIPad];
NSString *yesText = _LOC([NSBundle mainBundle], @"settings.yes");
NSString *cancelText = _LOC([NSBundle mainBundle], @"confirm.cancel");
NSString *deleteText = _LOC([NSBundle mainBundle], @"search.action.delete");
Class YTSettingsSectionItemClass = %c(YTSettingsSectionItem);
Class YTAlertViewClass = %c(YTAlertView);
if (tweakEnabled()) {
// AB flags
// Pre-calculate total method count for capacity allocation
NSUInteger estimatedMethodCount = 0;
for (NSString *classKey in cache) {
estimatedMethodCount += [cache[classKey] count];
}
NSMutableDictionary <NSString *, NSMutableArray <YTSettingsSectionItem *> *> *properties = [NSMutableDictionary dictionaryWithCapacity:EstimatedCategoryCount];
updateAllKeys(); // Update once before the loop
for (NSString *classKey in cache) {
@autoreleasepool { // Drain autorelease pool periodically to reduce peak memory
for (NSString *method in cache[classKey]) {
if (method.length == 0) continue; // Safety check
char c = tolower([method characterAtIndex:0]);
NSString *category = getCategory(c, method);
if (![properties objectForKey:category]) properties[category] = [NSMutableArray arrayWithCapacity:estimatedMethodCount / EstimatedCategoryDivisor];
NSString *methodKey = getKey(method, classKey); // Cache the key
BOOL modified = [allKeysSet containsObject:methodKey];
NSString *modifiedTitle = modified ? [NSString stringWithFormat:@"%@ *", method] : method;
YTSettingsSectionItem *methodSwitch = [YTSettingsSectionItemClass switchItemWithTitle:modifiedTitle
titleDescription:isPhone && method.length > LongMethodNameThreshold ? modifiedTitle : nil
accessibilityIdentifier:nil
switchOn:getValue(methodKey)
switchBlock:^BOOL (YTSettingsCell *cell, BOOL enabled) {
setValue(method, classKey, enabled);
return YES;
}
selectBlock:^BOOL (YTSettingsCell *cell, NSUInteger arg1) {
NSString *content = [NSString stringWithFormat:KeyFormatString, classKey, method];
YTAlertView *alertView = [YTAlertViewClass confirmationDialog];
alertView.title = method;
alertView.subtitle = content;
[alertView addTitle:LOC(@"COPY_TO_CLIPBOARD") withAction:^{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = content;
[[%c(GOOHUDManagerInternal) sharedInstance] showMessageMainThread:[%c(YTHUDMessage) messageWithText:LOC(@"COPIED_TO_CLIPBOARD")]];
}];
updateAllKeys();
NSString *key = getKey(method, classKey);
if ([allKeysSet containsObject:key]) {
[alertView addTitle:deleteText withAction:^{
[defaults removeObjectForKey:key];
allKeysNeedsUpdate = YES;
updateAllKeys();
}];
}
[alertView addCancelButton:NULL];
[alertView show];
return NO;
}
settingItemId:0];
[properties[category] addObject:methodSwitch];
}
} // @autoreleasepool
}
YTSettingsViewController *settingsViewController = [self valueForKey:@"_settingsViewControllerDelegate"];
BOOL grouped = groupedSettings();
for (NSString *category in properties) {
NSMutableArray <YTSettingsSectionItem *> *rows = properties[category];
totalSettings += rows.count;
if (grouped) {
[rows sortUsingDescriptors:@[titleSortDescriptor]];
NSString *shortTitle = [NSString stringWithFormat:@"\"%@\" (%ld)", category, rows.count];
NSString *title = [NSString stringWithFormat:@"%@ %@", LOC(@"SETTINGS_START_WITH"), shortTitle];
YTSettingsSectionItem *headerItem = [YTSettingsSectionItemClass itemWithTitle:title accessibilityIdentifier:nil detailTextBlock:nil selectBlock:nil];
headerItem.enabled = NO;
[rows insertObject:headerItem atIndex:0];
YTSettingsSectionItem *sectionItem = [YTSettingsSectionItemClass itemWithTitle:title accessibilityIdentifier:nil detailTextBlock:nil selectBlock:^BOOL (YTSettingsCell *cell, NSUInteger arg1) {
YTSettingsPickerViewController *picker = [[%c(YTSettingsPickerViewController) alloc] initWithNavTitle:shortTitle pickerSectionTitle:nil rows:rows selectedItemIndex:0 parentResponder:[self parentResponder]];
[settingsViewController pushViewController:picker];
return YES;
}];
[sectionItems addObject:sectionItem];
} else {
[sectionItems addObjectsFromArray:rows];
}
}
[sectionItems sortUsingDescriptors:@[titleSortDescriptor]];
// Import settings
YTSettingsSectionItem *import = [YTSettingsSectionItemClass itemWithTitle:LOC(@"IMPORT_SETTINGS")
titleDescription:[NSString stringWithFormat:LOC(@"IMPORT_SETTINGS_DESC"), @"YT(Cold|Hot|Global)Config.*: (0|1)"]
accessibilityIdentifier:nil
detailTextBlock:nil
selectBlock:^BOOL (YTSettingsCell *cell, NSUInteger arg1) {
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSArray *lines = [pasteboard.string componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
NSMutableDictionary *importedSettings = [NSMutableDictionary dictionaryWithCapacity:lines.count];
NSMutableArray *reportedSettings = [NSMutableArray arrayWithCapacity:lines.count];
for (NSString *line in lines) {
NSTextCheckingResult *match = [importRegex firstMatchInString:line options:0 range:NSMakeRange(0, [line length])];
if (!match) continue;
NSString *key = [line substringWithRange:[match rangeAtIndex:1]];
id cacheValue = [cache valueForKeyPath:key];
if (cacheValue == nil) continue;
NSString *valueString = [line substringWithRange:[match rangeAtIndex:2]];
int integerValue = [valueString integerValue];
if (integerValue == 0 && ![cacheValue boolValue]) continue;
if (integerValue == 1 && [cacheValue boolValue]) continue;
importedSettings[key] = @(integerValue);
[reportedSettings addObject:[NSString stringWithFormat:@"%@: %d", key, integerValue]];
}
if (reportedSettings.count == 0) {
YTAlertView *alertView = [YTAlertViewClass infoDialog];
alertView.title = LOC(@"SETTINGS_TO_IMPORT");
alertView.subtitle = LOC(@"NOTHING_TO_IMPORT");
[alertView show];
return NO;
}
[reportedSettings insertObject:[NSString stringWithFormat:LOC(@"SETTINGS_TO_IMPORT_DESC"), reportedSettings.count] atIndex:0];
YTAlertView *alertView = [YTAlertViewClass confirmationDialogWithAction:^{
for (NSString *key in importedSettings) {
setValueFromImport(key, [importedSettings[key] boolValue]);
}
updateAllKeys();
} actionTitle:LOC(@"IMPORT")];
alertView.title = LOC(@"SETTINGS_TO_IMPORT");
alertView.subtitle = [reportedSettings componentsJoinedByString:@"\n"];
[alertView show];
return YES;
}];
[sectionItems insertObject:import atIndex:0];
// Copy current settings
YTSettingsSectionItem *copyAll = [YTSettingsSectionItemClass itemWithTitle:LOC(@"COPY_CURRENT_SETTINGS")
titleDescription:LOC(@"COPY_CURRENT_SETTINGS_DESC")
accessibilityIdentifier:nil
detailTextBlock:nil
selectBlock:^BOOL (YTSettingsCell *cell, NSUInteger arg1) {
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
// Pre-calculate total count for capacity
NSUInteger totalCount = 0;
for (NSString *classKey in cache) {
totalCount += [cache[classKey] count];
}
NSMutableArray *content = [NSMutableArray arrayWithCapacity:totalCount + 5]; // +5 for header items
for (NSString *classKey in cache) {
[cache[classKey] enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSNumber *value, BOOL* stop) {
[content addObject:[NSString stringWithFormat:@"%@.%@: %d", classKey, key, [value boolValue]]];
}];
}
[content sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
[content insertObject:[NSString stringWithFormat:@"Device model: %@", [UIDevice machineName]] atIndex:0];
[content insertObject:[NSString stringWithFormat:@"App version: %@", [%c(YTVersionUtils) appVersion]] atIndex:0];
[content insertObject:EXCLUDED_METHODS atIndex:0];
[content insertObject:INCLUDED_CLASSES atIndex:0];
[content insertObject:[NSString stringWithFormat:@"YTABConfig version: %@", @(OS_STRINGIFY(TWEAK_VERSION))] atIndex:0];
pasteboard.string = [content componentsJoinedByString:@"\n"];
[[%c(GOOHUDManagerInternal) sharedInstance] showMessageMainThread:[%c(YTHUDMessage) messageWithText:LOC(@"COPIED_TO_CLIPBOARD")]];
return YES;
}];
[sectionItems insertObject:copyAll atIndex:0];
// View modified settings
YTSettingsSectionItem *modified = [YTSettingsSectionItemClass itemWithTitle:LOC(@"VIEW_MODIFIED_SETTINGS")
titleDescription:LOC(@"VIEW_MODIFIED_SETTINGS_DESC")
accessibilityIdentifier:nil
detailTextBlock:nil
selectBlock:^BOOL (YTSettingsCell *cell, NSUInteger arg1) {
updateAllKeys();
// Filter keys with prefix using NSPredicate for better performance
NSPredicate *prefixPredicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH %@", Prefix];
NSSet *filteredKeys = [allKeysSet filteredSetUsingPredicate:prefixPredicate];
NSMutableDictionary <NSString *, NSString *> *modifiedKeysMap = [NSMutableDictionary dictionaryWithCapacity:[filteredKeys count]];
for (NSString *key in filteredKeys) {
NSString *displayKey = [key substringFromIndex:prefixLength + 1];
modifiedKeysMap[displayKey] = key;
}
NSArray *sortedDisplayKeys = [[modifiedKeysMap allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSMutableArray <YTSettingsSectionItem *> *modifiedRows = [NSMutableArray arrayWithCapacity:sortedDisplayKeys.count + 2]; // +2 for copy and info items
// Copy to clipboard item
YTSettingsSectionItem *copyItem = [YTSettingsSectionItemClass itemWithTitle:LOC(@"COPY_TO_CLIPBOARD")
titleDescription:nil
accessibilityIdentifier:nil
detailTextBlock:nil
selectBlock:^BOOL (YTSettingsCell *copyCell, NSUInteger arg1) {
NSMutableArray *contentArray = [NSMutableArray arrayWithCapacity:sortedDisplayKeys.count];
for (NSString *displayKey in sortedDisplayKeys) {
NSString *fullKey = modifiedKeysMap[displayKey];
[contentArray addObject:[NSString stringWithFormat:@"%@: %d", displayKey, [defaults boolForKey:fullKey]]];
}
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = [contentArray componentsJoinedByString:@"\n"];
[[%c(GOOHUDManagerInternal) sharedInstance] showMessageMainThread:[%c(YTHUDMessage) messageWithText:LOC(@"COPIED_TO_CLIPBOARD")]];
return YES;
}];
[modifiedRows addObject:copyItem];
// Remove removed settings item
NSMutableArray *removedFullKeys = [NSMutableArray array];
for (NSString *displayKey in sortedDisplayKeys) {
NSArray *components = [displayKey componentsSeparatedByString:@"."];
if (components.count > 1) {
NSString *classKey = components[0];
NSString *methodKey = [displayKey substringFromIndex:classKey.length + 1];
if (!cache[classKey][methodKey]) {
[removedFullKeys addObject:modifiedKeysMap[displayKey]];
}
}
}
if (removedFullKeys.count > 0) {
YTSettingsSectionItem *removeRemovedItem = [YTSettingsSectionItemClass itemWithTitle:LOC(@"REMOVE_REMOVED_SETTINGS")
titleDescription:nil
accessibilityIdentifier:nil
detailTextBlock:nil
selectBlock:^BOOL (YTSettingsCell *cell, NSUInteger arg1) {
YTAlertView *alertView = [YTAlertViewClass confirmationDialogWithAction:^{
for (NSString *key in removedFullKeys) {
[defaults removeObjectForKey:key];
}
allKeysNeedsUpdate = YES;
updateAllKeys();
[settingsViewController.navigationController popViewControllerAnimated:YES];
} actionTitle:deleteText];
alertView.title = LOC(@"REMOVE_REMOVED_SETTINGS");
alertView.subtitle = [NSString stringWithFormat:LOC(@"REMOVE_REMOVED_SETTINGS_DESC"), (long)removedFullKeys.count];
[alertView show];
return YES;
}];
[modifiedRows addObject:removeRemovedItem];
}
// General information item
NSString *infoDescription = [NSString stringWithFormat:LOC(@"TOTAL_MODIFIED_SETTINGS"), sortedDisplayKeys.count];
YTSettingsSectionItem *infoItem = [YTSettingsSectionItemClass itemWithTitle:nil
titleDescription:infoDescription
accessibilityIdentifier:nil
detailTextBlock:nil
selectBlock:nil];
infoItem.enabled = NO;
[modifiedRows addObject:infoItem];
// Boolean toggles for each modified setting
for (NSString *displayKey in sortedDisplayKeys) {
NSString *fullKey = modifiedKeysMap[displayKey];
NSArray *components = [displayKey componentsSeparatedByString:@"."];
NSString *method = components.count > 1 ? components[1] : displayKey;
BOOL isRemoved = NO;
if (components.count > 1) {
NSString *classKey = components[0];
NSString *methodKey = [displayKey substringFromIndex:classKey.length + 1];
if (!cache[classKey][methodKey])
isRemoved = YES;
}
NSString *title = isRemoved ? [NSString stringWithFormat:@"%@ %@", displayKey, [tweakBundle localizedStringForKey:@"REMOVED" value:@"(Removed)" table:nil]] : displayKey;
YTSettingsSectionItem *toggleItem = [YTSettingsSectionItemClass switchItemWithTitle:title
titleDescription:isPhone && displayKey.length > 26 ? displayKey : nil
accessibilityIdentifier:nil
switchOn:[defaults boolForKey:fullKey]
switchBlock:^BOOL (YTSettingsCell *toggleCell, BOOL enabled) {
[defaults setBool:enabled forKey:fullKey];
if (components.count > 1) {
[cache setValue:@(enabled) forKeyPath:displayKey];
}
return YES;
}
selectBlock:^BOOL (YTSettingsCell *toggleCell, NSUInteger arg1) {
YTAlertView *alertView = [YTAlertViewClass confirmationDialog];
alertView.title = method;
alertView.subtitle = displayKey;
[alertView addTitle:LOC(@"COPY_TO_CLIPBOARD") withAction:^{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = displayKey;
[[%c(GOOHUDManagerInternal) sharedInstance] showMessageMainThread:[%c(YTHUDMessage) messageWithText:LOC(@"COPIED_TO_CLIPBOARD")]];
}];
[alertView addTitle:deleteText withAction:^{
[defaults removeObjectForKey:fullKey];
allKeysNeedsUpdate = YES;
updateAllKeys();
}];
[alertView addCancelButton:NULL];
[alertView show];
return NO;
}
settingItemId:0];
toggleItem.enabled = !isRemoved;
[modifiedRows addObject:toggleItem];
}
NSString *navTitle = LOC(@"MODIFIED_SETTINGS_TITLE");
YTSettingsPickerViewController *picker = [[%c(YTSettingsPickerViewController) alloc] initWithNavTitle:navTitle pickerSectionTitle:nil rows:modifiedRows selectedItemIndex:0 parentResponder:[self parentResponder]];
[settingsViewController pushViewController:picker];
return YES;
}];
[sectionItems insertObject:modified atIndex:0];
// Reset and kill
YTSettingsSectionItem *reset = [YTSettingsSectionItemClass itemWithTitle:LOC(@"RESET_KILL")
titleDescription:LOC(@"RESET_KILL_DESC")
accessibilityIdentifier:nil
detailTextBlock:nil
selectBlock:^BOOL (YTSettingsCell *cell, NSUInteger arg1) {
YTAlertView *alertView = [YTAlertViewClass confirmationDialogWithAction:^{
updateAllKeys();
NSPredicate *prefixPredicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH %@", Prefix];
NSSet *keysToDelete = [allKeysSet filteredSetUsingPredicate:prefixPredicate];
for (NSString *key in keysToDelete) {
[defaults removeObjectForKey:key];
}
exit(0);
} actionTitle:yesText];
alertView.title = LOC(@"WARNING");
alertView.subtitle = LOC(@"APPLY_DESC");
[alertView show];
return YES;
}];
[sectionItems insertObject:reset atIndex:0];
// Grouped settings
YTSettingsSectionItem *group = [YTSettingsSectionItemClass switchItemWithTitle:LOC(@"GROUPED")
titleDescription:nil
accessibilityIdentifier:nil
switchOn:groupedSettings()
switchBlock:^BOOL (YTSettingsCell *cell, BOOL enabled) {
YTAlertView *alertView = [YTAlertViewClass confirmationDialogWithAction:^{
[defaults setBool:enabled forKey:GroupedKey];
exit(0);
}
actionTitle:yesText
cancelAction:^{
[cell setSwitchOn:!enabled animated:YES];
[defaults setBool:!enabled forKey:GroupedKey];
}
cancelTitle:cancelText];
alertView.title = LOC(@"WARNING");
alertView.subtitle = LOC(@"APPLY_DESC");
[alertView show];
return YES;
}
settingItemId:0];
[sectionItems insertObject:group atIndex:0];
}
// Open megathread
YTSettingsSectionItem *thread = [YTSettingsSectionItemClass itemWithTitle:LOC(@"OPEN_MEGATHREAD")
titleDescription:LOC(@"OPEN_MEGATHREAD_DESC")
accessibilityIdentifier:nil
detailTextBlock:nil
selectBlock:^BOOL (YTSettingsCell *cell, NSUInteger arg1) {
return [%c(YTUIUtils) openURL:[NSURL URLWithString:@"https://github.com/PoomSmart/YTABConfig/discussions"]];
}];
[sectionItems insertObject:thread atIndex:0];
// Killswitch
YTSettingsSectionItem *master = [YTSettingsSectionItemClass switchItemWithTitle:LOC(@"ENABLED")
titleDescription:LOC(@"ENABLED_DESC")
accessibilityIdentifier:nil
switchOn:tweakEnabled()
switchBlock:^BOOL (YTSettingsCell *cell, BOOL enabled) {
[defaults setBool:enabled forKey:EnabledKey];
YTAlertView *alertView = [YTAlertViewClass confirmationDialogWithAction:^{ exit(0); }
actionTitle:yesText
cancelAction:^{
[cell setSwitchOn:!enabled animated:YES];
[defaults setBool:!enabled forKey:EnabledKey];
}
cancelTitle:cancelText];
alertView.title = LOC(@"WARNING");
alertView.subtitle = LOC(@"APPLY_DESC");
[alertView show];
return YES;
}
settingItemId:0];
[sectionItems insertObject:master atIndex:0];
if (tweakEnabled()) {
NSString *titleDescription =[NSString stringWithFormat:@"YTABConfig %@, %d feature flags.", @(OS_STRINGIFY(TWEAK_VERSION)), totalSettings];
YTSettingsSectionItem *info = [YTSettingsSectionItemClass itemWithTitle:nil
titleDescription:titleDescription
accessibilityIdentifier:nil
detailTextBlock:nil
selectBlock:nil];
info.enabled = NO;
[sectionItems insertObject:info atIndex:0];
}
YTSettingsViewController *delegate = [self valueForKey:@"_dataDelegate"];
NSString *title = @"A/B";
if ([delegate respondsToSelector:@selector(setSectionItems:forCategory:title:icon:titleDescription:headerHidden:)]) {
YTIIcon *icon = [%c(YTIIcon) new];
icon.iconType = YT_EXPERIMENT;
[delegate setSectionItems:sectionItems
forCategory:YTABCSection
title:title
icon:icon
titleDescription:nil
headerHidden:NO];
} else
[delegate setSectionItems:sectionItems
forCategory:YTABCSection
title:title
titleDescription:nil
headerHidden:NO];
}
- (void)updateSectionForCategory:(NSUInteger)category withEntry:(id)entry {
if (category == YTABCSection) {
[self updateYTABCSectionWithEntry:entry];
return;
}
%orig;
}
%end
void SearchHook() {
%init(Search);
}
%ctor {
defaults = [NSUserDefaults standardUserDefaults];
prefixLength = [Prefix length];
keyCache = [NSMutableDictionary new];
categoryCache = [NSMutableDictionary new];
titleSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES];
importRegex = [NSRegularExpression regularExpressionWithPattern:@"^(YT.*Config\\..*):\\s*(\\d)$" options:0 error:nil];
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&cacheMutex, &attr);
pthread_mutexattr_destroy(&attr);
// Clear caches on memory warning to reduce memory footprint
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidReceiveMemoryWarningNotification
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note) {
clearCaches();
}];
%init;
}