Skip to content

Commit

Permalink
Allow customizing number of trending subreddits to show
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyCA committed Dec 20, 2024
1 parent ba0718d commit 2125eac
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
1 change: 1 addition & 0 deletions CustomAPIViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ NSString *sImgurClientId;
NSString *sRandomSubredditsSource;
NSString *sRandNsfwSubredditsSource;
NSString *sTrendingSubredditsSource;
NSString *sTrendingSubredditsLimit;

BOOL sBlockAnnouncements;
26 changes: 17 additions & 9 deletions CustomAPIViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ typedef NS_ENUM(NSInteger, Tag) {
TagImgurClientId,
TagTrendingSubredditsSource,
TagRandomSubredditsSource,
TagRandNsfwSubredditsSource
TagRandNsfwSubredditsSource,
TagTrendingLimit,
};

- (UIImage *)decodeBase64ToImage:(NSString *)strEncodeData {
Expand Down Expand Up @@ -65,7 +66,7 @@ - (UIButton *)creditsButton:(NSString *)labelText subtitle:(NSString *)subtitle
return button;
}

- (UIStackView *)createLabeledStackViewWithLabelText:(NSString *)labelText placeholder:(NSString *)placeholder text:(NSString *)text tag:(NSInteger)tag {
- (UIStackView *)createLabeledStackViewWithLabelText:(NSString *)labelText placeholder:(NSString *)placeholder text:(NSString *)text tag:(NSInteger)tag isNumerical:(BOOL)isNumerical {
UIStackView *stackView = [[UIStackView alloc] init];
stackView.axis = UILayoutConstraintAxisVertical;
stackView.distribution = UIStackViewDistributionFillProportionally;
Expand All @@ -83,13 +84,20 @@ - (UIStackView *)createLabeledStackViewWithLabelText:(NSString *)labelText place
textField.delegate = self;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.font = [UIFont systemFontOfSize:14];
if (isNumerical) {
textField.keyboardType = UIKeyboardTypeNumberPad;
}

[stackView addArrangedSubview:label];
[stackView addArrangedSubview:textField];

return stackView;
}

- (UIStackView *)createLabeledStackViewWithLabelText:(NSString *)labelText placeholder:(NSString *)placeholder text:(NSString *)text tag:(NSInteger)tag {
return [self createLabeledStackViewWithLabelText:labelText placeholder:placeholder text:text tag:tag isNumerical:NO];
}

- (void)viewDidLoad {
[super viewDidLoad];

Expand Down Expand Up @@ -152,12 +160,12 @@ - (void)viewDidLoad {
UIStackView *flexStackView = [self createToggleSwitchWithKey:UDKeyEnableFLEX labelText:@"FLEX Debugging (Needs restart)" action:@selector(flexSwitchToggled:)];
[stackView addArrangedSubview:flexStackView];

UIStackView *limitTrendingStackView = [self createToggleSwitchWithKey:UDKeyLimitTrending labelText:@"Limit trending subreddits to 5" action:@selector(limitTrendingSwitchToggled:)];
[stackView addArrangedSubview:limitTrendingStackView];

UIStackView *randNsfwStackView = [self createToggleSwitchWithKey:UDKeyShowRandNsfw labelText:@"RandNSFW button" action:@selector(randNsfwSwitchToggled:)];
[stackView addArrangedSubview:randNsfwStackView];

UIStackView *trendingSubredditsLimitStackView = [self createLabeledStackViewWithLabelText:@"Limit trending subreddits to:" placeholder:@"(unlimited)" text:sTrendingSubredditsLimit tag:TagTrendingLimit isNumerical:YES];
[stackView addArrangedSubview:trendingSubredditsLimitStackView];

UIStackView *trendingSourceStackView = [self createLabeledStackViewWithLabelText:@"Trending subreddits source:" placeholder:defaultTrendingSubredditsSource text:sTrendingSubredditsSource tag:TagTrendingSubredditsSource];
[stackView addArrangedSubview:trendingSourceStackView];

Expand Down Expand Up @@ -278,6 +286,10 @@ - (void)textFieldDidEndEditing:(UITextField *)textField {
textField.text = [textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
sRandNsfwSubredditsSource = textField.text;
[[NSUserDefaults standardUserDefaults] setValue:sRandNsfwSubredditsSource forKey:UDKeyRandNsfwSubredditsSource];
} else if (textField.tag == TagTrendingLimit) {
textField.text = [textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
sTrendingSubredditsLimit = textField.text;
[[NSUserDefaults standardUserDefaults] setValue:sTrendingSubredditsLimit forKey:UDKeyTrendingSubredditsLimit];
}
}

Expand All @@ -294,10 +306,6 @@ - (void)flexSwitchToggled:(UISwitch *)sender {
[[NSUserDefaults standardUserDefaults] setBool:sender.isOn forKey:UDKeyEnableFLEX];
}

- (void)limitTrendingSwitchToggled:(UISwitch *)sender {
[[NSUserDefaults standardUserDefaults] setBool:sender.isOn forKey:UDKeyLimitTrending];
}

- (void)randNsfwSwitchToggled:(UISwitch *)sender {
[[NSUserDefaults standardUserDefaults] setBool:sender.isOn forKey:UDKeyShowRandNsfw];
}
Expand Down
10 changes: 6 additions & 4 deletions Tweak.xm
Original file line number Diff line number Diff line change
Expand Up @@ -498,9 +498,10 @@ static void TryResolveShareUrl(NSString *urlString, void (^successHandler)(NSStr
}

NSMutableDictionary *dict = [NSMutableDictionary dictionary];
// Limit to 5 subreddits, chosen randomly
if (subreddits.count > 5 && [[NSUserDefaults standardUserDefaults] boolForKey:UDKeyLimitTrending]) {
NSUInteger count = MIN(5, subreddits.count);
// Randomize and limit subreddits
bool limitSubreddits = [sTrendingSubredditsLimit length] > 0;
if (limitSubreddits && [sTrendingSubredditsLimit integerValue] < subreddits.count) {
NSUInteger count = [sTrendingSubredditsLimit integerValue];
NSMutableArray<NSString *> *randomSubreddits = [NSMutableArray arrayWithCapacity:count];
for (NSUInteger i = 0; i < count; i++) {
NSUInteger randomIndex = arc4random_uniform((uint32_t)subreddits.count);
Expand Down Expand Up @@ -823,7 +824,7 @@ static void initializeRandomSources() {
MediaShareLinkRegex = [NSRegularExpression regularExpressionWithPattern:MediaShareLinkPattern options:NSRegularExpressionCaseInsensitive error:&error];
ImgurTitleIdImageLinkRegex = [NSRegularExpression regularExpressionWithPattern:ImgurTitleIdImageLinkPattern options:NSRegularExpressionCaseInsensitive error:&error];

NSDictionary *defaultValues = @{UDKeyBlockAnnouncements: @YES, UDKeyEnableFLEX: @NO, UDKeyApolloShowUnreadComments: @NO, UDKeyLimitTrending: @YES, UDKeyShowRandNsfw: @NO, UDKeyRandomSubredditsSource:defaultRandomSubredditsSource, UDKeyRandNsfwSubredditsSource: @"", UDKeyTrendingSubredditsSource: defaultTrendingSubredditsSource };
NSDictionary *defaultValues = @{UDKeyBlockAnnouncements: @YES, UDKeyEnableFLEX: @NO, UDKeyApolloShowUnreadComments: @NO, UDKeyTrendingSubredditsLimit: @"5", UDKeyShowRandNsfw: @NO, UDKeyRandomSubredditsSource:defaultRandomSubredditsSource, UDKeyRandNsfwSubredditsSource: @"", UDKeyTrendingSubredditsSource: defaultTrendingSubredditsSource };
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];

sRedditClientId = (NSString *)[[[NSUserDefaults standardUserDefaults] objectForKey:UDKeyRedditClientId] ?: @"" copy];
Expand All @@ -833,6 +834,7 @@ static void initializeRandomSources() {
sRandomSubredditsSource = (NSString *)[[NSUserDefaults standardUserDefaults] objectForKey:UDKeyRandomSubredditsSource];
sRandNsfwSubredditsSource = (NSString *)[[NSUserDefaults standardUserDefaults] objectForKey:UDKeyRandNsfwSubredditsSource];
sTrendingSubredditsSource = (NSString *)[[NSUserDefaults standardUserDefaults] objectForKey:UDKeyTrendingSubredditsSource];
sTrendingSubredditsLimit = (NSString *)[[NSUserDefaults standardUserDefaults] objectForKey:UDKeyTrendingSubredditsLimit];

%init(SettingsGeneralViewController=objc_getClass("Apollo.SettingsGeneralViewController"), ApolloTabBarController=objc_getClass("Apollo.ApolloTabBarController"));

Expand Down
2 changes: 1 addition & 1 deletion UserDefaultConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ static NSString *const UDKeyRedditClientId = @"RedditApiClientId";
static NSString *const UDKeyImgurClientId = @"ImgurApiClientId";
static NSString *const UDKeyBlockAnnouncements = @"DisableApollonouncements";
static NSString *const UDKeyEnableFLEX = @"EnableFlexDebugging";
static NSString *const UDKeyLimitTrending = @"LimitCustomTrending";
static NSString *const UDKeyShowRandNsfw = @"ShowRandNsfwButton";
static NSString *const UDKeyRandomSubredditsSource = @"RandomSubredditsSource";
static NSString *const UDKeyRandNsfwSubredditsSource = @"RandNsfwSubredditsSource";
static NSString *const UDKeyTrendingSubredditsSource = @"TrendingSubredditsSource";
static NSString *const UDKeyTrendingSubredditsLimit = @"TrendingSubredditsLimit";

static NSString *const UDKeyApolloShowUnreadComments = @"ShowUnreadComments";

Expand Down

0 comments on commit 2125eac

Please sign in to comment.