FLEXGlobalsViewController.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //
  2. // FLEXGlobalsViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 2014-05-03.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXGlobalsViewController.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 "FLEXNetworkMITMViewController.h"
  22. #import "FLEXAddressExplorerCoordinator.h"
  23. #import "FLEXGlobalsSection.h"
  24. @interface FLEXGlobalsViewController ()
  25. /// Only displayed sections of the table view; empty sections are purged from this array.
  26. @property (nonatomic, copy) NSArray<FLEXGlobalsSection *> *sections;
  27. /// Every section in the table view, regardless of whether or not a section is empty.
  28. @property (nonatomic, readonly) NSArray<FLEXGlobalsSection *> *allSections;
  29. @end
  30. @implementation FLEXGlobalsViewController
  31. #pragma mark - Initialization
  32. + (NSString *)globalsTitleForSection:(FLEXGlobalsSectionKind)section {
  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. switch (row) {
  48. case FLEXGlobalsRowAppKeychainItems:
  49. return [FLEXKeychainTableViewController flex_concreteGlobalsEntry:row];
  50. case FLEXGlobalsRowAddressInspector:
  51. return [FLEXAddressExplorerCoordinator flex_concreteGlobalsEntry:row];
  52. case FLEXGlobalsRowBrowseRuntime:
  53. return [FLEXObjcRuntimeViewController 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 [FLEXNetworkMITMViewController flex_concreteGlobalsEntry:row];
  65. case FLEXGlobalsRowKeyWindow:
  66. case FLEXGlobalsRowRootViewController:
  67. case FLEXGlobalsRowProcessInfo:
  68. case FLEXGlobalsRowAppDelegate:
  69. case FLEXGlobalsRowUserDefaults:
  70. case FLEXGlobalsRowMainBundle:
  71. case FLEXGlobalsRowApplication:
  72. case FLEXGlobalsRowMainScreen:
  73. case FLEXGlobalsRowCurrentDevice:
  74. case FLEXGlobalsRowPasteboard:
  75. return [FLEXObjectExplorerFactory flex_concreteGlobalsEntry:row];
  76. default:
  77. @throw [NSException
  78. exceptionWithName:NSInternalInconsistencyException
  79. reason:@"Missing globals case in switch" userInfo:nil
  80. ];
  81. }
  82. }
  83. + (NSArray<FLEXGlobalsSection *> *)defaultGlobalSections {
  84. static NSArray<FLEXGlobalsSection *> *sections = nil;
  85. static dispatch_once_t onceToken;
  86. dispatch_once(&onceToken, ^{
  87. NSArray *rowsBySection = @[
  88. @[
  89. [self globalsEntryForRow:FLEXGlobalsRowNetworkHistory],
  90. [self globalsEntryForRow:FLEXGlobalsRowSystemLog],
  91. [self globalsEntryForRow:FLEXGlobalsRowProcessInfo],
  92. [self globalsEntryForRow:FLEXGlobalsRowLiveObjects],
  93. [self globalsEntryForRow:FLEXGlobalsRowAddressInspector],
  94. [self globalsEntryForRow:FLEXGlobalsRowBrowseRuntime],
  95. ],
  96. @[ // FLEXGlobalsSectionAppShortcuts
  97. [self globalsEntryForRow:FLEXGlobalsRowBrowseBundle],
  98. [self globalsEntryForRow:FLEXGlobalsRowBrowseContainer],
  99. [self globalsEntryForRow:FLEXGlobalsRowMainBundle],
  100. [self globalsEntryForRow:FLEXGlobalsRowUserDefaults],
  101. [self globalsEntryForRow:FLEXGlobalsRowAppKeychainItems],
  102. [self globalsEntryForRow:FLEXGlobalsRowApplication],
  103. [self globalsEntryForRow:FLEXGlobalsRowAppDelegate],
  104. [self globalsEntryForRow:FLEXGlobalsRowKeyWindow],
  105. [self globalsEntryForRow:FLEXGlobalsRowRootViewController],
  106. [self globalsEntryForRow:FLEXGlobalsRowCookies],
  107. ],
  108. @[ // FLEXGlobalsSectionMisc
  109. [self globalsEntryForRow:FLEXGlobalsRowPasteboard],
  110. [self globalsEntryForRow:FLEXGlobalsRowMainScreen],
  111. [self globalsEntryForRow:FLEXGlobalsRowCurrentDevice],
  112. ]
  113. ];
  114. sections = [NSArray flex_forEachUpTo:rowsBySection.count map:^FLEXGlobalsSection *(NSUInteger i) {
  115. NSString *title = [self globalsTitleForSection:i];
  116. return [FLEXGlobalsSection title:title rows:rowsBySection[i]];
  117. }];
  118. });
  119. return sections;
  120. }
  121. #pragma mark - UIViewController
  122. - (void)viewDidLoad {
  123. [super viewDidLoad];
  124. self.title = @"💪 FLEX";
  125. self.showsSearchBar = YES;
  126. self.searchBarDebounceInterval = kFLEXDebounceInstant;
  127. // Table view data
  128. _allSections = [[self class] defaultGlobalSections];
  129. if ([FLEXManager sharedManager].userGlobalEntries.count) {
  130. // Make custom section
  131. NSString *title = [[self class] globalsTitleForSection:FLEXGlobalsSectionCustom];
  132. FLEXGlobalsSection *custom = [FLEXGlobalsSection
  133. title:title
  134. rows:[FLEXManager sharedManager].userGlobalEntries
  135. ];
  136. _allSections = [_allSections arrayByAddingObject:custom];
  137. }
  138. self.sections = self.allSections;
  139. }
  140. - (void)viewWillAppear:(BOOL)animated {
  141. [super viewWillAppear:animated];
  142. [self disableToolbar];
  143. }
  144. #pragma mark - Search Bar
  145. - (void)updateSearchResults:(NSString *)newText {
  146. // Sections will adjust data based on this property
  147. for (FLEXTableViewSection *section in self.allSections) {
  148. section.filterText = newText;
  149. }
  150. // Recalculate empty sections
  151. self.sections = [self nonemptySections];
  152. // Refresh table view
  153. if (self.isViewLoaded) {
  154. [self.tableView reloadData];
  155. }
  156. }
  157. #pragma mark - Private
  158. - (NSArray<FLEXGlobalsSection *> *)nonemptySections {
  159. return [self.allSections flex_filtered:^BOOL(FLEXTableViewSection *section, NSUInteger idx) {
  160. return section.numberOfRows > 0;
  161. }];
  162. }
  163. #pragma mark - Table View Data Source
  164. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  165. return self.sections.count;
  166. }
  167. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  168. return self.sections[section].numberOfRows;
  169. }
  170. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  171. static NSString *CellIdentifier = @"Cell";
  172. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  173. if (!cell) {
  174. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  175. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  176. cell.textLabel.font = [UIFont systemFontOfSize:14.0];
  177. }
  178. [self.sections[indexPath.section] configureCell:cell forRow:indexPath.row];
  179. return cell;
  180. }
  181. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  182. return self.sections[section].title;
  183. }
  184. #pragma mark - Table View Delegate
  185. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  186. FLEXTableViewSection *section = self.sections[indexPath.section];
  187. void (^action)(UIViewController *) = [section didSelectRowAction:indexPath.row];
  188. UIViewController *details = [section viewControllerToPushForRow:indexPath.row];
  189. if (action) {
  190. action(self);
  191. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  192. } else if (details) {
  193. [self.navigationController pushViewController:details animated:YES];
  194. } else {
  195. [NSException raise:NSInternalInconsistencyException
  196. format:@"Row is selectable but has no action or view controller"];
  197. }
  198. }
  199. @end