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

bottomsheet animation show and hide after back pressed #32

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.github.terrakok.androidcomposeapp.screens

import androidx.activity.compose.BackHandler
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.ModalBottomSheetLayout
import androidx.compose.material.ModalBottomSheetValue
import androidx.compose.material.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.window.DialogProperties
import com.github.terrakok.modo.DialogScreen
import com.github.terrakok.modo.ExperimentalModoApi
Expand All @@ -15,6 +18,8 @@ import com.github.terrakok.modo.generateScreenKey
import com.github.terrakok.modo.stack.StackScreen
import com.github.terrakok.modo.stack.back
import com.github.terrakok.modo.util.currentOrThrow
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.launch
import kotlinx.parcelize.Parcelize

@OptIn(ExperimentalModoApi::class)
Expand All @@ -35,13 +40,34 @@ class SampleBottomSheet(
@OptIn(ExperimentalMaterialApi::class)
@Composable
override fun Content() {
val coroutineScope = rememberCoroutineScope()
val navigation = LocalContainerScreen.currentOrThrow as StackScreen
val state = rememberModalBottomSheetState(initialValue = ModalBottomSheetValue.HalfExpanded)
LaunchedEffect(key1 = state.currentValue) {
if (state.currentValue == ModalBottomSheetValue.Hidden) {
navigation.back()
val state = rememberModalBottomSheetState(initialValue = ModalBottomSheetValue.Hidden)

LaunchedEffect(key1 = Unit) {
coroutineScope.launch {
state.show()
}
}

LaunchedEffect(key1 = state) {
snapshotFlow { state.currentValue }.drop(1).collect { value ->
if (value == ModalBottomSheetValue.Hidden) {
navigation.back()
}
}
}

BackHandler {
coroutineScope.launch {
if (state.currentValue == ModalBottomSheetValue.Hidden) {
navigation.back()
} else {
state.hide()
}
}
}

ModalBottomSheetLayout(
sheetContent = {
SampleContent(i, LocalContainerScreen.current as StackScreen, isDialog = false)
Expand Down