FLEXGlobalsTableViewController.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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, FLEXGlobalsRow) {
  25. FLEXGlobalsRowNetworkHistory,
  26. FLEXGlobalsRowSystemLog,
  27. FLEXGlobalsRowLiveObjects,
  28. FLEXGlobalsRowAddressInspector,
  29. FLEXGlobalsRowFileBrowser,
  30. FLEXGlobalsCookies,
  31. FLEXGlobalsRowSystemLibraries,
  32. FLEXGlobalsRowAppClasses,
  33. FLEXGlobalsRowAppDelegate,
  34. FLEXGlobalsRowRootViewController,
  35. FLEXGlobalsRowUserDefaults,
  36. FLEXGlobalsRowMainBundle,
  37. FLEXGlobalsRowApplication,
  38. FLEXGlobalsRowKeyWindow,
  39. FLEXGlobalsRowMainScreen,
  40. FLEXGlobalsRowCurrentDevice,
  41. FLEXGlobalsRowCount
  42. };
  43. @interface FLEXGlobalsTableViewController ()
  44. @property (nonatomic, readonly) NSArray<FLEXGlobalsTableViewControllerEntry *> *entries;
  45. @end
  46. @implementation FLEXGlobalsTableViewController
  47. + (FLEXGlobalsTableViewControllerEntry *)globalsEntryForRow:(FLEXGlobalsRow)row {
  48. switch (row) {
  49. case FLEXGlobalsRowAppClasses:
  50. return [FLEXClassesTableViewController flex_concreteGlobalsEntry];
  51. case FLEXGlobalsRowAddressInspector:
  52. return [FLEXAddressExplorerCoordinator flex_concreteGlobalsEntry];
  53. case FLEXGlobalsRowSystemLibraries:
  54. return [FLEXLibrariesTableViewController flex_concreteGlobalsEntry];
  55. case FLEXGlobalsRowLiveObjects:
  56. return [FLEXLiveObjectsTableViewController flex_concreteGlobalsEntry];
  57. case FLEXGlobalsCookies:
  58. return [FLEXCookiesTableViewController flex_concreteGlobalsEntry];
  59. case FLEXGlobalsRowFileBrowser:
  60. return [FLEXFileBrowserTableViewController flex_concreteGlobalsEntry];
  61. case FLEXGlobalsRowSystemLog:
  62. return [FLEXSystemLogTableViewController flex_concreteGlobalsEntry];
  63. case FLEXGlobalsRowNetworkHistory:
  64. return [FLEXNetworkHistoryTableViewController flex_concreteGlobalsEntry];
  65. case FLEXGlobalsRowAppDelegate:
  66. return [FLEXGlobalsTableViewControllerEntry
  67. entryWithNameFuture:^NSString *{
  68. return [NSString stringWithFormat:@"👉 %@", [[UIApplication sharedApplication].delegate class]];
  69. } viewControllerFuture:^UIViewController *{
  70. id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate;
  71. return [FLEXObjectExplorerFactory explorerViewControllerForObject:appDelegate];
  72. }
  73. ];
  74. case FLEXGlobalsRowRootViewController:
  75. return [FLEXGlobalsTableViewControllerEntry
  76. entryWithNameFuture:^NSString *{
  77. return [NSString stringWithFormat:@"🌴 %@", [s_applicationWindow.rootViewController class]];
  78. } viewControllerFuture:^UIViewController *{
  79. UIViewController *rootViewController = s_applicationWindow.rootViewController;
  80. return [FLEXObjectExplorerFactory explorerViewControllerForObject:rootViewController];
  81. }
  82. ];
  83. case FLEXGlobalsRowUserDefaults:
  84. return [FLEXGlobalsTableViewControllerEntry
  85. entryWithNameFuture:^NSString *{
  86. return @"🚶 +[NSUserDefaults standardUserDefaults]";
  87. } viewControllerFuture:^UIViewController *{
  88. NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
  89. return [FLEXObjectExplorerFactory explorerViewControllerForObject:standardUserDefaults];
  90. }
  91. ];
  92. case FLEXGlobalsRowMainBundle:
  93. return [FLEXGlobalsTableViewControllerEntry
  94. entryWithNameFuture:^NSString *{
  95. return @"📦 +[NSBundle mainBundle]";
  96. } viewControllerFuture:^UIViewController *{
  97. NSBundle *mainBundle = [NSBundle mainBundle];
  98. return [FLEXObjectExplorerFactory explorerViewControllerForObject:mainBundle];
  99. }
  100. ];
  101. case FLEXGlobalsRowApplication:
  102. return [FLEXGlobalsTableViewControllerEntry
  103. entryWithNameFuture:^NSString *{
  104. return @"💾 +[UIApplication sharedApplication]";
  105. } viewControllerFuture:^UIViewController *{
  106. UIApplication *sharedApplication = [UIApplication sharedApplication];
  107. return [FLEXObjectExplorerFactory explorerViewControllerForObject:sharedApplication];
  108. }
  109. ];
  110. case FLEXGlobalsRowKeyWindow:
  111. return [FLEXGlobalsTableViewControllerEntry
  112. entryWithNameFuture:^NSString *{
  113. return @"🔑 -[UIApplication keyWindow]";
  114. } viewControllerFuture:^UIViewController *{
  115. return [FLEXObjectExplorerFactory explorerViewControllerForObject:s_applicationWindow];
  116. }
  117. ];
  118. case FLEXGlobalsRowMainScreen:
  119. return [FLEXGlobalsTableViewControllerEntry
  120. entryWithNameFuture:^NSString *{
  121. return @"💻 +[UIScreen mainScreen]";
  122. } viewControllerFuture:^UIViewController *{
  123. UIScreen *mainScreen = [UIScreen mainScreen];
  124. return [FLEXObjectExplorerFactory explorerViewControllerForObject:mainScreen];
  125. }
  126. ];
  127. case FLEXGlobalsRowCurrentDevice:
  128. return [FLEXGlobalsTableViewControllerEntry
  129. entryWithNameFuture:^NSString *{
  130. return @"📱 +[UIDevice currentDevice]";
  131. } viewControllerFuture:^UIViewController *{
  132. UIDevice *currentDevice = [UIDevice currentDevice];
  133. return [FLEXObjectExplorerFactory explorerViewControllerForObject:currentDevice];
  134. }
  135. ];
  136. default:
  137. @throw NSInternalInconsistencyException;
  138. }
  139. }
  140. + (NSArray<FLEXGlobalsTableViewControllerEntry *> *)defaultGlobalEntries
  141. {
  142. NSMutableArray<FLEXGlobalsTableViewControllerEntry *> *defaultGlobalEntries = [NSMutableArray array];
  143. for (FLEXGlobalsRow defaultRowIndex = 0; defaultRowIndex < FLEXGlobalsRowCount; defaultRowIndex++) {
  144. [defaultGlobalEntries addObject:[self globalsEntryForRow:defaultRowIndex]];
  145. }
  146. return defaultGlobalEntries;
  147. }
  148. - (id)initWithStyle:(UITableViewStyle)style
  149. {
  150. self = [super initWithStyle:style];
  151. if (self) {
  152. self.title = @"💪 FLEX";
  153. _entries = [[[self class] defaultGlobalEntries] arrayByAddingObjectsFromArray:[FLEXManager sharedManager].userGlobalEntries];
  154. }
  155. return self;
  156. }
  157. #pragma mark - Public
  158. + (void)setApplicationWindow:(UIWindow *)applicationWindow
  159. {
  160. s_applicationWindow = applicationWindow;
  161. }
  162. #pragma mark - UIViewController
  163. - (void)viewDidLoad
  164. {
  165. [super viewDidLoad];
  166. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(donePressed:)];
  167. }
  168. #pragma mark - Misc
  169. - (void)donePressed:(id)sender
  170. {
  171. [self.delegate globalsViewControllerDidFinish:self];
  172. }
  173. #pragma mark - Table Data Helpers
  174. - (FLEXGlobalsTableViewControllerEntry *)globalEntryAtIndexPath:(NSIndexPath *)indexPath
  175. {
  176. return self.entries[indexPath.row];
  177. }
  178. - (NSString *)titleForRowAtIndexPath:(NSIndexPath *)indexPath
  179. {
  180. FLEXGlobalsTableViewControllerEntry *entry = [self globalEntryAtIndexPath:indexPath];
  181. return entry.entryNameFuture();
  182. }
  183. #pragma mark - Table View Data Source
  184. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  185. {
  186. return 1;
  187. }
  188. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  189. {
  190. return [self.entries count];
  191. }
  192. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  193. {
  194. static NSString *CellIdentifier = @"Cell";
  195. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  196. if (!cell) {
  197. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  198. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  199. cell.textLabel.font = [FLEXUtility defaultFontOfSize:14.0];
  200. }
  201. cell.textLabel.text = [self titleForRowAtIndexPath:indexPath];
  202. return cell;
  203. }
  204. #pragma mark - Table View Delegate
  205. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  206. {
  207. FLEXGlobalsTableViewControllerEntry *entry = [self globalEntryAtIndexPath:indexPath];
  208. if (entry.viewControllerFuture) {
  209. [self.navigationController pushViewController:entry.viewControllerFuture() animated:YES];
  210. } else {
  211. entry.rowAction(self);
  212. }
  213. }
  214. @end