-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
FabricEnabledViewGroup.kt
49 lines (43 loc) · 2.05 KB
/
FabricEnabledViewGroup.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.swmansion.rnscreens
import android.view.ViewGroup
import androidx.annotation.UiThread
import com.facebook.react.bridge.ReactContext
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.WritableMap
import com.facebook.react.bridge.WritableNativeMap
import com.facebook.react.uimanager.FabricViewStateManager
import com.facebook.react.uimanager.PixelUtil
import kotlin.math.abs
abstract class FabricEnabledViewGroup constructor(context: ReactContext?) : ViewGroup(context), FabricViewStateManager.HasFabricViewStateManager {
private val mFabricViewStateManager: FabricViewStateManager = FabricViewStateManager()
override fun getFabricViewStateManager(): FabricViewStateManager {
return mFabricViewStateManager
}
protected fun updateScreenSizeFabric(width: Int, height: Int) {
updateState(width, height)
}
@UiThread
fun updateState(width: Int, height: Int) {
val realWidth: Float = PixelUtil.toDIPFromPixel(width.toFloat())
val realHeight: Float = PixelUtil.toDIPFromPixel(height.toFloat())
// Check incoming state values. If they're already the correct value, return early to prevent
// infinite UpdateState/SetState loop.
val currentState: ReadableMap? = mFabricViewStateManager.getStateData()
if (currentState != null) {
val delta = 0.9f
val stateFrameHeight: Float = if (currentState.hasKey("frameHeight")) currentState.getDouble("frameHeight").toFloat() else 0f
val stateFrameWidth: Float = if (currentState.hasKey("frameWidth")) currentState.getDouble("frameWidth").toFloat() else 0f
if (abs(stateFrameWidth - realWidth) < delta &&
abs(stateFrameHeight - realHeight) < delta
) {
return
}
}
mFabricViewStateManager.setState {
val map: WritableMap = WritableNativeMap()
map.putDouble("frameWidth", realWidth.toDouble())
map.putDouble("frameHeight", realHeight.toDouble())
map
}
}
}