You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

292 lines
10 KiB

  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.searchBarDebounceInterval = kFLEXDebounceInstant;
  147. // Table view data
  148. _sections = [[self class] defaultGlobalSections];
  149. if ([FLEXManager sharedManager].userGlobalEntries.count) {
  150. // Make custom section
  151. NSString *title = [[self class] globalsTitleForSection:FLEXGlobalsSectionCustom];
  152. FLEXTableViewSection *custom = [FLEXTableViewSection
  153. section:FLEXGlobalsSectionCustom
  154. title:title
  155. rows:[FLEXManager sharedManager].userGlobalEntries
  156. ];
  157. _sections = [_sections arrayByAddingObject:custom];
  158. }
  159. // Done button
  160. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
  161. initWithBarButtonSystemItem:UIBarButtonSystemItemDone
  162. target:self
  163. action:@selector(donePressed:)
  164. ];
  165. }
  166. #pragma mark - Search Bar
  167. - (void)updateSearchResults:(NSString *)newText {
  168. if (!newText.length) {
  169. self.filteredSections = nil;
  170. [self.tableView reloadData];
  171. return;
  172. }
  173. // Sections are a map of index to rows, since empty sections are omitted
  174. NSMutableArray *filteredSections = [NSMutableArray array];
  175. [self.sections enumerateObjectsUsingBlock:^(FLEXTableViewSection<FLEXGlobalsEntry *> *section, NSUInteger idx, BOOL *stop) {
  176. section = [section newSectionWithRowsMatchingQuery:newText];
  177. if (section) {
  178. [filteredSections addObject:section];
  179. }
  180. }];
  181. self.filteredSections = filteredSections.copy;
  182. [self.tableView reloadData];
  183. }
  184. #pragma mark - Misc
  185. - (void)donePressed:(id)sender
  186. {
  187. [self.delegate globalsViewControllerDidFinish:self];
  188. }
  189. #pragma mark - Table Data Helpers
  190. - (FLEXGlobalsEntry *)globalEntryAtIndexPath:(NSIndexPath *)indexPath
  191. {
  192. if (self.filteredSections) {
  193. return self.filteredSections[indexPath.section][indexPath.row];
  194. } else {
  195. return self.sections[indexPath.section][indexPath.row];
  196. }
  197. }
  198. - (NSString *)titleForSection:(NSInteger)section
  199. {
  200. if (self.filteredSections) {
  201. return self.filteredSections[section].title;
  202. } else {
  203. return self.sections[section].title;
  204. }
  205. }
  206. - (NSString *)titleForRowAtIndexPath:(NSIndexPath *)indexPath
  207. {
  208. FLEXGlobalsEntry *entry = [self globalEntryAtIndexPath:indexPath];
  209. return entry.entryNameFuture();
  210. }
  211. #pragma mark - Table View Data Source
  212. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  213. {
  214. return self.filteredSections ? self.filteredSections.count : self.sections.count;
  215. }
  216. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  217. {
  218. if (self.filteredSections) {
  219. return self.filteredSections[section].count;
  220. } else {
  221. return self.sections[section].count;
  222. }
  223. }
  224. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  225. {
  226. static NSString *CellIdentifier = @"Cell";
  227. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  228. if (!cell) {
  229. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  230. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  231. cell.textLabel.font = [FLEXUtility defaultFontOfSize:14.0];
  232. }
  233. cell.textLabel.text = [self titleForRowAtIndexPath:indexPath];
  234. return cell;
  235. }
  236. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  237. {
  238. return [self titleForSection:section];
  239. }
  240. #pragma mark - Table View Delegate
  241. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  242. {
  243. FLEXGlobalsEntry *entry = [self globalEntryAtIndexPath:indexPath];
  244. if (entry.viewControllerFuture) {
  245. [self.navigationController pushViewController:entry.viewControllerFuture() animated:YES];
  246. } else {
  247. entry.rowAction(self);
  248. }
  249. }
  250. @end