-
Notifications
You must be signed in to change notification settings - Fork 17
/
MainScreen.kt
207 lines (200 loc) · 8.97 KB
/
MainScreen.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package com.github.terrakok.modo.sample.screens
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import com.github.terrakok.modo.ExperimentalModoApi
import com.github.terrakok.modo.Screen
import com.github.terrakok.modo.ScreenKey
import com.github.terrakok.modo.generateScreenKey
import com.github.terrakok.modo.lifecycle.LifecycleScreenEffect
import com.github.terrakok.modo.lifecycle.OnScreenRemoved
import com.github.terrakok.modo.sample.ModoLegacyIntegrationActivity
import com.github.terrakok.modo.sample.ModoSampleActivity
import com.github.terrakok.modo.sample.fragment.ModoFragment
import com.github.terrakok.modo.sample.fragment.ModoFragmentIntegrationActivity
import com.github.terrakok.modo.sample.quickstart.QuickStartActivity
import com.github.terrakok.modo.sample.screens.base.ButtonsScreenContent
import com.github.terrakok.modo.sample.screens.containers.CustomStackSample
import com.github.terrakok.modo.sample.screens.containers.HorizontalPagerScreen
import com.github.terrakok.modo.sample.screens.containers.OpenActivityAction
import com.github.terrakok.modo.sample.screens.containers.SampleMultiScreen
import com.github.terrakok.modo.sample.screens.containers.StackInLazyColumnScreen
import com.github.terrakok.modo.sample.screens.containers.custom.MovableContentPlaygroundScreen
import com.github.terrakok.modo.sample.screens.containers.custom.RemovableItemContainerScreen
import com.github.terrakok.modo.sample.screens.containers.custom.SampleCustomContainerScreen
import com.github.terrakok.modo.sample.screens.dialogs.DialogsPlayground
import com.github.terrakok.modo.sample.screens.stack.StackActionsScreen
import com.github.terrakok.modo.sample.screens.viewmodel.AndroidViewModelSampleScreen
import com.github.terrakok.modo.stack.LocalStackNavigation
import com.github.terrakok.modo.stack.StackNavContainer
import com.github.terrakok.modo.stack.back
import com.github.terrakok.modo.stack.forward
import com.github.terrakok.modo.util.getActivity
import kotlinx.parcelize.Parcelize
import logcat.logcat
@Parcelize
class MainScreen(
private val screenIndex: Int,
private val canOpenFragment: Boolean = false,
override val screenKey: ScreenKey = generateScreenKey()
) : Screen {
@OptIn(ExperimentalModoApi::class)
@Composable
override fun Content(modifier: Modifier) {
OnScreenRemoved {
logcat { "Screen $screenKey was removed" }
}
LifecycleScreenEffect {
LifecycleEventObserver { _: LifecycleOwner, event: Lifecycle.Event ->
logcat { "$screenKey: Lifecycle.Event $event" }
}
}
MainScreenContent(
screenIndex = screenIndex,
screenKey = screenKey,
navigation = LocalStackNavigation.current,
modifier = modifier,
canOpenFragment = canOpenFragment,
)
}
}
@Composable
internal fun MainScreenContent(
screenIndex: Int,
screenKey: ScreenKey,
navigation: StackNavContainer?,
modifier: Modifier = Modifier,
canOpenFragment: Boolean = false,
) {
ButtonsScreenContent(
screenIndex = screenIndex,
screenName = "MainScreen",
screenKey = screenKey,
state = rememberButtons(
screenKey = screenKey,
navigation = navigation,
i = screenIndex,
canOpenFragment = canOpenFragment
),
modifier = modifier,
)
}
@Composable
internal fun MainScreenContent(
screenIndex: Int,
screenKey: ScreenKey,
counter: Int,
navigation: StackNavContainer,
modifier: Modifier = Modifier,
canOpenFragment: Boolean = false,
) {
ButtonsScreenContent(
screenIndex = screenIndex,
screenName = "MainScreen",
screenKey = screenKey,
state = rememberButtons(
screenKey = screenKey,
navigation = navigation,
i = screenIndex,
canOpenFragment = canOpenFragment
),
counter = counter,
modifier = modifier
)
}
@Suppress("LongMethod")
@Composable
private fun rememberButtons(
screenKey: ScreenKey,
navigation: StackNavContainer?,
i: Int,
canOpenFragment: Boolean
): GroupedButtonsState {
val context = LocalContext.current
val isFirstScreen by remember {
derivedStateOf {
navigation?.navigationState?.stack?.first()?.screenKey == screenKey
}
}
return remember(context, navigation, isFirstScreen) {
val activity = context.getActivity() as? FragmentActivity
GroupedButtonsState(
listOf(
GroupedButtonsState.Group(
title = null,
listOf(
ModoButtonSpec("Forward") { navigation?.forward(MainScreen(i + 1, canOpenFragment)) },
ModoButtonSpec("Back", isEnabled = !isFirstScreen) { navigation?.back() },
)
),
GroupedButtonsState.Group(
title = "Navigation samples",
listOf(
ModoButtonSpec("Stack actions") { navigation?.forward(StackActionsScreen(i + 1)) },
ModoButtonSpec("HorizontalPager") { navigation?.forward(HorizontalPagerScreen()) },
ModoButtonSpec("Custom Stack") { navigation?.forward(CustomStackSample(i + 1)) },
ModoButtonSpec("Stacks in LazyColumn") { navigation?.forward(StackInLazyColumnScreen()) },
ModoButtonSpec("Dialogs & BottomSheets") { navigation?.forward(DialogsPlayground(i + 1)) },
ModoButtonSpec("Multiscreen") { navigation?.forward(SampleMultiScreen()) },
ModoButtonSpec("Screen Effects") { navigation?.forward(ScreenEffectsSampleScreen(i + 1)) },
ModoButtonSpec("Custom Container Actions") { navigation?.forward(SampleCustomContainerScreen()) },
ModoButtonSpec("Custom Container") { navigation?.forward(RemovableItemContainerScreen()) },
// Just experiments
// ModoButtonSpec("2 items screen") { navigation.forward(TwoTopItemsStackScreen(i + 1)) },
// ModoButtonSpec("Demo") { navigation.forward(SaveableStateHolderDemoScreen()) },
ModoButtonSpec("Sample Screen Model") { navigation?.forward(ScreenModelSampleScreen()) },
ModoButtonSpec("Android ViewModel") { navigation?.forward(AndroidViewModelSampleScreen(i + 1)) },
ModoButtonSpec("List/Details") { navigation?.forward(ListScreen()) },
)
),
GroupedButtonsState.Group(
title = "Integrations",
listOfNotNull(
ModoButtonSpec("Modern Activity integration") {
navigation?.dispatch(OpenActivityAction<ModoSampleActivity>(context))
},
ModoButtonSpec("Legacy Activity integration") {
navigation?.dispatch(OpenActivityAction<ModoLegacyIntegrationActivity>(context))
},
ModoButtonSpec("Quick Start Activity") {
navigation?.dispatch(OpenActivityAction<QuickStartActivity>(context))
},
ModoButtonSpec("Fragment integration") {
navigation?.dispatch(OpenActivityAction<ModoFragmentIntegrationActivity>(context))
},
activity?.let { activity ->
if (canOpenFragment) {
ModoButtonSpec("New fragment") {
activity.supportFragmentManager.beginTransaction()
.replace(android.R.id.content, ModoFragment())
.addToBackStack("ModoFragment")
.commit()
}
} else {
null
}
},
)
),
GroupedButtonsState.Group(
title = "Dev playground",
listOf(
ModoButtonSpec("Movable Content") {
navigation?.forward(MovableContentPlaygroundScreen())
},
ModoButtonSpec("Animation Playground") {
navigation?.forward(AnimationPlaygroundScreen())
},
)
),
)
)
}
}