FLEXGlobalsTableViewController.m 11 KB

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