Skip to content

Commit

Permalink
Import settings feature
Browse files Browse the repository at this point in the history
  • Loading branch information
PoomSmart committed Feb 12, 2024
1 parent 6d06236 commit 265d2c6
Show file tree
Hide file tree
Showing 17 changed files with 158 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
TARGET := iphone:clang:latest:11.0
INSTALL_TARGET_PROCESSES = YouTube
ARCHS = arm64
PACKAGE_VERSION = 1.5.1
PACKAGE_VERSION = 1.6.0

include $(THEOS)/makefiles/common.mk

Expand Down
74 changes: 72 additions & 2 deletions Tweak.xm
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ static void setValue(NSString *method, NSString *classKey, BOOL value) {
[defaults setBool:value forKey:getKey(method, classKey)];
}

static void setValueFromImport(NSString *settingKey, BOOL value) {
[cache setValue:@(value) forKeyPath:settingKey];
[defaults setBool:value forKey:[NSString stringWithFormat:@"%@.%@", Prefix, settingKey]];
}

static void updateAllKeys() {
allKeys = [defaults dictionaryRepresentation].allKeys;
}
Expand All @@ -81,7 +86,7 @@ static BOOL getValueFromInvocation(id target, SEL selector) {
NSBundle *YTABCBundle() {
static NSBundle *bundle = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dispatch_once(&onceToken, ^{
NSString *tweakBundlePath = [[NSBundle mainBundle] pathForResource:@"YTABC" ofType:@"bundle"];
if (tweakBundlePath)
bundle = [NSBundle bundleWithPath:tweakBundlePath];
Expand Down Expand Up @@ -165,7 +170,9 @@ static NSString *getCategory(char c, NSString *method) {
NSString *deleteText = _LOC([NSBundle mainBundle], @"search.action.delete");
Class YTSettingsSectionItemClass = %c(YTSettingsSectionItem);
Class YTAlertViewClass = %c(YTAlertView);

if (tweakEnabled()) {
// AB flags
NSMutableDictionary <NSString *, NSMutableArray <YTSettingsSectionItem *> *> *properties = [NSMutableDictionary dictionary];
for (NSString *classKey in cache) {
for (NSString *method in cache[classKey]) {
Expand All @@ -175,6 +182,7 @@ static NSString *getCategory(char c, NSString *method) {
updateAllKeys();
BOOL modified = [allKeys containsObject:getKey(method, classKey)];
NSString *modifiedTitle = modified ? [NSString stringWithFormat:@"%@ *", method] : method;

YTSettingsSectionItem *methodSwitch = [YTSettingsSectionItemClass switchItemWithTitle:modifiedTitle
titleDescription:isPhone && method.length > 26 ? modifiedTitle : nil
accessibilityIdentifier:nil
Expand Down Expand Up @@ -219,6 +227,7 @@ static NSString *getCategory(char c, NSString *method) {
[rows sortUsingDescriptors:@[sort]];
NSString *shortTitle = [NSString stringWithFormat:@"\"%@\" (%ld)", category, rows.count];
NSString *title = [NSString stringWithFormat:@"%@ %@", LOC(@"SETTINGS_START_WITH"), shortTitle];

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:NSNotFound parentResponder:[self parentResponder]];
[settingsViewController pushViewController:picker];
Expand All @@ -231,6 +240,56 @@ static NSString *getCategory(char c, NSString *method) {
}
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES];
[sectionItems sortUsingDescriptors:@[sort]];

// Import settings
YTSettingsSectionItem *import = [YTSettingsSectionItemClass itemWithTitle:LOC(@"IMPORT_SETTINGS")
titleDescription:LOC(@"IMPORT_SETTINGS_DESC")
accessibilityIdentifier:nil
detailTextBlock:nil
selectBlock:^BOOL (YTSettingsCell *cell, NSUInteger arg1) {
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSArray *lines = [pasteboard.string componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
NSString *pattern = @"^(YT.*Config\\..*):\\s*(\\d)$";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
NSMutableDictionary *importedSettings = [NSMutableDictionary dictionary];
NSMutableArray *reportedSettings = [NSMutableArray array];

for (NSString *line in lines) {
NSTextCheckingResult *match = [regex firstMatchInString:line options:0 range:NSMakeRange(0, [line length])];
if (match) {
NSString *key = [line substringWithRange:[match rangeAtIndex:1]];
if ([cache valueForKeyPath:key] == nil) continue;
NSString *valueString = [line substringWithRange:[match rangeAtIndex:2]];
int integerValue = [valueString integerValue];
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
Expand All @@ -254,6 +313,8 @@ static NSString *getCategory(char c, NSString *method) {
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
Expand Down Expand Up @@ -281,6 +342,8 @@ static NSString *getCategory(char c, NSString *method) {
return YES;
}];
[sectionItems insertObject:modified atIndex:0];

// Reset and kill
YTSettingsSectionItem *reset = [YTSettingsSectionItemClass itemWithTitle:LOC(@"RESET_KILL")
titleDescription:LOC(@"RESET_KILL_DESC")
accessibilityIdentifier:nil
Expand All @@ -300,6 +363,8 @@ static NSString *getCategory(char c, NSString *method) {
return YES;
}];
[sectionItems insertObject:reset atIndex:0];

// Grouped settings
YTSettingsSectionItem *group = [YTSettingsSectionItemClass switchItemWithTitle:LOC(@"GROUPED")
titleDescription:nil
accessibilityIdentifier:nil
Expand All @@ -322,6 +387,8 @@ static NSString *getCategory(char c, NSString *method) {
settingItemId:0];
[sectionItems insertObject:group atIndex:0];
}

// Open megathread
YTSettingsSectionItem *thread = [YTSettingsSectionItemClass itemWithTitle:LOC(@"OPEN_MEGATHREAD")
titleDescription:LOC(@"OPEN_MEGATHREAD_DESC")
accessibilityIdentifier:nil
Expand All @@ -330,6 +397,8 @@ static NSString *getCategory(char c, NSString *method) {
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
Expand All @@ -345,10 +414,11 @@ static NSString *getCategory(char c, NSString *method) {
alertView.title = LOC(@"WARNING");
alertView.subtitle = LOC(@"APPLY_DESC");
[alertView show];
return YES;
return NO;
}
settingItemId:0];
[sectionItems insertObject:master atIndex:0];

YTSettingsViewController *delegate = [self valueForKey:@"_dataDelegate"];
NSString *title = @"A/B";
NSString *titleDescription = tweakEnabled() ? [NSString stringWithFormat:@"YTABConfig %@, %d feature flags.", @(OS_STRINGIFY(TWEAK_VERSION)), totalSettings] : nil;
Expand Down
Binary file modified layout/Library/Application Support/YTABC.bundle/Info.plist
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"COPY_CURRENT_SETTINGS_DESC" = "المس لنسخ إعداداتك الحالية.";
"VIEW_MODIFIED_SETTINGS" = "إظهار الإعدادات المعدَّلة";
"VIEW_MODIFIED_SETTINGS_DESC" = "المس لعرض جميع التغييرات التي أجريتها يدويَّاً.";
"IMPORT_SETTINGS" = "Import settings";
"IMPORT_SETTINGS_DESC" = "Import the settings from the clipboard.";
"NOTHING_TO_IMPORT" = "Nothing found from the clipboard.";
"SETTINGS_TO_IMPORT" = "Settings to import";
"SETTINGS_TO_IMPORT_DESC" = "Found %ld valid settings to be imported.";
"IMPORT" = "Import";

// Shown in alert
"MODIFIED_SETTINGS_TITLE" = "التغييرات";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"COPY_CURRENT_SETTINGS_DESC" = "Tippe hier, um alle Einstellungen in die Zwischenablage zu kopieren.";
"VIEW_MODIFIED_SETTINGS" = "Modifizierte Einstellungen anzeigen";
"VIEW_MODIFIED_SETTINGS_DESC" = "Tippe hier, um alle manuelle Änderungen zu sehen.";
"IMPORT_SETTINGS" = "Import settings";
"IMPORT_SETTINGS_DESC" = "Import the settings from the clipboard.";
"NOTHING_TO_IMPORT" = "Nothing found from the clipboard.";
"SETTINGS_TO_IMPORT" = "Settings to import";
"SETTINGS_TO_IMPORT_DESC" = "Found %ld valid settings to be imported.";
"IMPORT" = "Import";

// Shown in alert
"MODIFIED_SETTINGS_TITLE" = "Änderungen";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
"RESET_KILL" = "Reset and Kill";
"RESET_KILL_DESC" = "Tap to undo all of your changes and kill the app.";
"COPY_CURRENT_SETTINGS" = "Copy current settings";
"COPY_CURRENT_SETTINGS_DESC" = "Tap to copy the current settings to clipboard.";
"COPY_CURRENT_SETTINGS_DESC" = "Tap to copy the current settings to the clipboard.";
"VIEW_MODIFIED_SETTINGS" = "View modified settings";
"VIEW_MODIFIED_SETTINGS_DESC" = "Tap to view all the changes you made manually.";
"IMPORT_SETTINGS" = "Import settings";
"IMPORT_SETTINGS_DESC" = "Import the settings from the clipboard.";
"NOTHING_TO_IMPORT" = "Nothing found from the clipboard.";
"SETTINGS_TO_IMPORT" = "Settings to import";
"SETTINGS_TO_IMPORT_DESC" = "Found %ld valid settings to be imported.";
"IMPORT" = "Import";

// Shown in alert
"MODIFIED_SETTINGS_TITLE" = "Changes";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"COPY_CURRENT_SETTINGS_DESC" = "Toque para copiar la configuración actual en el portapapeles";
"VIEW_MODIFIED_SETTINGS" = "Ver ajustes modificados";
"VIEW_MODIFIED_SETTINGS_DESC" = "Pulse para ver todos los cambios realizados manualmente";
"IMPORT_SETTINGS" = "Import settings";
"IMPORT_SETTINGS_DESC" = "Import the settings from the clipboard.";
"NOTHING_TO_IMPORT" = "Nothing found from the clipboard.";
"SETTINGS_TO_IMPORT" = "Settings to import";
"SETTINGS_TO_IMPORT_DESC" = "Found %ld valid settings to be imported.";
"IMPORT" = "Import";

// Shown in alert
"MODIFIED_SETTINGS_TITLE" = "Cambios";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"COPY_CURRENT_SETTINGS_DESC" = "Érintse meg az aktuális beállítások vágólapra másolásához.";
"VIEW_MODIFIED_SETTINGS" = "A módosított beállítások megtekintése";
"VIEW_MODIFIED_SETTINGS_DESC" = "Érintse meg az összes manuális módosítás megtekintéséhez.";
"IMPORT_SETTINGS" = "Import settings";
"IMPORT_SETTINGS_DESC" = "Import the settings from the clipboard.";
"NOTHING_TO_IMPORT" = "Nothing found from the clipboard.";
"SETTINGS_TO_IMPORT" = "Settings to import";
"SETTINGS_TO_IMPORT_DESC" = "Found %ld valid settings to be imported.";
"IMPORT" = "Import";

// Shown in alert
"MODIFIED_SETTINGS_TITLE" = "Változtatások";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"COPY_CURRENT_SETTINGS_DESC" = "タップして現在の設定をクリップボードにコピーします";
"VIEW_MODIFIED_SETTINGS" = "変更された設定を表示します";
"VIEW_MODIFIED_SETTINGS_DESC" = "タップすると手動で行った全ての変更を表示します";
"IMPORT_SETTINGS" = "Import settings";
"IMPORT_SETTINGS_DESC" = "Import the settings from the clipboard.";
"NOTHING_TO_IMPORT" = "Nothing found from the clipboard.";
"SETTINGS_TO_IMPORT" = "Settings to import";
"SETTINGS_TO_IMPORT_DESC" = "Found %ld valid settings to be imported.";
"IMPORT" = "Import";

// Shown in alert
"MODIFIED_SETTINGS_TITLE" = "変更点";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"COPY_CURRENT_SETTINGS_DESC" = "현재 설정을 클립보드로 복사하려면 탭하세요.";
"VIEW_MODIFIED_SETTINGS" = "수정된 설정 보기";
"VIEW_MODIFIED_SETTINGS_DESC" = "수동으로 변경한 모든 내용을 보려면 탭하세요.";
"IMPORT_SETTINGS" = "Import settings";
"IMPORT_SETTINGS_DESC" = "Import the settings from the clipboard.";
"NOTHING_TO_IMPORT" = "Nothing found from the clipboard.";
"SETTINGS_TO_IMPORT" = "Settings to import";
"SETTINGS_TO_IMPORT_DESC" = "Found %ld valid settings to be imported.";
"IMPORT" = "Import";

// Shown in alert
"MODIFIED_SETTINGS_TITLE" = "변경 사항";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"COPY_CURRENT_SETTINGS_DESC" = "Toque para copiar as configurações atuais para a área de transferência.";
"VIEW_MODIFIED_SETTINGS" = "Ver configurações modificadas";
"VIEW_MODIFIED_SETTINGS_DESC" = "Toque para ver todas as alterações feitas manualmente.";
"IMPORT_SETTINGS" = "Import settings";
"IMPORT_SETTINGS_DESC" = "Import the settings from the clipboard.";
"NOTHING_TO_IMPORT" = "Nothing found from the clipboard.";
"SETTINGS_TO_IMPORT" = "Settings to import";
"SETTINGS_TO_IMPORT_DESC" = "Found %ld valid settings to be imported.";
"IMPORT" = "Import";

// Shown in alert
"MODIFIED_SETTINGS_TITLE" = "Mudanças";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"COPY_CURRENT_SETTINGS_DESC" = "Apăsați pentru a copia setările curente în clipboard.";
"VIEW_MODIFIED_SETTINGS" = "Vizualizare setări modificate";
"VIEW_MODIFIED_SETTINGS_DESC" = "Apăsați pentru a vizualiza toate modificările efectuate.";
"IMPORT_SETTINGS" = "Import settings";
"IMPORT_SETTINGS_DESC" = "Import the settings from the clipboard.";
"NOTHING_TO_IMPORT" = "Nothing found from the clipboard.";
"SETTINGS_TO_IMPORT" = "Settings to import";
"SETTINGS_TO_IMPORT_DESC" = "Found %ld valid settings to be imported.";
"IMPORT" = "Import";

// Shown in alert
"MODIFIED_SETTINGS_TITLE" = "Schimbări";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"COPY_CURRENT_SETTINGS_DESC" = "Нажмите, чтобы скопировать текущие настройки в буфер обмена.";
"VIEW_MODIFIED_SETTINGS" = "Отобразить изменения";
"VIEW_MODIFIED_SETTINGS_DESC" = "Нажмите, чтобы увидеть примененные вами изменения.";
"IMPORT_SETTINGS" = "Import settings";
"IMPORT_SETTINGS_DESC" = "Import the settings from the clipboard.";
"NOTHING_TO_IMPORT" = "Nothing found from the clipboard.";
"SETTINGS_TO_IMPORT" = "Settings to import";
"SETTINGS_TO_IMPORT_DESC" = "Found %ld valid settings to be imported.";
"IMPORT" = "Import";

// Shown in alert
"MODIFIED_SETTINGS_TITLE" = "Настройки";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"COPY_CURRENT_SETTINGS_DESC" = "Mevcut ayarları panoya kopyalamak için dokun.";
"VIEW_MODIFIED_SETTINGS" = "Değiştirilen ayarları görüntüle";
"VIEW_MODIFIED_SETTINGS_DESC" = "El ile yaptığın tüm değişiklikleri görüntülemek için dokun.";
"IMPORT_SETTINGS" = "Import settings";
"IMPORT_SETTINGS_DESC" = "Import the settings from the clipboard.";
"NOTHING_TO_IMPORT" = "Nothing found from the clipboard.";
"SETTINGS_TO_IMPORT" = "Settings to import";
"SETTINGS_TO_IMPORT_DESC" = "Found %ld valid settings to be imported.";
"IMPORT" = "Import";

// Shown in alert
"MODIFIED_SETTINGS_TITLE" = "Değişiklikler";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"COPY_CURRENT_SETTINGS_DESC" = "Sao chép các thay đổi hiện tại của bạn vào bộ nhớ tạm.";
"VIEW_MODIFIED_SETTINGS" = "Xem các thay đổi";
"VIEW_MODIFIED_SETTINGS_DESC" = "Chạm để xem tất cả các thay đổi mà bạn đã áp dụng.";
"IMPORT_SETTINGS" = "Import settings";
"IMPORT_SETTINGS_DESC" = "Import the settings from the clipboard.";
"NOTHING_TO_IMPORT" = "Nothing found from the clipboard.";
"SETTINGS_TO_IMPORT" = "Settings to import";
"SETTINGS_TO_IMPORT_DESC" = "Found %ld valid settings to be imported.";
"IMPORT" = "Import";

// Shown in alert
"MODIFIED_SETTINGS_TITLE" = "Các thay đổi";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"COPY_CURRENT_SETTINGS_DESC" = "将当前所有设置复制到剪贴板。";
"VIEW_MODIFIED_SETTINGS" = "查看已改动的选项";
"VIEW_MODIFIED_SETTINGS_DESC" = "查看您已手动更改的选项。";
"IMPORT_SETTINGS" = "Import settings";
"IMPORT_SETTINGS_DESC" = "Import the settings from the clipboard.";
"NOTHING_TO_IMPORT" = "Nothing found from the clipboard.";
"SETTINGS_TO_IMPORT" = "Settings to import";
"SETTINGS_TO_IMPORT_DESC" = "Found %ld valid settings to be imported.";
"IMPORT" = "Import";

// Shown in alert
"MODIFIED_SETTINGS_TITLE" = "已改动的选项";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"COPY_CURRENT_SETTINGS_DESC" = "點擊將目前的設定複製到剪貼簿";
"VIEW_MODIFIED_SETTINGS" = "查看修改的設定";
"VIEW_MODIFIED_SETTINGS_DESC" = "點擊查看您修改的所有設定";
"IMPORT_SETTINGS" = "Import settings";
"IMPORT_SETTINGS_DESC" = "Import the settings from the clipboard.";
"NOTHING_TO_IMPORT" = "Nothing found from the clipboard.";
"SETTINGS_TO_IMPORT" = "Settings to import";
"SETTINGS_TO_IMPORT_DESC" = "Found %ld valid settings to be imported.";
"IMPORT" = "Import";

// Shown in alert
"MODIFIED_SETTINGS_TITLE" = "已變更的";
Expand Down

0 comments on commit 265d2c6

Please sign in to comment.