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

commonMain.tech.skot.core.components.SKPagerWithTabs.kt Maven / Gradle / Ivy

There is a newer version: 1.2.9
Show newest version
package tech.skot.core.components

import tech.skot.core.di.coreViewInjector
import tech.skot.core.view.Icon
import tech.skot.core.view.SKSpannedString
import tech.skot.core.view.skSpannedString

class SKPagerWithTabs(
    initialPages: List = emptyList(),
    onSwipeToPage: ((index: Int) -> Unit)? = null,
    initialSelectedPageIndex: Int = 0,
    swipable: Boolean = false,
    initialTabsVisibility: SKPagerWithTabsVC.Visibility = SKPagerWithTabsVC.Visibility.Visible
) : SKComponent() {
    sealed class TabPage(val screen: SKScreen<*>)
    class Page(screen: SKScreen<*>, val label: String) : TabPage(screen)
    class ConfigurableTabPage(
        screen: SKScreen<*>,
        val tabConfig: TabConfig
    ) : TabPage(screen)

    sealed class TabConfig {
        class CustomTab(val component: SKComponent<*>) : TabConfig()
        class SpannableTitleTab(val title: SKSpannedString) : TabConfig()
        class TitleTab(val title: String) : TabConfig()
        class IconTitleTab(val title: SKSpannedString, val icon: Icon) : TabConfig()
        class IconTab(val icon: Icon) : TabConfig()
    }


    val pager = SKPager(
        initialScreens = initialPages.map { it.screen },
        onSwipeToPage = onSwipeToPage,
        initialSelectedPageIndex = initialSelectedPageIndex,
        swipable = swipable
    )

    var pages: List = initialPages
        set(value) {
            val newComponents = value
                .filterIsInstance().map { it.tabConfig }
                .filterIsInstance().map { it.component }

            field.filterIsInstance().map { it.tabConfig }
                .filterIsInstance().forEach {
                    if (!newComponents.contains(it.component)) {
                        it.component.onRemove()
                    }
                }

            pager.screens = value.map { it.screen }
            view.tabConfigs = mapTabConfig(value)
            field = value
        }

    var tabsVisibility: SKPagerWithTabsVC.Visibility = initialTabsVisibility
        set(value) {
            view.tabsVisibility = value
            field = value
        }

    private fun mapTabConfig(values: List): List {
        return values.map {
            when (it) {
                is Page -> {
                    SKPagerWithTabsVC.TabConfig.Title(it.label)
                }
                is ConfigurableTabPage -> {
                    when (val tab = it.tabConfig) {
                        is TabConfig.CustomTab -> {
                            SKPagerWithTabsVC.TabConfig.Custom(tab.component.view)
                        }
                        is TabConfig.TitleTab -> {
                            SKPagerWithTabsVC.TabConfig.Title(tab.title)
                        }
                        is TabConfig.IconTitleTab -> {
                            SKPagerWithTabsVC.TabConfig.IconTitle(tab.title, tab.icon)
                        }
                        is TabConfig.IconTab -> {
                            SKPagerWithTabsVC.TabConfig.Icon(tab.icon)
                        }
                        is TabConfig.SpannableTitleTab -> {
                            SKPagerWithTabsVC.TabConfig.SpannableTitle(tab.title)
                        }
                    }
                }
            }
        }
    }

    override val view =
        coreViewInjector.pagerWithTabs(pager.view, mapTabConfig(initialPages), initialTabsVisibility)

    override fun onRemove() {
        pager.onRemove()
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy