Skip to content

Commit b24f60f

Browse files
cipolleschifacebook-github-bot
authored andcommitted
Add ComponentWithState in Android (#34911)
Summary: Pull Request resolved: #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
1 parent 1a9cceb commit b24f60f

File tree

10 files changed

+112
-2
lines changed

10 files changed

+112
-2
lines changed

packages/rn-tester/NativeComponentWithState/NativeComponentWithState.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Pod::Spec.new do |s|
2323
"CLANG_CXX_LANGUAGE_STANDARD" => "c++17"
2424
}
2525

26-
s.source_files = "ios/**/*.{h,m,mm,cpp}"
26+
s.source_files = "{ios,cxx}/**/*.{h,m,mm,cpp}"
2727
s.requires_arc = true
2828

2929
install_modules_dependencies(s)

packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
import com.facebook.react.module.model.ReactModuleInfoProvider;
2424
import com.facebook.react.shell.MainReactPackage;
2525
import com.facebook.react.uiapp.component.MyNativeViewManager;
26+
import com.facebook.react.uiapp.component.NativeViewWithStateManager;
2627
import com.facebook.react.uimanager.ViewManager;
2728
import com.facebook.react.views.text.ReactFontManager;
2829
import com.facebook.soloader.SoLoader;
30+
import java.util.ArrayList;
2931
import java.util.Arrays;
3032
import java.util.Collections;
3133
import java.util.HashMap;
@@ -106,7 +108,10 @@ public List<NativeModule> createNativeModules(
106108
@Override
107109
public List<ViewManager> createViewManagers(
108110
@NonNull ReactApplicationContext reactContext) {
109-
return Collections.singletonList(new MyNativeViewManager());
111+
ArrayList<ViewManager> list = new ArrayList();
112+
list.add(new MyNativeViewManager());
113+
list.add(new NativeViewWithStateManager());
114+
return list;
110115
}
111116
});
112117
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.uiapp.component;
9+
10+
import android.content.Context;
11+
import android.net.Uri;
12+
import androidx.annotation.Nullable;
13+
import com.facebook.drawee.view.SimpleDraweeView;
14+
import com.facebook.react.bridge.ReadableMap;
15+
16+
class NativeViewWithState extends SimpleDraweeView {
17+
18+
public NativeViewWithState(Context context) {
19+
super(context);
20+
}
21+
22+
void setImageSource(@Nullable ReadableMap source) {
23+
String uri = source != null ? source.getString("uri") : null;
24+
Uri imageUri = Uri.parse(uri);
25+
this.setImageURI(imageUri);
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.uiapp.component;
9+
10+
import androidx.annotation.NonNull;
11+
import androidx.annotation.Nullable;
12+
import com.facebook.react.bridge.ReadableMap;
13+
import com.facebook.react.module.annotations.ReactModule;
14+
import com.facebook.react.uimanager.SimpleViewManager;
15+
import com.facebook.react.uimanager.ThemedReactContext;
16+
import com.facebook.react.uimanager.ViewManagerDelegate;
17+
import com.facebook.react.uimanager.annotations.ReactProp;
18+
import com.facebook.react.viewmanagers.RNTNativeComponentWithStateManagerDelegate;
19+
import com.facebook.react.viewmanagers.RNTNativeComponentWithStateManagerInterface;
20+
21+
/** View manager for {@link NativeViewVithState} components. */
22+
@ReactModule(name = NativeViewWithStateManager.REACT_CLASS)
23+
public class NativeViewWithStateManager extends SimpleViewManager<NativeViewWithState>
24+
implements RNTNativeComponentWithStateManagerInterface<NativeViewWithState> {
25+
26+
public static final String REACT_CLASS = "RNTNativeComponentWithState";
27+
28+
private final ViewManagerDelegate<NativeViewWithState> mDelegate =
29+
new RNTNativeComponentWithStateManagerDelegate<>(this);
30+
31+
@Nullable
32+
@Override
33+
protected ViewManagerDelegate<NativeViewWithState> getDelegate() {
34+
return mDelegate;
35+
}
36+
37+
@NonNull
38+
@Override
39+
public String getName() {
40+
return REACT_CLASS;
41+
}
42+
43+
@NonNull
44+
@Override
45+
protected NativeViewWithState createViewInstance(@NonNull ThemedReactContext reactContext) {
46+
return new NativeViewWithState(reactContext);
47+
}
48+
49+
@Override
50+
@ReactProp(name = "imageSource")
51+
public void setImageSource(NativeViewWithState view, @Nullable ReadableMap value) {
52+
view.setImageSource(value);
53+
}
54+
}

packages/rn-tester/android/app/src/main/jni/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@ project(appmodules)
1010

1111
include(${REACT_ANDROID_DIR}/cmake-utils/ReactNative-application.cmake)
1212

13+
# === Include NativeComponentWithState C++ files ===
14+
# The following lines are used to tell CMake to build some source code
15+
# that is not contained in the regular folder path for an Android app.
16+
# This is happening because the NativeComponentWithState component
17+
# has some C++ code that is shared between iOS and Android.
18+
# An alternative approach could have been to create a library within the
19+
# component folder, adding a CMakeList.txt file.
20+
target_sources(${CMAKE_PROJECT_NAME}
21+
PUBLIC
22+
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../NativeComponentWithState/cxx/RNTNativeComponentWithStateCustomShadowNode.cpp)
23+
24+
target_include_directories(${CMAKE_PROJECT_NAME}
25+
PUBLIC
26+
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../NativeComponentWithState/cxx)
27+
# === END Include NativeComponentWithState ===
28+
1329
add_subdirectory(${PROJECT_BUILD_DIR}/generated/source/codegen/jni/ codegen_build)
1430
add_subdirectory(${REACT_COMMON_DIR}/react/nativemodule/samples/platform/android/ sampleturbomodule_build)
1531

packages/rn-tester/android/app/src/main/jni/OnLoad.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <AppSpecs.h>
99
#include <DefaultComponentsRegistry.h>
1010
#include <DefaultTurboModuleManagerDelegate.h>
11+
#include <RNTNativeComponentWithStateCustomComponentDescriptor.h>
1112
#include <ReactCommon/SampleTurboModuleSpec.h>
1213
#include <fbjni/fbjni.h>
1314
#include <react/renderer/componentregistry/ComponentDescriptorProviderRegistry.h>
@@ -21,6 +22,8 @@ void registerComponents(
2122
std::shared_ptr<ComponentDescriptorProviderRegistry const> registry) {
2223
registry->add(concreteComponentDescriptorProvider<
2324
RNTMyNativeViewComponentDescriptor>());
25+
registry->add(concreteComponentDescriptorProvider<
26+
RNTNativeComponentWithStateCustomComponentDescriptor>());
2427
}
2528

2629
std::shared_ptr<TurboModule> provideModules(

packages/rn-tester/js/utils/RNTesterList.android.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ const Components: Array<RNTesterModuleInfo> = [
126126
category: 'UI',
127127
module: require('../examples/NewArchitecture/NewArchitectureExample'),
128128
},
129+
{
130+
key: 'ComponentWithState',
131+
category: 'UI',
132+
module: require('../examples/NewArchitecture/ComponentWithState'),
133+
},
129134
];
130135

131136
const APIs: Array<RNTesterModuleInfo> = [

0 commit comments

Comments
 (0)