Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent crash when there is no appearance model built #2375

Merged
merged 6 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions android/src/main/java/com/swmansion/rnscreens/Screen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import com.facebook.react.uimanager.UIManagerModule
import com.facebook.react.uimanager.events.EventDispatcher
import com.facebook.react.views.scroll.ReactScrollView
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.shape.CornerFamily
import com.google.android.material.shape.MaterialShapeDrawable
import com.google.android.material.shape.ShapeAppearanceModel
import com.swmansion.rnscreens.events.HeaderHeightChangeEvent
import com.swmansion.rnscreens.events.SheetDetentChangedEvent

Expand Down Expand Up @@ -54,10 +57,16 @@ class Screen(

// Props for controlling modal presentation
var isSheetGrabberVisible: Boolean = false

// corner radius must be updated after all props prop updates from a single transaction
// have been applied, because it depends on the presentation type.
private var shouldUpdateSheetCornerRadius = false
var sheetCornerRadius: Float = 0F
set(value) {
field = value
(fragment as? ScreenStackFragment)?.onSheetCornerRadiusChange()
if (field != value) {
field = value
shouldUpdateSheetCornerRadius = true
}
}
var sheetExpandsWhenScrolledToEdge: Boolean = true

Expand Down Expand Up @@ -420,6 +429,28 @@ class Screen(
)
}

internal fun onFinalizePropsUpdate() {
if (shouldUpdateSheetCornerRadius) {
shouldUpdateSheetCornerRadius = false
onSheetCornerRadiusChange()
}
}

internal fun onSheetCornerRadiusChange() {
if (stackPresentation !== StackPresentation.FORM_SHEET || background == null) {
return
}
(background as MaterialShapeDrawable?)?.let {
it.shapeAppearanceModel =
ShapeAppearanceModel
.Builder()
.apply {
setTopLeftCorner(CornerFamily.ROUNDED, sheetCornerRadius)
setTopRightCorner(CornerFamily.ROUNDED, sheetCornerRadius)
}.build()
}
}

enum class StackPresentation {
PUSH,
MODAL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,7 @@ class ScreenStackFragment :
}

internal fun onSheetCornerRadiusChange() {
(screen.background as MaterialShapeDrawable).shapeAppearanceModel =
ShapeAppearanceModel
.Builder()
.apply {
setTopLeftCorner(CornerFamily.ROUNDED, screen.sheetCornerRadius)
setTopRightCorner(CornerFamily.ROUNDED, screen.sheetCornerRadius)
}.build()
screen.onSheetCornerRadiusChange()
}

override fun onCreateView(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ open class ScreenViewManager :
return super.updateState(view, props, stateWrapper)
}

// Called after all props are updated for given view
override fun onAfterUpdateTransaction(view: Screen) {
super.onAfterUpdateTransaction(view)
view.onFinalizePropsUpdate()
}

@ReactProp(name = "activityState")
fun setActivityState(
view: Screen,
Expand Down
Loading