From 48ff3ec56dcb4756fcc1eb29ccee6bf318ce432f Mon Sep 17 00:00:00 2001 From: Dahoon Kim Date: Sat, 25 Jun 2022 03:20:15 +0900 Subject: [PATCH] Cache the latest screen and use it on scopedStore not to replay initial screen. --- .../TCACoordinators/TCARouter/TCARouter.swift | 8 +++- .../TCACoordinatorsExampleApp.swift | 40 +++++++++---------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/Sources/TCACoordinators/TCARouter/TCARouter.swift b/Sources/TCACoordinators/TCARouter/TCARouter.swift index 82243bb..d728d22 100644 --- a/Sources/TCACoordinators/TCARouter/TCARouter.swift +++ b/Sources/TCACoordinators/TCARouter/TCARouter.swift @@ -22,9 +22,13 @@ public struct TCARouter< func scopedStore(index: Int, screen: Screen) -> Store { let id = identifier(screen, index) + var screen = screen return store.scope( - state: { routes($0)[safe: index]?.screen ?? screen }, - action: { action(id, $0) } + state: { + screen = routes($0)[safe: index]?.screen ?? screen + return screen + }, + action: { action(id, $0) } ) } diff --git a/TCACoordinatorsExample/TCACoordinatorsExample/TCACoordinatorsExampleApp.swift b/TCACoordinatorsExample/TCACoordinatorsExample/TCACoordinatorsExampleApp.swift index 4f5e216..cd37d5a 100644 --- a/TCACoordinatorsExample/TCACoordinatorsExample/TCACoordinatorsExampleApp.swift +++ b/TCACoordinatorsExample/TCACoordinatorsExample/TCACoordinatorsExampleApp.swift @@ -23,27 +23,25 @@ struct MainTabCoordinatorView: View { let store: Store var body: some View { - WithViewStore(store) { _ in - TabView { - IndexedCoordinatorView( - store: store.scope( - state: \MainTabCoordinatorState.indexed, - action: MainTabCoordinatorAction.indexed - ) - ).tabItem { Text("Indexed") } - IdentifiedCoordinatorView( - store: store.scope( - state: \MainTabCoordinatorState.identified, - action: MainTabCoordinatorAction.identified - ) - ).tabItem { Text("Identified") } - AppCoordinatorView( - store: store.scope( - state: \MainTabCoordinatorState.app, - action: MainTabCoordinatorAction.app - ) - ).tabItem { Text("App") } - } + TabView { + IndexedCoordinatorView( + store: store.scope( + state: \MainTabCoordinatorState.indexed, + action: MainTabCoordinatorAction.indexed + ) + ).tabItem { Text("Indexed") } + IdentifiedCoordinatorView( + store: store.scope( + state: \MainTabCoordinatorState.identified, + action: MainTabCoordinatorAction.identified + ) + ).tabItem { Text("Identified") } + AppCoordinatorView( + store: store.scope( + state: \MainTabCoordinatorState.app, + action: MainTabCoordinatorAction.app + ) + ).tabItem { Text("App") } } } }