From dc0ad9363df082f74bb11d44fc6f73509cadb7aa Mon Sep 17 00:00:00 2001 From: lint <47455468+lint@users.noreply.github.com> Date: Sat, 11 Jan 2020 13:45:34 -0500 Subject: [PATCH] Fix Apollo crash, add option to show button --- Makefile | 1 + control | 4 +- prefs/Resources/Root.plist | 20 +++++++ tweak/Apollo.h | 1 + tweak/Apollo.xm | 110 ++++++++++++++++++++++++++++++------- 5 files changed, 115 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index 6f9bc4c..cbdf696 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ TWEAK_NAME = tfdidthatsay tfdidthatsay_FILES = $(wildcard tweak/*.xm tweak/assets/*.m tweak/assets/MMMarkdown/*.m) tfdidthatsay_CFLAGS = -fobjc-arc tweak/Reddit.xm_CFLAGS = -fno-objc-arc +tweak/Apollo.xm_CFLAGS = -fno-objc-arc include $(THEOS_MAKE_PATH)/tweak.mk diff --git a/control b/control index e9359b9..3147a04 100644 --- a/control +++ b/control @@ -1,7 +1,7 @@ Package: com.lint.undelete Name: TFDidThatSay? -Depends: mobilesubstrate, ws.hbang.common -Version: 1.3.0 +Depends: mobilesubstrate +Version: 1.3.1 Architecture: iphoneos-arm Description: See "[deleted]" comments and posts without leaving Reddit! Maintainer: lint diff --git a/prefs/Resources/Root.plist b/prefs/Resources/Root.plist index 7aa1a7f..7428935 100644 --- a/prefs/Resources/Root.plist +++ b/prefs/Resources/Root.plist @@ -146,6 +146,26 @@ label Deleted comments/posts only + + cell + PSGroupCell + footerText + In Apollo, show undelete option on the comment cell rather than in the action menu. + + + PostNotification + com.lint.undelete.prefs.changed + cell + PSSwitchCell + default + + defaults + com.lint.undelete.prefs + key + shouldApolloHaveButton + label + Show button in Apollo + cell PSGroupCell diff --git a/tweak/Apollo.h b/tweak/Apollo.h index 910b811..2827209 100644 --- a/tweak/Apollo.h +++ b/tweak/Apollo.h @@ -16,6 +16,7 @@ -(void) calculatedLayoutDidChange; //custom elements +@property(strong,nonatomic) UIButton *undeleteButton; -(void) undeleteCellWasSelected; @end diff --git a/tweak/Apollo.xm b/tweak/Apollo.xm index 0458824..2568e96 100644 --- a/tweak/Apollo.xm +++ b/tweak/Apollo.xm @@ -5,6 +5,7 @@ static BOOL isTFDeletedOnly; static BOOL isApolloEnabled; static CGFloat pushshiftRequestTimeoutValue; +static BOOL shouldApolloHaveButton; %group Apollo @@ -67,17 +68,19 @@ id apolloCommentController; if (shouldAddUndeleteCell){ if ([arg2 row] == [self tableView:arg1 numberOfRowsInSection:0] - 1){ - id undeleteCell = [[objc_getClass("Apollo.IconActionTableViewCell") alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"IconActionCell"]; - NSArray *visibleCells = [arg1 visibleCells]; - id prevCell = visibleCells[[visibleCells count] - 2]; + id undeleteCell = [arg1 dequeueReusableCellWithIdentifier:@"IconActionCell" forIndexPath:arg2]; + id prevCell = [arg1 dequeueReusableCellWithIdentifier:@"IconActionCell"]; UIImageView *prevCellImageView = MSHookIvar(prevCell, "iconImageView"); CGSize prevImageSize = [[prevCellImageView image] size]; - UIColor *menuColor = [prevCellImageView tintColor]; UIImage *undeleteImage = [UIImage imageWithContentsOfFile:@"/var/mobile/Library/Application Support/TFDidThatSay/eye160dark.png"]; CGFloat undeleteImageSizeValue = prevImageSize.width > prevImageSize.height ? prevImageSize.width : prevImageSize.height; + if (undeleteImageSizeValue == 0){ + undeleteImageSizeValue = 25; + } + UIGraphicsBeginImageContextWithOptions(CGSizeMake(undeleteImageSizeValue, undeleteImageSizeValue), NO, 0); [undeleteImage drawInRect:CGRectMake(0, 0, undeleteImageSizeValue, undeleteImageSizeValue)]; undeleteImage = [UIGraphicsGetImageFromCurrentImageContext() imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; @@ -87,10 +90,7 @@ id apolloCommentController; UIImageView *undeleteImageView = MSHookIvar(undeleteCell, "iconImageView"); undeleteLabel.text = @"TF Did That Say?"; - undeleteLabel.textColor = menuColor; - undeleteImageView.image = undeleteImage; - undeleteImageView.tintColor = menuColor; return undeleteCell; } @@ -135,15 +135,19 @@ id apolloCommentController; %hook CommentCellNode +%property(strong,nonatomic) UIButton *undeleteButton; -(void) moreOptionsTappedWithSender:(id) arg1{ - NSString *commentBody = [MSHookIvar(self, "comment") body]; - - if ([%c(TFHelper) shouldShowUndeleteButtonWithInfo:commentBody isDeletedOnly:isTFDeletedOnly]){ - shouldAddUndeleteCell = YES; - apolloCommentCell = self; - apolloCommentController = nil; + if (!shouldApolloHaveButton){ + + NSString *commentBody = [MSHookIvar(self, "comment") body]; + + if ([%c(TFHelper) shouldShowUndeleteButtonWithInfo:commentBody isDeletedOnly:isTFDeletedOnly]){ + shouldAddUndeleteCell = YES; + apolloCommentCell = self; + apolloCommentController = nil; + } } %orig; @@ -151,17 +155,74 @@ id apolloCommentController; -(void) longPressedWithGestureRecognizer:(id) arg1{ - NSString *commentBody = [MSHookIvar(self, "comment") body]; - - if ([%c(TFHelper) shouldShowUndeleteButtonWithInfo:commentBody isDeletedOnly:isTFDeletedOnly]){ - shouldAddUndeleteCell = YES; - apolloCommentCell = self; - apolloCommentController = nil; + if (!shouldApolloHaveButton){ + + NSString *commentBody = [MSHookIvar(self, "comment") body]; + + if ([%c(TFHelper) shouldShowUndeleteButtonWithInfo:commentBody isDeletedOnly:isTFDeletedOnly]){ + shouldAddUndeleteCell = YES; + apolloCommentCell = self; + apolloCommentController = nil; + } } %orig; } +-(void) didLoad { + %orig; + + if (shouldApolloHaveButton){ + + NSString *commentBody = [MSHookIvar(self, "comment") body]; + + if ([%c(TFHelper) shouldShowUndeleteButtonWithInfo:commentBody isDeletedOnly:isTFDeletedOnly]){ + + CGFloat imageSize = 20.0f; + + UIButton *undeleteButton = [UIButton buttonWithType:UIButtonTypeCustom]; + [undeleteButton addTarget:self action:@selector(didTapUndeleteButton:) forControlEvents:UIControlEventTouchUpInside]; + undeleteButton.frame = CGRectMake(0, 0, imageSize, imageSize); + + UIImage* undeleteImage = [UIImage imageWithContentsOfFile:@"/var/mobile/Library/Application Support/TFDidThatSay/eye160dark.png"]; + [undeleteButton setImage:undeleteImage forState:UIControlStateNormal]; + + [[self view] addSubview:undeleteButton]; + [self setUndeleteButton:undeleteButton]; + } + } +} + +-(void) _layoutSublayouts{ + %orig; + + if (shouldApolloHaveButton){ + if ([self undeleteButton]){ + + CGFloat imageSize = 20.0f; + + id moreNode = MSHookIvar(self, "moreOptionsNode"); + id ageNode = MSHookIvar(self, "ageNode"); + + CGRect nodeFrame = [moreNode frame]; + CGFloat centerHeight = (nodeFrame.size.height + nodeFrame.origin.y * 2) / 2.0f; + CGFloat nodeSpacing = [ageNode frame].origin.x - nodeFrame.origin.x - nodeFrame.size.width; + + [[self undeleteButton] setFrame:CGRectMake(nodeFrame.origin.x - imageSize - nodeSpacing, centerHeight - (imageSize / 2), imageSize, imageSize)]; + } + } +} + +%new +-(void) didTapUndeleteButton:(id) sender{ + + [sender setEnabled:NO]; + + id comment = MSHookIvar(self, "comment"); + + [%c(TFHelper) getUndeleteDataWithID:[[comment fullName] componentsSeparatedByString:@"_"][1] isComment:YES timeout:pushshiftRequestTimeoutValue extraData:@{@"sender" : sender} completionTarget:self completionSelector:@selector(completeUndeleteCommentAction:)]; +} + %new -(void) undeleteCellWasSelected{ @@ -189,6 +250,10 @@ id apolloCommentController; [authorNode setAttributedTitle:newAuthorAttributedString forState:UIControlStateNormal]; [bodyNode setAttributedString:[%c(MarkdownRenderer) attributedStringFromMarkdown:body withAttributes:apolloBodyAttributes]]; + + if ([data objectForKey:@"sender"]) { + [data[@"sender"] setEnabled:YES]; + } } %end @@ -306,11 +371,18 @@ static void loadPrefs(){ } else { pushshiftRequestTimeoutValue = 10; } + + if ([prefs objectForKey:@"shouldApolloHaveButton"] != nil){ + shouldApolloHaveButton = [[prefs objectForKey:@"shouldApolloHaveButton"] boolValue]; + } else { + shouldApolloHaveButton = NO; + } } else { isApolloEnabled = YES; isTFDeletedOnly = YES; pushshiftRequestTimeoutValue = 10; + shouldApolloHaveButton = NO; } }