commonMain.com.copperleaf.ballast.navigation.vm.ballastUtils.kt Maven / Gradle / Ivy
package com.copperleaf.ballast.navigation.vm
import com.copperleaf.ballast.BallastInterceptor
import com.copperleaf.ballast.BallastInterceptorScope
import com.copperleaf.ballast.BallastNotification
import com.copperleaf.ballast.BallastViewModel
import com.copperleaf.ballast.BallastViewModelConfiguration
import com.copperleaf.ballast.EventHandler
import com.copperleaf.ballast.EventHandlerScope
import com.copperleaf.ballast.InputHandler
import com.copperleaf.ballast.InputHandlerScope
import com.copperleaf.ballast.core.BasicViewModel
import com.copperleaf.ballast.core.BootstrapInterceptor
import com.copperleaf.ballast.core.FifoInputStrategy
import com.copperleaf.ballast.navigation.routing.Route
import com.copperleaf.ballast.navigation.routing.RouterContract
import com.copperleaf.ballast.navigation.routing.RoutingTable
import com.copperleaf.ballast.navigation.routing.build
import com.copperleaf.ballast.navigation.routing.directions
import com.copperleaf.ballast.navigation.routing.isStatic
import com.copperleaf.ballast.plusAssign
import com.copperleaf.ballast.withViewModel
// Configure Router ViewModel
// ---------------------------------------------------------------------------------------------------------------------
/**
* Configure a ViewModel to be used as a Router. If [initialRoute] is provided, it will be automatically set as the
* initial route using a [BootstrapInterceptor]. You may wish to keep this value as `null` to load the initial route
* from some other means.
*/
public fun BallastViewModelConfiguration.Builder.withRouter(
routingTable: RoutingTable,
initialRoute: T?,
): RouterBuilder {
return this
.withViewModel(
initialState = RouterContract.State(routingTable = routingTable),
inputHandler = RouterInputHandlerImpl(),
name = "Router",
)
.apply {
this.inputStrategy = FifoInputStrategy.typed()
initialRoute?.let { initialRoute ->
check(initialRoute.isStatic()) {
"For a Route to be used as a Start Destination, it must be fully static. All path segments and " +
"declared query parameters must either be static or optional."
}
this += BootstrapInterceptor {
RouterContract.Inputs.GoToDestination(initialRoute.directions().build())
}
}
}
}
// Aliases to Ballast classes
// ---------------------------------------------------------------------------------------------------------------------
public typealias RouterBuilder = BallastViewModelConfiguration.TypedBuilder<
RouterContract.Inputs,
RouterContract.Events,
RouterContract.State>
public typealias Router = BallastViewModel<
RouterContract.Inputs,
RouterContract.Events,
RouterContract.State>
public typealias BasicRouter = BasicViewModel<
RouterContract.Inputs,
RouterContract.Events,
RouterContract.State>
public typealias RouterInputHandler = InputHandler<
RouterContract.Inputs,
RouterContract.Events,
RouterContract.State>
public typealias RouterInputHandlerScope = InputHandlerScope<
RouterContract.Inputs,
RouterContract.Events,
RouterContract.State>
public typealias RouterEventHandler = EventHandler<
RouterContract.Inputs,
RouterContract.Events,
RouterContract.State>
public typealias RouterEventHandlerScope = EventHandlerScope<
RouterContract.Inputs,
RouterContract.Events,
RouterContract.State>
public typealias RouterInterceptor = BallastInterceptor<
RouterContract.Inputs,
RouterContract.Events,
RouterContract.State>
public typealias RouterInterceptorScope = BallastInterceptorScope<
RouterContract.Inputs,
RouterContract.Events,
RouterContract.State>
public typealias RouterNotification = BallastNotification<
RouterContract.Inputs,
RouterContract.Events,
RouterContract.State>
© 2015 - 2025 Weber Informatics LLC | Privacy Policy