FLEXGlobalsViewController.m 10 KB

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