FLEXGlobalsViewController.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 "FLEXNetworkHistoryTableViewController.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. + (NSString *)globalsTitleForSection:(FLEXGlobalsSectionKind)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 FLEXGlobalsRowAppKeychainItems:
  50. return [FLEXKeychainTableViewController flex_concreteGlobalsEntry:row];
  51. case FLEXGlobalsRowAddressInspector:
  52. return [FLEXAddressExplorerCoordinator flex_concreteGlobalsEntry:row];
  53. case FLEXGlobalsRowBrowseRuntime:
  54. return [FLEXObjcRuntimeViewController flex_concreteGlobalsEntry:row];
  55. case FLEXGlobalsRowLiveObjects:
  56. return [FLEXLiveObjectsTableViewController flex_concreteGlobalsEntry:row];
  57. case FLEXGlobalsRowCookies:
  58. return [FLEXCookiesTableViewController flex_concreteGlobalsEntry:row];
  59. case FLEXGlobalsRowBrowseBundle:
  60. case FLEXGlobalsRowBrowseContainer:
  61. return [FLEXFileBrowserTableViewController flex_concreteGlobalsEntry:row];
  62. case FLEXGlobalsRowSystemLog:
  63. return [FLEXSystemLogTableViewController flex_concreteGlobalsEntry:row];
  64. case FLEXGlobalsRowNetworkHistory:
  65. return [FLEXNetworkHistoryTableViewController flex_concreteGlobalsEntry:row];
  66. case FLEXGlobalsRowKeyWindow:
  67. case FLEXGlobalsRowRootViewController:
  68. case FLEXGlobalsRowProcessInfo:
  69. case FLEXGlobalsRowAppDelegate:
  70. case FLEXGlobalsRowUserDefaults:
  71. case FLEXGlobalsRowMainBundle:
  72. case FLEXGlobalsRowApplication:
  73. case FLEXGlobalsRowMainScreen:
  74. case FLEXGlobalsRowCurrentDevice:
  75. case FLEXGlobalsRowPasteboard:
  76. return [FLEXObjectExplorerFactory flex_concreteGlobalsEntry:row];
  77. default:
  78. @throw [NSException
  79. exceptionWithName:NSInternalInconsistencyException
  80. reason:@"Missing globals case in switch" userInfo:nil
  81. ];
  82. }
  83. }
  84. + (NSArray<FLEXGlobalsSection *> *)defaultGlobalSections
  85. {
  86. static NSArray<FLEXGlobalsSection *> *sections = nil;
  87. static dispatch_once_t onceToken;
  88. dispatch_once(&onceToken, ^{
  89. NSArray *rowsBySection = @[
  90. @[
  91. [self globalsEntryForRow:FLEXGlobalsRowNetworkHistory],
  92. [self globalsEntryForRow:FLEXGlobalsRowSystemLog],
  93. [self globalsEntryForRow:FLEXGlobalsRowProcessInfo],
  94. [self globalsEntryForRow:FLEXGlobalsRowLiveObjects],
  95. [self globalsEntryForRow:FLEXGlobalsRowAddressInspector],
  96. [self globalsEntryForRow:FLEXGlobalsRowBrowseRuntime],
  97. ],
  98. @[ // FLEXGlobalsSectionAppShortcuts
  99. [self globalsEntryForRow:FLEXGlobalsRowBrowseBundle],
  100. [self globalsEntryForRow:FLEXGlobalsRowBrowseContainer],
  101. [self globalsEntryForRow:FLEXGlobalsRowMainBundle],
  102. [self globalsEntryForRow:FLEXGlobalsRowUserDefaults],
  103. [self globalsEntryForRow:FLEXGlobalsRowAppKeychainItems],
  104. [self globalsEntryForRow:FLEXGlobalsRowApplication],
  105. [self globalsEntryForRow:FLEXGlobalsRowAppDelegate],
  106. [self globalsEntryForRow:FLEXGlobalsRowKeyWindow],
  107. [self globalsEntryForRow:FLEXGlobalsRowRootViewController],
  108. [self globalsEntryForRow:FLEXGlobalsRowCookies],
  109. ],
  110. @[ // FLEXGlobalsSectionMisc
  111. [self globalsEntryForRow:FLEXGlobalsRowPasteboard],
  112. [self globalsEntryForRow:FLEXGlobalsRowMainScreen],
  113. [self globalsEntryForRow:FLEXGlobalsRowCurrentDevice],
  114. ]
  115. ];
  116. sections = [NSArray flex_forEachUpTo:rowsBySection.count map:^FLEXGlobalsSection *(NSUInteger i) {
  117. NSString *title = [self globalsTitleForSection:i];
  118. return [FLEXGlobalsSection title:title rows:rowsBySection[i]];
  119. }];
  120. });
  121. return sections;
  122. }
  123. #pragma mark - UIViewController
  124. - (void)viewDidLoad
  125. {
  126. [super viewDidLoad];
  127. self.title = @"💪 FLEX";
  128. self.showsSearchBar = YES;
  129. self.searchBarDebounceInterval = kFLEXDebounceInstant;
  130. // Table view data
  131. _allSections = [[self class] defaultGlobalSections];
  132. if ([FLEXManager sharedManager].userGlobalEntries.count) {
  133. // Make custom section
  134. NSString *title = [[self class] globalsTitleForSection:FLEXGlobalsSectionCustom];
  135. FLEXGlobalsSection *custom = [FLEXGlobalsSection
  136. title:title
  137. rows:[FLEXManager sharedManager].userGlobalEntries
  138. ];
  139. _allSections = [_allSections arrayByAddingObject:custom];
  140. }
  141. self.sections = self.allSections;
  142. }
  143. #pragma mark - Search Bar
  144. - (void)updateSearchResults:(NSString *)newText {
  145. // Sections will adjust data based on this property
  146. for (FLEXTableViewSection *section in self.allSections) {
  147. section.filterText = newText;
  148. }
  149. // Recalculate empty sections
  150. self.sections = [self nonemptySections];
  151. // Refresh table view
  152. if (self.isViewLoaded) {
  153. [self.tableView reloadData];
  154. }
  155. }
  156. #pragma mark - Private
  157. - (NSArray<FLEXGlobalsSection *> *)nonemptySections
  158. {
  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. {
  166. return self.sections.count;
  167. }
  168. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  169. {
  170. return self.sections[section].numberOfRows;
  171. }
  172. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  173. {
  174. static NSString *CellIdentifier = @"Cell";
  175. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  176. if (!cell) {
  177. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  178. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  179. cell.textLabel.font = [UIFont systemFontOfSize:14.0];
  180. }
  181. [self.sections[indexPath.section] configureCell:cell forRow:indexPath.row];
  182. return cell;
  183. }
  184. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  185. {
  186. return self.sections[section].title;
  187. }
  188. #pragma mark - Table View Delegate
  189. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  190. {
  191. FLEXTableViewSection *section = self.sections[indexPath.section];
  192. void (^action)(UIViewController *) = [section didSelectRowAction:indexPath.row];
  193. UIViewController *details = [section viewControllerToPushForRow:indexPath.row];
  194. if (action) {
  195. action(self);
  196. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  197. } else if (details) {
  198. [self.navigationController pushViewController:details animated:YES];
  199. } else {
  200. [NSException raise:NSInternalInconsistencyException
  201. format:@"Row is selectable but has no action or view controller"];
  202. }
  203. }
  204. @end