Add ComponentWithState in Android (#34911)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/34911

This diff adds the Same component with state added in the previous diff for iOS

## Changelog
[Android][Added] - ComponentWithState

Reviewed By: cortinico

Differential Revision: D40108233

fbshipit-source-id: b5bd1d1bdd7053920f737772c85034e4c5aed26a
This commit is contained in:
Riccardo Cipolleschi 2022-10-10 02:51:06 -07:00 коммит произвёл Facebook GitHub Bot
Родитель 1a9cceb20b
Коммит b24f60f729
10 изменённых файлов: 112 добавлений и 2 удалений

Просмотреть файл

@ -23,7 +23,7 @@ Pod::Spec.new do |s|
"CLANG_CXX_LANGUAGE_STANDARD" => "c++17"
}
s.source_files = "ios/**/*.{h,m,mm,cpp}"
s.source_files = "{ios,cxx}/**/*.{h,m,mm,cpp}"
s.requires_arc = true
install_modules_dependencies(s)

Просмотреть файл

@ -23,9 +23,11 @@ import com.facebook.react.module.model.ReactModuleInfo;
import com.facebook.react.module.model.ReactModuleInfoProvider;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.react.uiapp.component.MyNativeViewManager;
import com.facebook.react.uiapp.component.NativeViewWithStateManager;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.views.text.ReactFontManager;
import com.facebook.soloader.SoLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
@ -106,7 +108,10 @@ public class RNTesterApplication extends Application implements ReactApplication
@Override
public List<ViewManager> createViewManagers(
@NonNull ReactApplicationContext reactContext) {
return Collections.singletonList(new MyNativeViewManager());
ArrayList<ViewManager> list = new ArrayList();
list.add(new MyNativeViewManager());
list.add(new NativeViewWithStateManager());
return list;
}
});
}

Просмотреть файл

@ -0,0 +1,27 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.uiapp.component;
import android.content.Context;
import android.net.Uri;
import androidx.annotation.Nullable;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.react.bridge.ReadableMap;
class NativeViewWithState extends SimpleDraweeView {
public NativeViewWithState(Context context) {
super(context);
}
void setImageSource(@Nullable ReadableMap source) {
String uri = source != null ? source.getString("uri") : null;
Uri imageUri = Uri.parse(uri);
this.setImageURI(imageUri);
}
}

Просмотреть файл

@ -0,0 +1,54 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.uiapp.component;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.ViewManagerDelegate;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.facebook.react.viewmanagers.RNTNativeComponentWithStateManagerDelegate;
import com.facebook.react.viewmanagers.RNTNativeComponentWithStateManagerInterface;
/** View manager for {@link NativeViewVithState} components. */
@ReactModule(name = NativeViewWithStateManager.REACT_CLASS)
public class NativeViewWithStateManager extends SimpleViewManager<NativeViewWithState>
implements RNTNativeComponentWithStateManagerInterface<NativeViewWithState> {
public static final String REACT_CLASS = "RNTNativeComponentWithState";
private final ViewManagerDelegate<NativeViewWithState> mDelegate =
new RNTNativeComponentWithStateManagerDelegate<>(this);
@Nullable
@Override
protected ViewManagerDelegate<NativeViewWithState> getDelegate() {
return mDelegate;
}
@NonNull
@Override
public String getName() {
return REACT_CLASS;
}
@NonNull
@Override
protected NativeViewWithState createViewInstance(@NonNull ThemedReactContext reactContext) {
return new NativeViewWithState(reactContext);
}
@Override
@ReactProp(name = "imageSource")
public void setImageSource(NativeViewWithState view, @Nullable ReadableMap value) {
view.setImageSource(value);
}
}

Просмотреть файл

@ -10,6 +10,22 @@ project(appmodules)
include(${REACT_ANDROID_DIR}/cmake-utils/ReactNative-application.cmake)
# === Include NativeComponentWithState C++ files ===
# The following lines are used to tell CMake to build some source code
# that is not contained in the regular folder path for an Android app.
# This is happening because the NativeComponentWithState component
# has some C++ code that is shared between iOS and Android.
# An alternative approach could have been to create a library within the
# component folder, adding a CMakeList.txt file.
target_sources(${CMAKE_PROJECT_NAME}
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../NativeComponentWithState/cxx/RNTNativeComponentWithStateCustomShadowNode.cpp)
target_include_directories(${CMAKE_PROJECT_NAME}
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../NativeComponentWithState/cxx)
# === END Include NativeComponentWithState ===
add_subdirectory(${PROJECT_BUILD_DIR}/generated/source/codegen/jni/ codegen_build)
add_subdirectory(${REACT_COMMON_DIR}/react/nativemodule/samples/platform/android/ sampleturbomodule_build)

Просмотреть файл

@ -8,6 +8,7 @@
#include <AppSpecs.h>
#include <DefaultComponentsRegistry.h>
#include <DefaultTurboModuleManagerDelegate.h>
#include <RNTNativeComponentWithStateCustomComponentDescriptor.h>
#include <ReactCommon/SampleTurboModuleSpec.h>
#include <fbjni/fbjni.h>
#include <react/renderer/componentregistry/ComponentDescriptorProviderRegistry.h>
@ -21,6 +22,8 @@ void registerComponents(
std::shared_ptr<ComponentDescriptorProviderRegistry const> registry) {
registry->add(concreteComponentDescriptorProvider<
RNTMyNativeViewComponentDescriptor>());
registry->add(concreteComponentDescriptorProvider<
RNTNativeComponentWithStateCustomComponentDescriptor>());
}
std::shared_ptr<TurboModule> provideModules(

Просмотреть файл

@ -126,6 +126,11 @@ const Components: Array<RNTesterModuleInfo> = [
category: 'UI',
module: require('../examples/NewArchitecture/NewArchitectureExample'),
},
{
key: 'ComponentWithState',
category: 'UI',
module: require('../examples/NewArchitecture/ComponentWithState'),
},
];
const APIs: Array<RNTesterModuleInfo> = [