diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManager.java index d5b567aab78..7b2fe5ad65f 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManager.java @@ -27,6 +27,7 @@ import com.facebook.react.uimanager.annotations.ReactPropGroup; import com.facebook.react.uimanager.annotations.ReactPropertyHolder; import com.facebook.yoga.YogaMeasureMode; import java.util.HashMap; +import java.util.Iterator; import java.util.Map; import java.util.Stack; @@ -41,7 +42,6 @@ public abstract class ViewManager private static final String NAME = ViewManager.class.getSimpleName(); - private boolean mIsDelegateLoaded = false; private @Nullable ViewManagerDelegate mDelegate = null; /** @@ -91,11 +91,11 @@ public abstract class ViewManager * @param props {@link ReactStylesDiffMap} props to update the view with */ public void updateProperties(@NonNull T viewToUpdate, ReactStylesDiffMap props) { - final ViewManagerDelegate delegate = getOrCreateViewManagerDelegate(); - if (delegate != null) { - ViewManagerPropertyUpdater.updateProps(delegate, viewToUpdate, props); - } else { - ViewManagerPropertyUpdater.updateProps(this, viewToUpdate, props); + ViewManagerDelegate delegate = getOrCreateViewManagerDelegate(); + Iterator> iterator = props.mBackingMap.getEntryIterator(); + while (iterator.hasNext()) { + Map.Entry entry = iterator.next(); + delegate.setProperty(viewToUpdate, entry.getKey(), entry.getValue()); } onAfterUpdateTransaction(viewToUpdate); } @@ -106,22 +106,23 @@ public abstract class ViewManager * then get calls to {@link ViewManagerDelegate#setProperty(View, String, Object)} for every prop * that must be updated and it's the delegate's responsibility to apply these values to the view. * - *

By default this method returns {@code null}, which means that the view manager doesn't have - * a delegate and the view props should be set internally by the view manager itself. + *

By default, this methods returns a generic {@link ViewManagerDelegate} which uses {@link + * ViewManagerSetter} to apply property updates. * * @return an instance of {@link ViewManagerDelegate} if the props of the view managed by this * view manager should be set via this delegate */ - protected @Nullable ViewManagerDelegate getDelegate() { - return null; + protected ViewManagerDelegate getDelegate() { + return new ViewManagerPropertyUpdater.GenericViewManagerDelegate(this); } - private @Nullable ViewManagerDelegate getOrCreateViewManagerDelegate() { - if (!mIsDelegateLoaded) { - mDelegate = getDelegate(); - mIsDelegateLoaded = true; + private ViewManagerDelegate getOrCreateViewManagerDelegate() { + ViewManagerDelegate delegate = mDelegate; + if (delegate == null) { + delegate = getDelegate(); + mDelegate = delegate; } - return mDelegate; + return delegate; } /** Creates a view with knowledge of props and state. */ @@ -316,10 +317,7 @@ public abstract class ViewManager * @param args optional arguments for the command */ public void receiveCommand(@NonNull T root, String commandId, @Nullable ReadableArray args) { - final ViewManagerDelegate delegate = getOrCreateViewManagerDelegate(); - if (delegate != null) { - delegate.receiveCommand(root, commandId, args); - } + getOrCreateViewManagerDelegate().receiveCommand(root, commandId, args); } /** diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManagerPropertyUpdater.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManagerPropertyUpdater.kt index de7e27d588a..cdac6cfac77 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManagerPropertyUpdater.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManagerPropertyUpdater.kt @@ -9,6 +9,8 @@ package com.facebook.react.uimanager import android.view.View import com.facebook.common.logging.FLog +import com.facebook.react.bridge.ReadableArray +import com.facebook.react.common.annotations.DeprecatedInNewArchitecture import com.facebook.react.uimanager.ViewManagersPropertyCache.PropSetter import java.util.HashMap @@ -40,6 +42,7 @@ public object ViewManagerPropertyUpdater { } @JvmStatic + @Deprecated("Use ViewManager#updateProperties to update a view's properties") public fun , V : View> updateProps( delegate: T, view: V, @@ -53,6 +56,7 @@ public object ViewManagerPropertyUpdater { } @JvmStatic + @Deprecated("Use ViewManager#updateProperties to update a view's properties") public fun updateProps( manager: ViewManager, view: V, @@ -66,6 +70,7 @@ public object ViewManagerPropertyUpdater { } } + @DeprecatedInNewArchitecture @JvmStatic public fun > updateProps(node: T, props: ReactStylesDiffMap) { val setter = findNodeSetter(node.javaClass) @@ -168,4 +173,17 @@ public object ViewManagerPropertyUpdater { } } } + + internal class GenericViewManagerDelegate(private val manager: ViewManager) : + ViewManagerDelegate { + private val setter = findManagerSetter(manager.javaClass) + + @Suppress("ACCIDENTAL_OVERRIDE") + override fun setProperty(view: T, propName: String, value: Any?): Unit { + setter.setProperty(manager, view, propName, value) + } + + @Suppress("ACCIDENTAL_OVERRIDE") + override fun receiveCommand(view: T, commandName: String, args: ReadableArray?) = Unit + } }