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.

4 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. #import "Slide.h"
  2. #import "assets/TFHelper.h"
  3. #import "assets/MMMarkdown/MMMarkdown.h"
  4. static BOOL isEnabled;
  5. static BOOL isSlideEnabled;
  6. static BOOL isTFDeletedOnly;
  7. static CGFloat pushshiftRequestTimeoutValue;
  8. %group Slide
  9. BOOL shouldSlideOverridePostInfo = NO;
  10. NSString *slidePostOverrideAuthor;
  11. NSString *slidePostOverrideBodyHtml;
  12. @implementation FontGenerator
  13. + (UIFont *)fontOfSize:(CGFloat)size submission:(BOOL)isSubmission willOffset:(BOOL)willOffset {
  14. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  15. NSString *fontName;
  16. CGFloat fontSize = size;
  17. if (willOffset) {
  18. if (isSubmission){
  19. fontSize += ([userDefaults objectForKey:@"POST_FONT_SIZE"] == nil) ? 0 : [userDefaults integerForKey:@"POST_FONT_SIZE"];
  20. } else {
  21. fontSize += ([userDefaults objectForKey:@"COMMENT_FONT_SIZE"] == nil) ? -2 : [userDefaults integerForKey:@"COMMENT_FONT_SIZE"];
  22. }
  23. }
  24. if ([userDefaults stringForKey:(isSubmission ? @"postfont" : @"commentfont")] == nil) {
  25. fontName = isSubmission ? @"AvenirNext-DemiBold" : @"AvenirNext-Medium";
  26. } else {
  27. fontName = [userDefaults stringForKey:(isSubmission ? @"postfont" : @"commentfont")];
  28. }
  29. UIFont *font = [UIFont fontWithName:fontName size:fontSize];
  30. if (!font) {
  31. font = [UIFont systemFontOfSize:fontSize];
  32. }
  33. return font;
  34. }
  35. + (UIFont *)boldFontOfSize:(CGFloat)size submission:(BOOL)isSubmission willOffset:(BOOL)willOffset {
  36. UIFont *font = [self fontOfSize:size submission:isSubmission willOffset:willOffset];
  37. if ([font.fontName isEqualToString:[UIFont systemFontOfSize:10].fontName]){
  38. return [UIFont boldSystemFontOfSize:font.pointSize];
  39. } else {
  40. UIFontDescriptor *desc = [font.fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold];
  41. if (desc == nil){
  42. return font;
  43. } else {
  44. return [UIFont fontWithDescriptor:desc size: 0];
  45. }
  46. }
  47. }
  48. + (UIFont *)italicFontOfSize:(CGFloat)size submission:(BOOL)isSubmission willOffset:(BOOL)willOffset {
  49. UIFont *font = [self fontOfSize:size submission:isSubmission willOffset:willOffset];
  50. if ([font.fontName isEqualToString:[UIFont systemFontOfSize:10].fontName]) {
  51. return [UIFont italicSystemFontOfSize:font.pointSize];
  52. } else {
  53. UIFontDescriptor *desc = [font.fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitItalic];
  54. if (desc == nil) {
  55. return font;
  56. } else {
  57. return [UIFont fontWithDescriptor:desc size: 0];
  58. }
  59. }
  60. }
  61. @end
  62. @implementation ColorUtil
  63. + (UIColor *)accentColorForSub:(NSString *)subreddit {
  64. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  65. NSData *colorData = [userDefaults dataForKey:[NSString stringWithFormat:@"accent+%@", subreddit]];
  66. UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];
  67. if (color) {
  68. return color;
  69. } else {
  70. UIColor *baseAccentColor = [NSKeyedUnarchiver unarchiveObjectWithData:[userDefaults dataForKey:@"accentcolor"]];
  71. if (baseAccentColor) {
  72. return baseAccentColor;
  73. } else {
  74. return [UIColor colorWithRed:0.161 green:0.475 blue:1.0 alpha:1.0];
  75. }
  76. }
  77. }
  78. + (UIColor *)fontColorForTheme:(NSString *)theme {
  79. UIColor *fontColor;
  80. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  81. //if only switch blocks worked with strings...
  82. if ([theme isEqualToString:@"light"]) {
  83. fontColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.87];
  84. } else if ([theme isEqualToString:@"dark"]) {
  85. fontColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.87];
  86. } else if ([theme isEqualToString:@"black"]) {
  87. fontColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.87];
  88. } else if ([theme isEqualToString:@"blue"]) {
  89. fontColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.87];
  90. } else if ([theme isEqualToString:@"sepia"]) {
  91. fontColor = [UIColor colorWithRed:0.243 green:.239 blue:.212 alpha:0.87];
  92. } else if ([theme isEqualToString:@"red"]) {
  93. fontColor = [UIColor colorWithRed:1.0 green:0.969 blue:0.929 alpha:0.87];
  94. } else if ([theme isEqualToString:@"deep"]) {
  95. fontColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.87];
  96. } else if ([theme isEqualToString:@"mint"]) {
  97. fontColor = [UIColor colorWithRed:0.035 green:0.212 blue:0.059 alpha:0.87];
  98. } else if ([theme isEqualToString:@"cream"]) {
  99. fontColor = [UIColor colorWithRed:0.267 green:0.255 blue:0.224 alpha:0.87];
  100. } else if ([theme isEqualToString:@"acontrast"]) {
  101. fontColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.87];
  102. } else if ([theme isEqualToString:@"pink"]) {
  103. fontColor = [UIColor colorWithRed:0.149 green:0.157 blue:0.267 alpha:0.87];
  104. } else if ([theme isEqualToString:@"solarize"]) {
  105. fontColor = [UIColor colorWithRed:0.514 green:0.580 blue:0.588 alpha:0.87];
  106. } else if (!theme) {
  107. fontColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.87];
  108. } else {
  109. NSString *customThemeString = [[userDefaults stringForKey:[NSString stringWithFormat:@"Theme+%@", theme]] stringByRemovingPercentEncoding];
  110. if (customThemeString) {
  111. NSString *customFontColorHex = [customThemeString componentsSeparatedByString:@"#"][4];
  112. fontColor = [UIColor colorWithHex:customFontColorHex];
  113. } else {
  114. fontColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.87];
  115. }
  116. }
  117. return fontColor;
  118. }
  119. + (UIColor *)backgroundColorForTheme:(NSString *)theme {
  120. UIColor *backgroundColor;
  121. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  122. if ([theme isEqualToString:@"light"]) {
  123. backgroundColor = [UIColor colorWithRed:0.352 green:0.352 blue:0.352 alpha:1.0];
  124. } else if ([theme isEqualToString:@"dark"]) {
  125. backgroundColor = [UIColor colorWithRed:0.051 green:0.051 blue:0.051 alpha:1.0];
  126. } else if ([theme isEqualToString:@"black"]) {
  127. backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1.0];
  128. } else if ([theme isEqualToString:@"blue"]) {
  129. backgroundColor = [UIColor colorWithRed:0.071 green:0.094 blue:0.106 alpha:1.0];
  130. } else if ([theme isEqualToString:@"sepia"]) {
  131. backgroundColor = [UIColor colorWithRed:0.310 green:0.302 blue:0.267 alpha:1.0];
  132. } else if ([theme isEqualToString:@"red"]) {
  133. backgroundColor = [UIColor colorWithRed:0.075 green:0.055 blue:0.051 alpha:1.0];
  134. } else if ([theme isEqualToString:@"deep"]) {
  135. backgroundColor = [UIColor colorWithRed:0.035 green:0.035 blue:0.043 alpha:1.0];
  136. } else if ([theme isEqualToString:@"mint"]) {
  137. backgroundColor = [UIColor colorWithRed:0.364 green:0.376 blue:0.357 alpha:1.0];
  138. } else if ([theme isEqualToString:@"cream"]) {
  139. backgroundColor = [UIColor colorWithRed:0.322 green:0.314 blue:0.286 alpha:1.0];
  140. } else if ([theme isEqualToString:@"acontrast"]) {
  141. backgroundColor = [UIColor colorWithRed:0.027 green:0.027 blue:0.24 alpha:1.0];
  142. } else if ([theme isEqualToString:@"pink"]) {
  143. backgroundColor = [UIColor colorWithRed:1.0 green:0.376 blue:0.357 alpha:1.0];
  144. } else if ([theme isEqualToString:@"solarize"]) {
  145. backgroundColor = [UIColor colorWithRed:0.040 green:0.067 blue:0.082 alpha:1.0];
  146. } else if (!theme) {
  147. backgroundColor = [UIColor colorWithRed:0.352 green:0.352 blue:0.352 alpha:1.0];
  148. } else {
  149. NSString *customThemeString = [[userDefaults stringForKey:[NSString stringWithFormat:@"Theme+%@", theme]] stringByRemovingPercentEncoding];
  150. if (customThemeString) {
  151. NSString *customFontColorHex = [customThemeString componentsSeparatedByString:@"#"][3];
  152. backgroundColor = [UIColor colorWithHex:customFontColorHex];
  153. } else {
  154. backgroundColor = [UIColor colorWithRed:0.352 green:0.352 blue:0.352 alpha:1.0];
  155. }
  156. }
  157. return backgroundColor;
  158. }
  159. @end
  160. static UIButton *createUndeleteButton(){
  161. UIButton *undeleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
  162. UIImage *undeleteImage = [UIImage imageWithContentsOfFile:@"/var/mobile/Library/Application Support/TFDidThatSay/eye160white.png"];
  163. UIGraphicsBeginImageContextWithOptions(CGSizeMake(20, 20), NO, 0);
  164. [undeleteImage drawInRect:CGRectMake(0, 0, 20, 20)];
  165. undeleteImage = UIGraphicsGetImageFromCurrentImageContext();
  166. UIGraphicsEndImageContext();
  167. UIGraphicsBeginImageContextWithOptions(CGSizeMake(35, 35), NO, 0);
  168. CGContextRef context = UIGraphicsGetCurrentContext();
  169. UIGraphicsPushContext(context);
  170. [undeleteImage drawAtPoint:CGPointMake(7.5, 7.5)];
  171. UIGraphicsPopContext();
  172. undeleteImage = UIGraphicsGetImageFromCurrentImageContext();
  173. UIGraphicsEndImageContext();
  174. [undeleteButton setImage:undeleteImage forState:UIControlStateNormal];
  175. undeleteButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
  176. return undeleteButton;
  177. }
  178. %hook UIColor
  179. %new
  180. + (UIColor *)colorWithHex:(NSString *)arg1 {
  181. if (!arg1){
  182. NSString *firstChar = [arg1 substringToIndex:1];
  183. if ([firstChar isEqualToString:@"#"]){
  184. arg1 = [arg1 substringWithRange:NSMakeRange(1, [arg1 length]-1)];
  185. }
  186. unsigned rgbValue = 0;
  187. NSScanner *scanner = [NSScanner scannerWithString:arg1];
  188. [scanner scanHexInt:&rgbValue];
  189. return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:0.87];
  190. } else {
  191. return nil;
  192. }
  193. }
  194. %new
  195. - (NSString *)hexString {
  196. const CGFloat *components = CGColorGetComponents(self.CGColor);
  197. CGFloat r = components[0];
  198. CGFloat g = components[1];
  199. CGFloat b = components[2];
  200. return [NSString stringWithFormat:@"#%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255)];
  201. }
  202. %end
  203. %hook CommentDepthCell
  204. - (void)doShortClick {
  205. %orig;
  206. [self addUndeleteButtonToMenu];
  207. }
  208. - (void)doLongClick {
  209. %orig;
  210. [self addUndeleteButtonToMenu];
  211. }
  212. %new
  213. - (void)addUndeleteButtonToMenu {
  214. NSString *commentBody = [MSHookIvar<id>(self, "comment") body];
  215. if ([%c(TFHelper) shouldShowUndeleteButtonWithInfo:commentBody isDeletedOnly:isTFDeletedOnly]) {
  216. id controller = MSHookIvar<id>(self, "parent");
  217. if (MSHookIvar<id>(controller, "menuCell")) {
  218. UIStackView *menu = MSHookIvar<UIStackView *>(self, "menu");
  219. if (![[[[menu arrangedSubviews] lastObject] actionsForTarget:self forControlEvent:UIControlEventTouchUpInside] containsObject:@"handleUndeleteComment:"]) {
  220. UIButton *undeleteButton = createUndeleteButton();
  221. [undeleteButton addTarget:self action:@selector(handleUndeleteComment:) forControlEvents:UIControlEventTouchUpInside];
  222. [menu addArrangedSubview:undeleteButton];
  223. }
  224. }
  225. }
  226. }
  227. %new
  228. - (void)handleUndeleteComment:(id)sender {
  229. [sender setEnabled:NO];
  230. id comment = MSHookIvar<id>(self, "comment");
  231. [%c(TFHelper) getUndeleteDataWithID:[[comment id] componentsSeparatedByString:@"_"][1] isComment:YES timeout:pushshiftRequestTimeoutValue extraData:@{@"sender" : sender} completionTarget:self completionSelector:@selector(completeUndeleteCommentAction:)];
  232. }
  233. %new
  234. - (void)completeUndeleteCommentAction:(NSDictionary *)data {
  235. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  236. id textStackDisplayView = MSHookIvar<id>(self, "commentBody");
  237. id comment = MSHookIvar<id>(self, "comment");
  238. NSString *author = data[@"author"];
  239. NSString *body = data[@"body"];
  240. //Attributed string generation rewrote from Slide_for_Reddit.TextDisplayStackView.createAttributedChunk(...)
  241. UIFont *font = [%c(FontGenerator) fontOfSize:MSHookIvar<CGFloat>(textStackDisplayView, "fontSize") submission:NO willOffset:YES];
  242. NSString *themeName = [userDefaults stringForKey:@"theme"];
  243. UIColor *fontColor = [%c(ColorUtil) fontColorForTheme:themeName];
  244. UIColor *accentColor = [%c(ColorUtil) accentColorForSub:[comment subreddit]];
  245. NSString *html = [%c(MMMarkdown) HTMLStringWithMarkdown:body extensions:MMMarkdownExtensionsGitHubFlavored error:nil];
  246. html = [[html stringByReplacingOccurrencesOfString:@"<sup>" withString:@"<font size=\"1\">"] stringByReplacingOccurrencesOfString:@"</sup>" withString:@"</font>"];
  247. html = [[html stringByReplacingOccurrencesOfString:@"<del>" withString:@"<font color=\"green\">"] stringByReplacingOccurrencesOfString:@"</del>" withString:@"</font>"];
  248. html = [[html stringByReplacingOccurrencesOfString:@"<code>" withString:@"<font color=\"blue\">"] stringByReplacingOccurrencesOfString:@"</code>" withString:@"</font>"];
  249. html = [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  250. DTHTMLAttributedStringBuilder *dthtmlBuilder = [[%c(DTHTMLAttributedStringBuilder) alloc] initWithHTML:[html dataUsingEncoding:NSUTF8StringEncoding] options:@{@"DTUseiOS6Attributes": @YES, @"DTDefaultTextColor": fontColor, @"DTDefaultFontSize": @([font pointSize])} documentAttributes:nil];
  251. NSMutableAttributedString *htmlAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:[dthtmlBuilder generatedAttributedString]];
  252. NSRange htmlStringRange = NSMakeRange(0, [htmlAttributedString length]);
  253. [[htmlAttributedString mutableString] replaceOccurrencesOfString:@"\t•\t" withString:@" • " options:0 range: htmlStringRange];
  254. [[htmlAttributedString mutableString] replaceOccurrencesOfString:@"\t◦\t" withString:@"  ◦ " options:0 range: htmlStringRange];
  255. [[htmlAttributedString mutableString] replaceOccurrencesOfString:@"\t▪\t" withString:@" ▪ " options:0 range: htmlStringRange];
  256. [htmlAttributedString removeAttribute:@"CTForegroundColorFromContext" range:htmlStringRange];
  257. [htmlAttributedString enumerateAttributesInRange:htmlStringRange options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(NSDictionary *attributes, NSRange range, BOOL *stop) {
  258. for (NSString *key in attributes) {
  259. if ([(UIColor *) attributes[key] isKindOfClass:[UIColor class]]) {
  260. UIColor *attrColor = (UIColor *) attributes[key];
  261. if ([[attrColor hexString] isEqualToString:@"#0000FF"]) {
  262. UIFont *tempFont = [UIFont fontWithName:@"Courier" size:font.pointSize];
  263. [htmlAttributedString setAttributes:@{NSForegroundColorAttributeName: accentColor, NSBackgroundColorAttributeName: [%c(ColorUtil) backgroundColorForTheme:themeName], NSFontAttributeName: (tempFont ? tempFont : font)} range:range];
  264. } else if ([[attrColor hexString] isEqualToString:@"#008000"]) {
  265. [htmlAttributedString setAttributes:@{NSForegroundColorAttributeName: fontColor, NSFontAttributeName:font} range:range];
  266. }
  267. } else if ([(NSURL *) attributes[key] isKindOfClass:[NSURL class]]) {
  268. NSURL *attrUrl = (NSURL *)attributes[key];
  269. if (([userDefaults objectForKey:@"ENLARGE_LINKS"] == nil) ? YES : [userDefaults boolForKey:@"ENLARGE_LINKS"]) {
  270. [htmlAttributedString addAttribute:NSFontAttributeName value:[%c(FontGenerator) boldFontOfSize:18 submission:NO willOffset:YES] range:range];
  271. }
  272. [htmlAttributedString addAttribute:NSForegroundColorAttributeName value:accentColor range:range];
  273. [htmlAttributedString addAttribute:NSUnderlineColorAttributeName value:[UIColor clearColor] range:range];
  274. //skipping showLinkContentType b/c not necessary and spoilers b/c MMMarkdown doesn't support them
  275. [htmlAttributedString yy_setTextHighlightRange:range color: accentColor backgroundColor:nil userInfo:@{@"url": attrUrl}];
  276. break;
  277. }
  278. }
  279. }];
  280. [htmlAttributedString beginEditing];
  281. [htmlAttributedString enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, [htmlAttributedString length]) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
  282. UIFont *attrFont = (UIFont *)value;
  283. BOOL isBold = (attrFont.fontDescriptor.symbolicTraits & UIFontDescriptorTraitBold) != 0;
  284. BOOL isItalic = (attrFont.fontDescriptor.symbolicTraits & UIFontDescriptorTraitItalic) != 0;
  285. UIFont *newFont = font;
  286. if (isBold){
  287. newFont = [%c(FontGenerator) boldFontOfSize:attrFont.pointSize submission:NO willOffset:NO];
  288. } else if (isItalic){
  289. newFont = [%c(FontGenerator) italicFontOfSize:attrFont.pointSize submission:NO willOffset:NO];
  290. }
  291. [htmlAttributedString removeAttribute:NSFontAttributeName range:range];
  292. [htmlAttributedString addAttribute:NSFontAttributeName value:newFont range:range];
  293. }];
  294. [htmlAttributedString endEditing];
  295. NSMutableAttributedString *newCommentText = [MSHookIvar<NSMutableAttributedString *>(self, "cellContent") initWithAttributedString:htmlAttributedString];
  296. NSAttributedString *tempAttributedString = [[NSAttributedString alloc] initWithString:@""];
  297. [newCommentText appendAttributedString:tempAttributedString]; //to keep the compiler happy
  298. [comment setAuthor:author];
  299. [comment setBody:body];
  300. id controller = MSHookIvar<id>(self, "parent");
  301. [self showMenu:nil];
  302. [MSHookIvar<id>(controller, "tableView") reloadData];
  303. [data[@"sender"] setEnabled:YES];
  304. }
  305. %end
  306. %hook RSubmission
  307. - (id)author {
  308. if (shouldSlideOverridePostInfo){
  309. return slidePostOverrideAuthor;
  310. } else {
  311. return %orig;
  312. }
  313. }
  314. - (id)htmlBody {
  315. if (shouldSlideOverridePostInfo){
  316. return slidePostOverrideBodyHtml;
  317. }
  318. else {
  319. return %orig;
  320. }
  321. }
  322. %end
  323. %hook CommentViewController
  324. %property(strong, nonatomic) UIButton *undeleteButton;
  325. - (void)viewDidLoad {
  326. %orig;
  327. shouldSlideOverridePostInfo = NO;
  328. slidePostOverrideAuthor = nil;
  329. slidePostOverrideBodyHtml = nil;
  330. UIButton *undeleteButton = createUndeleteButton();
  331. [undeleteButton setImage:[[undeleteButton currentImage] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
  332. [undeleteButton addTarget:self action:@selector(handleUndeletePost:) forControlEvents:UIControlEventTouchUpInside];
  333. [self setUndeleteButton:undeleteButton];
  334. }
  335. - (void)viewDidLayoutSubviews {
  336. %orig;
  337. if ([self undeleteButton]) {
  338. if ([self shouldAddUndeleteButtonToToolbar]) {
  339. [self addUndeleteButtonToToolbar];
  340. }
  341. }
  342. }
  343. - (void)loadAll:(id)arg1 {
  344. %orig;
  345. if ([self undeleteButton]){
  346. if ([self shouldAddUndeleteButtonToToolbar]){
  347. [self addUndeleteButtonToToolbar];
  348. }
  349. }
  350. }
  351. %new
  352. - (BOOL)shouldAddUndeleteButtonToToolbar {
  353. id post = MSHookIvar<id>(self, "submission");
  354. if ([post isSelf]) {
  355. NSString *postBody = [post body];
  356. if ([%c(TFHelper) shouldShowUndeleteButtonWithInfo:postBody isDeletedOnly:isTFDeletedOnly]) {
  357. return YES;
  358. }
  359. }
  360. return NO;
  361. }
  362. %new
  363. - (void)addUndeleteButtonToToolbar {
  364. UIToolbar *toolbar = [[self navigationController] toolbar];
  365. NSMutableArray *toolbarItems = [[toolbar items] mutableCopy];
  366. UIView *firstView = [toolbarItems[0] customView];
  367. if (firstView) {
  368. UIColor *tintColor = [toolbar tintColor];
  369. UIButton *undeleteButton = [self undeleteButton];
  370. [undeleteButton setTintColor:tintColor];
  371. if ([firstView isMemberOfClass:[UIView class]]) {
  372. if (![undeleteButton isDescendantOfView:firstView]){
  373. [firstView addSubview:undeleteButton];
  374. [undeleteButton setFrame:firstView.bounds];
  375. }
  376. } else if ([firstView isMemberOfClass:[UIButton class]] && undeleteButton != firstView) {
  377. UIBarButtonItem *undeleteItem = [[UIBarButtonItem alloc] initWithCustomView:undeleteButton];
  378. [toolbarItems insertObject:toolbarItems[1] atIndex:0];
  379. [toolbarItems insertObject:undeleteItem atIndex:0];
  380. [toolbar setItems:toolbarItems animated:NO];
  381. }
  382. }
  383. }
  384. %new
  385. - (void)handleUndeletePost:(id)sender {
  386. shouldSlideOverridePostInfo = YES;
  387. [sender setEnabled:NO];
  388. [%c(TFHelper) getUndeleteDataWithID:[MSHookIvar<id>(self, "submission") name] isComment:NO timeout:pushshiftRequestTimeoutValue extraData:@{@"sender" : sender} completionTarget:self completionSelector:@selector(completeUndeletePostAction:)];
  389. }
  390. %new
  391. - (void)completeUndeletePostAction:(NSDictionary *)data {
  392. slidePostOverrideAuthor = data[@"author"];
  393. slidePostOverrideBodyHtml = [%c(MMMarkdown) HTMLStringWithMarkdown:data[@"body"] extensions:MMMarkdownExtensionsGitHubFlavored error:nil];
  394. [self refresh:nil];
  395. [data[@"sender"] setEnabled:YES];
  396. }
  397. %end
  398. %end
  399. static void loadPrefs(){
  400. NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/User/Library/Preferences/com.lint.undelete.prefs.plist"];
  401. if (prefs){
  402. isEnabled = [prefs objectForKey:@"isEnabled"] ? [[prefs objectForKey:@"isEnabled"] boolValue] : YES;
  403. isSlideEnabled = [prefs objectForKey:@"isSlideEnabled"] ? [[prefs objectForKey:@"isSlideEnabled"] boolValue] : YES;
  404. isTFDeletedOnly = [prefs objectForKey:@"isTFDeletedOnly"] ? [[prefs objectForKey:@"isTFDeletedOnly"] boolValue] : YES;
  405. pushshiftRequestTimeoutValue = [prefs objectForKey:@"requestTimeoutValue"] ? [[prefs objectForKey:@"requestTimeoutValue"] doubleValue] : 10;
  406. } else {
  407. isEnabled = YES;
  408. isSlideEnabled = YES;
  409. isTFDeletedOnly = YES;
  410. pushshiftRequestTimeoutValue = 10;
  411. }
  412. }
  413. static void prefsChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
  414. loadPrefs();
  415. }
  416. %ctor {
  417. loadPrefs();
  418. NSString* processName = [[NSProcessInfo processInfo] processName];
  419. if ([processName isEqualToString:@"Slide for Reddit"]){
  420. if (isSlideEnabled && isEnabled){
  421. CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, (CFNotificationCallback)prefsChanged, CFSTR("com.lint.undelete.prefs.changed"), NULL, CFNotificationSuspensionBehaviorCoalesce);
  422. %init(Slide, CommentDepthCell = objc_getClass("Slide_for_Reddit.CommentDepthCell"), RSubmission = objc_getClass("Slide_for_Reddit.RSubmission"), CommentViewController = objc_getClass("Slide_for_Reddit.CommentViewController"));
  423. }
  424. }
  425. }