Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

164 lines
4.6KB

  1. #import "AlienBlue.h"
  2. #import "assets/TFHelper.h"
  3. #import "assets/MMMarkdown/MMMarkdown.h"
  4. static BOOL isAlienBlueEnabled;
  5. static BOOL isTFDeletedOnly;
  6. static CGFloat pushshiftRequestTimeoutValue;
  7. %group AlienBlue
  8. %hook CommentOptionsDrawerView
  9. -(id) initWithNode:(id) arg1 {
  10. id orig = %orig;
  11. NSString *body;
  12. if ([self isPostHeader]) {
  13. body = [[arg1 post] selftext];
  14. } else {
  15. body = [[(CommentNode *)arg1 comment] body];
  16. }
  17. if ([%c(TFHelper) shouldShowUndeleteButtonWithInfo:body isDeletedOnly:isTFDeletedOnly]){
  18. CGSize refSize = [[self buttons][0] frame].size;
  19. UIButton *undeleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
  20. [undeleteButton setFrame:CGRectMake(0, 0, refSize.width, refSize.height)];
  21. if ([self isPostHeader]){
  22. [undeleteButton addTarget:self action:@selector(didTapPostUndeleteButton:) forControlEvents:UIControlEventTouchUpInside];
  23. } else {
  24. [undeleteButton addTarget:self action:@selector(didTapCommentUndeleteButton:) forControlEvents:UIControlEventTouchUpInside];
  25. }
  26. if ([%c(Resources) isNight]) {
  27. [undeleteButton setImage:[UIImage imageWithContentsOfFile:@"/var/mobile/Library/Application Support/TFDidThatSay/eye160dark.png"] forState:UIControlStateNormal];
  28. } else {
  29. [undeleteButton setImage:[UIImage imageWithContentsOfFile:@"/var/mobile/Library/Application Support/TFDidThatSay/eye160light.png"] forState:UIControlStateNormal];
  30. }
  31. undeleteButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
  32. undeleteButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
  33. [self addButton:undeleteButton];
  34. }
  35. return orig;
  36. }
  37. %new
  38. -(void) didTapCommentUndeleteButton:(id) sender {
  39. [sender setEnabled:NO];
  40. id comment = [[self node] comment];
  41. [%c(TFHelper) getUndeleteDataWithID:[comment ident] isComment:YES timeout:pushshiftRequestTimeoutValue extraData:@{@"sender" : sender} completionTarget:self completionSelector:@selector(completeUndeleteCommentAction:)];
  42. }
  43. %new
  44. -(void) completeUndeleteCommentAction:(NSDictionary *) data{
  45. id comment = [[self node] comment];
  46. NSString *body = data[@"body"];
  47. NSString *bodyHTML = [%c(MMMarkdown) HTMLStringWithMarkdown:body extensions:MMMarkdownExtensionsGitHubFlavored error:nil];
  48. [comment setAuthor:data[@"author"]];
  49. [comment setBody:body];
  50. [comment setBodyHTML:bodyHTML];
  51. [[self delegate] respondToStyleChange];
  52. [data[@"sender"] setEnabled:YES];
  53. }
  54. %new
  55. -(void) didTapPostUndeleteButton:(id) sender {
  56. [sender setEnabled:NO];
  57. id post = [[self node] post];
  58. [%c(TFHelper) getUndeleteDataWithID:[post ident] isComment:NO timeout:pushshiftRequestTimeoutValue extraData:@{@"sender" : sender} completionTarget:self completionSelector:@selector(completeUndeletePostAction:)];
  59. }
  60. %new
  61. -(void) completeUndeletePostAction:(NSDictionary *) data{
  62. id post = [[self node] post];
  63. id postComment = [[self node] comment]; //Don't know why he used a comment to store info about a post, but it exists
  64. NSString *body = data[@"body"];
  65. NSString *bodyHTML = [%c(MMMarkdown) HTMLStringWithMarkdown:body extensions:MMMarkdownExtensionsGitHubFlavored error:nil];
  66. [post setAuthor:data[@"author"]];
  67. [post setSelftext:body];
  68. [postComment setBodyHTML:bodyHTML];
  69. [[self delegate] respondToStyleChange];
  70. [data[@"sender"] setEnabled:YES];
  71. }
  72. %end
  73. %end
  74. static void loadPrefs(){
  75. NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/User/Library/Preferences/com.lint.undelete.prefs.plist"];
  76. if (prefs){
  77. if ([prefs objectForKey:@"isAlienBlueEnabled"] != nil){
  78. isAlienBlueEnabled = [[prefs objectForKey:@"isAlienBlueEnabled"] boolValue];
  79. } else {
  80. isAlienBlueEnabled = YES;
  81. }
  82. if ([prefs objectForKey:@"isTFDeletedOnly"] != nil){
  83. isTFDeletedOnly = [[prefs objectForKey:@"isTFDeletedOnly"] boolValue];
  84. } else {
  85. isTFDeletedOnly = YES;
  86. }
  87. if ([prefs objectForKey:@"requestTimeoutValue"] != nil){
  88. pushshiftRequestTimeoutValue = [[prefs objectForKey:@"requestTimeoutValue"] doubleValue];
  89. } else {
  90. pushshiftRequestTimeoutValue = 10;
  91. }
  92. } else {
  93. isAlienBlueEnabled = YES;
  94. isTFDeletedOnly = YES;
  95. pushshiftRequestTimeoutValue = 10;
  96. }
  97. }
  98. static void prefsChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
  99. loadPrefs();
  100. }
  101. %ctor {
  102. loadPrefs();
  103. NSString* processName = [[NSProcessInfo processInfo] processName];
  104. if ([processName isEqualToString:@"AlienBlue"]){
  105. if (isAlienBlueEnabled){
  106. CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, prefsChanged, CFSTR("com.lint.undelete.prefs.changed"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
  107. %init(AlienBlue);
  108. }
  109. }
  110. }