Skip to content

Commit

Permalink
Convert + Internalize SwipeRefreshLayoutManager (facebook#47470)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#47470

Convert to Kotlin and formalize it being an internal class (some methods were already `protected` in Java)

Changelog:
[Android] [Breaking] - Stable API - Make SwipeRefreshLayoutManager internal

Reviewed By: cortinico

Differential Revision: D65481861

fbshipit-source-id: afc5c624373fbcd3ca2d28b2834d2682de672997
  • Loading branch information
Thomas Nardone authored and facebook-github-bot committed Nov 7, 2024
1 parent 8b053a4 commit d02da99
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 227 deletions.
31 changes: 0 additions & 31 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -7155,37 +7155,6 @@ public class com/facebook/react/views/swiperefresh/ReactSwipeRefreshLayout : and
public fun setRefreshing (Z)V
}

public class com/facebook/react/views/swiperefresh/SwipeRefreshLayoutManager : com/facebook/react/uimanager/ViewGroupManager, com/facebook/react/viewmanagers/AndroidSwipeRefreshLayoutManagerInterface {
public static final field REACT_CLASS Ljava/lang/String;
public fun <init> ()V
protected synthetic fun addEventEmitters (Lcom/facebook/react/uimanager/ThemedReactContext;Landroid/view/View;)V
protected fun addEventEmitters (Lcom/facebook/react/uimanager/ThemedReactContext;Lcom/facebook/react/views/swiperefresh/ReactSwipeRefreshLayout;)V
protected synthetic fun createViewInstance (Lcom/facebook/react/uimanager/ThemedReactContext;)Landroid/view/View;
protected fun createViewInstance (Lcom/facebook/react/uimanager/ThemedReactContext;)Lcom/facebook/react/views/swiperefresh/ReactSwipeRefreshLayout;
protected fun getDelegate ()Lcom/facebook/react/uimanager/ViewManagerDelegate;
public fun getExportedCustomDirectEventTypeConstants ()Ljava/util/Map;
public fun getExportedViewConstants ()Ljava/util/Map;
public fun getName ()Ljava/lang/String;
public synthetic fun receiveCommand (Landroid/view/View;Ljava/lang/String;Lcom/facebook/react/bridge/ReadableArray;)V
public fun receiveCommand (Lcom/facebook/react/views/swiperefresh/ReactSwipeRefreshLayout;Ljava/lang/String;Lcom/facebook/react/bridge/ReadableArray;)V
public synthetic fun setColors (Landroid/view/View;Lcom/facebook/react/bridge/ReadableArray;)V
public fun setColors (Lcom/facebook/react/views/swiperefresh/ReactSwipeRefreshLayout;Lcom/facebook/react/bridge/ReadableArray;)V
public synthetic fun setEnabled (Landroid/view/View;Z)V
public fun setEnabled (Lcom/facebook/react/views/swiperefresh/ReactSwipeRefreshLayout;Z)V
public synthetic fun setNativeRefreshing (Landroid/view/View;Z)V
public fun setNativeRefreshing (Lcom/facebook/react/views/swiperefresh/ReactSwipeRefreshLayout;Z)V
public synthetic fun setProgressBackgroundColor (Landroid/view/View;Ljava/lang/Integer;)V
public fun setProgressBackgroundColor (Lcom/facebook/react/views/swiperefresh/ReactSwipeRefreshLayout;Ljava/lang/Integer;)V
public synthetic fun setProgressViewOffset (Landroid/view/View;F)V
public fun setProgressViewOffset (Lcom/facebook/react/views/swiperefresh/ReactSwipeRefreshLayout;F)V
public synthetic fun setRefreshing (Landroid/view/View;Z)V
public fun setRefreshing (Lcom/facebook/react/views/swiperefresh/ReactSwipeRefreshLayout;Z)V
public synthetic fun setSize (Landroid/view/View;Ljava/lang/String;)V
public fun setSize (Lcom/facebook/react/views/swiperefresh/ReactSwipeRefreshLayout;I)V
public fun setSize (Lcom/facebook/react/views/swiperefresh/ReactSwipeRefreshLayout;Lcom/facebook/react/bridge/Dynamic;)V
public fun setSize (Lcom/facebook/react/views/swiperefresh/ReactSwipeRefreshLayout;Ljava/lang/String;)V
}

