FLEXGlobalsTableViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 FLEXGlobalsRowRootViewController:
  69. return [FLEXGlobalsEntry
  70. entryWithNameFuture:^NSString *{
  71. return [NSString stringWithFormat:@"🌴 %@", [s_applicationWindow.rootViewController class]];
  72. } viewControllerFuture:^UIViewController *{
  73. UIViewController *rootViewController = s_applicationWindow.rootViewController;
  74. return [FLEXObjectExplorerFactory explorerViewControllerForObject:rootViewController];
  75. }
  76. ];
  77. case FLEXGlobalsRowKeyWindow:
  78. return [FLEXGlobalsEntry
  79. entryWithNameFuture:^NSString *{
  80. return @"🔑 -[UIApplication keyWindow]";
  81. } viewControllerFuture:^UIViewController *{
  82. return [FLEXObjectExplorerFactory explorerViewControllerForObject:s_applicationWindow];
  83. }
  84. ];
  85. case FLEXGlobalsRowProcessInfo:
  86. case FLEXGlobalsRowAppDelegate:
  87. case FLEXGlobalsRowUserDefaults:
  88. case FLEXGlobalsRowMainBundle:
  89. case FLEXGlobalsRowApplication:
  90. case FLEXGlobalsRowMainScreen:
  91. case FLEXGlobalsRowCurrentDevice:
  92. case FLEXGlobalsRowPasteboard:
  93. return [FLEXObjectExplorerFactory flex_concreteGlobalsEntry:row];
  94. default:
  95. @throw NSInternalInconsistencyException;
  96. }
  97. }
  98. + (NSArray<FLEXTableViewSection<FLEXGlobalsEntry *> *> *)defaultGlobalSections
  99. {
  100. static NSArray<FLEXTableViewSection<FLEXGlobalsEntry *> *> *sections = nil;
  101. static dispatch_once_t onceToken;
  102. dispatch_once(&onceToken, ^{
  103. NSArray *rows = @[
  104. @[
  105. [self globalsEntryForRow:FLEXGlobalsRowProcessInfo],
  106. [self globalsEntryForRow:FLEXGlobalsRowNetworkHistory],
  107. [self globalsEntryForRow:FLEXGlobalsRowSystemLog],
  108. [self globalsEntryForRow:FLEXGlobalsRowLiveObjects],
  109. [self globalsEntryForRow:FLEXGlobalsRowAddressInspector],
  110. [self globalsEntryForRow:FLEXGlobalsRowSystemLibraries],
  111. [self globalsEntryForRow:FLEXGlobalsRowAppClasses],
  112. ],
  113. @[ // FLEXGlobalsSectionAppShortcuts
  114. [self globalsEntryForRow:FLEXGlobalsRowMainBundle],
  115. [self globalsEntryForRow:FLEXGlobalsRowUserDefaults],
  116. [self globalsEntryForRow:FLEXGlobalsRowAppKeyChainItems],
  117. [self globalsEntryForRow:FLEXGlobalsRowApplication],
  118. [self globalsEntryForRow:FLEXGlobalsRowAppDelegate],
  119. [self globalsEntryForRow:FLEXGlobalsRowKeyWindow],
  120. [self globalsEntryForRow:FLEXGlobalsRowRootViewController],
  121. [self globalsEntryForRow:FLEXGlobalsRowCookies],
  122. [self globalsEntryForRow:FLEXGlobalsRowBrowseBundle],
  123. [self globalsEntryForRow:FLEXGlobalsRowBrowseContainer],
  124. ],
  125. @[ // FLEXGlobalsSectionMisc
  126. [self globalsEntryForRow:FLEXGlobalsRowPasteboard],
  127. [self globalsEntryForRow:FLEXGlobalsRowMainScreen],
  128. [self globalsEntryForRow:FLEXGlobalsRowCurrentDevice],
  129. ]
  130. ];
  131. NSMutableArray *tmp = [NSMutableArray array];
  132. for (NSInteger i = 0; i < FLEXGlobalsSectionCount - 1; i++) { // Skip custom
  133. NSString *title = [self globalsTitleForSection:i];
  134. [tmp addObject:[FLEXTableViewSection section:i title:title rows:rows[i]]];
  135. }
  136. sections = tmp.copy;
  137. });
  138. return sections;
  139. }
  140. #pragma mark - Public
  141. + (void)setApplicationWindow:(UIWindow *)applicationWindow
  142. {
  143. s_applicationWindow = applicationWindow;
  144. }
  145. #pragma mark - UIViewController
  146. - (void)viewDidLoad
  147. {
  148. [super viewDidLoad];
  149. self.title = @"💪 FLEX";
  150. self.showsSearchBar = YES;
  151. self.hideSearchBarInitially = YES;
  152. self.searchBarDebounceInterval = kFLEXDebounceInstant;
  153. // Table view data
  154. _sections = [[self class] defaultGlobalSections];
  155. if ([FLEXManager sharedManager].userGlobalEntries.count) {
  156. // Make custom section
  157. NSString *title = [[self class] globalsTitleForSection:FLEXGlobalsSectionCustom];
  158. FLEXTableViewSection *custom = [FLEXTableViewSection
  159. section:FLEXGlobalsSectionCustom
  160. title:title
  161. rows:[FLEXManager sharedManager].userGlobalEntries
  162. ];
  163. _sections = [_sections arrayByAddingObject:custom];
  164. }
  165. // Done button
  166. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(donePressed:)];
  167. }
  168. #pragma mark - Search Bar
  169. - (void)updateSearchResults:(NSString *)newText {
  170. if (!newText.length) {
  171. self.filteredSections = nil;
  172. [self.tableView reloadData];
  173. return;
  174. }
  175. // Sections are a map of index to rows, since empty sections are omitted
  176. NSMutableArray *filteredSections = [NSMutableArray array];
  177. [self.sections enumerateObjectsUsingBlock:^(FLEXTableViewSection<FLEXGlobalsEntry *> *section, NSUInteger idx, BOOL *stop) {
  178. section = [section newSectionWithRowsMatchingQuery:newText];
  179. if (section) {
  180. [filteredSections addObject:section];
  181. }
  182. }];
  183. self.filteredSections = filteredSections.copy;
  184. [self.tableView reloadData];
  185. }
  186. #pragma mark - Misc
  187. - (void)donePressed:(id)sender
  188. {
  189. [self.delegate globalsViewControllerDidFinish:self];
  190. }
  191. #pragma mark - Table Data Helpers
  192. - (FLEXGlobalsEntry *)globalEntryAtIndexPath:(NSIndexPath *)indexPath
  193. {
  194. if (self.filteredSections) {
  195. return self.filteredSections[indexPath.section][indexPath.row];
  196. } else {
  197. return self.sections[indexPath.section][indexPath.row];
  198. }
  199. }
  200. - (NSString *)titleForSection:(NSInteger)section
  201. {
  202. if (self.filteredSections) {
  203. return self.filteredSections[section].title;
  204. } else {
  205. return self.sections[section].title;
  206. }
  207. }
  208. - (NSString *)titleForRowAtIndexPath:(NSIndexPath *)indexPath
  209. {
  210. FLEXGlobalsEntry *entry = [self globalEntryAtIndexPath:indexPath];
  211. return entry.entryNameFuture();
  212. }
  213. #pragma mark - Table View Data Source
  214. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  215. {
  216. return self.filteredSections ? self.filteredSections.count : self.sections.count;
  217. }
  218. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  219. {
  220. if (self.filteredSections) {
  221. return self.filteredSections[section].count;
  222. } else {
  223. return self.sections[section].count;
  224. }
  225. }
  226. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  227. {
  228. static NSString *CellIdentifier = @"Cell";
  229. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  230. if (!cell) {
  231. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  232. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  233. cell.textLabel.font = [FLEXUtility defaultFontOfSize:14.0];
  234. }
  235. cell.textLabel.text = [self titleForRowAtIndexPath:indexPath];
  236. return cell;
  237. }
  238. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  239. {
  240. return [self titleForSection:section];
  241. }
  242. #pragma mark - Table View Delegate
  243. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  244. {
  245. FLEXGlobalsEntry *entry = [self globalEntryAtIndexPath:indexPath];
  246. if (entry.viewControllerFuture) {
  247. [self.navigationController pushViewController:entry.viewControllerFuture() animated:YES];
  248. } else {
  249. entry.rowAction(self);
  250. }
  251. }
  252. @end