Skip to content
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
20 changes: 18 additions & 2 deletions resources/android/BottomSheetRenderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.nativephp.mobile.ui.nativerender.NativeUIBridge
import com.nativephp.mobile.ui.nativerender.NativeUINode
import com.nativephp.mobile.ui.nativerender.RenderNode
import com.nativephp.plugins.native_ui.NativeUITheme
import kotlinx.coroutines.isActive

/**
* Material3 ModalBottomSheet. Visibility driven by `visible`; drag-down and
Expand Down Expand Up @@ -84,15 +85,30 @@ object BottomSheetRenderer {
// Leaving composition the moment `visible` turns false would skip the
// slide-down, so the sheet stays until hide() has played it out.
var shown by remember { mutableStateOf(visible) }
// Shown again before hide() has played out, the sheet stays composed
// and the hide stops wherever it got to. ModalBottomSheet only plays
// show() when it first composes, so without this the sheet would stay
// down there, off screen, with its window still over the app.
var hiding by remember { mutableStateOf(false) }
LaunchedEffect(visible) {
if (visible) {
shown = true
if (hiding) {
hiding = false
sheetState.show()
}
} else if (shown) {
// A drag or M3's own show() cancels hide(); the sheet still goes.
hiding = true
try {
if (!permanent) sheetState.hide()
} finally {
shown = false
// A drag or M3's own show() cancels hide(); the sheet still
// goes. Being shown again cancels this effect instead, and
// the effect that cancelled it brings the sheet back up.
if (isActive) {
hiding = false
shown = false
}
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion tests/BottomSheetRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,16 @@

expect($renderer)
->toBeString()
->toMatch('/try \{\s*if \(!permanent\) sheetState\.hide\(\)\s*\} finally \{\s*shown = false\s*\}/');
->toMatch('/try \{\s*if \(!permanent\) sheetState\.hide\(\)\s*\} finally \{.*?if \(isActive\) \{\s*hiding = false\s*shown = false\s*\}/s');
});

test('brings an Android bottom sheet back up when it is shown again while sliding away', function () {
$renderer = file_get_contents(dirname(__DIR__).'/resources/android/BottomSheetRenderer.kt');

expect($renderer)
->toBeString()
->toMatch('/if \(visible\) \{\s*shown = true\s*if \(hiding\) \{\s*hiding = false\s*sheetState\.show\(\)\s*\}/')
->toMatch('/hiding = true\s*try \{/');
});

test('keeps the detents an Android bottom sheet opened with while it closes', function () {
Expand Down
5 changes: 3 additions & 2 deletions tests/android/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ viewport's top, not just that a scroll was invoked.

`BottomSheetCloseTest` closes an open sheet and checks its content leaves
composition: when its detents change in the same render, and when a finger
grabs it while it slides away. `RenderNode` here draws each child as
`NodeView` does.
grabs it while it slides away. It also shows a sheet again while it slides
away and checks it comes back up to where it opened. `RenderNode` here draws
each child as `NodeView` does.

`DrawerFillHeightTest` opens a modal drawer from its menu button and checks
an `h-full` child reaches the bottom of the drawer, a sibling without it
Expand Down
18 changes: 18 additions & 0 deletions tests/android/src/test/kotlin/BottomSheetCloseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.test.*
import androidx.compose.ui.test.junit4.createComposeRule
import com.nativephp.mobile.ui.nativerender.*
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
Expand Down Expand Up @@ -41,6 +43,22 @@ class BottomSheetCloseTest {
compose.onNodeWithTag("node-2").assertDoesNotExist()
}

@Test fun aSheetShownAgainWhileSlidingAwayComesBackUp() {
val openTop = sheetTop()
compose.mainClock.autoAdvance = false
compose.runOnIdle { sheet.value = sheet(visible = false, detents = "large") }
advanceFrames(150)
assertTrue(sheetTop()!! > openTop!!)
compose.runOnIdle { sheet.value = sheet(visible = true, detents = "large") }
advanceFrames(2000)
assertEquals(openTop, sheetTop())
}

/** Steps the clock a frame at a time, reading the tree, so each frame's effects run before the next. */
private fun advanceFrames(millis: Long) = repeat((millis / 50).toInt()) { compose.mainClock.advanceTimeBy(50); sheetTop() }

private fun sheetTop(): Float? = compose.onAllNodesWithTag("node-2").fetchSemanticsNodes().firstOrNull()?.boundsInRoot?.top

private fun sheet(visible: Boolean, detents: String) = NativeUINode(
1, "bottom_sheet", null, null, GenericProps(mapOf("visible" to visible, "detents" to detents, "on_dismiss" to 9)), 0, 0,
listOf(NativeUINode(2, "", null, null, GenericProps(), 0, 0, emptyList())))
Expand Down
Loading