Skip to content

Commit

Permalink
chore(android): Implement missing codegen interfaces (software-mansio…
Browse files Browse the repository at this point in the history
…n#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.
  • Loading branch information
maciekstosio authored and ja1ns committed Oct 9, 2024
1 parent 9f21a7b commit fb8c7e6
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ScreenContainer>() {
class ScreenContainerViewManager : ViewGroupManager<ScreenContainer>(), RNSScreenContainerManagerInterface<ScreenContainer> {
private val delegate: ViewManagerDelegate<ScreenContainer>

init {
delegate = RNSScreenContainerManagerDelegate<ScreenContainer, ScreenContainerViewManager>(this)
}

protected override fun getDelegate(): ViewManagerDelegate<ScreenContainer> {
return delegate
}

override fun getName(): String = REACT_CLASS

override fun createViewInstance(reactContext: ThemedReactContext): ScreenContainer = ScreenContainer(reactContext)
Expand Down
105 changes: 80 additions & 25 deletions android/src/main/java/com/swmansion/rnscreens/SearchBarManager.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,7 +18,17 @@ import com.swmansion.rnscreens.events.SearchBarOpenEvent
import com.swmansion.rnscreens.events.SearchBarSearchButtonPressEvent

@ReactModule(name = SearchBarManager.REACT_CLASS)
class SearchBarManager : ViewGroupManager<SearchBarView>() {
class SearchBarManager : ViewGroupManager<SearchBarView>(), RNSSearchBarManagerInterface<SearchBarView> {
private val delegate: ViewManagerDelegate<SearchBarView>

init {
delegate = RNSSearchBarManagerDelegate<SearchBarView, SearchBarManager>(this)
}

protected override fun getDelegate(): ViewManagerDelegate<SearchBarView> {
return delegate
}

override fun getName(): String {
return REACT_CLASS
}
Expand All @@ -30,7 +43,7 @@ class SearchBarManager : ViewGroupManager<SearchBarView>() {
}

@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
Expand All @@ -48,17 +61,17 @@ class SearchBarManager : ViewGroupManager<SearchBarView>() {
}

@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
Expand All @@ -71,46 +84,32 @@ class SearchBarManager : ViewGroupManager<SearchBarView>() {
}

@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<String, Any>? {
return MapBuilder.of(
SearchBarBlurEvent.EVENT_NAME,
Expand All @@ -131,4 +130,60 @@ class SearchBarManager : ViewGroupManager<SearchBarView>() {
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")
}
}
Original file line number Diff line number Diff line change
@@ -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<T extends View, U extends BaseViewManagerInterface<T> & RNSScreenContainerManagerInterface<T>> extends BaseViewManagerDelegate<T, U> {
public RNSScreenContainerManagerDelegate(U viewManager) {
super(viewManager);
}
@Override
public void setProperty(T view, String propName, @Nullable Object value) {
super.setProperty(view, propName, value);
}
}
Original file line number Diff line number Diff line change
@@ -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<T extends View> {
// No props
}
Original file line number Diff line number Diff line change
@@ -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<T extends View, U extends BaseViewManagerInterface<T> & RNSSearchBarManagerInterface<T>> extends BaseViewManagerDelegate<T, U> {
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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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<T extends View> {
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);
}

0 comments on commit fb8c7e6

Please sign in to comment.