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

ability use custom dialog without system wrapper #33

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ import androidx.compose.ui.window.DialogProperties
@ExperimentalModoApi
interface DialogScreen : Screen {

fun provideDialogConfig(): DialogConfig = DialogConfig()
fun provideDialogConfig(): DialogConfig = DialogConfig.System()

data class DialogConfig(
val useSystemDim: Boolean = true,
val dialogProperties: DialogProperties = DialogProperties()
)
sealed interface DialogConfig {

data class System(
val useSystemDim: Boolean = true,
val dialogProperties: DialogProperties = DialogProperties()
) : DialogConfig

data object Custom : DialogConfig

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,29 @@ abstract class StackScreen(
val dialogConfig = remember(dialog) {
dialog.provideDialogConfig()
}
Dialog(
onDismissRequest = { back() },
properties = dialogConfig.dialogProperties
) {
DisposableEffect(Unit) {
onDispose {
(renderer as ComposeRenderer).transitionCompleteChannel.trySend(Unit)
when (dialogConfig) {
is DialogScreen.DialogConfig.System -> {
Dialog(
onDismissRequest = { back() },
properties = dialogConfig.dialogProperties
) {
DisposableEffect(Unit) {
onDispose {
(renderer as ComposeRenderer).transitionCompleteChannel.trySend(Unit)
}
}
val parent = LocalView.current.parent
LaunchedEffect(key1 = parent) {
if (!dialogConfig.useSystemDim) {
(parent as? DialogWindowProvider)?.window?.setDimAmount(0f)
}
}
Content(dialog, content)
}
}
val parent = LocalView.current.parent
LaunchedEffect(key1 = parent) {
if (!dialogConfig.useSystemDim) {
(parent as? DialogWindowProvider)?.window?.setDimAmount(0f)
}
is DialogScreen.DialogConfig.Custom -> {
Content(dialog, content)
}
Content(dialog, content)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.terrakok.androidcomposeapp
package com.github.terrakok.androidcomposeapp.screens

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
Expand All @@ -11,6 +11,7 @@ import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp

@Immutable
Expand Down Expand Up @@ -60,6 +61,6 @@ fun ModoButton(
action: () -> Unit
) {
Button(onClick = action, modifier) {
Text(text = text)
Text(text = text, textAlign = TextAlign.Center)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SampleBottomSheetStack(
private val navModel: StackNavModel = StackNavModel(SampleScreen(i + 1))
) : StackScreen(navModel), DialogScreen {

override fun provideDialogConfig(): DialogScreen.DialogConfig = DialogScreen.DialogConfig(
override fun provideDialogConfig(): DialogScreen.DialogConfig = DialogScreen.DialogConfig.System(
useSystemDim = false,
dialogProperties = DialogProperties(
usePlatformDefaultWidth = false,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.github.terrakok.androidcomposeapp.screens

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 com.github.terrakok.modo.DialogScreen
import com.github.terrakok.modo.ExperimentalModoApi
import com.github.terrakok.modo.LocalContainerScreen
import com.github.terrakok.modo.ScreenKey
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.parcelize.Parcelize

@OptIn(ExperimentalModoApi::class)
@Parcelize
class SampleCustomBottomSheet(
private val i: Int,
override val screenKey: ScreenKey = generateScreenKey()
) : DialogScreen {

override fun provideDialogConfig() = DialogScreen.DialogConfig.Custom

@OptIn(ExperimentalMaterialApi::class)
@Composable
override fun Content() {
val navigation = LocalContainerScreen.currentOrThrow as StackScreen
val state = rememberModalBottomSheetState(initialValue = ModalBottomSheetValue.HalfExpanded)
LaunchedEffect(key1 = state.currentValue) {
if (state.currentValue == ModalBottomSheetValue.Hidden) {
navigation.back()
}
}
ModalBottomSheetLayout(
sheetContent = {
SampleContent(i, LocalContainerScreen.current as StackScreen, isDialog = false)
},
sheetState = state
) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SampleDialog(
override val screenKey: ScreenKey = generateScreenKey()
) : DialogScreen {

override fun provideDialogConfig(): DialogScreen.DialogConfig = DialogScreen.DialogConfig(
override fun provideDialogConfig(): DialogScreen.DialogConfig = DialogScreen.DialogConfig.System(
useSystemDim = true,
dialogProperties = DialogProperties(
usePlatformDefaultWidth = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ private fun rememberButtons(
"Model" to { navigator.forward(ModelSampleScreen()) },
"Bottom Sheet" to { navigator.forward(SampleBottomSheetStack(i + 1)) },
"Android ViewModel" to { navigator.forward(AndroidViewModelSampleScreen(i + 1)) },
"Custom Bottom Sheet" to { navigator.forward(SampleCustomBottomSheet(i + 1)) },
).let {
ButtonsState(it)
}
Expand Down