1 import UIKit
2 import ObjectiveC
3 
4 /**
5  A protocol that helps in identifying whether the instance of `ViewModuleWrapper` is of a dynamically created class.
6  */
7 @objc
8 protocol DynamicModuleWrapperProtocol {
9   @objc
10   optional func wrappedModule() -> ViewModuleWrapper
11 }
12 
13 /**
14  Each module that has a view manager definition needs to be wrapped by `RCTViewManager`.
15  Unfortunately, we can't use just one class because React Native checks for duplicated classes.
16  We're generating its subclasses in runtime as a workaround.
17  */
18 @objc
19 public class ViewModuleWrapper: RCTViewManager, DynamicModuleWrapperProtocol {
20   /**
21    A reference to the module holder that stores the module definition.
22    Enforced unwrapping is required since it can be set right after the object is initialized.
23    */
24   var wrappedModuleHolder: ModuleHolder!
25 
26   /**
27    The designated initializer. At first, we use this base class to hide `ModuleHolder` from Objective-C runtime.
28    */
29   public init(_ wrappedModuleHolder: ModuleHolder) {
30     self.wrappedModuleHolder = wrappedModuleHolder
31   }
32 
33   /**
34    The designated initializer that is used by React Native to create module instances.
35    Must be called on a dynamic class to get access to underlying wrapped module. Throws fatal exception otherwise.
36    */
37   @objc
38   public override init() {
39     super.init()
40     guard let module = (self as DynamicModuleWrapperProtocol).wrappedModule?() else {
41       fatalError("Something unexpected has happened. Only dynamically created `ViewModuleWrapper` can be initialized without params.")
42     }
43     self.wrappedModuleHolder = module.wrappedModuleHolder
44   }
45 
46   /**
47    Dummy initializer, for use only in `EXModuleRegistryAdapter.extraModulesForModuleRegistry:`.
48    */
49   @objc
50   public init(dummy: Any?) {
51     super.init()
52   }
53 
54   /**
55    Returns the original name of the wrapped module.
56    */
57   @objc
58   public func name() -> String {
59     return wrappedModuleHolder.name
60   }
61 
62   /**
63    Static function that returns the class name, but keep in mind that dynamic wrappers
64    have custom class name (see `objc_allocateClassPair` invocation in `createViewModuleWrapperClass`).
65    */
66   @objc
67   public override class func moduleName() -> String {
68     return NSStringFromClass(Self.self)
69   }
70 
71   /**
72    The view manager wrapper doesn't require main queue setup — it doesn't call any UI-related stuff on `init`.
73    Also, lazy-loaded modules must return false here.
74    */
75   @objc
76   public override class func requiresMainQueueSetup() -> Bool {
77     return false
78   }
79 
80   /**
81    Creates a view from the wrapped module.
82    */
83   @objc
84   public override func view() -> UIView! {
85     guard let view = wrappedModuleHolder.definition.viewManager?.createView() else {
86       fatalError("Module `\(wrappedModuleHolder.name)` doesn't define the view manager nor view factory.")
87     }
88     return view
89   }
90 
91   /**
92    The config for `proxiedProperties` prop. In Objective-C style, this function is generated by `RCT_CUSTOM_VIEW_PROPERTY` macro.
93    */
94   @objc
95   public class func propConfig_proxiedProperties() -> [String] {
96     return ["NSDictionary", "__custom__"];
97   }
98 
99   /**
100    The setter for `proxiedProperties` prop. In Objective-C style, this function is generated by `RCT_CUSTOM_VIEW_PROPERTY` macro.
101    */
102   @objc
103   public func set_proxiedProperties(_ json: Any?, forView view: UIView, withDefaultView defaultView: UIView) {
104     guard let json = json as? [String: Any?],
105           let props = wrappedModuleHolder.definition.viewManager?.propsDict() else {
106       return
107     }
108     for (key, value) in json {
109       if let prop = props[key] {
110         prop.set(value: value, onView: view)
111       }
112     }
113   }
114 
115   /**
116    Creates a subclass of `ViewModuleWrapper` in runtime. The new class overrides `moduleName` stub.
117    */
118   @objc
119   public static func createViewModuleWrapperClass(module: ViewModuleWrapper) -> ViewModuleWrapper.Type? {
120     // We're namespacing the view name so we know it uses our architecture.
121     let prefixedViewName = "ViewManagerAdapter_\(module.name())"
122 
123     return prefixedViewName.withCString { viewNamePtr in
124       // Create a new meta class that inherits from `ViewModuleWrapper`. The class name passed here, doesn't work for Swift classes,
125       // so we also have to override `moduleName` class method.
126       let wrapperClass: AnyClass? = objc_allocateClassPair(ViewModuleWrapper.self, viewNamePtr, 0)
127 
128       // Dynamically add instance method returning wrapped module to the dynamic wrapper class.
129       // React Native initializes modules with `init` without params,
130       // so there is no other way to pass it to the instances.
131       let wrappedModuleBlock: @convention(block) () -> ViewModuleWrapper = { module }
132       let wrappedModuleImp: IMP = imp_implementationWithBlock(wrappedModuleBlock)
133       class_addMethod(wrapperClass, Selector("wrappedModule"), wrappedModuleImp, "@@:")
134 
135       return wrapperClass as? ViewModuleWrapper.Type
136     }
137   }
138 }
139