From 3f3b9cf167f5e067508bf01a0547a118198d9a78 Mon Sep 17 00:00:00 2001 From: lint <47455468+lint@users.noreply.github.com> Date: Thu, 21 Nov 2019 19:06:28 -0500 Subject: [PATCH] Improve preferences --- prefs/Resources/Root.plist | 48 +++++++++++++++++++++++++++++ tweak/Apollo.xm | 55 ++++++++++++++++++++++++--------- tweak/Narwhal.xm | 44 ++++++++++++++++++++------ tweak/Reddit.xm | 63 +++++++++++++++++++++++++------------- 4 files changed, 165 insertions(+), 45 deletions(-) diff --git a/prefs/Resources/Root.plist b/prefs/Resources/Root.plist index 8d91249..2869a5c 100644 --- a/prefs/Resources/Root.plist +++ b/prefs/Resources/Root.plist @@ -4,6 +4,48 @@ items + + cell + PSGroupCell + label + Enabled + + + cell + PSSwitchCell + default + + defaults + com.lint.undelete.prefs + key + isRedditEnabled + label + Enable Reddit + + + cell + PSSwitchCell + default + + defaults + com.lint.undelete.prefs + key + isApolloEnabled + label + Enable Apollo + + + cell + PSSwitchCell + default + + defaults + com.lint.undelete.prefs + key + isNarwhalEnabled + label + Enable Narwhal + cell PSGroupCell @@ -48,6 +90,12 @@ showValue + + cell + PSGroupCell + footerText + In order to apply preferences, restart the app. If changes do not immediately take effect, wait a few seconds and try again. + title TFDidThatSay? diff --git a/tweak/Apollo.xm b/tweak/Apollo.xm index 571e05e..319c111 100644 --- a/tweak/Apollo.xm +++ b/tweak/Apollo.xm @@ -1,10 +1,9 @@ -#import #import "Apollo.h" -HBPreferences *apolloPrefs; -BOOL isApolloDeletedCommentsOnly; -CGFloat apolloRequestTimeoutValue; +static BOOL isApolloDeletedCommentsOnly; +static BOOL isApolloEnabled; +static CGFloat pushshiftRequestTimeoutValue; %group Apollo @@ -54,7 +53,7 @@ NSDictionary* apolloBodyAttributes = nil; [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/comment/?ids=%@&fields=author,body",[[comment fullName] componentsSeparatedByString:@"_"][1]]]]; [request setHTTPMethod:@"GET"]; - [request setTimeoutInterval:[apolloPrefs doubleForKey:@"requestTimeoutValue" default:10]]; + [request setTimeoutInterval:pushshiftRequestTimeoutValue]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { @@ -97,9 +96,7 @@ NSDictionary* apolloBodyAttributes = nil; id commentBody = [MSHookIvar(self, "comment") body]; - BOOL isDeletedOnly = [apolloPrefs boolForKey:@"isApolloDeletedCommentsOnly"]; - - if ((isDeletedOnly && ([commentBody isEqualToString:@"[deleted]"] || [commentBody isEqualToString:@"[removed]"])) || !isDeletedOnly) { + if ((isApolloDeletedCommentsOnly && ([commentBody isEqualToString:@"[deleted]"] || [commentBody isEqualToString:@"[removed]"])) || !isApolloDeletedCommentsOnly) { CGFloat imageSize = 20.0f; @@ -157,7 +154,7 @@ NSDictionary* apolloBodyAttributes = nil; [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/submission/?ids=%@&fields=author,selftext",[[post fullName] componentsSeparatedByString:@"_"][1]]]]; [request setHTTPMethod:@"GET"]; - [request setTimeoutInterval:[apolloPrefs doubleForKey:@"requestTimeoutValue" default:10]]; + [request setTimeoutInterval:pushshiftRequestTimeoutValue]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { @@ -238,15 +235,45 @@ NSDictionary* apolloBodyAttributes = nil; %end -%ctor { +static void loadPrefs(){ + NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/User/Library/Preferences/com.lint.undelete.prefs.plist"]; - apolloPrefs = [[HBPreferences alloc] initWithIdentifier:@"com.lint.undelete.prefs"]; - [apolloPrefs registerBool:&isApolloDeletedCommentsOnly default:YES forKey:@"isApolloDeletedCommentsOnly"]; - [apolloPrefs registerDouble:&apolloRequestTimeoutValue default:10 forKey:@"requestTimeoutValue"]; + if (prefs){ + + if ([prefs objectForKey:@"isApolloEnabled"] != nil) { + isApolloEnabled = [[prefs objectForKey:@"isApolloEnabled"] boolValue]; + } else { + isApolloEnabled = YES; + } + + if ([prefs objectForKey:@"requestTimeoutValue"] != nil){ + pushshiftRequestTimeoutValue = [[prefs objectForKey:@"requestTimeoutValue"] doubleValue]; + } else { + pushshiftRequestTimeoutValue = 10; + } + + if ([prefs objectForKey:@"isApolloDeletedCommentsOnly"] != nil) { + isApolloDeletedCommentsOnly = [[prefs objectForKey:@"isApolloDeletedCommentsOnly"] boolValue]; + } else { + isApolloDeletedCommentsOnly = YES; + } + + } else { + isApolloEnabled = YES; + pushshiftRequestTimeoutValue = 10; + isApolloDeletedCommentsOnly = YES; + } +} + + +%ctor { + loadPrefs(); NSString* processName = [[NSProcessInfo processInfo] processName]; if ([processName isEqualToString:@"Apollo"]){ - %init(Apollo, ApolloCommentsHeaderCellNode = objc_getClass("Apollo.CommentsHeaderCellNode"), ApolloCommentCellNode = objc_getClass("Apollo.CommentCellNode"), ApolloApolloButtonNode = objc_getClass("Apollo.ApolloButtonNode")); + if (isApolloEnabled){ + %init(Apollo, ApolloCommentsHeaderCellNode = objc_getClass("Apollo.CommentsHeaderCellNode"), ApolloCommentCellNode = objc_getClass("Apollo.CommentCellNode"), ApolloApolloButtonNode = objc_getClass("Apollo.ApolloButtonNode")); + } } } diff --git a/tweak/Narwhal.xm b/tweak/Narwhal.xm index 5234891..3d1543b 100644 --- a/tweak/Narwhal.xm +++ b/tweak/Narwhal.xm @@ -1,9 +1,8 @@ -#import #import "Narwhal.h" -HBPreferences *narwhalPrefs; -CGFloat narwhalRequestTimeoutValue; +static BOOL isNarwhalEnabled; +static CGFloat pushshiftRequestTimeoutValue; %group Narwhal @@ -18,7 +17,7 @@ void getUndeleteCommentData(id controller, id comment){ [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/comment/?ids=%@&fields=author,body",[[comment fullName] componentsSeparatedByString:@"_"][1]]]]; [request setHTTPMethod:@"GET"]; - [request setTimeoutInterval:[narwhalPrefs doubleForKey:@"requestTimeoutValue" default:10]]; + [request setTimeoutInterval:pushshiftRequestTimeoutValue]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { @@ -112,7 +111,7 @@ void getUndeleteCommentData(id controller, id comment){ [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/submission/?ids=%@&fields=author,selftext",[[post fullName] componentsSeparatedByString:@"_"][1]]]]; [request setHTTPMethod:@"GET"]; - [request setTimeoutInterval:[narwhalPrefs doubleForKey:@"requestTimeoutValue" default:10]]; + [request setTimeoutInterval:pushshiftRequestTimeoutValue]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { @@ -212,15 +211,40 @@ void getUndeleteCommentData(id controller, id comment){ %end -%ctor { + +static void loadPrefs(){ + NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/User/Library/Preferences/com.lint.undelete.prefs.plist"]; - narwhalPrefs = [[HBPreferences alloc] initWithIdentifier:@"com.lint.undelete.prefs"]; - [narwhalPrefs registerDouble:&narwhalRequestTimeoutValue default:10 forKey:@"requestTimeoutValue"]; + if (prefs){ + + if ([prefs objectForKey:@"isNarwhalEnabled"] != nil){ + isNarwhalEnabled = [[prefs objectForKey:@"isNarwhalEnabled"] boolValue]; + } else { + isNarwhalEnabled = YES; + } + + if ([prefs objectForKey:@"requestTimeoutValue"] != nil){ + pushshiftRequestTimeoutValue = [[prefs objectForKey:@"requestTimeoutValue"] doubleValue]; + } else { + pushshiftRequestTimeoutValue = 10; + } + + } else { + isNarwhalEnabled = YES; + pushshiftRequestTimeoutValue = 10; + } +} + + +%ctor { + loadPrefs(); NSString* processName = [[NSProcessInfo processInfo] processName]; - if ([processName isEqualToString:@"narwhal"]){ - %init(Narwhal); + if ([processName isEqualToString:@"narwhal"]){ + if (isNarwhalEnabled){ + %init(Narwhal); + } } } diff --git a/tweak/Reddit.xm b/tweak/Reddit.xm index 9140e0e..1e88d9a 100644 --- a/tweak/Reddit.xm +++ b/tweak/Reddit.xm @@ -1,9 +1,8 @@ -#import #import "Reddit.h" -HBPreferences *redditPrefs; -CGFloat redditRequestTimeoutValue; +static CGFloat pushshiftRequestTimeoutValue; +static BOOL isRedditEnabled; NSArray *redditVersion; @@ -78,7 +77,7 @@ NSArray *redditVersion; [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/comment/?ids=%@&fields=author,body",[[comment pk] componentsSeparatedByString:@"_"][1]]]]; [request setHTTPMethod:@"GET"]; - [request setTimeoutInterval:[redditPrefs doubleForKey:@"requestTimeoutValue" default:10]]; + [request setTimeoutInterval:pushshiftRequestTimeoutValue]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { @@ -223,7 +222,7 @@ NSArray *redditVersion; [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/submission/?ids=%@&fields=author,selftext",[[post pk] componentsSeparatedByString:@"_"][1]]]]; [request setHTTPMethod:@"GET"]; - [request setTimeoutInterval:[redditPrefs doubleForKey:@"requestTimeoutValue" default:10]]; + [request setTimeoutInterval:pushshiftRequestTimeoutValue]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { @@ -379,7 +378,7 @@ NSArray *redditVersion; [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/comment/?ids=%@&fields=author,body",[[comment pk] componentsSeparatedByString:@"_"][1]]]]; [request setHTTPMethod:@"GET"]; - [request setTimeoutInterval:[redditPrefs doubleForKey:@"requestTimeoutValue" default:10]]; + [request setTimeoutInterval:pushshiftRequestTimeoutValue]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { @@ -432,7 +431,7 @@ NSArray *redditVersion; [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/comment/?ids=%@&fields=author,body",[[comment pk] componentsSeparatedByString:@"_"][1]]]]; [request setHTTPMethod:@"GET"]; - [request setTimeoutInterval:[redditPrefs doubleForKey:@"requestTimeoutValue" default:10]]; + [request setTimeoutInterval:pushshiftRequestTimeoutValue]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { @@ -523,7 +522,7 @@ NSArray *redditVersion; [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/submission/?ids=%@&fields=author,selftext",[[post pk] componentsSeparatedByString:@"_"][1]]]]; [request setHTTPMethod:@"GET"]; - [request setTimeoutInterval:[redditPrefs doubleForKey:@"requestTimeoutValue" default:10]]; + [request setTimeoutInterval:pushshiftRequestTimeoutValue]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { @@ -579,7 +578,7 @@ NSArray *redditVersion; [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/submission/?ids=%@&fields=author,selftext",[[post pk] componentsSeparatedByString:@"_"][1]]]]; [request setHTTPMethod:@"GET"]; - [request setTimeoutInterval:[redditPrefs doubleForKey:@"requestTimeoutValue" default:10]]; + [request setTimeoutInterval:pushshiftRequestTimeoutValue]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { @@ -712,25 +711,47 @@ NSArray *redditVersion; %end +static void loadPrefs(){ + NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/User/Library/Preferences/com.lint.undelete.prefs.plist"]; + + if (prefs){ + + if ([prefs objectForKey:@"isRedditEnabled"] != nil){ + isRedditEnabled = [[prefs objectForKey:@"isRedditEnabled"] boolValue]; + } else { + isRedditEnabled = YES; + } + + if ([prefs objectForKey:@"requestTimeoutValue"] != nil){ + pushshiftRequestTimeoutValue = [[prefs objectForKey:@"requestTimeoutValue"] doubleValue]; + } else { + pushshiftRequestTimeoutValue = 10; + } + + } else { + isRedditEnabled = YES; + pushshiftRequestTimeoutValue = 10; + } +} %ctor{ - - redditPrefs = [[HBPreferences alloc] initWithIdentifier:@"com.lint.undelete.prefs"]; - [redditPrefs registerDouble:&redditRequestTimeoutValue default:10 forKey:@"requestTimeoutValue"]; + loadPrefs(); NSString* processName = [[NSProcessInfo processInfo] processName]; redditVersion = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] componentsSeparatedByString:@"."]; - if ([processName isEqualToString:@"Reddit"]){ - if ([redditVersion[0] isEqualToString:@"4"]){ - if ([redditVersion[1] integerValue] <= 32){ - %init(Reddit_v4_ios10); - } else{ - %init(Reddit_v4_current); - } - } else if ([redditVersion[0] isEqualToString:@"3"]) { - %init(Reddit_v3); + if ([processName isEqualToString:@"Reddit"]){ + if (isRedditEnabled) { + if ([redditVersion[0] isEqualToString:@"4"]){ + if ([redditVersion[1] integerValue] <= 32){ + %init(Reddit_v4_ios10); + } else{ + %init(Reddit_v4_current); + } + } else if ([redditVersion[0] isEqualToString:@"3"]) { + %init(Reddit_v3); + } } } }