FLEXGlobalsTableViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 "FLEXLibrariesTableViewController.h"
  11. #import "FLEXClassesTableViewController.h"
  12. #import "FLEXObjectExplorerViewController.h"
  13. #import "FLEXObjectExplorerFactory.h"
  14. #import "FLEXLiveObjectsTableViewController.h"
  15. #import "FLEXFileBrowserTableViewController.h"
  16. typedef NSString *(^FLEXGlobalsTableViewControllerEntryNameFuture)(void);
  17. typedef UIViewController *(^FLEXGlobalsTableViewControllerViewControllerFuture)(void);
  18. static __weak UIWindow *s_applicationWindow = nil;
  19. static NSMutableArray *s_globalEntries = nil;
  20. @interface FLEXGlobalsTableViewControllerEntry : NSObject
  21. @property (nonatomic, readonly, copy) FLEXGlobalsTableViewControllerEntryNameFuture entryName;
  22. @property (nonatomic, readonly, copy) FLEXGlobalsTableViewControllerViewControllerFuture viewControllerToPush;
  23. + (instancetype)entryWithName:(FLEXGlobalsTableViewControllerEntryNameFuture)name viewControllerToPush:(FLEXGlobalsTableViewControllerViewControllerFuture)viewControllerToPush;
  24. @end
  25. @implementation FLEXGlobalsTableViewControllerEntry
  26. + (instancetype)entryWithName:(FLEXGlobalsTableViewControllerEntryNameFuture)name viewControllerToPush:(FLEXGlobalsTableViewControllerViewControllerFuture)viewControllerToPush
  27. {
  28. NSParameterAssert(name);
  29. NSParameterAssert(viewControllerToPush);
  30. FLEXGlobalsTableViewControllerEntry *entry = [[self alloc] init];
  31. entry->_entryName = [name copy];
  32. entry->_viewControllerToPush = [viewControllerToPush copy];
  33. return entry;
  34. }
  35. @end
  36. typedef NS_ENUM(NSUInteger, FLEXGlobalsRow) {
  37. FLEXGlobalsRowAppClasses,
  38. FLEXGlobalsRowSystemLibraries,
  39. FLEXGlobalsRowLiveObjects,
  40. FLEXGlobalsRowAppDelegate,
  41. FLEXGlobalsRowRootViewController,
  42. FLEXGlobalsRowUserDefaults,
  43. FLEXGlobalsRowApplication,
  44. FLEXGlobalsRowKeyWindow,
  45. FLEXGlobalsRowMainScreen,
  46. FLEXGlobalsRowCurrentDevice,
  47. FLEXGlobalsRowFileBrowser,
  48. FLEXGlobalsRowCount
  49. };
  50. @interface FLEXGlobalsTableViewController ()
  51. /// [FLEXGlobalsTableViewControllerEntry *]
  52. @property (nonatomic, readonly, copy) NSArray *entries;
  53. @end
  54. @implementation FLEXGlobalsTableViewController
  55. + (void)initialize
  56. {
  57. if (self == [FLEXGlobalsTableViewController class]) {
  58. [self initializeStandardGlobalEntries];
  59. }
  60. }
  61. + (void)initializeStandardGlobalEntries {
  62. s_globalEntries = [NSMutableArray array];
  63. for (FLEXGlobalsRow defaultRowIndex = 0; defaultRowIndex < FLEXGlobalsRowCount; defaultRowIndex++) {
  64. FLEXGlobalsTableViewControllerEntryNameFuture title = nil;
  65. FLEXGlobalsTableViewControllerViewControllerFuture viewControllerFuture = nil;
  66. switch (defaultRowIndex) {
  67. case FLEXGlobalsRowAppClasses:
  68. title = ^NSString *{
  69. return [NSString stringWithFormat:@"📕 %@ Classes", [FLEXUtility applicationName]];;
  70. };
  71. viewControllerFuture = ^UIViewController *{
  72. FLEXClassesTableViewController *classesViewController = [[FLEXClassesTableViewController alloc] init];
  73. classesViewController.binaryImageName = [FLEXUtility applicationImageName];
  74. return classesViewController;
  75. };
  76. break;
  77. case FLEXGlobalsRowSystemLibraries: {
  78. NSString *titleString = @"📚 System Libraries";
  79. title = ^NSString *{
  80. return titleString;
  81. };
  82. viewControllerFuture = ^UIViewController *{
  83. FLEXLibrariesTableViewController *librariesViewController = [[FLEXLibrariesTableViewController alloc] init];
  84. librariesViewController.title = titleString;
  85. return librariesViewController;
  86. };
  87. break;
  88. }
  89. case FLEXGlobalsRowLiveObjects:
  90. title = ^NSString *{
  91. return @"💩 Heap Objects";
  92. };
  93. viewControllerFuture = ^UIViewController *{
  94. return [[FLEXLiveObjectsTableViewController alloc] init];
  95. };
  96. break;
  97. case FLEXGlobalsRowAppDelegate:
  98. title = ^NSString *{
  99. return [NSString stringWithFormat:@"👉 %@", [[[UIApplication sharedApplication] delegate] class]];
  100. };
  101. viewControllerFuture = ^UIViewController *{
  102. id <UIApplicationDelegate> appDelegate = [[UIApplication sharedApplication] delegate];
  103. return [FLEXObjectExplorerFactory explorerViewControllerForObject:appDelegate];
  104. };
  105. break;
  106. case FLEXGlobalsRowRootViewController:
  107. title = ^NSString *{
  108. return [NSString stringWithFormat:@"🌴 %@", [[s_applicationWindow rootViewController] class]];
  109. };
  110. viewControllerFuture = ^UIViewController *{
  111. UIViewController *rootViewController = [s_applicationWindow rootViewController];
  112. return [FLEXObjectExplorerFactory explorerViewControllerForObject:rootViewController];
  113. };
  114. break;
  115. case FLEXGlobalsRowUserDefaults:
  116. title = ^NSString *{
  117. return @"🚶 +[NSUserDefaults standardUserDefaults]";
  118. };
  119. viewControllerFuture = ^UIViewController *{
  120. NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
  121. return [FLEXObjectExplorerFactory explorerViewControllerForObject:standardUserDefaults];
  122. };
  123. break;
  124. case FLEXGlobalsRowApplication:
  125. title = ^NSString *{
  126. return @"💾 +[UIApplication sharedApplication]";
  127. };
  128. viewControllerFuture = ^UIViewController *{
  129. UIApplication *sharedApplication = [UIApplication sharedApplication];
  130. return [FLEXObjectExplorerFactory explorerViewControllerForObject:sharedApplication];
  131. };
  132. break;
  133. case FLEXGlobalsRowKeyWindow:
  134. title = ^NSString *{
  135. return @"🔑 -[UIApplication keyWindow]";
  136. };
  137. viewControllerFuture = ^UIViewController *{
  138. return [FLEXObjectExplorerFactory explorerViewControllerForObject:s_applicationWindow];
  139. };
  140. break;
  141. case FLEXGlobalsRowMainScreen:
  142. title = ^NSString *{
  143. return @"💻 +[UIScreen mainScreen]";
  144. };
  145. viewControllerFuture = ^UIViewController *{
  146. UIScreen *mainScreen = [UIScreen mainScreen];
  147. return [FLEXObjectExplorerFactory explorerViewControllerForObject:mainScreen];
  148. };
  149. break;
  150. case FLEXGlobalsRowCurrentDevice:
  151. title = ^NSString *{
  152. return @"📱 +[UIDevice currentDevice]";
  153. };
  154. viewControllerFuture = ^UIViewController *{
  155. UIDevice *currentDevice = [UIDevice currentDevice];
  156. return [FLEXObjectExplorerFactory explorerViewControllerForObject:currentDevice];
  157. };
  158. break;
  159. case FLEXGlobalsRowFileBrowser:
  160. title = ^NSString *{
  161. return @"📁 File Browser";
  162. };
  163. viewControllerFuture = ^UIViewController *{
  164. return [[FLEXFileBrowserTableViewController alloc] init];
  165. };
  166. break;
  167. case FLEXGlobalsRowCount:
  168. break;
  169. }
  170. NSParameterAssert(title);
  171. NSParameterAssert(viewControllerFuture);
  172. [s_globalEntries addObject:[FLEXGlobalsTableViewControllerEntry entryWithName:title
  173. viewControllerToPush:viewControllerFuture]];
  174. }
  175. }
  176. - (id)initWithStyle:(UITableViewStyle)style
  177. {
  178. self = [super initWithStyle:style];
  179. if (self) {
  180. self.title = @"🌎 Global State";
  181. _entries = [s_globalEntries copy];
  182. }
  183. return self;
  184. }
  185. #pragma mark - Public
  186. + (void)setApplicationWindow:(UIWindow *)applicationWindow
  187. {
  188. s_applicationWindow = applicationWindow;
  189. }
  190. + (void)registerGlobalEntryWithName:(NSString *)entryName objectFutureBlock:(id (^)(void))objectFutureBlock
  191. {
  192. NSParameterAssert(entryName);
  193. NSParameterAssert(objectFutureBlock);
  194. NSAssert([NSThread isMainThread], @"This method must be called from the main thread.");
  195. entryName = entryName.copy;
  196. FLEXGlobalsTableViewControllerEntry *entry = [FLEXGlobalsTableViewControllerEntry entryWithName:^NSString *{
  197. return entryName;
  198. }
  199. viewControllerToPush:^UIViewController *
  200. {
  201. return [FLEXObjectExplorerFactory explorerViewControllerForObject:objectFutureBlock()];
  202. }];
  203. [s_globalEntries addObject:entry];
  204. }
  205. #pragma mark - UIViewController
  206. - (void)viewDidLoad
  207. {
  208. [super viewDidLoad];
  209. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(donePressed:)];
  210. }
  211. #pragma mark -
  212. - (void)donePressed:(id)sender
  213. {
  214. [self.delegate globalsViewControllerDidFinish:self];
  215. }
  216. #pragma mark - Table Data Helpers
  217. - (FLEXGlobalsTableViewControllerEntry *)globalEntryAtIndexPath:(NSIndexPath *)indexPath
  218. {
  219. return self.entries[indexPath.row];
  220. }
  221. - (NSString *)titleForRowAtIndexPath:(NSIndexPath *)indexPath
  222. {
  223. FLEXGlobalsTableViewControllerEntry *entry = [self globalEntryAtIndexPath:indexPath];
  224. return entry.entryName();
  225. }
  226. - (UIViewController *)viewControllerToPushForRowAtIndexPath:(NSIndexPath *)indexPath
  227. {
  228. FLEXGlobalsTableViewControllerEntry *entry = [self globalEntryAtIndexPath:indexPath];
  229. return entry.viewControllerToPush();
  230. }
  231. #pragma mark - Table View Data Source
  232. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  233. {
  234. return 1;
  235. }
  236. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  237. {
  238. return [self.entries count];
  239. }
  240. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  241. {
  242. static NSString *CellIdentifier = @"Cell";
  243. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  244. if (!cell) {
  245. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  246. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  247. cell.textLabel.font = [FLEXUtility defaultTableViewCellLabelFont];
  248. }
  249. cell.textLabel.text = [self titleForRowAtIndexPath:indexPath];
  250. return cell;
  251. }
  252. #pragma mark - Table View Delegate
  253. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  254. {
  255. UIViewController *viewControllerToPush = [self viewControllerToPushForRowAtIndexPath:indexPath];
  256. [self.navigationController pushViewController:viewControllerToPush animated:YES];
  257. }
  258. @end