Skip to content

Commit d1377a3

Browse files
committed
feat: rename DrawerState.visibilityPercentage to visibilityProgress, make it receive a dp parameter, coerce its result into a range from 0f - 1f
- fix corresponding unit tests
1 parent f35854d commit d1377a3

File tree

2 files changed

+35
-31
lines changed

2 files changed

+35
-31
lines changed
Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
package com.w2sv.composed.material3.extensions
22

3-
import androidx.annotation.FloatRange
43
import androidx.compose.material3.DrawerDefaults
54
import androidx.compose.material3.DrawerState
65
import androidx.compose.runtime.Composable
76
import androidx.compose.runtime.State
87
import androidx.compose.runtime.derivedStateOf
98
import androidx.compose.runtime.remember
9+
import androidx.compose.ui.unit.Dp
1010
import com.w2sv.composed.core.extensions.toPx
1111

1212
/**
1313
* @return Float [State] whose value ranges from 0.0 (drawer fully closed) to 1.0 (drawer fully open).
14-
* @param maxWidthPx The with in pixels upon the drawer being fully expanded. If unmodified, this will equal [DrawerDefaults.MaximumDrawerWidth].
14+
* @param maxWidthPx The max drawer sheet width.
1515
*/
16-
fun DrawerState.visibilityPercentage(@FloatRange(from = 0.0) maxWidthPx: Float): State<Float> {
17-
return derivedStateOf { currentOffset / maxWidthPx + 1 } // currentOffset = -MAX when completely hidden, 0 when completely visible
16+
fun DrawerState.visibilityProgress(maxWidthPx: Float): State<Float> {
17+
require(maxWidthPx != 0f) { "maxWidthPx can not be 0f" }
18+
return derivedStateOf {
19+
// currentOffset ∈ [-maxWidthPx (fully hidden), 0 (fully visible)]
20+
((currentOffset + maxWidthPx) / maxWidthPx).coerceIn(0f, 1f)
21+
}
1822
}
1923

2024
/**
21-
* @see visibilityPercentage
25+
* @return the remembered drawer visibility percentage ranging from 0 to 1.
26+
* @param maxWidth The max drawer sheet width. Will equal [DrawerDefaults.MaximumDrawerWidth] by default.
2227
*/
2328
@Composable
24-
fun DrawerState.rememberVisibilityPercentage(
25-
@FloatRange(from = 0.0) maxWidthPx: Float = DrawerDefaults.MaximumDrawerWidth.toPx()
26-
): State<Float> =
27-
remember {
28-
visibilityPercentage(maxWidthPx)
29-
}
29+
fun DrawerState.rememberVisibilityProgress(maxWidth: Dp = DrawerDefaults.MaximumDrawerWidth): State<Float> {
30+
val maxWidthPx = maxWidth.toPx()
31+
return remember(maxWidthPx) { visibilityProgress(maxWidthPx) }
32+
}
Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package com.w2sv.composed.material3.extensions
22

3+
import androidx.compose.foundation.layout.width
34
import androidx.compose.material3.DrawerState
45
import androidx.compose.material3.DrawerValue
6+
import androidx.compose.material3.ModalDrawerSheet
57
import androidx.compose.material3.ModalNavigationDrawer
6-
import androidx.compose.runtime.getValue
8+
import androidx.compose.runtime.State
9+
import androidx.compose.ui.Modifier
710
import androidx.compose.ui.test.junit4.createComposeRule
8-
import com.w2sv.composed.material3.extensions.visibilityPercentage
9-
import kotlin.properties.Delegates
10-
import kotlinx.coroutines.test.runTest
11+
import androidx.compose.ui.unit.dp
12+
import kotlinx.coroutines.runBlocking
1113
import org.junit.Assert.assertEquals
12-
import org.junit.Ignore
1314
import org.junit.Rule
1415
import org.junit.Test
1516
import org.junit.runner.RunWith
@@ -21,29 +22,29 @@ class DrawerStateKtTest {
2122
@get:Rule
2223
val composeTestRule = createComposeRule()
2324

24-
private var maxWidthPx by Delegates.notNull<Float>()
25-
26-
@Ignore("Mysteriously not working anymore after update of compose dependencies. DrawerState.currentOffset always null.")
2725
@Test
28-
fun visibilityPercentage() =
29-
runTest {
30-
val drawerState = DrawerState(initialValue = DrawerValue.Closed)
26+
fun visibilityProgress() {
27+
val drawerState = DrawerState(initialValue = DrawerValue.Closed)
28+
lateinit var visibilityProgress: State<Float>
29+
val sheetWidth = 300.dp
30+
31+
with(composeTestRule) {
32+
setContent {
33+
visibilityProgress = drawerState.rememberVisibilityProgress(sheetWidth)
3134

32-
composeTestRule.setContent {
33-
maxWidthPx = 120f
3435
ModalNavigationDrawer(
35-
drawerContent = { /*TODO*/ },
3636
drawerState = drawerState,
37+
drawerContent = { ModalDrawerSheet(modifier = Modifier.width(sheetWidth)) {} },
3738
content = {}
3839
)
3940
}
4041

41-
val visibilityPercentage by drawerState.visibilityPercentage(maxWidthPx)
42-
43-
assertEquals(0f, visibilityPercentage)
42+
runOnIdle {
43+
assertEquals(0f, visibilityProgress.value)
4444

45-
drawerState.snapTo(DrawerValue.Open)
46-
47-
assertEquals(1f, visibilityPercentage)
45+
runBlocking { drawerState.snapTo(DrawerValue.Open) }
46+
assertEquals(1f, visibilityProgress.value)
47+
}
4848
}
49+
}
4950
}

0 commit comments

Comments
 (0)