All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.ru.alexgladkov.odyssey.compose.controllers.MultiStackRootController.kt Maven / Gradle / Ivy

There is a newer version: 1.4.0-alpha05
Show newest version
package ru.alexgladkov.odyssey.compose.controllers

import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import ru.alexgladkov.odyssey.compose.RootController
import ru.alexgladkov.odyssey.compose.navigation.bottom_bar_navigation.TabInfo
import ru.alexgladkov.odyssey.compose.navigation.bottom_bar_navigation.TabsNavModel
import ru.alexgladkov.odyssey.core.breadcrumbs.Breadcrumb
import ru.alexgladkov.odyssey.core.configuration.RootConfiguration
import ru.alexgladkov.odyssey.core.configuration.RootControllerType

data class TabNavigationModel(
    val tabInfo: TabInfo,
    val rootController: RootController
)

class MultiStackRootController(
    val rootControllerType: RootControllerType,
    val tabsNavModel: TabsNavModel<*>
) : RootController(configuration = RootConfiguration(rootControllerType = rootControllerType)) {
    private val _tabItems: MutableList = mutableListOf()
    private var currentTabPosition: Int = -1
    private val _stackChangeObserver: MutableStateFlow =
        MutableStateFlow(null)

    override var debugName: String? = "MultiStackRootController"
    val stackChangeObserver = _stackChangeObserver.asStateFlow()
    val tabItems = _tabItems

    fun setupWithTabs(tabItems: List, startPosition: Int = 0) {
        if (startPosition >= tabItems.size) throw IllegalStateException("Setup error: Position must be less than tab items count")
        _tabItems.clear()
        _tabItems.addAll(tabItems)
        _stackChangeObserver.value = tabItems[startPosition]
    }

    @Deprecated("Use switchTab with position instead")
    fun switchTab(tabNavigationModel: TabNavigationModel) {
        switchTab(_tabItems.indexOf(tabNavigationModel))
    }

    fun switchTab(position: Int) {
        if (position >= _tabItems.size) throw IllegalStateException("Position must be less than tab items count")
        onScreenNavigate?.invoke(Breadcrumb.TabSwitch(currentTabPosition, position))
        currentTabPosition = position
        _stackChangeObserver.value = _tabItems[position]
    }

    override fun popBackStack() {
        val rootController = _stackChangeObserver.value?.rootController
        rootController?.popBackStack()
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy