FLEXGlobalsTableViewController.m 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //
  2. // FLEXGlobalsTableViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 2014-05-03.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXGlobalsTableViewController.h"
  9. #import "FLEXUtility.h"
  10. #import "FLEXRuntimeUtility.h"
  11. #import "FLEXObjcRuntimeViewController.h"
  12. #import "FLEXKeychainTableViewController.h"
  13. #import "FLEXObjectExplorerViewController.h"
  14. #import "FLEXObjectExplorerFactory.h"
  15. #import "FLEXLiveObjectsTableViewController.h"
  16. #import "FLEXFileBrowserTableViewController.h"
  17. #import "FLEXCookiesTableViewController.h"
  18. #import "FLEXGlobalsEntry.h"
  19. #import "FLEXManager+Private.h"
  20. #import "FLEXSystemLogTableViewController.h"
  21. #import "FLEXNetworkHistoryTableViewController.h"
  22. #import "FLEXAddressExplorerCoordinator.h"
  23. #import "FLEXTableViewSection.h"
  24. static __weak UIWindow *s_applicationWindow = nil;
  25. @interface FLEXGlobalsTableViewController ()
  26. @property (nonatomic, readonly) NSArray<FLEXTableViewSection<FLEXGlobalsEntry *> *> *sections;
  27. @property (nonatomic, copy) NSArray<FLEXTableViewSection<FLEXGlobalsEntry *> *> *filteredSections;
  28. @end
  29. @implementation FLEXGlobalsTableViewController
  30. + (NSString *)globalsTitleForSection:(FLEXGlobalsSection)section
  31. {
  32. switch (section) {
  33. case FLEXGlobalsSectionProcessAndEvents:
  34. return @"Process and Events";
  35. case FLEXGlobalsSectionAppShortcuts:
  36. return @"App Shortcuts";
  37. case FLEXGlobalsSectionMisc:
  38. return @"Miscellaneous";
  39. case FLEXGlobalsSectionCustom:
  40. return @"Custom Additions";
  41. default:
  42. @throw NSInternalInconsistencyException;
  43. }
  44. }
  45. + (FLEXGlobalsEntry *)globalsEntryForRow:(FLEXGlobalsRow)row
  46. {
  47. switch (row) {
  48. case FLEXGlobalsRowAppKeychainItems:
  49. return [FLEXKeychainTableViewController flex_concreteGlobalsEntry:row];
  50. case FLEXGlobalsRowAddressInspector:
  51. return [FLEXAddressExplorerCoordinator flex_concreteGlobalsEntry:row];
  52. case FLEXGlobalsRowBrowseRuntime:
  53. return [FLEXObjcRuntimeViewController flex_concreteGlobalsEntry:row];
  54. case FLEXGlobalsRowLiveObjects:
  55. return [FLEXLiveObjectsTableViewController flex_concreteGlobalsEntry:row];
  56. case FLEXGlobalsRowCookies:
  57. return [FLEXCookiesTableViewController flex_concreteGlobalsEntry:row];
  58. case FLEXGlobalsRowBrowseBundle:
  59. case FLEXGlobalsRowBrowseContainer:
  60. return [FLEXFileBrowserTableViewController flex_concreteGlobalsEntry:row];
  61. case FLEXGlobalsRowSystemLog:
  62. return [FLEXSystemLogTableViewController flex_concreteGlobalsEntry:row];
  63. case FLEXGlobalsRowNetworkHistory:
  64. return [FLEXNetworkHistoryTableViewController flex_concreteGlobalsEntry:row];
  65. case FLEXGlobalsRowKeyWindow:
  66. return [FLEXGlobalsEntry
  67. entryWithNameFuture:^NSString *{
  68. return @"🔑 -[UIApplication keyWindow]";
  69. } viewControllerFuture:^UIViewController *{
  70. return [FLEXObjectExplorerFactory explorerViewControllerForObject:s_applicationWindow];
  71. }
  72. ];
  73. case FLEXGlobalsRowRootViewController:
  74. case FLEXGlobalsRowProcessInfo:
  75. case FLEXGlobalsRowAppDelegate:
  76. case FLEXGlobalsRowUserDefaults:
  77. case FLEXGlobalsRowMainBundle:
  78. case FLEXGlobalsRowApplication:
  79. case FLEXGlobalsRowMainScreen:
  80. case FLEXGlobalsRowCurrentDevice:
  81. case FLEXGlobalsRowPasteboard:
  82. return [FLEXObjectExplorerFactory flex_concreteGlobalsEntry:row];
  83. default:
  84. @throw [NSException
  85. exceptionWithName:NSInternalInconsistencyException
  86. reason:@"Missing globals case in switch" userInfo:nil
  87. ];
  88. }
  89. }
  90. + (NSArray<FLEXTableViewSection<FLEXGlobalsEntry *> *> *)defaultGlobalSections
  91. {
  92. static NSArray<FLEXTableViewSection<FLEXGlobalsEntry *> *> *sections = nil;
  93. static dispatch_once_t onceToken;
  94. dispatch_once(&onceToken, ^{
  95. NSArray *rows = @[
  96. @[
  97. [self globalsEntryForRow:FLEXGlobalsRowNetworkHistory],
  98. [self globalsEntryForRow:FLEXGlobalsRowSystemLog],
  99. [self globalsEntryForRow:FLEXGlobalsRowProcessInfo],
  100. [self globalsEntryForRow:FLEXGlobalsRowLiveObjects],
  101. [self globalsEntryForRow:FLEXGlobalsRowAddressInspector],
  102. [self globalsEntryForRow:FLEXGlobalsRowBrowseRuntime],
  103. ],
  104. @[ // FLEXGlobalsSectionAppShortcuts
  105. [self globalsEntryForRow:FLEXGlobalsRowBrowseBundle],
  106. [self globalsEntryForRow:FLEXGlobalsRowBrowseContainer],
  107. [self globalsEntryForRow:FLEXGlobalsRowMainBundle],
  108. [self globalsEntryForRow:FLEXGlobalsRowUserDefaults],
  109. [self globalsEntryForRow:FLEXGlobalsRowAppKeychainItems],
  110. [self globalsEntryForRow:FLEXGlobalsRowApplication],
  111. [self globalsEntryForRow:FLEXGlobalsRowAppDelegate],
  112. [self globalsEntryForRow:FLEXGlobalsRowKeyWindow],
  113. [self globalsEntryForRow:FLEXGlobalsRowRootViewController],
  114. [self globalsEntryForRow:FLEXGlobalsRowCookies],
  115. ],
  116. @[ // FLEXGlobalsSectionMisc
  117. [self globalsEntryForRow:FLEXGlobalsRowPasteboard],
  118. [self globalsEntryForRow:FLEXGlobalsRowMainScreen],
  119. [self globalsEntryForRow:FLEXGlobalsRowCurrentDevice],
  120. ]
  121. ];
  122. NSMutableArray *tmp = [NSMutableArray array];
  123. for (NSInteger i = 0; i < FLEXGlobalsSectionCount - 1; i++) { // Skip custom
  124. NSString *title = [self globalsTitleForSection:i];
  125. [tmp addObject:[FLEXTableViewSection section:i title:title rows:rows[i]]];
  126. }
  127. sections = tmp.copy;
  128. });
  129. return sections;
  130. }
  131. #pragma mark - Public
  132. + (void)setApplicationWindow:(UIWindow *)applicationWindow
  133. {
  134. s_applicationWindow = applicationWindow;
  135. }
  136. #pragma mark - UIViewController
  137. - (void)viewDidLoad
  138. {
  139. [super viewDidLoad];
  140. self.title = @"💪 FLEX";
  141. self.showsSearchBar = YES;
  142. self.searchBarDebounceInterval = kFLEXDebounceInstant;
  143. // Table view data
  144. _sections = [[self class] defaultGlobalSections];
  145. if ([FLEXManager sharedManager].userGlobalEntries.count) {
  146. // Make custom section
  147. NSString *title = [[self class] globalsTitleForSection:FLEXGlobalsSectionCustom];
  148. FLEXTableViewSection *custom = [FLEXTableViewSection
  149. section:FLEXGlobalsSectionCustom
  150. title:title
  151. rows:[FLEXManager sharedManager].userGlobalEntries
  152. ];
  153. _sections = [_sections arrayByAddingObject:custom];
  154. }
  155. // Done button
  156. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
  157. initWithBarButtonSystemItem:UIBarButtonSystemItemDone
  158. target:self
  159. action:@selector(donePressed:)
  160. ];
  161. }
  162. #pragma mark - Search Bar
  163. - (void)updateSearchResults:(NSString *)newText {
  164. if (!newText.length) {
  165. self.filteredSections = nil;
  166. [self.tableView reloadData];
  167. return;
  168. }
  169. // Sections are a map of index to rows, since empty sections are omitted
  170. NSMutableArray *filteredSections = [NSMutableArray array];
  171. [self.sections enumerateObjectsUsingBlock:^(FLEXTableViewSection<FLEXGlobalsEntry *> *section, NSUInteger idx, BOOL *stop) {
  172. section = [section newSectionWithRowsMatchingQuery:newText];
  173. if (section) {
  174. [filteredSections addObject:section];
  175. }
  176. }];
  177. self.filteredSections = filteredSections.copy;
  178. [self.tableView reloadData];
  179. }
  180. #pragma mark - Misc
  181. - (void)donePressed:(id)sender
  182. {
  183. [self.delegate globalsViewControllerDidFinish:self];
  184. }
  185. #pragma mark - Table Data Helpers
  186. - (FLEXGlobalsEntry *)globalEntryAtIndexPath:(NSIndexPath *)indexPath
  187. {
  188. if (self.filteredSections) {
  189. return self.filteredSections[indexPath.section][indexPath.row];
  190. } else {
  191. return self.sections[indexPath.section][indexPath.row];
  192. }
  193. }
  194. - (NSString *)titleForSection:(NSInteger)section
  195. {
  196. if (self.filteredSections) {
  197. return self.filteredSections[section].title;
  198. } else {
  199. return self.sections[section].title;
  200. }
  201. }
  202. - (NSString *)titleForRowAtIndexPath:(NSIndexPath *)indexPath
  203. {
  204. FLEXGlobalsEntry *entry = [self globalEntryAtIndexPath:indexPath];
  205. return entry.entryNameFuture();
  206. }
  207. #pragma mark - Table View Data Source
  208. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  209. {
  210. return self.filteredSections ? self.filteredSections.count : self.sections.count;
  211. }
  212. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  213. {
  214. if (self.filteredSections) {
  215. return self.filteredSections[section].count;
  216. } else {
  217. return self.sections[section].count;
  218. }
  219. }
  220. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  221. {
  222. static NSString *CellIdentifier = @"Cell";
  223. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  224. if (!cell) {
  225. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  226. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  227. cell.textLabel.font = [UIFont systemFontOfSize:14.0];
  228. }
  229. cell.textLabel.text = [self titleForRowAtIndexPath:indexPath];
  230. return cell;
  231. }
  232. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  233. {
  234. return [self titleForSection:section];
  235. }
  236. #pragma mark - Table View Delegate
  237. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  238. {
  239. FLEXGlobalsEntry *entry = [self globalEntryAtIndexPath:indexPath];
  240. if (entry.viewControllerFuture) {
  241. [self.navigationController pushViewController:entry.viewControllerFuture() animated:YES];
  242. } else {
  243. entry.rowAction(self);
  244. }
  245. }
  246. @end