FLEXGlobalsTableViewController.m 10 KB

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