FLEXObjectExplorerFactory.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // FLEXObjectExplorerFactory.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 5/15/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXObjectExplorerFactory.h"
  9. #import "FLEXGlobalsTableViewController.h"
  10. #import "FLEXAlert.h"
  11. #import "FLEXClassShortcuts.h"
  12. #import "FLEXViewShortcuts.h"
  13. #import "FLEXViewControllerShortcuts.h"
  14. #import "FLEXImageShortcuts.h"
  15. #import "FLEXLayerShortcuts.h"
  16. #import "FLEXColorPreviewSection.h"
  17. #import "FLEXDefaultsContentSection.h"
  18. #import "FLEXBundleShortcuts.h"
  19. #import "FLEXBlockShortcuts.h"
  20. #import <objc/runtime.h>
  21. @implementation FLEXObjectExplorerFactory
  22. static NSMutableDictionary<Class, Class> *classesToRegisteredSections = nil;
  23. + (void)initialize
  24. {
  25. if (self == [FLEXObjectExplorerFactory class]) {
  26. #define ClassKey(name) (Class<NSCopying>)[name class]
  27. #define ClassKeyByName(str) (Class<NSCopying>)NSClassFromString(@ #str)
  28. classesToRegisteredSections = [NSMutableDictionary dictionaryWithDictionary:@{
  29. ClassKey(NSArray) : [FLEXCollectionContentSection class],
  30. ClassKey(NSSet) : [FLEXCollectionContentSection class],
  31. ClassKey(NSDictionary) : [FLEXCollectionContentSection class],
  32. ClassKey(NSUserDefaults) : [FLEXDefaultsContentSection class],
  33. ClassKey(UIViewController) : [FLEXViewControllerShortcuts class],
  34. ClassKey(UIView) : [FLEXViewShortcuts class],
  35. ClassKey(UIImage) : [FLEXImageShortcuts class],
  36. ClassKey(CALayer) : [FLEXLayerShortcuts class],
  37. ClassKey(UIColor) : [FLEXColorPreviewSection class],
  38. ClassKey(NSBundle) : [FLEXBundleShortcuts class],
  39. ClassKeyByName(NSBlock) : [FLEXBlockShortcuts class],
  40. }];
  41. #undef ClassKey
  42. #undef ClassKeyByName
  43. }
  44. }
  45. + (FLEXObjectExplorerViewController *)explorerViewControllerForObject:(id)object
  46. {
  47. // Can't explore nil
  48. if (!object) {
  49. return nil;
  50. }
  51. // If we're given an object, this will look up it's class hierarchy
  52. // until it finds a registration. This will work for KVC classes,
  53. // since they are children of the original class, and not siblings.
  54. // If we are given an object, object_getClass will return a metaclass,
  55. // and the same thing will happen. FLEXClassShortcuts is the default
  56. // shortcut section for NSObject.
  57. //
  58. // TODO: rename it to FLEXNSObjectShortcuts or something?
  59. Class sectionClass = nil;
  60. Class cls = object_getClass(object);
  61. do {
  62. sectionClass = classesToRegisteredSections[(Class<NSCopying>)cls];
  63. } while (!sectionClass && (cls = [cls superclass]));
  64. if (!sectionClass) {
  65. sectionClass = [FLEXShortcutsSection class];
  66. }
  67. return [FLEXObjectExplorerViewController
  68. exploringObject:object
  69. customSection:[sectionClass forObject:object]
  70. ];
  71. }
  72. + (void)registerExplorerSection:(Class)explorerClass forClass:(Class)objectClass
  73. {
  74. classesToRegisteredSections[(Class<NSCopying>)objectClass] = explorerClass;
  75. }
  76. #pragma mark - FLEXGlobalsEntry
  77. + (NSString *)globalsEntryTitle:(FLEXGlobalsRow)row
  78. {
  79. switch (row) {
  80. case FLEXGlobalsRowAppDelegate:
  81. return @"👉 App delegate";
  82. case FLEXGlobalsRowRootViewController:
  83. return @"🌴 Root view controller";
  84. case FLEXGlobalsRowProcessInfo:
  85. return @"🚦 NSProcessInfo.processInfo";
  86. case FLEXGlobalsRowUserDefaults:
  87. return @"💾 Preferences (NSUserDefaults)";
  88. case FLEXGlobalsRowMainBundle:
  89. return @"📦 NSBundle.mainBundle";
  90. case FLEXGlobalsRowApplication:
  91. return @"🚀 UIApplication.sharedApplication";
  92. case FLEXGlobalsRowMainScreen:
  93. return @"💻 UIScreen.mainScreen";
  94. case FLEXGlobalsRowCurrentDevice:
  95. return @"📱 UIDevice.currentDevice";
  96. case FLEXGlobalsRowPasteboard:
  97. return @"📋 UIPasteboard.generalPasteboard";
  98. default: return nil;
  99. }
  100. }
  101. + (UIViewController *)globalsEntryViewController:(FLEXGlobalsRow)row
  102. {
  103. switch (row) {
  104. case FLEXGlobalsRowAppDelegate: {
  105. id<UIApplicationDelegate> appDelegate = UIApplication.sharedApplication.delegate;
  106. return [self explorerViewControllerForObject:appDelegate];
  107. }
  108. case FLEXGlobalsRowProcessInfo:
  109. return [self explorerViewControllerForObject:NSProcessInfo.processInfo];
  110. case FLEXGlobalsRowUserDefaults:
  111. return [self explorerViewControllerForObject:NSUserDefaults.standardUserDefaults];
  112. case FLEXGlobalsRowMainBundle:
  113. return [self explorerViewControllerForObject:NSBundle.mainBundle];
  114. case FLEXGlobalsRowApplication:
  115. return [self explorerViewControllerForObject:UIApplication.sharedApplication];
  116. case FLEXGlobalsRowMainScreen:
  117. return [self explorerViewControllerForObject:UIScreen.mainScreen];
  118. case FLEXGlobalsRowCurrentDevice:
  119. return [self explorerViewControllerForObject:UIDevice.currentDevice];
  120. case FLEXGlobalsRowPasteboard:
  121. return [self explorerViewControllerForObject:UIPasteboard.generalPasteboard];
  122. case FLEXGlobalsRowRootViewController: {
  123. id<UIApplicationDelegate> delegate = UIApplication.sharedApplication.delegate;
  124. if ([delegate respondsToSelector:@selector(window)]) {
  125. return [self explorerViewControllerForObject:delegate.window.rootViewController];
  126. }
  127. return nil;
  128. }
  129. default: return nil;
  130. }
  131. }
  132. + (FLEXGlobalsTableViewControllerRowAction)globalsEntryRowAction:(FLEXGlobalsRow)row
  133. {
  134. switch (row) {
  135. case FLEXGlobalsRowRootViewController: {
  136. // Check if the app delegate responds to -window. If not, present an alert
  137. return ^(FLEXGlobalsTableViewController *host) {
  138. id<UIApplicationDelegate> delegate = UIApplication.sharedApplication.delegate;
  139. if ([delegate respondsToSelector:@selector(window)]) {
  140. UIViewController *explorer = [self explorerViewControllerForObject:
  141. delegate.window.rootViewController
  142. ];
  143. [host.navigationController pushViewController:explorer animated:YES];
  144. } else {
  145. NSString *msg = @"The app delegate doesn't respond to -window";
  146. [FLEXAlert showAlert:@":(" message:msg from:host];
  147. }
  148. };
  149. }
  150. default: return nil;
  151. }
  152. }
  153. @end