Skip to content

Instantly share code, notes, and snippets.

@cjbrooks12
Created April 21, 2023 03:38
Show Gist options
  • Save cjbrooks12/d6934cfcb4e38ee3293e14d19007066d to your computer and use it in GitHub Desktop.
Save cjbrooks12/d6934cfcb4e38ee3293e14d19007066d to your computer and use it in GitHub Desktop.
enum class AppScreen(
routeFormat: String,
override val annotations: Set<RouteAnnotation> = emptySet(),
) : Route {
Home("/app/home"),
PostList("/app/posts?sort={?}"),
PostDetails("/app/posts/{postId}"),
;
override val matcher: RouteMatcher = RouteMatcher.create(routeFormat)
}
import Combine
import SwiftUI
import shared
struct BallastNavigationView: View {
@ObservedObject var vm = BallastObservable<
RouterContract.Inputs<AppScreen>,
RouterContract.Events<AppScreen>,
RouterContract.State<AppScreen>
>(
viewModelFactory: { IosRouterViewModel() },
)
var body: some View {
BallastNavigationViewContent(
vmState: observableModel.vmState,
postInput: observableModel.postInput
)
.withViewModel(vm) {
vm.activate()
}
}
}
struct BallastNavigationViewContent: View {
var vmState: ExampleContract.State
var postInput: (ExampleContract.Inputs) -> Void
var body: some View {
RoutingUtilsKt.renderCurrentDestination(
route = { match, route ->
switch route {
case .Home:
HomeView()
case .PostList:
PostListView(
match.queryParamters["sort"]
)
case .Home:
PostDetailsView(
match.pathParamters["postId"]
)
},
notFound = { NotFoundView() }
)
}
}
class IosRouterViewModel() : IosViewModel<
<RouterContract.Inputs<AppScreen>,
RouterContract.Events<AppScreen>,
RouterContract.State<AppScreen>
>(
config = BallastViewModelConfiguration.Builder()
.withRouter(RoutingTable.fromEnum(AppScreens.values()), AppScreens.Home)
.build(),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment