From fb8c7e644bc17f03f411241fe1f9a7a24ad38f5c Mon Sep 17 00:00:00 2001 From: Maciej Stosio Date: Wed, 29 May 2024 11:09:10 +0200 Subject: [PATCH] chore(android): Implement missing codegen interfaces (#2163) ## Description Currently when we change native props autogenerated by codegen we need to manually update changed files. We're going to automate so everything is copied automatically, but as a first step we need to implement those that aren't yet. This is what this PR introduces. ## Test code and steps to reproduce Go through Example app, nothing should change. --- .../rnscreens/ScreenContainerViewManager.kt | 15 ++- .../swmansion/rnscreens/SearchBarManager.kt | 105 +++++++++++++----- .../RNSScreenContainerManagerDelegate.java | 25 +++++ .../RNSScreenContainerManagerInterface.java | 16 +++ .../RNSSearchBarManagerDelegate.java | 99 +++++++++++++++++ .../RNSSearchBarManagerInterface.java | 37 ++++++ 6 files changed, 271 insertions(+), 26 deletions(-) create mode 100644 android/src/paper/java/com/facebook/react/viewmanagers/RNSScreenContainerManagerDelegate.java create mode 100644 android/src/paper/java/com/facebook/react/viewmanagers/RNSScreenContainerManagerInterface.java create mode 100644 android/src/paper/java/com/facebook/react/viewmanagers/RNSSearchBarManagerDelegate.java create mode 100644 android/src/paper/java/com/facebook/react/viewmanagers/RNSSearchBarManagerInterface.java diff --git a/android/src/main/java/com/swmansion/rnscreens/ScreenContainerViewManager.kt b/android/src/main/java/com/swmansion/rnscreens/ScreenContainerViewManager.kt index 4b181911d6..b7758771ed 100644 --- a/android/src/main/java/com/swmansion/rnscreens/ScreenContainerViewManager.kt +++ b/android/src/main/java/com/swmansion/rnscreens/ScreenContainerViewManager.kt @@ -6,9 +6,22 @@ import com.facebook.react.module.annotations.ReactModule import com.facebook.react.uimanager.LayoutShadowNode import com.facebook.react.uimanager.ThemedReactContext import com.facebook.react.uimanager.ViewGroupManager +import com.facebook.react.uimanager.ViewManagerDelegate +import com.facebook.react.viewmanagers.RNSScreenContainerManagerDelegate +import com.facebook.react.viewmanagers.RNSScreenContainerManagerInterface @ReactModule(name = ScreenContainerViewManager.REACT_CLASS) -class ScreenContainerViewManager : ViewGroupManager() { +class ScreenContainerViewManager : ViewGroupManager(), RNSScreenContainerManagerInterface { + private val delegate: ViewManagerDelegate + + init { + delegate = RNSScreenContainerManagerDelegate(this) + } + + protected override fun getDelegate(): ViewManagerDelegate { + return delegate + } + override fun getName(): String = REACT_CLASS override fun createViewInstance(reactContext: ThemedReactContext): ScreenContainer = ScreenContainer(reactContext) diff --git a/android/src/main/java/com/swmansion/rnscreens/SearchBarManager.kt b/android/src/main/java/com/swmansion/rnscreens/SearchBarManager.kt index d28563e10c..613c41abbd 100644 --- a/android/src/main/java/com/swmansion/rnscreens/SearchBarManager.kt +++ b/android/src/main/java/com/swmansion/rnscreens/SearchBarManager.kt @@ -1,12 +1,15 @@ package com.swmansion.rnscreens +import android.util.Log import com.facebook.react.bridge.JSApplicationIllegalArgumentException -import com.facebook.react.bridge.ReadableArray import com.facebook.react.common.MapBuilder import com.facebook.react.module.annotations.ReactModule import com.facebook.react.uimanager.ThemedReactContext import com.facebook.react.uimanager.ViewGroupManager +import com.facebook.react.uimanager.ViewManagerDelegate import com.facebook.react.uimanager.annotations.ReactProp +import com.facebook.react.viewmanagers.RNSSearchBarManagerDelegate +import com.facebook.react.viewmanagers.RNSSearchBarManagerInterface import com.swmansion.rnscreens.events.SearchBarBlurEvent import com.swmansion.rnscreens.events.SearchBarChangeTextEvent import com.swmansion.rnscreens.events.SearchBarCloseEvent @@ -15,7 +18,17 @@ import com.swmansion.rnscreens.events.SearchBarOpenEvent import com.swmansion.rnscreens.events.SearchBarSearchButtonPressEvent @ReactModule(name = SearchBarManager.REACT_CLASS) -class SearchBarManager : ViewGroupManager() { +class SearchBarManager : ViewGroupManager(), RNSSearchBarManagerInterface { + private val delegate: ViewManagerDelegate + + init { + delegate = RNSSearchBarManagerDelegate(this) + } + + protected override fun getDelegate(): ViewManagerDelegate { + return delegate + } + override fun getName(): String { return REACT_CLASS } @@ -30,7 +43,7 @@ class SearchBarManager : ViewGroupManager() { } @ReactProp(name = "autoCapitalize") - fun setAutoCapitalize(view: SearchBarView, autoCapitalize: String?) { + override fun setAutoCapitalize(view: SearchBarView, autoCapitalize: String?) { view.autoCapitalize = when (autoCapitalize) { null, "none" -> SearchBarView.SearchBarAutoCapitalize.NONE "words" -> SearchBarView.SearchBarAutoCapitalize.WORDS @@ -48,17 +61,17 @@ class SearchBarManager : ViewGroupManager() { } @ReactProp(name = "barTintColor", customType = "Color") - fun setTintColor(view: SearchBarView, color: Int?) { + override fun setBarTintColor(view: SearchBarView, color: Int?) { view.tintColor = color } @ReactProp(name = "disableBackButtonOverride") - fun setDisableBackButtonOverride(view: SearchBarView, disableBackButtonOverride: Boolean?) { + override fun setDisableBackButtonOverride(view: SearchBarView, disableBackButtonOverride: Boolean) { view.shouldOverrideBackButton = disableBackButtonOverride != true } @ReactProp(name = "inputType") - fun setInputType(view: SearchBarView, inputType: String?) { + override fun setInputType(view: SearchBarView, inputType: String?) { view.inputType = when (inputType) { null, "text" -> SearchBarView.SearchBarInputTypes.TEXT "phone" -> SearchBarView.SearchBarInputTypes.PHONE @@ -71,46 +84,32 @@ class SearchBarManager : ViewGroupManager() { } @ReactProp(name = "placeholder") - fun setPlaceholder(view: SearchBarView, placeholder: String?) { + override fun setPlaceholder(view: SearchBarView, placeholder: String?) { if (placeholder != null) { view.placeholder = placeholder } } @ReactProp(name = "textColor", customType = "Color") - fun setTextColor(view: SearchBarView, color: Int?) { + override fun setTextColor(view: SearchBarView, color: Int?) { view.textColor = color } @ReactProp(name = "headerIconColor", customType = "Color") - fun setHeaderIconColor(view: SearchBarView, color: Int?) { + override fun setHeaderIconColor(view: SearchBarView, color: Int?) { view.headerIconColor = color } @ReactProp(name = "hintTextColor", customType = "Color") - fun setHintTextColor(view: SearchBarView, color: Int?) { + override fun setHintTextColor(view: SearchBarView, color: Int?) { view.hintTextColor = color } @ReactProp(name = "shouldShowHintSearchIcon") - fun setShouldShowHintSearchIcon(view: SearchBarView, shouldShowHintSearchIcon: Boolean?) { + override fun setShouldShowHintSearchIcon(view: SearchBarView, shouldShowHintSearchIcon: Boolean) { view.shouldShowHintSearchIcon = shouldShowHintSearchIcon ?: true } - fun setPlacement(view: SearchBarView, placeholder: String?) = Unit - - override fun receiveCommand(root: SearchBarView, commandId: String?, args: ReadableArray?) { - when (commandId) { - "focus" -> root.handleFocusJsRequest() - "blur" -> root.handleBlurJsRequest() - "clearText" -> root.handleClearTextJsRequest() - "toggleCancelButton" -> root.handleToggleCancelButtonJsRequest(false) // just a dummy argument - "setText" -> root.handleSetTextJsRequest(args?.getString(0)) - "cancelSearch" -> root.handleCancelSearchJsRequest() - else -> throw JSApplicationIllegalArgumentException("Unsupported native command received: $commandId") - } - } - override fun getExportedCustomDirectEventTypeConstants(): Map? { return MapBuilder.of( SearchBarBlurEvent.EVENT_NAME, @@ -131,4 +130,60 @@ class SearchBarManager : ViewGroupManager() { companion object { const val REACT_CLASS = "RNSSearchBar" } + + private fun logNotAvailable(propName: String) { + Log.w("RN SCREENS", "$propName prop is not available on Android") + } + + // NativeCommands + + override fun blur(view: SearchBarView?) { + view?.handleBlurJsRequest() + } + + override fun focus(view: SearchBarView?) { + view?.handleFocusJsRequest() + } + + override fun clearText(view: SearchBarView?) { + view?.handleClearTextJsRequest() + } + + override fun toggleCancelButton(view: SearchBarView?, flag: Boolean) { + view?.handleToggleCancelButtonJsRequest(flag) + } + + override fun setText(view: SearchBarView?, text: String?) { + view?.handleSetTextJsRequest(text) + } + + override fun cancelSearch(view: SearchBarView?) { + view?.handleFocusJsRequest() + } + + // iOS only + + override fun setPlacement(view: SearchBarView, placeholder: String?) { + logNotAvailable("setPlacement") + } + + override fun setHideWhenScrolling(view: SearchBarView?, value: Boolean) { + logNotAvailable("hideWhenScrolling") + } + + override fun setObscureBackground(view: SearchBarView?, value: Boolean) { + logNotAvailable("hideNavigationBar") + } + + override fun setHideNavigationBar(view: SearchBarView?, value: Boolean) { + logNotAvailable("hideNavigationBar") + } + + override fun setCancelButtonText(view: SearchBarView?, value: String?) { + logNotAvailable("cancelButtonText") + } + + override fun setTintColor(view: SearchBarView?, value: Int?) { + logNotAvailable("tintColor") + } } diff --git a/android/src/paper/java/com/facebook/react/viewmanagers/RNSScreenContainerManagerDelegate.java b/android/src/paper/java/com/facebook/react/viewmanagers/RNSScreenContainerManagerDelegate.java new file mode 100644 index 0000000000..b214b1a1cf --- /dev/null +++ b/android/src/paper/java/com/facebook/react/viewmanagers/RNSScreenContainerManagerDelegate.java @@ -0,0 +1,25 @@ +/** +* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen). +* +* Do not edit this file as changes may cause incorrect behavior and will be lost +* once the code is regenerated. +* +* @generated by codegen project: GeneratePropsJavaDelegate.js +*/ + +package com.facebook.react.viewmanagers; + +import android.view.View; +import androidx.annotation.Nullable; +import com.facebook.react.uimanager.BaseViewManagerDelegate; +import com.facebook.react.uimanager.BaseViewManagerInterface; + +public class RNSScreenContainerManagerDelegate & RNSScreenContainerManagerInterface> extends BaseViewManagerDelegate { + public RNSScreenContainerManagerDelegate(U viewManager) { + super(viewManager); + } + @Override + public void setProperty(T view, String propName, @Nullable Object value) { + super.setProperty(view, propName, value); + } +} diff --git a/android/src/paper/java/com/facebook/react/viewmanagers/RNSScreenContainerManagerInterface.java b/android/src/paper/java/com/facebook/react/viewmanagers/RNSScreenContainerManagerInterface.java new file mode 100644 index 0000000000..6f1d776d3f --- /dev/null +++ b/android/src/paper/java/com/facebook/react/viewmanagers/RNSScreenContainerManagerInterface.java @@ -0,0 +1,16 @@ +/** +* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen). +* +* Do not edit this file as changes may cause incorrect behavior and will be lost +* once the code is regenerated. +* +* @generated by codegen project: GeneratePropsJavaInterface.js +*/ + +package com.facebook.react.viewmanagers; + +import android.view.View; + +public interface RNSScreenContainerManagerInterface { + // No props +} diff --git a/android/src/paper/java/com/facebook/react/viewmanagers/RNSSearchBarManagerDelegate.java b/android/src/paper/java/com/facebook/react/viewmanagers/RNSSearchBarManagerDelegate.java new file mode 100644 index 0000000000..3b99b3f75d --- /dev/null +++ b/android/src/paper/java/com/facebook/react/viewmanagers/RNSSearchBarManagerDelegate.java @@ -0,0 +1,99 @@ +/** +* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen). +* +* Do not edit this file as changes may cause incorrect behavior and will be lost +* once the code is regenerated. +* +* @generated by codegen project: GeneratePropsJavaDelegate.js +*/ + +package com.facebook.react.viewmanagers; + +import android.view.View; +import androidx.annotation.Nullable; +import com.facebook.react.bridge.ColorPropConverter; +import com.facebook.react.bridge.ReadableArray; +import com.facebook.react.uimanager.BaseViewManagerDelegate; +import com.facebook.react.uimanager.BaseViewManagerInterface; + +public class RNSSearchBarManagerDelegate & RNSSearchBarManagerInterface> extends BaseViewManagerDelegate { + public RNSSearchBarManagerDelegate(U viewManager) { + super(viewManager); + } + @Override + public void setProperty(T view, String propName, @Nullable Object value) { + switch (propName) { + case "hideWhenScrolling": + mViewManager.setHideWhenScrolling(view, value == null ? false : (boolean) value); + break; + case "autoCapitalize": + mViewManager.setAutoCapitalize(view, (String) value); + break; + case "placeholder": + mViewManager.setPlaceholder(view, value == null ? null : (String) value); + break; + case "placement": + mViewManager.setPlacement(view, (String) value); + break; + case "obscureBackground": + mViewManager.setObscureBackground(view, value == null ? false : (boolean) value); + break; + case "hideNavigationBar": + mViewManager.setHideNavigationBar(view, value == null ? false : (boolean) value); + break; + case "cancelButtonText": + mViewManager.setCancelButtonText(view, value == null ? null : (String) value); + break; + case "barTintColor": + mViewManager.setBarTintColor(view, ColorPropConverter.getColor(value, view.getContext())); + break; + case "tintColor": + mViewManager.setTintColor(view, ColorPropConverter.getColor(value, view.getContext())); + break; + case "textColor": + mViewManager.setTextColor(view, ColorPropConverter.getColor(value, view.getContext())); + break; + case "disableBackButtonOverride": + mViewManager.setDisableBackButtonOverride(view, value == null ? false : (boolean) value); + break; + case "inputType": + mViewManager.setInputType(view, value == null ? null : (String) value); + break; + case "hintTextColor": + mViewManager.setHintTextColor(view, ColorPropConverter.getColor(value, view.getContext())); + break; + case "headerIconColor": + mViewManager.setHeaderIconColor(view, ColorPropConverter.getColor(value, view.getContext())); + break; + case "shouldShowHintSearchIcon": + mViewManager.setShouldShowHintSearchIcon(view, value == null ? true : (boolean) value); + break; + default: + super.setProperty(view, propName, value); + } + } + + @Override + public void receiveCommand(T view, String commandName, ReadableArray args) { + switch (commandName) { + case "blur": + mViewManager.blur(view); + break; + case "focus": + mViewManager.focus(view); + break; + case "clearText": + mViewManager.clearText(view); + break; + case "toggleCancelButton": + mViewManager.toggleCancelButton(view, args.getBoolean(0)); + break; + case "setText": + mViewManager.setText(view, args.getString(0)); + break; + case "cancelSearch": + mViewManager.cancelSearch(view); + break; + } + } +} diff --git a/android/src/paper/java/com/facebook/react/viewmanagers/RNSSearchBarManagerInterface.java b/android/src/paper/java/com/facebook/react/viewmanagers/RNSSearchBarManagerInterface.java new file mode 100644 index 0000000000..428c1d436e --- /dev/null +++ b/android/src/paper/java/com/facebook/react/viewmanagers/RNSSearchBarManagerInterface.java @@ -0,0 +1,37 @@ +/** +* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen). +* +* Do not edit this file as changes may cause incorrect behavior and will be lost +* once the code is regenerated. +* +* @generated by codegen project: GeneratePropsJavaInterface.js +*/ + +package com.facebook.react.viewmanagers; + +import android.view.View; +import androidx.annotation.Nullable; + +public interface RNSSearchBarManagerInterface { + void setHideWhenScrolling(T view, boolean value); + void setAutoCapitalize(T view, @Nullable String value); + void setPlaceholder(T view, @Nullable String value); + void setPlacement(T view, @Nullable String value); + void setObscureBackground(T view, boolean value); + void setHideNavigationBar(T view, boolean value); + void setCancelButtonText(T view, @Nullable String value); + void setBarTintColor(T view, @Nullable Integer value); + void setTintColor(T view, @Nullable Integer value); + void setTextColor(T view, @Nullable Integer value); + void setDisableBackButtonOverride(T view, boolean value); + void setInputType(T view, @Nullable String value); + void setHintTextColor(T view, @Nullable Integer value); + void setHeaderIconColor(T view, @Nullable Integer value); + void setShouldShowHintSearchIcon(T view, boolean value); + void blur(T view); + void focus(T view); + void clearText(T view); + void toggleCancelButton(T view, boolean flag); + void setText(T view, String text); + void cancelSearch(T view); +}