FLEXGlobalsTableViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 "FLEXLibrariesTableViewController.h"
  12. #import "FLEXClassesTableViewController.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 "FLEXTableViewSection.h"
  24. static __weak UIWindow *s_applicationWindow = nil;
  25. typedef NS_ENUM(NSUInteger, FLEXGlobalsSection) {
  26. /// NSProcessInfo, Network history, system log,
  27. /// heap, address explorer, libraries, app classes
  28. FLEXGlobalsSectionProcessAndEvents,
  29. /// Browse container, browse bundle, NSBundle.main,
  30. /// NSUserDefaults.standard, UIApplication,
  31. /// app delegate, key window, root VC, cookies
  32. FLEXGlobalsSectionAppShortcuts,
  33. /// UIPasteBoard.general, UIScreen, UIDevice
  34. FLEXGlobalsSectionMisc,
  35. FLEXGlobalsSectionCustom,
  36. FLEXGlobalsSectionCount
  37. };
  38. typedef NS_ENUM(NSUInteger, FLEXGlobalsRow) {
  39. FLEXGlobalsRowNetworkHistory,
  40. FLEXGlobalsRowSystemLog,
  41. FLEXGlobalsRowLiveObjects,
  42. FLEXGlobalsRowAddressInspector,
  43. FLEXGlobalsRowFileBrowser,
  44. FLEXGlobalsRowCookies,
  45. FLEXGlobalsRowSystemLibraries,
  46. FLEXGlobalsRowAppClasses,
  47. FLEXGlobalsRowAppDelegate,
  48. FLEXGlobalsRowRootViewController,
  49. FLEXGlobalsRowUserDefaults,
  50. FLEXGlobalsRowMainBundle,
  51. FLEXGlobalsRowApplication,
  52. FLEXGlobalsRowKeyWindow,
  53. FLEXGlobalsRowMainScreen,
  54. FLEXGlobalsRowCurrentDevice,
  55. FLEXGlobalsRowCount
  56. };
  57. @interface FLEXGlobalsTableViewController ()
  58. @property (nonatomic, readonly) NSArray<FLEXTableViewSection<FLEXGlobalsEntry *> *> *sections;
  59. @property (nonatomic, copy) NSArray<FLEXTableViewSection<FLEXGlobalsEntry *> *> *filteredSections;
  60. @end
  61. @implementation FLEXGlobalsTableViewController
  62. + (NSString *)globalsTitleForSection:(FLEXGlobalsSection)section
  63. {
  64. switch (section) {
  65. case FLEXGlobalsSectionProcessAndEvents:
  66. return @"Process and Events";
  67. case FLEXGlobalsSectionAppShortcuts:
  68. return @"App Shortcuts";
  69. case FLEXGlobalsSectionMisc:
  70. return @"Miscellaneous";
  71. case FLEXGlobalsSectionCustom:
  72. return @"Custom Additions";
  73. default:
  74. @throw NSInternalInconsistencyException;
  75. }
  76. }
  77. + (FLEXGlobalsEntry *)globalsEntryForRow:(FLEXGlobalsRow)row
  78. {
  79. switch (row) {
  80. case FLEXGlobalsRowAppClasses:
  81. return [FLEXClassesTableViewController flex_concreteGlobalsEntry];
  82. case FLEXGlobalsRowAddressInspector:
  83. return [FLEXAddressExplorerCoordinator flex_concreteGlobalsEntry];
  84. case FLEXGlobalsRowSystemLibraries:
  85. return [FLEXLibrariesTableViewController flex_concreteGlobalsEntry];
  86. case FLEXGlobalsRowLiveObjects:
  87. return [FLEXLiveObjectsTableViewController flex_concreteGlobalsEntry];
  88. case FLEXGlobalsRowCookies:
  89. return [FLEXCookiesTableViewController flex_concreteGlobalsEntry];
  90. case FLEXGlobalsRowFileBrowser:
  91. return [FLEXFileBrowserTableViewController flex_concreteGlobalsEntry];
  92. case FLEXGlobalsRowSystemLog:
  93. return [FLEXSystemLogTableViewController flex_concreteGlobalsEntry];
  94. case FLEXGlobalsRowNetworkHistory:
  95. return [FLEXNetworkHistoryTableViewController flex_concreteGlobalsEntry];
  96. case FLEXGlobalsRowAppDelegate:
  97. return [FLEXGlobalsEntry
  98. entryWithNameFuture:^NSString *{
  99. return [NSString stringWithFormat:@"👉 %@", [[UIApplication sharedApplication].delegate class]];
  100. } viewControllerFuture:^UIViewController *{
  101. id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate;
  102. return [FLEXObjectExplorerFactory explorerViewControllerForObject:appDelegate];
  103. }
  104. ];
  105. case FLEXGlobalsRowRootViewController:
  106. return [FLEXGlobalsEntry
  107. entryWithNameFuture:^NSString *{
  108. return [NSString stringWithFormat:@"🌴 %@", [s_applicationWindow.rootViewController class]];
  109. } viewControllerFuture:^UIViewController *{
  110. UIViewController *rootViewController = s_applicationWindow.rootViewController;
  111. return [FLEXObjectExplorerFactory explorerViewControllerForObject:rootViewController];
  112. }
  113. ];
  114. case FLEXGlobalsRowUserDefaults:
  115. return [FLEXGlobalsEntry
  116. entryWithNameFuture:^NSString *{
  117. return @"🚶 +[NSUserDefaults standardUserDefaults]";
  118. } viewControllerFuture:^UIViewController *{
  119. NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
  120. return [FLEXObjectExplorerFactory explorerViewControllerForObject:standardUserDefaults];
  121. }
  122. ];
  123. case FLEXGlobalsRowMainBundle:
  124. return [FLEXGlobalsEntry
  125. entryWithNameFuture:^NSString *{
  126. return @"📦 +[NSBundle mainBundle]";
  127. } viewControllerFuture:^UIViewController *{
  128. NSBundle *mainBundle = [NSBundle mainBundle];
  129. return [FLEXObjectExplorerFactory explorerViewControllerForObject:mainBundle];
  130. }
  131. ];
  132. case FLEXGlobalsRowApplication:
  133. return [FLEXGlobalsEntry
  134. entryWithNameFuture:^NSString *{
  135. return @"💾 +[UIApplication sharedApplication]";
  136. } viewControllerFuture:^UIViewController *{
  137. UIApplication *sharedApplication = [UIApplication sharedApplication];
  138. return [FLEXObjectExplorerFactory explorerViewControllerForObject:sharedApplication];
  139. }
  140. ];
  141. case FLEXGlobalsRowKeyWindow:
  142. return [FLEXGlobalsEntry
  143. entryWithNameFuture:^NSString *{
  144. return @"🔑 -[UIApplication keyWindow]";
  145. } viewControllerFuture:^UIViewController *{
  146. return [FLEXObjectExplorerFactory explorerViewControllerForObject:s_applicationWindow];
  147. }
  148. ];
  149. case FLEXGlobalsRowMainScreen:
  150. return [FLEXGlobalsEntry
  151. entryWithNameFuture:^NSString *{
  152. return @"💻 +[UIScreen mainScreen]";
  153. } viewControllerFuture:^UIViewController *{
  154. UIScreen *mainScreen = [UIScreen mainScreen];
  155. return [FLEXObjectExplorerFactory explorerViewControllerForObject:mainScreen];
  156. }
  157. ];
  158. case FLEXGlobalsRowCurrentDevice:
  159. return [FLEXGlobalsEntry
  160. entryWithNameFuture:^NSString *{
  161. return @"📱 +[UIDevice currentDevice]";
  162. } viewControllerFuture:^UIViewController *{
  163. UIDevice *currentDevice = [UIDevice currentDevice];
  164. return [FLEXObjectExplorerFactory explorerViewControllerForObject:currentDevice];
  165. }
  166. ];
  167. default:
  168. @throw NSInternalInconsistencyException;
  169. }
  170. }
  171. + (NSArray<FLEXTableViewSection<FLEXGlobalsEntry *> *> *)defaultGlobalSections
  172. {
  173. static NSArray<FLEXTableViewSection<FLEXGlobalsEntry *> *> *sections = nil;
  174. static dispatch_once_t onceToken;
  175. dispatch_once(&onceToken, ^{
  176. NSArray *rows = @[
  177. @[
  178. [self globalsEntryForRow:FLEXGlobalsRowNetworkHistory],
  179. [self globalsEntryForRow:FLEXGlobalsRowSystemLog],
  180. [self globalsEntryForRow:FLEXGlobalsRowLiveObjects],
  181. [self globalsEntryForRow:FLEXGlobalsRowAddressInspector],
  182. [self globalsEntryForRow:FLEXGlobalsRowSystemLibraries],
  183. [self globalsEntryForRow:FLEXGlobalsRowAppClasses],
  184. ],
  185. @[ // FLEXGlobalsSectionAppShortcuts
  186. [self globalsEntryForRow:FLEXGlobalsRowMainBundle],
  187. [self globalsEntryForRow:FLEXGlobalsRowUserDefaults],
  188. [self globalsEntryForRow:FLEXGlobalsRowApplication],
  189. [self globalsEntryForRow:FLEXGlobalsRowAppDelegate],
  190. [self globalsEntryForRow:FLEXGlobalsRowKeyWindow],
  191. [self globalsEntryForRow:FLEXGlobalsRowRootViewController],
  192. [self globalsEntryForRow:FLEXGlobalsRowCookies],
  193. ],
  194. @[ // FLEXGlobalsSectionMisc
  195. [self globalsEntryForRow:FLEXGlobalsRowMainScreen],
  196. [self globalsEntryForRow:FLEXGlobalsRowCurrentDevice],
  197. ]
  198. ];
  199. NSMutableArray *tmp = [NSMutableArray array];
  200. for (NSInteger i = 0; i < FLEXGlobalsSectionCount - 1; i++) { // Skip custom
  201. NSString *title = [self globalsTitleForSection:i];
  202. [tmp addObject:[FLEXTableViewSection section:i title:title rows:rows[i]]];
  203. }
  204. sections = tmp.copy;
  205. });
  206. return sections;
  207. }
  208. #pragma mark - Public
  209. + (void)setApplicationWindow:(UIWindow *)applicationWindow
  210. {
  211. s_applicationWindow = applicationWindow;
  212. }
  213. #pragma mark - UIViewController
  214. - (void)viewDidLoad
  215. {
  216. [super viewDidLoad];
  217. self.title = @"💪 FLEX";
  218. self.showsSearchBar = YES;
  219. self.hideSearchBarInitially = YES;
  220. self.searchBarDebounceInterval = kFLEXDebounceInstant;
  221. // Table view data
  222. _sections = [[self class] defaultGlobalSections];
  223. if ([FLEXManager sharedManager].userGlobalEntries.count) {
  224. // Make custom section
  225. NSString *title = [[self class] globalsTitleForSection:FLEXGlobalsSectionCustom];
  226. FLEXTableViewSection *custom = [FLEXTableViewSection
  227. section:FLEXGlobalsSectionCustom
  228. title:title
  229. rows:[FLEXManager sharedManager].userGlobalEntries
  230. ];
  231. _sections = [_sections arrayByAddingObject:custom];
  232. }
  233. // Done button
  234. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(donePressed:)];
  235. }
  236. #pragma mark - Search Bar
  237. - (void)updateSearchResults:(NSString *)newText {
  238. if (!newText.length) {
  239. self.filteredSections = nil;
  240. [self.tableView reloadData];
  241. return;
  242. }
  243. // Sections are a map of index to rows, since empty sections are omitted
  244. NSMutableArray *filteredSections = [NSMutableArray array];
  245. [self.sections enumerateObjectsUsingBlock:^(FLEXTableViewSection<FLEXGlobalsEntry *> *section, NSUInteger idx, BOOL *stop) {
  246. section = [section newSectionWithRowsMatchingQuery:newText];
  247. if (section) {
  248. [filteredSections addObject:section];
  249. }
  250. }];
  251. self.filteredSections = filteredSections.copy;
  252. [self.tableView reloadData];
  253. }
  254. #pragma mark - Misc
  255. - (void)donePressed:(id)sender
  256. {
  257. [self.delegate globalsViewControllerDidFinish:self];
  258. }
  259. #pragma mark - Table Data Helpers
  260. - (FLEXGlobalsEntry *)globalEntryAtIndexPath:(NSIndexPath *)indexPath
  261. {
  262. if (self.filteredSections) {
  263. return self.filteredSections[indexPath.section][indexPath.row];
  264. } else {
  265. return self.sections[indexPath.section][indexPath.row];
  266. }
  267. }
  268. - (NSString *)titleForSection:(NSInteger)section
  269. {
  270. if (self.filteredSections) {
  271. return self.filteredSections[section].title;
  272. } else {
  273. return self.sections[section].title;
  274. }
  275. }
  276. - (NSString *)titleForRowAtIndexPath:(NSIndexPath *)indexPath
  277. {
  278. FLEXGlobalsEntry *entry = [self globalEntryAtIndexPath:indexPath];
  279. return entry.entryNameFuture();
  280. }
  281. #pragma mark - Table View Data Source
  282. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  283. {
  284. return self.filteredSections ? self.filteredSections.count : self.sections.count;
  285. }
  286. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  287. {
  288. if (self.filteredSections) {
  289. return self.filteredSections[section].count;
  290. } else {
  291. return self.sections[section].count;
  292. }
  293. }
  294. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  295. {
  296. static NSString *CellIdentifier = @"Cell";
  297. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  298. if (!cell) {
  299. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  300. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  301. cell.textLabel.font = [FLEXUtility defaultFontOfSize:14.0];
  302. }
  303. cell.textLabel.text = [self titleForRowAtIndexPath:indexPath];
  304. return cell;
  305. }
  306. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  307. {
  308. return [self titleForSection:section];
  309. }
  310. #pragma mark - Table View Delegate
  311. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  312. {
  313. FLEXGlobalsEntry *entry = [self globalEntryAtIndexPath:indexPath];
  314. if (entry.viewControllerFuture) {
  315. [self.navigationController pushViewController:entry.viewControllerFuture() animated:YES];
  316. } else {
  317. entry.rowAction(self);
  318. }
  319. }
  320. @end