-
Notifications
You must be signed in to change notification settings - Fork 17
/
SampleStack.kt
105 lines (98 loc) · 3.66 KB
/
SampleStack.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.github.terrakok.modo.sample.screens.containers
import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.lifecycle.LifecycleEventObserver
import com.github.terrakok.modo.DialogScreen
import com.github.terrakok.modo.ExperimentalModoApi
import com.github.terrakok.modo.Screen
import com.github.terrakok.modo.lifecycle.LifecycleScreenEffect
import com.github.terrakok.modo.sample.SlideTransition
import com.github.terrakok.modo.sample.screens.dialogs.SampleBottomSheet
import com.github.terrakok.modo.sample.screens.dialogs.SampleBottomSheetStack
import com.github.terrakok.modo.stack.DialogPlaceHolder
import com.github.terrakok.modo.stack.StackNavModel
import com.github.terrakok.modo.stack.StackReducerAction
import com.github.terrakok.modo.stack.StackScreen
import com.github.terrakok.modo.stack.StackState
import com.github.terrakok.modo.stack.back
import kotlinx.parcelize.Parcelize
import logcat.logcat
class OpenActivityAction(
private val context: Context,
private val clazz: Class<*>
) : StackReducerAction {
override fun reduce(oldState: StackState): StackState {
context.startActivity(
Intent(context, clazz)
)
return oldState
}
companion object {
inline operator fun <reified T : Activity> invoke(context: Context) = OpenActivityAction(context, T::class.java)
}
}
@Parcelize
open class SampleStack(
private val stackNavModel: StackNavModel
) : StackScreen(stackNavModel) {
constructor(rootScreen: Screen) : this(StackNavModel(rootScreen))
@OptIn(ExperimentalModoApi::class)
@Composable
override fun Content(modifier: Modifier) {
LifecycleScreenEffect {
LifecycleEventObserver { _, event ->
logcat(tag = "SampleStack") { "$screenKey lifecycle event $event" }
}
}
TopScreenContent(modifier) { modifier ->
SlideTransition(modifier)
}
}
@OptIn(ExperimentalModoApi::class)
@Composable
override fun DecorateCustomDialog(dialog: DialogScreen, modifier: Modifier, content: @Composable (Modifier) -> Unit) {
val isDialogPlaceHolder = remember(dialog) {
dialog is DialogPlaceHolder
}
val isBottomSheet = remember {
dialog is SampleBottomSheet || dialog is SampleBottomSheetStack
}
val background by animateColorAsState(
targetValue = if (isDialogPlaceHolder || isBottomSheet) Color.Transparent else Color.Black.copy(alpha = 0.5f),
label = "Dialog dim"
)
val backgroundClickableModifier = if (!isDialogPlaceHolder) {
Modifier
.clickable(
interactionSource = remember {
MutableInteractionSource()
},
indication = null
) {
back()
}
} else {
Modifier
}
Box(
modifier = modifier
.fillMaxSize()
.then(backgroundClickableModifier)
.background(background),
) {
content(Modifier)
}
}
}