Fix hidden item sheet nested scroll

This commit is contained in:
MM20 2022-05-29 20:27:48 +02:00
parent 9bdac79b20
commit a1a625e5e0
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389

View File

@ -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(
} }
} }
AnimatedVisibility(
animationState,
enter = slideIn { IntOffset(0, it.height) }
) {
val swipeState = val swipeState =
rememberSwipeableState(initialValue = SwipeState.Default) { rememberSwipeableState(initialValue = SwipeState.Default) {
if (it == SwipeState.Dismiss) onDismiss() if (it == SwipeState.Dismiss) onDismiss()
return@rememberSwipeableState true 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(
animationState,
enter = slideIn { IntOffset(0, it.height) }
) {
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()
) { ) {
SmallTopAppBar(
title = {
Text( Text(
stringResource(R.string.preference_hidden_items), stringResource(R.string.preference_hidden_items),
style = MaterialTheme.typography.titleLarge, overflow = TextOverflow.Ellipsis,
modifier = Modifier.padding(24.dp) 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),
)
}
}
} }
} }
} }