Ahmad Hassan
03/20/2023, 12:51 PMArkadii Ivanov
03/20/2023, 1:09 PMAhmad Hassan
03/20/2023, 5:34 PMArkadii Ivanov
03/20/2023, 5:37 PMAhmad Hassan
03/20/2023, 5:38 PMArkadii Ivanov
03/20/2023, 5:39 PMAhmad Hassan
03/20/2023, 5:40 PMArkadii Ivanov
03/20/2023, 5:40 PMAhmad Hassan
03/20/2023, 5:43 PMArkadii Ivanov
03/20/2023, 5:44 PMAhmad Hassan
03/20/2023, 5:47 PMArkadii Ivanov
03/20/2023, 5:48 PMAhmad Hassan
03/20/2023, 5:49 PMArkadii Ivanov
03/20/2023, 5:50 PMAhmad Hassan
03/20/2023, 5:50 PMArkadii Ivanov
03/20/2023, 5:51 PMAhmad Hassan
03/20/2023, 5:53 PMclass NoteRootComponent(
componentContext: ComponentContext,
) : ComponentContext by componentContext, KoinComponent {
private val navigation = StackNavigation<Config>()
private val _stack = childStack(
source = navigation,
initialConfiguration = Config.List,
handleBackButton = true,
childFactory = ::createChild
)
val stack: Value<ChildStack<*, Child>> = _stack
private fun createChild(config: Config, componentContext: ComponentContext): Child {
return when (config) {
is Config.List -> Child.List(noteList(componentContext))
is Config.Details -> Child.Details(noteDetail(config))
}
}
private fun noteList(componentContext: ComponentContext) =
NoteListViewModelComponent(componentContext, noteDataSource = get()) {
navigation.push(Config.Details(it))
}
private fun noteDetail(config: Config.Details): NoteDetailViewModelComponent {
return instanceKeeper.getOrCreate {
NoteDetailViewModelComponent(
Dispatchers.Main, noteDataSource = get(),
noteId = config.id
) {
navigation.pop()
}
}
}
sealed class Child {
class List(val component: NoteListViewModelComponent) : Child()
class Details(val component: NoteDetailViewModelComponent) : Child()
}
private sealed class Config : Parcelable {
@Parcelize
object List : Config()
@Parcelize
data class Details(val id: Long?) : Config()
}
}
Arkadii Ivanov
03/20/2023, 5:54 PMAhmad Hassan
03/20/2023, 5:57 PMArkadii Ivanov
03/20/2023, 6:00 PMAhmad Hassan
03/20/2023, 6:03 PMArkadii Ivanov
03/20/2023, 6:04 PMAhmad Hassan
03/20/2023, 6:24 PMArkadii Ivanov
03/20/2023, 6:26 PM