FLEXGlobalsViewController.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // FLEXGlobalsViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 2014-05-03.
  6. // Copyright (c) 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXGlobalsViewController.h"
  9. #import "FLEXUtility.h"
  10. #import "FLEXRuntimeUtility.h"
  11. #import "FLEXObjcRuntimeViewController.h"
  12. #import "FLEXKeychainViewController.h"
  13. #import "FLEXObjectExplorerViewController.h"
  14. #import "FLEXObjectExplorerFactory.h"
  15. #import "FLEXLiveObjectsController.h"
  16. #import "FLEXFileBrowserController.h"
  17. #import "FLEXCookiesViewController.h"
  18. #import "FLEXGlobalsEntry.h"
  19. #import "FLEXManager+Private.h"
  20. #import "FLEXSystemLogViewController.h"
  21. #import "FLEXNetworkMITMViewController.h"
  22. #import "FLEXAddressExplorerCoordinator.h"
  23. #import "FLEXGlobalsSection.h"
  24. #import "UIBarButtonItem+FLEX.h"
  25. @interface FLEXGlobalsViewController ()
  26. /// Only displayed sections of the table view; empty sections are purged from this array.
  27. @property (nonatomic) NSArray<FLEXGlobalsSection *> *sections;
  28. /// Every section in the table view, regardless of whether or not a section is empty.
  29. @property (nonatomic, readonly) NSArray<FLEXGlobalsSection *> *allSections;
  30. @property (nonatomic, readonly) BOOL manuallyDeselectOnAppear;
  31. @end
  32. @implementation FLEXGlobalsViewController
  33. @dynamic sections, allSections;
  34. #pragma mark - Initialization
  35. + (NSString *)globalsTitleForSection:(FLEXGlobalsSectionKind)section {
  36. switch (section) {
  37. case FLEXGlobalsSectionCustom:
  38. return @"Custom Additions";
  39. case FLEXGlobalsSectionProcessAndEvents:
  40. return @"Process and Events";
  41. case FLEXGlobalsSectionAppShortcuts:
  42. return @"App Shortcuts";
  43. case FLEXGlobalsSectionMisc:
  44. return @"Miscellaneous";
  45. default:
  46. @throw NSInternalInconsistencyException;
  47. }
  48. }
  49. + (FLEXGlobalsEntry *)globalsEntryForRow:(FLEXGlobalsRow)row {
  50. switch (row) {
  51. case FLEXGlobalsRowAppKeychainItems:
  52. return [FLEXKeychainViewController flex_concreteGlobalsEntry:row];
  53. case FLEXGlobalsRowAddressInspector:
  54. return [FLEXAddressExplorerCoordinator flex_concreteGlobalsEntry:row];
  55. case FLEXGlobalsRowBrowseRuntime:
  56. return [FLEXObjcRuntimeViewController flex_concreteGlobalsEntry:row];
  57. case FLEXGlobalsRowLiveObjects:
  58. return [FLEXLiveObjectsController flex_concreteGlobalsEntry:row];
  59. case FLEXGlobalsRowCookies:
  60. return [FLEXCookiesViewController flex_concreteGlobalsEntry:row];
  61. case FLEXGlobalsRowBrowseBundle:
  62. case FLEXGlobalsRowBrowseContainer:
  63. return [FLEXFileBrowserController flex_concreteGlobalsEntry:row];
  64. case FLEXGlobalsRowSystemLog:
  65. return [FLEXSystemLogViewController flex_concreteGlobalsEntry:row];
  66. case FLEXGlobalsRowNetworkHistory:
  67. return [FLEXNetworkMITMViewController flex_concreteGlobalsEntry:row];
  68. case FLEXGlobalsRowKeyWindow:
  69. case FLEXGlobalsRowRootViewController:
  70. case FLEXGlobalsRowProcessInfo:
  71. case FLEXGlobalsRowAppDelegate:
  72. case FLEXGlobalsRowUserDefaults:
  73. case FLEXGlobalsRowMainBundle:
  74. case FLEXGlobalsRowApplication:
  75. case FLEXGlobalsRowMainScreen:
  76. case FLEXGlobalsRowCurrentDevice:
  77. case FLEXGlobalsRowPasteboard:
  78. case FLEXGlobalsRowURLSession:
  79. case FLEXGlobalsRowURLCache:
  80. case FLEXGlobalsRowNotificationCenter:
  81. case FLEXGlobalsRowMenuController:
  82. case FLEXGlobalsRowFileManager:
  83. case FLEXGlobalsRowTimeZone:
  84. case FLEXGlobalsRowLocale:
  85. case FLEXGlobalsRowCalendar:
  86. case FLEXGlobalsRowMainRunLoop:
  87. case FLEXGlobalsRowMainThread:
  88. case FLEXGlobalsRowOperationQueue:
  89. return [FLEXObjectExplorerFactory flex_concreteGlobalsEntry:row];
  90. default:
  91. @throw [NSException
  92. exceptionWithName:NSInternalInconsistencyException
  93. reason:@"Missing globals case in switch" userInfo:nil
  94. ];
  95. }
  96. }
  97. + (NSArray<FLEXGlobalsSection *> *)defaultGlobalSections {
  98. static NSMutableArray<FLEXGlobalsSection *> *sections = nil;
  99. static dispatch_once_t onceToken;
  100. dispatch_once(&onceToken, ^{
  101. NSDictionary<NSNumber *, NSArray<FLEXGlobalsEntry *> *> *rowsBySection = @{
  102. @(FLEXGlobalsSectionProcessAndEvents) : @[
  103. [self globalsEntryForRow:FLEXGlobalsRowNetworkHistory],
  104. [self globalsEntryForRow:FLEXGlobalsRowSystemLog],
  105. [self globalsEntryForRow:FLEXGlobalsRowProcessInfo],
  106. [self globalsEntryForRow:FLEXGlobalsRowLiveObjects],
  107. [self globalsEntryForRow:FLEXGlobalsRowAddressInspector],
  108. [self globalsEntryForRow:FLEXGlobalsRowBrowseRuntime],
  109. ],
  110. @(FLEXGlobalsSectionAppShortcuts) : @[
  111. [self globalsEntryForRow:FLEXGlobalsRowBrowseBundle],
  112. [self globalsEntryForRow:FLEXGlobalsRowBrowseContainer],
  113. [self globalsEntryForRow:FLEXGlobalsRowMainBundle],
  114. [self globalsEntryForRow:FLEXGlobalsRowUserDefaults],
  115. [self globalsEntryForRow:FLEXGlobalsRowAppKeychainItems],
  116. [self globalsEntryForRow:FLEXGlobalsRowApplication],
  117. [self globalsEntryForRow:FLEXGlobalsRowAppDelegate],
  118. [self globalsEntryForRow:FLEXGlobalsRowKeyWindow],
  119. [self globalsEntryForRow:FLEXGlobalsRowRootViewController],
  120. [self globalsEntryForRow:FLEXGlobalsRowCookies],
  121. ],
  122. @(FLEXGlobalsSectionMisc) : @[
  123. #if !TARGET_OS_TV
  124. [self globalsEntryForRow:FLEXGlobalsRowPasteboard],
  125. #endif
  126. [self globalsEntryForRow:FLEXGlobalsRowMainScreen],
  127. [self globalsEntryForRow:FLEXGlobalsRowCurrentDevice],
  128. [self globalsEntryForRow:FLEXGlobalsRowURLSession],
  129. [self globalsEntryForRow:FLEXGlobalsRowURLCache],
  130. [self globalsEntryForRow:FLEXGlobalsRowNotificationCenter],
  131. #if !TARGET_OS_TV
  132. [self globalsEntryForRow:FLEXGlobalsRowMenuController],
  133. #endif
  134. [self globalsEntryForRow:FLEXGlobalsRowFileManager],
  135. [self globalsEntryForRow:FLEXGlobalsRowTimeZone],
  136. [self globalsEntryForRow:FLEXGlobalsRowLocale],
  137. [self globalsEntryForRow:FLEXGlobalsRowCalendar],
  138. [self globalsEntryForRow:FLEXGlobalsRowMainRunLoop],
  139. [self globalsEntryForRow:FLEXGlobalsRowMainThread],
  140. [self globalsEntryForRow:FLEXGlobalsRowOperationQueue],
  141. ]
  142. };
  143. sections = [NSMutableArray array];
  144. for (FLEXGlobalsSectionKind i = FLEXGlobalsSectionCustom + 1; i < FLEXGlobalsSectionCount; ++i) {
  145. NSString *title = [self globalsTitleForSection:i];
  146. [sections addObject:[FLEXGlobalsSection title:title rows:rowsBySection[@(i)]]];
  147. }
  148. });
  149. return sections;
  150. }
  151. #pragma mark - Overrides
  152. - (void)viewDidLoad {
  153. [super viewDidLoad];
  154. self.title = @"💪 FLEX";
  155. self.showsSearchBar = YES;
  156. self.searchBarDebounceInterval = kFLEXDebounceInstant;
  157. #if !TARGET_OS_TV
  158. self.navigationItem.backBarButtonItem = [UIBarButtonItem flex_backItemWithTitle:@"Back"];
  159. #endif
  160. _manuallyDeselectOnAppear = NSProcessInfo.processInfo.operatingSystemVersion.majorVersion < 10;
  161. }
  162. - (void)viewWillAppear:(BOOL)animated {
  163. [super viewWillAppear:animated];
  164. [self disableToolbar];
  165. if (self.manuallyDeselectOnAppear) {
  166. [self.tableView deselectRowAtIndexPath:self.tableView.indexPathForSelectedRow animated:YES];
  167. }
  168. }
  169. - (NSArray<FLEXGlobalsSection *> *)makeSections {
  170. NSMutableArray<FLEXGlobalsSection *> *sections = [NSMutableArray array];
  171. // Do we have custom sections to add?
  172. if (FLEXManager.sharedManager.userGlobalEntries.count) {
  173. NSString *title = [[self class] globalsTitleForSection:FLEXGlobalsSectionCustom];
  174. FLEXGlobalsSection *custom = [FLEXGlobalsSection
  175. title:title
  176. rows:FLEXManager.sharedManager.userGlobalEntries
  177. ];
  178. [sections addObject:custom];
  179. }
  180. [sections addObjectsFromArray:[self.class defaultGlobalSections]];
  181. return sections;
  182. }
  183. @end