Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

577 lines
21KB

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