public class com/facebook/react/views/switchview/ReactSwitchManager : com/facebook/react/uimanager/SimpleViewManager, com/facebook/react/viewmanagers/AndroidSwitchManagerInterface {
public static final field REACT_CLASS Ljava/lang/String;
public fun <init> ()V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import com.facebook.react.uimanager.events.Event
internal class RefreshEvent : Event<RefreshEvent> {

@Deprecated("Use constructor with surfaceId", ReplaceWith("RefreshEvent(surfaceId, viewTag)"))
protected constructor(viewTag: Int) : this(ViewUtil.NO_SURFACE_ID, viewTag)
constructor(viewTag: Int) : this(ViewUtil.NO_SURFACE_ID, viewTag)

protected constructor(surfaceId: Int, viewTag: Int) : super(surfaceId, viewTag)
constructor(surfaceId: Int, viewTag: Int) : super(surfaceId, viewTag)

override public fun getEventName(): String {
return "topRefresh"
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* 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.views.swiperefresh

import android.graphics.Color
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.facebook.react.bridge.ColorPropConverter
import com.facebook.react.bridge.Dynamic
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableType
import com.facebook.react.module.annotations.ReactModule
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.UIManagerHelper
import com.facebook.react.uimanager.ViewGroupManager
import com.facebook.react.uimanager.ViewManagerDelegate
import com.facebook.react.uimanager.ViewProps
import com.facebook.react.uimanager.annotations.ReactProp
import com.facebook.react.viewmanagers.AndroidSwipeRefreshLayoutManagerDelegate
import com.facebook.react.viewmanagers.AndroidSwipeRefreshLayoutManagerInterface
import java.util.HashMap

/**
* ViewManager for [ReactSwipeRefreshLayout] which allows the user to "pull to refresh" a child
* view. Emits an `onRefresh` event when this happens.
*/
@ReactModule(name = SwipeRefreshLayoutManager.REACT_CLASS)
internal open class SwipeRefreshLayoutManager :
ViewGroupManager<ReactSwipeRefreshLayout>(),
AndroidSwipeRefreshLayoutManagerInterface<ReactSwipeRefreshLayout> {

private val delegate: ViewManagerDelegate<ReactSwipeRefreshLayout> =
AndroidSwipeRefreshLayoutManagerDelegate(this)

override fun createViewInstance(reactContext: ThemedReactContext): ReactSwipeRefreshLayout =
ReactSwipeRefreshLayout(reactContext)

override fun getName(): String = REACT_CLASS

@ReactProp(name = ViewProps.ENABLED, defaultBoolean = true)
override fun setEnabled(view: ReactSwipeRefreshLayout, enabled: Boolean) {
view.isEnabled = enabled
}

@ReactProp(name = "colors", customType = "ColorArray")
override fun setColors(view: ReactSwipeRefreshLayout, colors: ReadableArray?) {
if (colors != null) {
val colorValues = IntArray(colors.size())
for (i in 0..<colors.size()) {
if (colors.getType(i) == ReadableType.Map) {
colorValues[i] = ColorPropConverter.getColor(colors.getMap(i), view.context)
} else {
colorValues[i] = colors.getInt(i)
}
}
view.setColorSchemeColors(*colorValues)
} else {
view.setColorSchemeColors()
}
}

@ReactProp(name = "progressBackgroundColor", customType = "Color")
override fun setProgressBackgroundColor(view: ReactSwipeRefreshLayout, color: Int?) {
view.setProgressBackgroundColorSchemeColor(color ?: Color.TRANSPARENT)
}

// TODO(T46143833): Remove this method once the 'size' prop has been migrated to String in JS.
fun setSize(view: ReactSwipeRefreshLayout, value: Int): Unit {
view.setSize(value)
}

override fun setSize(view: ReactSwipeRefreshLayout, size: String?) {
if (size == null || size.equals("default")) {
view.setSize(SwipeRefreshLayout.DEFAULT)
} else if (size.equals("large")) {
view.setSize(SwipeRefreshLayout.LARGE)
} else {
throw IllegalArgumentException("Size must be 'default' or 'large', received: $size")
}
}

// This prop temporarily takes both 0 and 1 as well as 'default' and 'large'.
// 0 and 1 are deprecated and will be removed in a future release.
// See T46143833
@ReactProp(name = "size")
fun setSize(view: ReactSwipeRefreshLayout, size: Dynamic): Unit {
when {
size.isNull -> view.setSize(SwipeRefreshLayout.DEFAULT)
size.type == ReadableType.Number -> view.setSize(size.asInt())
size.type == ReadableType.String -> this.setSize(view, size.asString())
else -> throw IllegalArgumentException("Size must be 'default' or 'large'")
}
}

@ReactProp(name = "refreshing")
override fun setRefreshing(view: ReactSwipeRefreshLayout, refreshing: Boolean) {
view.isRefreshing = refreshing
}

@ReactProp(name = "progressViewOffset", defaultFloat = 0f)
override fun setProgressViewOffset(view: ReactSwipeRefreshLayout, offset: Float) {
view.setProgressViewOffset(offset)
}

override fun setNativeRefreshing(view: ReactSwipeRefreshLayout, value: Boolean) {
setRefreshing(view, value)
}

override fun addEventEmitters(reactContext: ThemedReactContext, view: ReactSwipeRefreshLayout) {
view.setOnRefreshListener {
val eventDispatcher = UIManagerHelper.getEventDispatcherForReactTag(reactContext, view.id)
eventDispatcher?.dispatchEvent(RefreshEvent(UIManagerHelper.getSurfaceId(view), view.id))
}
}

override fun receiveCommand(
root: ReactSwipeRefreshLayout,
commandId: String,
args: ReadableArray?
) {
when (commandId) {
"setNativeRefreshing" ->
if (args != null) {
setRefreshing(root, args.getBoolean(0))
}
else -> {}
}
}

override fun getExportedViewConstants(): MutableMap<String, Any> =
mutableMapOf(
"SIZE" to
mutableMapOf(
"DEFAULT" to SwipeRefreshLayout.DEFAULT, "LARGE" to SwipeRefreshLayout.LARGE))

override fun getExportedCustomDirectEventTypeConstants(): MutableMap<String, Any> {
val baseEventTypeConstants = super.getExportedCustomDirectEventTypeConstants()
val eventTypeConstants: MutableMap<String, Any> = baseEventTypeConstants ?: HashMap()
eventTypeConstants.putAll(
mutableMapOf("topRefresh" to mutableMapOf("registrationName" to "onRefresh")))
return eventTypeConstants
}

override fun getDelegate(): ViewManagerDelegate<ReactSwipeRefreshLayout> = delegate

companion object {
const val REACT_CLASS: String = "AndroidSwipeRefreshLayout"
}
}

0 comments on commit d02da99

Please sign in to comment.