Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface RootComponent {
- val childStack: Value<ChildStack<*, Child>>
- fun onBackClicked(toIndex: Int)
- fun onHomeTabClicked()
- fun onCartTabClicked()
- fun onProfileTabClicked()
- // Определяет все возможные дочерние компоненты
- sealed class Child {
- class HomeChild(val component: HomeComponent) : Child()
- class CartChild(val component: CartComponent) : Child()
- class ProfileChild(val component: ProfileComponent) : Child()
- }
- }
- class DefaultRootComponent(componentContext: ComponentContext):RootComponent,ComponentContext by componentContext {
- private val navigation = StackNavigation<Config>()
- override val childStack: Value<ChildStack<*, RootComponent.Child>> =
- childStack(
- source = navigation,
- serializer = Config.serializer(),
- initialConfiguration = Config.Home, // // Исходный дочерний компонент — Home
- handleBackButton = true, // Автоматически извлекать из стека при нажатии кнопки «Назад»
- childFactory = ::child,
- )
- private fun child(config: Config, componentContext: ComponentContext): RootComponent.Child =
- when (config) {
- is Config.Home -> HomeChild(homeComponent(componentContext))
- is Config.Cart -> CartChild(cartComponent(componentContext))
- is Config.Profile -> ProfileChild(profileComponent(componentContext))
- }
- private fun homeComponent(componentContext: ComponentContext): HomeComponent =
- DefaultHomeComponent(componentContext = componentContext)
- private fun cartComponent(componentContext: ComponentContext): CartComponent =
- DefaultCartComponent(componentContext = componentContext)
- private fun profileComponent(componentContext: ComponentContext): ProfileComponent =
- DefaultProfileComponent(componentContext = componentContext)
- override fun onBackClicked(toIndex: Int) { navigation.popTo(index = toIndex) }
- override fun onHomeTabClicked() {
- navigation.bringToFront(Config.Home)
- }
- override fun onCartTabClicked() {
- navigation.bringToFront(Config.Cart)
- }
- override fun onProfileTabClicked() {
- navigation.bringToFront(Config.Profile)
- }
- @Serializable // kotlinx-serialization plugin must be applied
- private sealed interface Config {
- @Serializable
- data object Home : Config
- @Serializable
- data object Cart : Config
- @Serializable
- data object Profile : Config
- // @Serializable
- // data class Cart(val item: String) : Config
- //
- // @Serializable
- // data class Profile(val item: String) : Config
- }
- }
- interface HomeComponent {
- val model: Value<Model>
- fun onItemClicked(item: String)
- data class Model(
- val items: List<String>,
- )
- }
- class DefaultHomeComponent(componentContext: ComponentContext) : HomeComponent {
- override val model: Value<HomeComponent.Model> = MutableValue(HomeComponent.Model(items = List(100) { "Item $it" }))
- override fun onItemClicked(item: String) {}
- }
- interface CartComponent
- class DefaultCartComponent(componentContext: ComponentContext) : CartComponent, ComponentContext by componentContext {
- // Omitted code
- }
- interface ProfileComponent
- class DefaultProfileComponent(componentContext: ComponentContext) : ProfileComponent, ComponentContext by componentContext {
- // Omitted code
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement