FLEXGlobalsTableViewController.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 "FLEXGlobalsSection.h"
  24. static __weak UIWindow *s_applicationWindow = nil;
  25. @interface FLEXGlobalsTableViewController ()
  26. /// Only displayed sections of the table view; empty sections are purged from this array.
  27. @property (nonatomic, copy) NSArray<FLEXGlobalsSection *> *sections;
  28. /// Every section in the table view, regardless of whether or not a section is empty.
  29. @property (nonatomic, readonly) NSArray<FLEXGlobalsSection *> *allSections;
  30. @end
  31. @implementation FLEXGlobalsTableViewController
  32. + (NSString *)globalsTitleForSection:(FLEXGlobalsSectionKind)section
  33. {
  34. switch (section) {
  35. case FLEXGlobalsSectionProcessAndEvents:
  36. return @"Process and Events";
  37. case FLEXGlobalsSectionAppShortcuts:
  38. return @"App Shortcuts";
  39. case FLEXGlobalsSectionMisc:
  40. return @"Miscellaneous";
  41. case FLEXGlobalsSectionCustom:
  42. return @"Custom Additions";
  43. default:
  44. @throw NSInternalInconsistencyException;
  45. }
  46. }
  47. + (FLEXGlobalsEntry *)globalsEntryForRow:(FLEXGlobalsRow)row
  48. {
  49. switch (row) {
  50. case FLEXGlobalsRowAppKeychainItems:
  51. return [FLEXKeychainTableViewController flex_concreteGlobalsEntry:row];
  52. case FLEXGlobalsRowAddressInspector:
  53. return [FLEXAddressExplorerCoordinator flex_concreteGlobalsEntry:row];
  54. case FLEXGlobalsRowBrowseRuntime:
  55. return [FLEXObjcRuntimeViewController flex_concreteGlobalsEntry:row];
  56. case FLEXGlobalsRowLiveObjects:
  57. return [FLEXLiveObjectsTableViewController flex_concreteGlobalsEntry:row];
  58. case FLEXGlobalsRowCookies:
  59. return [FLEXCookiesTableViewController flex_concreteGlobalsEntry:row];
  60. case FLEXGlobalsRowBrowseBundle:
  61. case FLEXGlobalsRowBrowseContainer:
  62. return [FLEXFileBrowserTableViewController flex_concreteGlobalsEntry:row];
  63. case FLEXGlobalsRowSystemLog:
  64. return [FLEXSystemLogTableViewController flex_concreteGlobalsEntry:row];
  65. case FLEXGlobalsRowNetworkHistory:
  66. return [FLEXNetworkHistoryTableViewController flex_concreteGlobalsEntry:row];
  67. case FLEXGlobalsRowKeyWindow:
  68. return [FLEXGlobalsEntry
  69. entryWithNameFuture:^NSString *{
  70. return @"🔑 -[UIApplication keyWindow]";
  71. } viewControllerFuture:^UIViewController *{
  72. return [FLEXObjectExplorerFactory explorerViewControllerForObject:s_applicationWindow];
  73. }
  74. ];
  75. case FLEXGlobalsRowRootViewController:
  76. case FLEXGlobalsRowProcessInfo:
  77. case FLEXGlobalsRowAppDelegate:
  78. case FLEXGlobalsRowUserDefaults:
  79. case FLEXGlobalsRowMainBundle:
  80. case FLEXGlobalsRowApplication:
  81. case FLEXGlobalsRowMainScreen:
  82. case FLEXGlobalsRowCurrentDevice:
  83. case FLEXGlobalsRowPasteboard:
  84. return [FLEXObjectExplorerFactory flex_concreteGlobalsEntry:row];
  85. default:
  86. @throw [NSException
  87. exceptionWithName:NSInternalInconsistencyException
  88. reason:@"Missing globals case in switch" userInfo:nil
  89. ];
  90. }
  91. }
  92. + (NSArray<FLEXGlobalsSection *> *)defaultGlobalSections
  93. {
  94. static NSArray<FLEXGlobalsSection *> *sections = nil;
  95. static dispatch_once_t onceToken;
  96. dispatch_once(&onceToken, ^{
  97. NSArray *rowsBySection = @[
  98. @[
  99. [self globalsEntryForRow:FLEXGlobalsRowNetworkHistory],
  100. [self globalsEntryForRow:FLEXGlobalsRowSystemLog],
  101. [self globalsEntryForRow:FLEXGlobalsRowProcessInfo],
  102. [self globalsEntryForRow:FLEXGlobalsRowLiveObjects],
  103. [self globalsEntryForRow:FLEXGlobalsRowAddressInspector],
  104. [self globalsEntryForRow:FLEXGlobalsRowBrowseRuntime],
  105. ],
  106. @[ // FLEXGlobalsSectionAppShortcuts
  107. [self globalsEntryForRow:FLEXGlobalsRowBrowseBundle],
  108. [self globalsEntryForRow:FLEXGlobalsRowBrowseContainer],
  109. [self globalsEntryForRow:FLEXGlobalsRowMainBundle],
  110. [self globalsEntryForRow:FLEXGlobalsRowUserDefaults],
  111. [self globalsEntryForRow:FLEXGlobalsRowAppKeychainItems],
  112. [self globalsEntryForRow:FLEXGlobalsRowApplication],
  113. [self globalsEntryForRow:FLEXGlobalsRowAppDelegate],
  114. [self globalsEntryForRow:FLEXGlobalsRowKeyWindow],
  115. [self globalsEntryForRow:FLEXGlobalsRowRootViewController],
  116. [self globalsEntryForRow:FLEXGlobalsRowCookies],
  117. ],
  118. @[ // FLEXGlobalsSectionMisc
  119. [self globalsEntryForRow:FLEXGlobalsRowPasteboard],
  120. [self globalsEntryForRow:FLEXGlobalsRowMainScreen],
  121. [self globalsEntryForRow:FLEXGlobalsRowCurrentDevice],
  122. ]
  123. ];
  124. sections = [NSArray flex_forEachUpTo:rowsBySection.count map:^FLEXGlobalsSection *(NSUInteger i) {
  125. NSString *title = [self globalsTitleForSection:i];
  126. return [FLEXGlobalsSection title:title rows:rowsBySection[i]];
  127. }];
  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. _allSections = [[self class] defaultGlobalSections];
  145. if ([FLEXManager sharedManager].userGlobalEntries.count) {
  146. // Make custom section
  147. NSString *title = [[self class] globalsTitleForSection:FLEXGlobalsSectionCustom];
  148. FLEXGlobalsSection *custom = [FLEXGlobalsSection
  149. title:title
  150. rows:[FLEXManager sharedManager].userGlobalEntries
  151. ];
  152. _allSections = [_allSections arrayByAddingObject:custom];
  153. }
  154. self.sections = self.allSections;
  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. // Sections will adjust data based on this property
  165. for (FLEXTableViewSection *section in self.allSections) {
  166. section.filterText = newText;
  167. }
  168. // Recalculate empty sections
  169. self.sections = [self nonemptySections];
  170. // Refresh table view
  171. if (self.isViewLoaded) {
  172. [self.tableView reloadData];
  173. }
  174. }
  175. #pragma mark - Private
  176. - (void)donePressed:(id)sender
  177. {
  178. [self.delegate globalsViewControllerDidFinish:self];
  179. }
  180. - (NSArray<FLEXGlobalsSection *> *)nonemptySections
  181. {
  182. return [self.allSections flex_filtered:^BOOL(FLEXTableViewSection *section, NSUInteger idx) {
  183. return section.numberOfRows > 0;
  184. }];
  185. }
  186. #pragma mark - Table View Data Source
  187. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  188. {
  189. return self.sections.count;
  190. }
  191. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  192. {
  193. return self.sections[section].numberOfRows;
  194. }
  195. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  196. {
  197. static NSString *CellIdentifier = @"Cell";
  198. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  199. if (!cell) {
  200. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  201. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  202. cell.textLabel.font = [UIFont systemFontOfSize:14.0];
  203. }
  204. [self.sections[indexPath.section] configureCell:cell forRow:indexPath.row];
  205. return cell;
  206. }
  207. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  208. {
  209. return self.sections[section].title;
  210. }
  211. #pragma mark - Table View Delegate
  212. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  213. {
  214. FLEXTableViewSection *section = self.sections[indexPath.section];
  215. void (^action)(UIViewController *) = [section didSelectRowAction:indexPath.row];
  216. UIViewController *details = [section viewControllerToPushForRow:indexPath.row];
  217. if (action) {
  218. action(self);
  219. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  220. } else if (details) {
  221. [self.navigationController pushViewController:details animated:YES];
  222. } else {
  223. [NSException raise:NSInternalInconsistencyException
  224. format:@"Row is selectable but has no action or view controller"];
  225. }
  226. }
  227. @end