FLEXGlobalsTableViewController.m 10 KB

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