forked from joreilly/Confetti
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConferenceComponent.kt
More file actions
123 lines (104 loc) · 4.5 KB
/
ConferenceComponent.kt
File metadata and controls
123 lines (104 loc) · 4.5 KB
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
package dev.johnoreilly.confetti.decompose
import com.arkivanov.decompose.ComponentContext
import com.arkivanov.decompose.router.stack.ChildStack
import com.arkivanov.decompose.router.stack.StackNavigation
import com.arkivanov.decompose.router.stack.bringToFront
import com.arkivanov.decompose.router.stack.childStack
import com.arkivanov.decompose.router.stack.navigate
import com.arkivanov.decompose.router.stack.pop
import com.arkivanov.decompose.router.stack.push
import com.arkivanov.decompose.value.Value
import com.arkivanov.essenty.backhandler.BackHandlerOwner
import dev.johnoreilly.confetti.auth.User
import dev.johnoreilly.confetti.decompose.ConferenceComponent.Child
import kotlinx.serialization.Serializable
import org.koin.core.component.KoinComponent
interface ConferenceComponent : BackHandlerOwner {
val stack: Value<ChildStack<*, Child>>
val conferenceThemeColor: String?
fun onBackClicked()
fun onBackClicked(toIndex: Int)
sealed class Child {
class Home(val component: HomeComponent) : Child()
class SessionDetails(val component: SessionDetailsComponent) : Child()
class SpeakerDetails(val component: SpeakerDetailsComponent) : Child()
class Settings(val component: SettingsComponent?) : Child()
}
}
class DefaultConferenceComponent(
componentContext: ComponentContext,
private val user: User?,
private val conference: String,
override val conferenceThemeColor: String?,
private val onSwitchConference: () -> Unit,
private val onSignOut: () -> Unit,
private val onSignIn: () -> Unit,
private val settingsComponent: SettingsComponent?
) : ConferenceComponent, KoinComponent, ComponentContext by componentContext {
private val navigation = StackNavigation<Config>()
override val stack: Value<ChildStack<*, Child>> =
childStack(
source = navigation,
serializer = Config.serializer(),
initialConfiguration = Config.Home,
handleBackButton = true,
childFactory = ::child,
)
private fun child(config: Config, componentContext: ComponentContext): Child =
when (config) {
is Config.Home ->
Child.Home(
DefaultHomeComponent(
componentContext = componentContext,
conference = conference,
user = user,
onSwitchConference = onSwitchConference,
onSessionSelected = { navigation.push(Config.SessionDetails(sessionId = it)) },
onSpeakerSelected = { navigation.push(Config.SpeakerDetails(speakerId = it)) },
onSignIn = onSignIn,
onSignOut = onSignOut,
onShowSettings = { navigation.push(Config.Settings) },
)
)
is Config.SessionDetails ->
Child.SessionDetails(
DefaultSessionDetailsComponent(
componentContext = componentContext,
conference = conference,
sessionId = config.sessionId,
user = user,
onFinished = navigation::pop,
onSignIn = onSignIn,
onSpeakerSelected = { navigation.bringToFront(Config.SpeakerDetails(speakerId = it)) },
)
)
is Config.SpeakerDetails ->
Child.SpeakerDetails(
DefaultSpeakerDetailsComponent(
componentContext = componentContext,
conference = conference,
speakerId = config.speakerId,
onSessionSelected = { navigation.bringToFront(Config.SessionDetails(sessionId = it)) },
onFinished = navigation::pop,
)
)
is Config.Settings -> Child.Settings(settingsComponent)
}
override fun onBackClicked() {
navigation.pop()
}
override fun onBackClicked(toIndex: Int) {
navigation.navigate { it.take(toIndex + 1) }
}
@Serializable
private sealed class Config {
@Serializable
data object Home : Config()
@Serializable
data class SessionDetails(val sessionId: String) : Config()
@Serializable
data class SpeakerDetails(val speakerId: String) : Config()
@Serializable
data object Settings : Config()
}
}