You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
5.6 KiB

  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 "FLEXObjectExplorerViewController.h"
  10. #import "FLEXArrayExplorerViewController.h"
  11. #import "FLEXSetExplorerViewController.h"
  12. #import "FLEXDictionaryExplorerViewController.h"
  13. #import "FLEXDefaultsExplorerViewController.h"
  14. #import "FLEXViewControllerExplorerViewController.h"
  15. #import "FLEXViewExplorerViewController.h"
  16. #import "FLEXImageExplorerViewController.h"
  17. #import "FLEXClassExplorerViewController.h"
  18. #import "FLEXLayerExplorerViewController.h"
  19. #import "FLEXColorExplorerViewController.h"
  20. #import "FLEXBundleExplorerViewController.h"
  21. #import <objc/runtime.h>
  22. @implementation FLEXObjectExplorerFactory
  23. + (FLEXObjectExplorerViewController *)explorerViewControllerForObject:(id)object
  24. {
  25. // Bail for nil object. We can't explore nil.
  26. if (!object) {
  27. return nil;
  28. }
  29. static NSDictionary<NSString *, Class> *explorerSubclassesForObjectTypeStrings = nil;
  30. static dispatch_once_t once;
  31. dispatch_once(&once, ^{
  32. explorerSubclassesForObjectTypeStrings = @{NSStringFromClass([NSArray class]) : [FLEXArrayExplorerViewController class],
  33. NSStringFromClass([NSSet class]) : [FLEXSetExplorerViewController class],
  34. NSStringFromClass([NSDictionary class]) : [FLEXDictionaryExplorerViewController class],
  35. NSStringFromClass([NSUserDefaults class]) : [FLEXDefaultsExplorerViewController class],
  36. NSStringFromClass([UIViewController class]) : [FLEXViewControllerExplorerViewController class],
  37. NSStringFromClass([UIView class]) : [FLEXViewExplorerViewController class],
  38. NSStringFromClass([UIImage class]) : [FLEXImageExplorerViewController class],
  39. NSStringFromClass([CALayer class]) : [FLEXLayerExplorerViewController class],
  40. NSStringFromClass([UIColor class]) : [FLEXColorExplorerViewController class],
  41. NSStringFromClass([NSBundle class]) : [FLEXBundleExplorerViewController class],
  42. };
  43. });
  44. Class explorerClass = nil;
  45. BOOL objectIsClass = class_isMetaClass(object_getClass(object));
  46. if (objectIsClass) {
  47. explorerClass = [FLEXClassExplorerViewController class];
  48. } else {
  49. explorerClass = [FLEXObjectExplorerViewController class];
  50. for (NSString *objectTypeString in explorerSubclassesForObjectTypeStrings) {
  51. Class objectClass = NSClassFromString(objectTypeString);
  52. if ([object isKindOfClass:objectClass]) {
  53. explorerClass = explorerSubclassesForObjectTypeStrings[objectTypeString];
  54. break;
  55. }
  56. }
  57. }
  58. FLEXObjectExplorerViewController *explorerViewController = [explorerClass new];
  59. explorerViewController.object = object;
  60. return explorerViewController;
  61. }
  62. #pragma mark - FLEXGlobalsEntry
  63. + (NSString *)globalsEntryTitle:(FLEXGlobalsRow)row
  64. {
  65. switch (row) {
  66. case FLEXGlobalsRowAppDelegate:
  67. return @"👉 App delegate";
  68. case FLEXGlobalsRowRootViewController:
  69. return @"🌴 Root view controller";
  70. case FLEXGlobalsRowProcessInfo:
  71. return @"🚦 NSProcessInfo.processInfo";
  72. case FLEXGlobalsRowUserDefaults:
  73. return @"💾 Preferences (NSUserDefaults)";
  74. case FLEXGlobalsRowMainBundle:
  75. return @"📦 NSBundle.mainBundle";
  76. case FLEXGlobalsRowApplication:
  77. return @"🚀 UIApplication.sharedApplication";
  78. case FLEXGlobalsRowMainScreen:
  79. return @"💻 UIScreen.mainScreen";
  80. case FLEXGlobalsRowCurrentDevice:
  81. return @"📱 UIDevice.currentDevice";
  82. case FLEXGlobalsRowPasteboard:
  83. return @"📋 UIPasteboard.generalPasteboard";
  84. default: return nil;
  85. }
  86. }
  87. + (UIViewController *)globalsEntryViewController:(FLEXGlobalsRow)row
  88. {
  89. switch (row) {
  90. case FLEXGlobalsRowAppDelegate: {
  91. id<UIApplicationDelegate> appDelegate = UIApplication.sharedApplication.delegate;
  92. return [self explorerViewControllerForObject:appDelegate];
  93. }
  94. case FLEXGlobalsRowProcessInfo:
  95. return [self explorerViewControllerForObject:NSProcessInfo.processInfo];
  96. case FLEXGlobalsRowUserDefaults:
  97. return [self explorerViewControllerForObject:NSUserDefaults.standardUserDefaults];
  98. case FLEXGlobalsRowMainBundle:
  99. return [self explorerViewControllerForObject:NSBundle.mainBundle];
  100. case FLEXGlobalsRowApplication:
  101. return [self explorerViewControllerForObject:UIApplication.sharedApplication];
  102. case FLEXGlobalsRowMainScreen:
  103. return [self explorerViewControllerForObject:UIScreen.mainScreen];
  104. case FLEXGlobalsRowCurrentDevice:
  105. return [self explorerViewControllerForObject:UIDevice.currentDevice];
  106. case FLEXGlobalsRowPasteboard:
  107. return [self explorerViewControllerForObject:UIPasteboard.generalPasteboard];
  108. case FLEXGlobalsRowRootViewController:
  109. return [self explorerViewControllerForObject:UIApplication.sharedApplication.delegate.window.rootViewController];
  110. default: return nil;
  111. }
  112. }
  113. @end