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 PMArkadii Ivanov
03/20/2023, 5:38 PMAhmad Hassan
03/20/2023, 5:38 PMAhmad Hassan
03/20/2023, 5:39 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 PMArkadii Ivanov
03/20/2023, 5:45 PMAhmad Hassan
03/20/2023, 5:47 PMArkadii Ivanov
03/20/2023, 5:48 PMArkadii Ivanov
03/20/2023, 5:49 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 PMArkadii Ivanov
03/20/2023, 5:52 PMArkadii Ivanov
03/20/2023, 5:53 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 PMArkadii Ivanov
03/20/2023, 5:55 PMAhmad Hassan
03/20/2023, 5:57 PMAhmad Hassan
03/20/2023, 5:58 PMArkadii Ivanov
03/20/2023, 6:00 PMArkadii Ivanov
03/20/2023, 6:01 PMArkadii Ivanov
03/20/2023, 6:01 PMAhmad Hassan
03/20/2023, 6:03 PMArkadii Ivanov
03/20/2023, 6:04 PMArkadii Ivanov
03/20/2023, 6:04 PMArkadii Ivanov
03/20/2023, 6:11 PMAhmad Hassan
03/20/2023, 6:24 PMArkadii Ivanov
03/20/2023, 6:26 PMFabio Sánchez
08/22/2024, 12:25 PMArkadii Ivanov
08/22/2024, 12:41 PMInstanceKeeper.Instance
interface and use InstanceKeeper
in the hosting component. But if that's not possible, you can try https://arkivanov.github.io/Decompose/tips-tricks/composable-viewmodel/Fabio Sánchez
08/22/2024, 2:10 PMFabio Sánchez
08/22/2024, 2:16 PMArkadii Ivanov
08/22/2024, 2:25 PM