Skip to content

Commit

Permalink
fix(#439): remove deprecated FabricViewStateManager class usage (#462)
Browse files Browse the repository at this point in the history
FabricStateViewManager is deprecated in RN core and it should be
migrated just to direct usage of StateWrapper
(it should be safe to use it even with older versions of RN)
  • Loading branch information
mateusz1913 authored Dec 20, 2023
1 parent 0df6e3c commit 1398d8b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class SafeAreaContextPackage : TurboReactPackage() {
moduleClass.name,
true,
reactModule.needsEagerInit,
/** TODO remove the parameter once support for RN < 0.73 is dropped */
reactModule.hasConstants,
reactModule.isCxxModule,
TurboModule::class.java.isAssignableFrom(moduleClass))
Expand Down
27 changes: 14 additions & 13 deletions android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,28 @@ import android.util.Log
import android.view.View
import android.view.ViewTreeObserver
import com.facebook.react.bridge.Arguments
import com.facebook.react.uimanager.FabricViewStateManager
import com.facebook.react.uimanager.FabricViewStateManager.HasFabricViewStateManager
import com.facebook.react.uimanager.StateWrapper
import com.facebook.react.uimanager.UIManagerModule
import com.facebook.react.views.view.ReactViewGroup
import java.util.*
import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.withLock

private const val MAX_WAIT_TIME_NANO = 500000000L // 500ms

class SafeAreaView(context: Context?) :
ReactViewGroup(context), ViewTreeObserver.OnPreDrawListener, HasFabricViewStateManager {
ReactViewGroup(context), ViewTreeObserver.OnPreDrawListener {
private var mMode = SafeAreaViewMode.PADDING
private var mInsets: EdgeInsets? = null
private var mEdges: SafeAreaViewEdges? = null
private var mProviderView: View? = null
private val mFabricViewStateManager = FabricViewStateManager()
private var mStateWrapper: StateWrapper? = null

override fun getFabricViewStateManager(): FabricViewStateManager {
return mFabricViewStateManager
fun getStateWrapper(): StateWrapper? {
return mStateWrapper
}

fun setStateWrapper(stateWrapper: StateWrapper?) {
mStateWrapper = stateWrapper
}

private fun updateInsets() {
Expand All @@ -37,12 +39,11 @@ class SafeAreaView(context: Context?) :
SafeAreaViewEdgeModes.ADDITIVE,
SafeAreaViewEdgeModes.ADDITIVE,
SafeAreaViewEdgeModes.ADDITIVE)
if (mFabricViewStateManager.hasStateWrapper()) {
mFabricViewStateManager.setState {
val map = Arguments.createMap()
map.putMap("insets", edgeInsetsToJsMap(insets))
map
}
val stateWrapper = getStateWrapper()
if (stateWrapper != null) {
val map = Arguments.createMap()
map.putMap("insets", edgeInsetsToJsMap(insets))
stateWrapper.updateState(map)
} else {
val localData = SafeAreaViewLocalData(insets = insets, mode = mMode, edges = edges)
val reactContext = getReactContext(this)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.th3rdwave.safeareacontext

import java.util.*

enum class SafeAreaViewEdgeModes {
OFF,
ADDITIVE,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.th3rdwave.safeareacontext

import java.util.*

data class SafeAreaViewLocalData(
val insets: EdgeInsets,
val mode: SafeAreaViewMode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.facebook.react.uimanager.annotations.ReactProp
import com.facebook.react.viewmanagers.RNCSafeAreaViewManagerInterface
import com.facebook.react.views.view.ReactViewGroup
import com.facebook.react.views.view.ReactViewManager
import java.util.*

@ReactModule(name = SafeAreaViewManager.REACT_CLASS)
class SafeAreaViewManager : ReactViewManager(), RNCSafeAreaViewManagerInterface<SafeAreaView> {
Expand Down Expand Up @@ -64,7 +63,7 @@ class SafeAreaViewManager : ReactViewManager(), RNCSafeAreaViewManagerInterface<
props: ReactStylesDiffMap?,
stateWrapper: StateWrapper?
): Any? {
(view as SafeAreaView).fabricViewStateManager.setStateWrapper(stateWrapper)
(view as SafeAreaView).setStateWrapper(stateWrapper)
return null
}

Expand Down

0 comments on commit 1398d8b

Please sign in to comment.