Skip to content

Commit

Permalink
Add ComponentWithState in Android (#34911)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cipolleschi authored and facebook-github-bot committed Oct 10, 2022
1 parent 1a9cceb commit b24f60f
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
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;
Expand Down Expand Up @@ -106,7 +108,10 @@ public List<NativeModule> createNativeModules(
@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;
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
16 changes: 16 additions & 0 deletions packages/rn-tester/android/app/src/main/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions packages/rn-tester/android/app/src/main/jni/OnLoad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions packages/rn-tester/js/utils/RNTesterList.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -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> = [
Expand Down

0 comments on commit b24f60f

Please sign in to comment.