12dd697a6SWill Schurman package host.exp.exponent.experience
22dd697a6SWill Schurman 
32dd697a6SWill Schurman import com.facebook.react.bridge.NativeModule
42dd697a6SWill Schurman import com.facebook.react.bridge.ReactApplicationContext
52dd697a6SWill Schurman import expo.modules.adapters.react.ReactModuleRegistryProvider
62dd697a6SWill Schurman import expo.modules.core.ModuleRegistry
72dd697a6SWill Schurman import expo.modules.core.interfaces.RegistryLifecycleListener
850661f5cSWill Schurman import expo.modules.manifests.core.Manifest
9*229dec0dSŁukasz Kosmaty import host.exp.exponent.kernel.ExperienceKey
10*229dec0dSŁukasz Kosmaty import host.exp.exponent.utils.ScopedContext
11*229dec0dSŁukasz Kosmaty import versioned.host.exp.exponent.modules.universal.ConstantsBinding
12*229dec0dSŁukasz Kosmaty import versioned.host.exp.exponent.modules.universal.ExpoModuleRegistryAdapter
13*229dec0dSŁukasz Kosmaty import versioned.host.exp.exponent.modules.universal.ScopedUIManagerModuleWrapper
14*229dec0dSŁukasz Kosmaty import versioned.host.exp.exponent.modules.universal.UpdatesBinding
152dd697a6SWill Schurman 
162dd697a6SWill Schurman open class DetachedModuleRegistryAdapter(moduleRegistryProvider: ReactModuleRegistryProvider) :
172dd697a6SWill Schurman   ExpoModuleRegistryAdapter(moduleRegistryProvider) {
182dd697a6SWill Schurman 
createNativeModulesnull192dd697a6SWill Schurman   override fun createNativeModules(
202dd697a6SWill Schurman     scopedContext: ScopedContext,
212dd697a6SWill Schurman     experienceKey: ExperienceKey,
22280cec8aSWill Schurman     experienceProperties: Map<String, Any?>,
2350661f5cSWill Schurman     manifest: Manifest,
242dd697a6SWill Schurman     otherModules: List<NativeModule>
252dd697a6SWill Schurman   ): List<NativeModule> {
262dd697a6SWill Schurman     val reactApplicationContext = scopedContext.context as ReactApplicationContext
272dd697a6SWill Schurman 
282dd697a6SWill Schurman     // We only use React application context, because we're detached -- no scopes
29fa5bb8bdSKudo Chien     val moduleRegistry = mModuleRegistryProvider[reactApplicationContext]
302dd697a6SWill Schurman 
312dd697a6SWill Schurman     moduleRegistry.registerInternalModule(
322dd697a6SWill Schurman       ConstantsBinding(
332dd697a6SWill Schurman         scopedContext,
342dd697a6SWill Schurman         experienceProperties,
352dd697a6SWill Schurman         manifest
362dd697a6SWill Schurman       )
372dd697a6SWill Schurman     )
382dd697a6SWill Schurman 
392dd697a6SWill Schurman     // Overriding expo-updates UpdatesService
402dd697a6SWill Schurman     moduleRegistry.registerInternalModule(UpdatesBinding(scopedContext, experienceProperties))
412dd697a6SWill Schurman 
422dd697a6SWill Schurman     // ReactAdapterPackage requires ReactContext
432dd697a6SWill Schurman     val reactContext = scopedContext.context as ReactApplicationContext
442dd697a6SWill Schurman     for (internalModule in mReactAdapterPackage.createInternalModules(reactContext)) {
452dd697a6SWill Schurman       moduleRegistry.registerInternalModule(internalModule)
462dd697a6SWill Schurman     }
472dd697a6SWill Schurman 
482dd697a6SWill Schurman     // Overriding ScopedUIManagerModuleWrapper from ReactAdapterPackage
492dd697a6SWill Schurman     moduleRegistry.registerInternalModule(ScopedUIManagerModuleWrapper(reactContext))
502dd697a6SWill Schurman 
512dd697a6SWill Schurman     // Adding other modules (not universal) to module registry as consumers.
522dd697a6SWill Schurman     // It allows these modules to refer to universal modules.
532dd697a6SWill Schurman     for (otherModule in otherModules) {
542dd697a6SWill Schurman       if (otherModule is RegistryLifecycleListener) {
552dd697a6SWill Schurman         moduleRegistry.registerExtraListener(otherModule as RegistryLifecycleListener)
562dd697a6SWill Schurman       }
572dd697a6SWill Schurman     }
582dd697a6SWill Schurman     configureModuleRegistry(moduleRegistry, reactApplicationContext)
59fcc3d068SŁukasz Kosmaty     return getNativeModulesFromModuleRegistry(reactApplicationContext, moduleRegistry, null)
602dd697a6SWill Schurman   }
612dd697a6SWill Schurman 
configureModuleRegistrynull622dd697a6SWill Schurman   protected open fun configureModuleRegistry(
632dd697a6SWill Schurman     moduleRegistry: ModuleRegistry,
642dd697a6SWill Schurman     reactContext: ReactApplicationContext
652dd697a6SWill Schurman   ) {
662dd697a6SWill Schurman     // Subclasses may add more modules here.
672dd697a6SWill Schurman   }
682dd697a6SWill Schurman }
69