You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
6.7KB

  1. #import "Beam.h"
  2. #import "assets/TFHelper.h"
  3. static BOOL isEnabled;
  4. static BOOL isBeamEnabled;
  5. static BOOL isTFDeletedOnly;
  6. static CGFloat pushshiftRequestTimeoutValue;
  7. %group Beam
  8. %hook CommentCell
  9. %property(strong, nonatomic) UIButton *undeleteButton;
  10. - (void)layoutSubviews {
  11. %orig;
  12. UIButton *undeleteButton = [self undeleteButton];
  13. if (undeleteButton) {
  14. if ([self isCollapsed]) {
  15. [undeleteButton setHidden:YES];
  16. } else {
  17. [undeleteButton setHidden:NO];
  18. }
  19. } else {
  20. NSString *author = [[self comment] author];
  21. if ([%c(TFHelper) shouldShowUndeleteButtonWithInfo:author isDeletedOnly:isTFDeletedOnly]) {
  22. CGFloat authorTextHeight = [[self authorButton] frame].size.height;
  23. undeleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
  24. [undeleteButton addTarget:self action:@selector(handleUndeleteCommentAction:) forControlEvents:UIControlEventTouchUpInside];
  25. [undeleteButton setImage:[UIImage imageWithContentsOfFile:@"/var/mobile/Library/Application Support/TFDidThatSay/eye160dark.png"] forState:UIControlStateNormal];
  26. undeleteButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
  27. undeleteButton.imageEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
  28. undeleteButton.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width - authorTextHeight - 5, 5, authorTextHeight, authorTextHeight);
  29. [[self commentContentView] addSubview:undeleteButton];
  30. [self setUndeleteButton:undeleteButton];
  31. }
  32. }
  33. }
  34. %new
  35. - (void)handleUndeleteCommentAction:(id)sender {
  36. [sender setEnabled:NO];
  37. [%c(TFHelper) getUndeleteDataWithID:[[self comment] identifier] isComment:YES timeout:pushshiftRequestTimeoutValue extraData:@{@"sender" : sender} completionTarget:self completionSelector:@selector(completeUndeleteCommentAction:)];
  38. }
  39. %new
  40. - (void)completeUndeleteCommentAction:(NSDictionary *)data {
  41. id comment = [self comment];
  42. [comment setAuthor:data[@"author"]];
  43. [comment setContent:data[@"body"]];
  44. [comment setMarkdownString:nil];
  45. [self setCommentDidChange:YES];
  46. [self reloadContents];
  47. [[MSHookIvar<id>(self, "delegate") tableView] reloadData];
  48. [data[@"sender"] setEnabled:YES];
  49. }
  50. %end
  51. %hook PostSelfTextPartCell
  52. - (void)setSelected:(BOOL)arg1 animated:(BOOL)arg2{
  53. %orig;
  54. id postViewController = [self _viewControllerForAncestor];
  55. if ([postViewController isMemberOfClass:objc_getClass("beam.PostDetailEmbeddedViewController")]) {
  56. [postViewController setSelfTextView:self];
  57. [postViewController setPost:[self post]];
  58. }
  59. }
  60. %end
  61. %hook PostMetadataView
  62. - (void)layoutSubviews {
  63. %orig;
  64. id postViewController = MSHookIvar<id>(self, "delegate");
  65. if ([postViewController isMemberOfClass:objc_getClass("beam.PostDetailEmbeddedViewController")]) {
  66. [postViewController setMetadataView:self];
  67. }
  68. }
  69. %end
  70. %hook PostToolbarView
  71. %property(strong, nonatomic) UIButton *undeleteButton;
  72. - (void)layoutSubviews {
  73. %orig;
  74. if (![self undeleteButton] && [[[self post] isSelfText] boolValue] && [MSHookIvar<id>(self, "delegate") isMemberOfClass:objc_getClass("beam.PostDetailEmbeddedViewController")]) {
  75. NSString *author = [[self post] author];
  76. if ([%c(TFHelper) shouldShowUndeleteButtonWithInfo:author isDeletedOnly:isTFDeletedOnly]) {
  77. id moreButton = MSHookIvar<id>(self, "moreButton");
  78. CGFloat buttonHeight = [moreButton frame].size.height;
  79. UIButton *undeleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
  80. [undeleteButton addTarget:MSHookIvar<id>(self, "delegate") action:@selector(handleUndeletePostAction:) forControlEvents:UIControlEventTouchUpInside];
  81. [undeleteButton setImage:[UIImage imageWithContentsOfFile:@"/var/mobile/Library/Application Support/TFDidThatSay/eye160dark.png"] forState:UIControlStateNormal];
  82. undeleteButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
  83. undeleteButton.imageEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
  84. undeleteButton.frame = CGRectMake([moreButton frame].origin.x - buttonHeight, 1, buttonHeight, buttonHeight);
  85. [self addSubview:undeleteButton];
  86. [self setUndeleteButton:undeleteButton];
  87. }
  88. }
  89. }
  90. %end
  91. %hook PostDetailEmbeddedViewController
  92. %property(strong, nonatomic) id selfTextView;
  93. %property(strong, nonatomic) id metadataView;
  94. %property(strong, nonatomic) id post;
  95. %new
  96. - (void)handleUndeletePostAction:(id)sender {
  97. [sender setEnabled:NO];
  98. id post = [self post];
  99. if (post){
  100. [%c(TFHelper) getUndeleteDataWithID:[post identifier] isComment:NO timeout:pushshiftRequestTimeoutValue extraData:@{@"sender" : sender} completionTarget:self completionSelector:@selector(completeUndeletePostAction:)];
  101. }
  102. }
  103. %new
  104. - (void)completeUndeletePostAction:(NSDictionary *)data {
  105. id post = [self post];
  106. [post setAuthor:data[@"author"]];
  107. [post setContent:data[@"body"]];
  108. [post setMarkdownString:nil];
  109. if ([self selfTextView]) {
  110. [[self selfTextView] reloadContents];
  111. }
  112. if ([self metadataView]) {
  113. [[self metadataView] setPost:post];
  114. }
  115. [[self tableView] reloadData];
  116. [data[@"sender"] setEnabled:YES];
  117. }
  118. %end
  119. %end
  120. static void loadPrefs(){
  121. NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/User/Library/Preferences/com.lint.undelete.prefs.plist"];
  122. if (prefs){
  123. isEnabled = [prefs objectForKey:@"isEnabled"] ? [[prefs objectForKey:@"isEnabled"] boolValue] : YES;
  124. isBeamEnabled = [prefs objectForKey:@"isBeamEnabled"] ? [[prefs objectForKey:@"isBeamEnabled"] boolValue] : YES;
  125. isTFDeletedOnly = [prefs objectForKey:@"isTFDeletedOnly"] ? [[prefs objectForKey:@"isTFDeletedOnly"] boolValue] : YES;
  126. pushshiftRequestTimeoutValue = [prefs objectForKey:@"requestTimeoutValue"] ? [[prefs objectForKey:@"requestTimeoutValue"] doubleValue] : 10;
  127. } else {
  128. isEnabled = YES;
  129. isBeamEnabled = YES;
  130. isTFDeletedOnly = YES;
  131. pushshiftRequestTimeoutValue = 10;
  132. }
  133. }
  134. static void prefsChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
  135. loadPrefs();
  136. }
  137. %ctor{
  138. loadPrefs();
  139. NSString* processName = [[NSProcessInfo processInfo] processName];
  140. if ([processName isEqualToString:@"beam"]){
  141. if (isBeamEnabled && isEnabled){
  142. CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, (CFNotificationCallback)prefsChanged, CFSTR("com.lint.undelete.prefs.changed"), NULL, CFNotificationSuspensionBehaviorCoalesce);
  143. %init(Beam, CommentCell = objc_getClass("beam.CommentCell"), PostDetailEmbeddedViewController = objc_getClass("beam.PostDetailEmbeddedViewController"), PostToolbarView = objc_getClass("beam.PostToolbarView"), PostSelfTextPartCell = objc_getClass("beam.PostSelfTextPartCell"), PostMetadataView = objc_getClass("beam.PostMetadataView"));
  144. }
  145. }
  146. }