From 5e7d88f3fadf99dbadbcd8ee24ae2436b2d12e9e Mon Sep 17 00:00:00 2001 From: Nicola Corti Date: Wed, 8 Dec 2021 16:57:29 +0000 Subject: [PATCH] [Fabric] Add a MainComponentsRegistry for the application --- .../components/MainComponentsRegistry.java | 28 +++++++++ .../src/main/jni/MainComponentsRegistry.cpp | 62 +++++++++++++++++++ .../app/src/main/jni/MainComponentsRegistry.h | 35 +++++++++++ android/app/src/main/jni/OnLoad.cpp | 2 + 4 files changed, 127 insertions(+) create mode 100644 android/app/src/main/java/com/rnnewarchitectureapp/components/MainComponentsRegistry.java create mode 100644 android/app/src/main/jni/MainComponentsRegistry.cpp create mode 100644 android/app/src/main/jni/MainComponentsRegistry.h diff --git a/android/app/src/main/java/com/rnnewarchitectureapp/components/MainComponentsRegistry.java b/android/app/src/main/java/com/rnnewarchitectureapp/components/MainComponentsRegistry.java new file mode 100644 index 00000000..61593d25 --- /dev/null +++ b/android/app/src/main/java/com/rnnewarchitectureapp/components/MainComponentsRegistry.java @@ -0,0 +1,28 @@ +package com.rnnewarchitectureapp.components; + +import com.facebook.common.internal.DoNotStrip; +import com.facebook.jni.HybridData; +import com.facebook.react.fabric.ComponentFactory; +import com.facebook.soloader.SoLoader; + +@DoNotStrip +public class MainComponentsRegistry { + static { + SoLoader.loadLibrary("fabricjni"); + } + + @DoNotStrip private final HybridData mHybridData; + + @DoNotStrip + private native HybridData initHybrid(ComponentFactory componentFactory); + + @DoNotStrip + private MainComponentsRegistry(ComponentFactory componentFactory) { + mHybridData = initHybrid(componentFactory); + } + + @DoNotStrip + public static MainComponentsRegistry register(ComponentFactory componentFactory) { + return new MainComponentsRegistry(componentFactory); + } +} diff --git a/android/app/src/main/jni/MainComponentsRegistry.cpp b/android/app/src/main/jni/MainComponentsRegistry.cpp new file mode 100644 index 00000000..7425ed99 --- /dev/null +++ b/android/app/src/main/jni/MainComponentsRegistry.cpp @@ -0,0 +1,62 @@ +#include "MainComponentsRegistry.h" + +#include +#include +#include +#include +#include + +namespace facebook { + namespace react { + + MainComponentsRegistry::MainComponentsRegistry( + ComponentFactory *delegate) + : delegate_(delegate) {} + + std::shared_ptr + MainComponentsRegistry::sharedProviderRegistry() { + auto providerRegistry = CoreComponentsRegistry::sharedProviderRegistry(); + + providerRegistry->add(concreteComponentDescriptorProvider< + AnswerViewerComponentDescriptor>()); + + return providerRegistry; + } + + jni::local_ref + MainComponentsRegistry::initHybrid( + jni::alias_ref, + ComponentFactory *delegate) { + auto instance = makeCxxInstance(delegate); + + auto buildRegistryFunction = + [](EventDispatcher::Weak const &eventDispatcher, + ContextContainer::Shared const &contextContainer) + -> ComponentDescriptorRegistry::Shared { + auto registry = MainComponentsRegistry::sharedProviderRegistry() + ->createComponentDescriptorRegistry( + {eventDispatcher, contextContainer}); + + auto mutableRegistry = + std::const_pointer_cast(registry); + + mutableRegistry->setFallbackComponentDescriptor( + std::make_shared( + ComponentDescriptorParameters{ + eventDispatcher, contextContainer, nullptr})); + + return registry; + }; + + delegate->buildRegistryFunction = buildRegistryFunction; + return instance; + } + + void MainComponentsRegistry::registerNatives() { + registerHybrid({ + makeNativeMethod("initHybrid", MainComponentsRegistry::initHybrid), + }); + } + + } // namespace react +} // namespace facebook \ No newline at end of file diff --git a/android/app/src/main/jni/MainComponentsRegistry.h b/android/app/src/main/jni/MainComponentsRegistry.h new file mode 100644 index 00000000..76c41142 --- /dev/null +++ b/android/app/src/main/jni/MainComponentsRegistry.h @@ -0,0 +1,35 @@ +#pragma once + +#include +#include +#include +#include + +namespace facebook { + namespace react { + + class MainComponentsRegistry + : public facebook::jni::HybridClass { + public: + constexpr static auto kJavaDescriptor = + "Lcom/rnnewarchitectureapp/components/MainComponentsRegistry;"; + + static void registerNatives(); + + MainComponentsRegistry(ComponentFactory *delegate); + + private: + friend HybridBase; + + static std::shared_ptr + sharedProviderRegistry(); + + const ComponentFactory *delegate_; + + static jni::local_ref initHybrid( + jni::alias_ref, + ComponentFactory *delegate); + }; + + } // namespace react +} // namespace facebook \ No newline at end of file diff --git a/android/app/src/main/jni/OnLoad.cpp b/android/app/src/main/jni/OnLoad.cpp index 103cd744..6dd35674 100644 --- a/android/app/src/main/jni/OnLoad.cpp +++ b/android/app/src/main/jni/OnLoad.cpp @@ -1,8 +1,10 @@ #include #include "MainApplicationTurboModuleManagerDelegate.h" +#include "MainComponentsRegistry.h" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *) { return facebook::jni::initialize(vm, [] { facebook::react::MainApplicationTurboModuleManagerDelegate::registerNatives(); + facebook::react::MainComponentsRegistry::registerNatives(); }); } \ No newline at end of file