FLEXObjectExplorerFactory.m 6.3 KB

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