Fix hidden item sheet nested scroll
This commit is contained in:
parent
9bdac79b20
commit
a1a625e5e0
@ -4,26 +4,34 @@ import androidx.compose.animation.AnimatedVisibility
|
|||||||
import androidx.compose.animation.core.MutableTransitionState
|
import androidx.compose.animation.core.MutableTransitionState
|
||||||
import androidx.compose.animation.slideIn
|
import androidx.compose.animation.slideIn
|
||||||
import androidx.compose.foundation.gestures.Orientation
|
import androidx.compose.foundation.gestures.Orientation
|
||||||
import androidx.compose.foundation.layout.*
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
|
import androidx.compose.foundation.layout.offset
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.rememberScrollState
|
import androidx.compose.foundation.rememberScrollState
|
||||||
|
import androidx.compose.foundation.shape.CornerSize
|
||||||
import androidx.compose.foundation.verticalScroll
|
import androidx.compose.foundation.verticalScroll
|
||||||
import androidx.compose.material.ExperimentalMaterialApi
|
import androidx.compose.material.ExperimentalMaterialApi
|
||||||
import androidx.compose.material.FractionalThreshold
|
import androidx.compose.material.FractionalThreshold
|
||||||
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.rounded.Settings
|
||||||
import androidx.compose.material.rememberSwipeableState
|
import androidx.compose.material.rememberSwipeableState
|
||||||
import androidx.compose.material.swipeable
|
import androidx.compose.material.swipeable
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.*
|
||||||
import androidx.compose.material3.Surface
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.material3.TextButton
|
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.collectAsState
|
import androidx.compose.runtime.collectAsState
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.ui.Alignment
|
|
||||||
import androidx.compose.ui.ExperimentalComposeUiApi
|
import androidx.compose.ui.ExperimentalComposeUiApi
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.geometry.Offset
|
||||||
|
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
|
||||||
|
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
|
||||||
|
import androidx.compose.ui.input.nestedscroll.nestedScroll
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import androidx.compose.ui.text.style.TextOverflow
|
||||||
import androidx.compose.ui.unit.IntOffset
|
import androidx.compose.ui.unit.IntOffset
|
||||||
|
import androidx.compose.ui.unit.Velocity
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.compose.ui.window.Dialog
|
import androidx.compose.ui.window.Dialog
|
||||||
import androidx.compose.ui.window.DialogProperties
|
import androidx.compose.ui.window.DialogProperties
|
||||||
@ -49,18 +57,64 @@ fun HiddenItemsSheet(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val swipeState =
|
||||||
|
rememberSwipeableState(initialValue = SwipeState.Default) {
|
||||||
|
if (it == SwipeState.Dismiss) onDismiss()
|
||||||
|
return@rememberSwipeableState true
|
||||||
|
}
|
||||||
|
|
||||||
|
val nestedScrollConnection = remember {
|
||||||
|
object : NestedScrollConnection {
|
||||||
|
|
||||||
|
override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
|
||||||
|
if (available.y > 0) {
|
||||||
|
return super.onPreScroll(available, source)
|
||||||
|
}
|
||||||
|
val c = swipeState.performDrag(available.y)
|
||||||
|
return Offset(available.x, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onPostScroll(
|
||||||
|
consumed: Offset,
|
||||||
|
available: Offset,
|
||||||
|
source: NestedScrollSource
|
||||||
|
): Offset {
|
||||||
|
if (available.y < 0) {
|
||||||
|
return super.onPreScroll(available, source)
|
||||||
|
}
|
||||||
|
val c = swipeState.performDrag(available.y)
|
||||||
|
return Offset(available.x, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun onPreFling(available: Velocity): Velocity {
|
||||||
|
if(available.y > 0) {
|
||||||
|
return super.onPreFling(available)
|
||||||
|
}
|
||||||
|
swipeState.performFling(available.y)
|
||||||
|
return available
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun onPostFling(
|
||||||
|
consumed: Velocity,
|
||||||
|
available: Velocity
|
||||||
|
): Velocity {
|
||||||
|
if (available.y < 0) {
|
||||||
|
return super.onPreFling(available)
|
||||||
|
}
|
||||||
|
swipeState.performFling(available.y)
|
||||||
|
return available
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
AnimatedVisibility(
|
AnimatedVisibility(
|
||||||
animationState,
|
animationState,
|
||||||
enter = slideIn { IntOffset(0, it.height) }
|
enter = slideIn { IntOffset(0, it.height) }
|
||||||
) {
|
) {
|
||||||
val swipeState =
|
|
||||||
rememberSwipeableState(initialValue = SwipeState.Default) {
|
|
||||||
if (it == SwipeState.Dismiss) onDismiss()
|
|
||||||
return@rememberSwipeableState true
|
|
||||||
}
|
|
||||||
Surface(
|
Surface(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxSize()
|
.fillMaxSize()
|
||||||
|
.nestedScroll(nestedScrollConnection)
|
||||||
.swipeable(
|
.swipeable(
|
||||||
swipeState,
|
swipeState,
|
||||||
mapOf(
|
mapOf(
|
||||||
@ -70,19 +124,38 @@ fun HiddenItemsSheet(
|
|||||||
orientation = Orientation.Vertical,
|
orientation = Orientation.Vertical,
|
||||||
thresholds = { _, _ -> FractionalThreshold(0.5f) },
|
thresholds = { _, _ -> FractionalThreshold(0.5f) },
|
||||||
)
|
)
|
||||||
.offset { IntOffset(0, swipeState.offset.value.roundToInt()) }
|
.offset { IntOffset(0, swipeState.offset.value.roundToInt()) },
|
||||||
|
shape = MaterialTheme.shapes.large.copy(
|
||||||
|
bottomEnd = CornerSize(0f),
|
||||||
|
bottomStart = CornerSize(0f),
|
||||||
|
)
|
||||||
) {
|
) {
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxSize()
|
.fillMaxSize()
|
||||||
) {
|
) {
|
||||||
Text(
|
|
||||||
stringResource(R.string.preference_hidden_items),
|
|
||||||
style = MaterialTheme.typography.titleLarge,
|
|
||||||
modifier = Modifier.padding(24.dp)
|
|
||||||
|
|
||||||
|
SmallTopAppBar(
|
||||||
|
title = {
|
||||||
|
Text(
|
||||||
|
stringResource(R.string.preference_hidden_items),
|
||||||
|
overflow = TextOverflow.Ellipsis,
|
||||||
|
modifier = Modifier.padding(horizontal = 16.dp),
|
||||||
|
maxLines = 1
|
||||||
|
)
|
||||||
|
},
|
||||||
|
actions = {
|
||||||
|
IconButton(onClick = { /*TODO*/ }) {
|
||||||
|
Icon(
|
||||||
|
imageVector = Icons.Rounded.Settings,
|
||||||
|
contentDescription = stringResource(
|
||||||
|
R.string.settings
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
val items by viewModel.hiddenItems.collectAsState(emptyList())
|
val items by viewModel.hiddenItems.collectAsState(emptyList())
|
||||||
SearchResultGrid(
|
SearchResultGrid(
|
||||||
items,
|
items,
|
||||||
@ -91,18 +164,6 @@ fun HiddenItemsSheet(
|
|||||||
.padding(8.dp)
|
.padding(8.dp)
|
||||||
.verticalScroll(rememberScrollState())
|
.verticalScroll(rememberScrollState())
|
||||||
)
|
)
|
||||||
Box(
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(12.dp),
|
|
||||||
contentAlignment = Alignment.CenterEnd
|
|
||||||
) {
|
|
||||||
TextButton(onClick = { onDismiss() }) {
|
|
||||||
Text(
|
|
||||||
stringResource(id = R.string.close),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user