commonMain.com.attafitamim.navigation.router.base.navigator.SimpleNavigationBuffer.kt Maven / Gradle / Ivy
package com.attafitamim.navigation.router.base.navigator
import com.attafitamim.navigation.router.core.commands.Command
import com.attafitamim.navigation.router.core.commands.MessageHandler
import com.attafitamim.navigation.router.core.navigator.NavigationBuffer
import com.attafitamim.navigation.router.core.navigator.Navigator
import com.attafitamim.navigation.router.core.navigator.NavigatorHolder
class SimpleNavigationBuffer(
private val messageHandler: MessageHandler
) : NavigationBuffer, NavigatorHolder {
private var navigators = mutableMapOf()
private val pendingCommands = mutableMapOf>>()
override fun setNavigator(navigator: Navigator, navigatorKey: String?) {
if (navigators.containsKey(navigatorKey)) {
pendingCommands[navigatorKey]?.clear()
}
navigators[navigatorKey] = navigator
pendingCommands[navigatorKey]?.apply {
forEach(navigator::applyCommands)
clear()
}
}
override fun removeNavigator(navigatorKey: String?) {
navigators.remove(navigatorKey)
}
override fun applyCommands(navigatorKey: String?, commands: Array) {
messageHandler.post {
val navigator = navigators[navigatorKey]
navigator?.applyCommands(commands) ?: provideCommandsList(navigatorKey).add(commands)
}
}
private fun provideCommandsList(navigatorKey: String?): MutableList> =
pendingCommands[navigatorKey] ?: mutableListOf>().also { list ->
pendingCommands[navigatorKey] = list
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy