diff --git a/secant/Features/Home/HomeStore.swift b/secant/Features/Home/HomeStore.swift index da0e456..9acaac9 100644 --- a/secant/Features/Home/HomeStore.swift +++ b/secant/Features/Home/HomeStore.swift @@ -2,7 +2,7 @@ import ComposableArchitecture import SwiftUI struct HomeState: Equatable { - enum Route: Equatable { + enum Route: Equatable, CaseIterable { case history case send case recoveryPhraseDisplay @@ -82,6 +82,15 @@ extension HomeViewStore { .map(\.id) } + func bindingForRoute(_ route: HomeState.Route) -> Binding { + self.binding( + get: { $0.route == route }, + send: { isActive in + return .updateRoute(isActive ? route : nil) + } + ) + } + var showHistoryBinding: Binding { self.binding( get: { $0.route == .history }, diff --git a/secant/Features/Home/Views/HomeView.swift b/secant/Features/Home/Views/HomeView.swift index f50fcc1..a9f9f39 100644 --- a/secant/Features/Home/Views/HomeView.swift +++ b/secant/Features/Home/Views/HomeView.swift @@ -4,79 +4,59 @@ import ComposableArchitecture struct HomeView: View { let store: Store + var navigationRouteValues: [RouteValue] = HomeState.Route.allCases + .enumerated() + .filter { $0.1 != .history } + .map { RouteValue(id: $0.0, route: $0.1) } + + var modalRoutes: [RouteValue] = HomeState.Route.allCases + .enumerated() + .filter { $0.1 == .history } + .map { RouteValue(id: $0.0, route: $0.1) } + + @ViewBuilder func view(for route: HomeState.Route) -> some View { + switch route { + case .history: + TransactionHistoryView(store: store.historyStore()) + case .send: + SendView( + store: .init( + initialState: .placeholder, + reducer: SendReducer.default( + whenDone: { HomeViewStore(store).send(.updateRoute(nil)) } + ).debug(), + environment: () + ) + ) + case .recoveryPhraseDisplay: + RecoveryPhraseDisplayView(store: .demo) + } + } + var body: some View { WithViewStore(store) { viewStore in VStack { - Button( - action: { viewStore.toggleShowingHistory() }, - label: { Text(viewStore.historyToggleString()) } - ) - .primaryButtonStyle - .frame(height: 50) + List { + Section(header: Text("Navigation Stack Routes")) { + ForEach(navigationRouteValues) { routeValue in + Text("\(String(describing: routeValue.route))") + .navigationLink( + isActive: viewStore.bindingForRoute(routeValue.route), + destination: { + view(for: routeValue.route) + } + ) + } + } - Button( - action: { viewStore.toggleSelectedTransaction() }, - label: { Text("Toggle Selected Transaction") } - ) - .primaryButtonStyle - .frame(height: 50) - - Button( - action: { viewStore.send(.updateRoute(.recoveryPhraseDisplay)) }, - label: { Text("Show Recovery Phrase Demo") } - ) - .primaryButtonStyle - .frame(height: 50) - - Button( - action: { viewStore.send(.updateRoute(.send)) }, - label: { Text("Go to Send") } - ) - .primaryButtonStyle - .frame(height: 50) - - Spacer() - - HStack { - VStack(alignment: .leading) { - Text("Route: \(String(dumping: viewStore.route))") - Text( - // swiftlint:disable:next line_length - "SelectedTransaction: \(String(dumping: viewStore.transactionHistoryState.route.map(/TransactionHistoryState.Route.showTransaction)))" + Section(header: Text("Other Actions")) { + Button( + action: { viewStore.toggleSelectedTransaction() }, + label: { Text("Toggle Selected Transaction") } ) } - .multilineTextAlignment(.leading) - .frame(maxWidth: .infinity, alignment: .leading) } - - Spacer() } - .padding(.horizontal, 30) - .navigationBarTitle("Home", displayMode: .inline) - .navigationLinkEmpty( - isActive: viewStore.showPhraseDisplayBinding, - destination: { - RecoveryPhraseDisplayView(store: .demo) - } - ) - .navigationLinkEmpty( - isActive: viewStore.showSendBinding, - destination: { - SendView( - store: .init( - initialState: .init( - transaction: .placeholder, - route: nil - ), - reducer: SendReducer.default( - whenDone: { viewStore.send(.updateRoute(nil)) } - ) - .debug(), - environment: () - ) - ) - } - ) .fullScreenCover( isPresented: viewStore.showHistoryBinding, content: { @@ -90,10 +70,18 @@ struct HomeView: View { } } ) + .navigationBarTitle("Home") } } } +struct RouteValue: Identifiable { + let id: Int + let route: HomeState.Route +} + +// MARK: - Previews + extension HomeStore { static var placeholder: HomeStore { HomeStore(