FLEXGlobalsViewController.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 "FLEXSystemLogViewController.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 [FLEXSystemLogViewController 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. case FLEXGlobalsRowURLSession:
  76. case FLEXGlobalsRowURLCache:
  77. case FLEXGlobalsRowNotificationCenter:
  78. case FLEXGlobalsRowMenuController:
  79. case FLEXGlobalsRowFileManager:
  80. case FLEXGlobalsRowTimeZone:
  81. case FLEXGlobalsRowLocale:
  82. case FLEXGlobalsRowCalendar:
  83. case FLEXGlobalsRowMainRunLoop:
  84. case FLEXGlobalsRowMainThread:
  85. case FLEXGlobalsRowOperationQueue:
  86. return [FLEXObjectExplorerFactory flex_concreteGlobalsEntry:row];
  87. default:
  88. @throw [NSException
  89. exceptionWithName:NSInternalInconsistencyException
  90. reason:@"Missing globals case in switch" userInfo:nil
  91. ];
  92. }
  93. }
  94. + (NSArray<FLEXGlobalsSection *> *)defaultGlobalSections {
  95. static NSArray<FLEXGlobalsSection *> *sections = nil;
  96. static dispatch_once_t onceToken;
  97. dispatch_once(&onceToken, ^{
  98. NSArray *rowsBySection = @[
  99. @[
  100. [self globalsEntryForRow:FLEXGlobalsRowNetworkHistory],
  101. [self globalsEntryForRow:FLEXGlobalsRowSystemLog],
  102. [self globalsEntryForRow:FLEXGlobalsRowProcessInfo],
  103. [self globalsEntryForRow:FLEXGlobalsRowLiveObjects],
  104. [self globalsEntryForRow:FLEXGlobalsRowAddressInspector],
  105. [self globalsEntryForRow:FLEXGlobalsRowBrowseRuntime],
  106. ],
  107. @[ // FLEXGlobalsSectionAppShortcuts
  108. [self globalsEntryForRow:FLEXGlobalsRowBrowseBundle],
  109. [self globalsEntryForRow:FLEXGlobalsRowBrowseContainer],
  110. [self globalsEntryForRow:FLEXGlobalsRowMainBundle],
  111. [self globalsEntryForRow:FLEXGlobalsRowUserDefaults],
  112. [self globalsEntryForRow:FLEXGlobalsRowAppKeychainItems],
  113. [self globalsEntryForRow:FLEXGlobalsRowApplication],
  114. [self globalsEntryForRow:FLEXGlobalsRowAppDelegate],
  115. [self globalsEntryForRow:FLEXGlobalsRowKeyWindow],
  116. [self globalsEntryForRow:FLEXGlobalsRowRootViewController],
  117. [self globalsEntryForRow:FLEXGlobalsRowCookies],
  118. ],
  119. @[ // FLEXGlobalsSectionMisc
  120. [self globalsEntryForRow:FLEXGlobalsRowPasteboard],
  121. [self globalsEntryForRow:FLEXGlobalsRowMainScreen],
  122. [self globalsEntryForRow:FLEXGlobalsRowCurrentDevice],
  123. [self globalsEntryForRow:FLEXGlobalsRowURLSession],
  124. [self globalsEntryForRow:FLEXGlobalsRowURLCache],
  125. [self globalsEntryForRow:FLEXGlobalsRowNotificationCenter],
  126. [self globalsEntryForRow:FLEXGlobalsRowMenuController],
  127. [self globalsEntryForRow:FLEXGlobalsRowFileManager],
  128. [self globalsEntryForRow:FLEXGlobalsRowTimeZone],
  129. [self globalsEntryForRow:FLEXGlobalsRowLocale],
  130. [self globalsEntryForRow:FLEXGlobalsRowCalendar],
  131. [self globalsEntryForRow:FLEXGlobalsRowMainRunLoop],
  132. [self globalsEntryForRow:FLEXGlobalsRowMainThread],
  133. [self globalsEntryForRow:FLEXGlobalsRowOperationQueue],
  134. ]
  135. ];
  136. sections = [NSArray flex_forEachUpTo:rowsBySection.count map:^FLEXGlobalsSection *(NSUInteger i) {
  137. NSString *title = [self globalsTitleForSection:i];
  138. return [FLEXGlobalsSection title:title rows:rowsBySection[i]];
  139. }];
  140. });
  141. return sections;
  142. }
  143. #pragma mark - UIViewController
  144. - (void)viewDidLoad {
  145. [super viewDidLoad];
  146. self.title = @"💪 FLEX";
  147. self.showsSearchBar = YES;
  148. self.searchBarDebounceInterval = kFLEXDebounceInstant;
  149. // Table view data
  150. _allSections = [[self class] defaultGlobalSections];
  151. if ([FLEXManager sharedManager].userGlobalEntries.count) {
  152. // Make custom section
  153. NSString *title = [[self class] globalsTitleForSection:FLEXGlobalsSectionCustom];
  154. FLEXGlobalsSection *custom = [FLEXGlobalsSection
  155. title:title
  156. rows:[FLEXManager sharedManager].userGlobalEntries
  157. ];
  158. _allSections = [_allSections arrayByAddingObject:custom];
  159. }
  160. self.sections = self.allSections;
  161. }
  162. - (void)viewWillAppear:(BOOL)animated {
  163. [super viewWillAppear:animated];
  164. [self disableToolbar];
  165. }
  166. #pragma mark - Search Bar
  167. - (void)updateSearchResults:(NSString *)newText {
  168. // Sections will adjust data based on this property
  169. for (FLEXTableViewSection *section in self.allSections) {
  170. section.filterText = newText;
  171. }
  172. // Recalculate empty sections
  173. self.sections = [self nonemptySections];
  174. // Refresh table view
  175. if (self.isViewLoaded) {
  176. [self.tableView reloadData];
  177. }
  178. }
  179. #pragma mark - Private
  180. - (NSArray<FLEXGlobalsSection *> *)nonemptySections {
  181. return [self.allSections flex_filtered:^BOOL(FLEXTableViewSection *section, NSUInteger idx) {
  182. return section.numberOfRows > 0;
  183. }];
  184. }
  185. #pragma mark - Table View Data Source
  186. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  187. return self.sections.count;
  188. }
  189. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  190. return self.sections[section].numberOfRows;
  191. }
  192. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  193. static NSString *CellIdentifier = @"Cell";
  194. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  195. if (!cell) {
  196. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  197. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  198. cell.textLabel.font = [UIFont systemFontOfSize:14.0];
  199. }
  200. [self.sections[indexPath.section] configureCell:cell forRow:indexPath.row];
  201. return cell;
  202. }
  203. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  204. return self.sections[section].title;
  205. }
  206. #pragma mark - Table View Delegate
  207. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  208. FLEXTableViewSection *section = self.sections[indexPath.section];
  209. void (^action)(UIViewController *) = [section didSelectRowAction:indexPath.row];
  210. UIViewController *details = [section viewControllerToPushForRow:indexPath.row];
  211. if (action) {
  212. action(self);
  213. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  214. } else if (details) {
  215. [self.navigationController pushViewController:details animated:YES];
  216. } else {
  217. [NSException raise:NSInternalInconsistencyException
  218. format:@"Row is selectable but has no action or view controller"];
  219. }
  220. }
  221. @end