-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4959aa0
commit 44c1f87
Showing
4 changed files
with
109 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 70 additions & 67 deletions
137
Sources/ComposableArchitecture/SwiftUI/NavigationDestination.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,81 @@ | ||
import SwiftUI | ||
|
||
extension View { | ||
@available(iOS 16, macOS 13, tvOS 16, watchOS 9, *) | ||
public func navigationDestination<State, Action, Destination: View>( | ||
store: Store<PresentationState<State>, PresentationAction<Action>>, | ||
@ViewBuilder destination: @escaping (Store<State, Action>) -> Destination | ||
) -> some View { | ||
self.navigationDestination( | ||
store: store, state: { $0 }, action: { $0 }, destination: destination | ||
) | ||
} | ||
#if swift(>=5.7) | ||
extension View { | ||
@available(iOS 16, macOS 13, tvOS 16, watchOS 9, *) | ||
public func navigationDestination<State, Action, Destination: View>( | ||
store: Store<PresentationState<State>, PresentationAction<Action>>, | ||
@ViewBuilder destination: @escaping (Store<State, Action>) -> Destination | ||
) -> some View { | ||
self.navigationDestination( | ||
store: store, state: { $0 }, action: { $0 }, destination: destination | ||
) | ||
} | ||
|
||
@available(iOS 16, macOS 13, tvOS 16, watchOS 9, *) | ||
public func navigationDestination< | ||
State, Action, DestinationState, DestinationAction, Destination: View | ||
>( | ||
store: Store<PresentationState<State>, PresentationAction<Action>>, | ||
state toDestinationState: @escaping (State) -> DestinationState?, | ||
action fromDestinationAction: @escaping (DestinationAction) -> Action, | ||
@ViewBuilder destination: @escaping (Store<DestinationState, DestinationAction>) -> Destination | ||
) -> some View { | ||
self.modifier( | ||
PresentationNavigationDestinationModifier( | ||
store: store, | ||
state: toDestinationState, | ||
action: fromDestinationAction, | ||
content: destination | ||
@available(iOS 16, macOS 13, tvOS 16, watchOS 9, *) | ||
public func navigationDestination< | ||
State, Action, DestinationState, DestinationAction, Destination: View | ||
>( | ||
store: Store<PresentationState<State>, PresentationAction<Action>>, | ||
state toDestinationState: @escaping (State) -> DestinationState?, | ||
action fromDestinationAction: @escaping (DestinationAction) -> Action, | ||
@ViewBuilder destination: @escaping (Store<DestinationState, DestinationAction>) -> | ||
Destination | ||
) -> some View { | ||
self.modifier( | ||
PresentationNavigationDestinationModifier( | ||
store: store, | ||
state: toDestinationState, | ||
action: fromDestinationAction, | ||
content: destination | ||
) | ||
) | ||
) | ||
} | ||
} | ||
} | ||
|
||
@available(iOS 16, macOS 13, tvOS 16, watchOS 9, *) | ||
private struct PresentationNavigationDestinationModifier< | ||
State, | ||
Action, | ||
DestinationState, | ||
DestinationAction, | ||
CoverContent: View | ||
>: ViewModifier { | ||
let store: Store<PresentationState<State>, PresentationAction<Action>> | ||
@StateObject var viewStore: ViewStore<Bool, PresentationAction<Action>> | ||
let toDestinationState: (State) -> DestinationState? | ||
let fromDestinationAction: (DestinationAction) -> Action | ||
let coverContent: (Store<DestinationState, DestinationAction>) -> CoverContent | ||
@available(iOS 16, macOS 13, tvOS 16, watchOS 9, *) | ||
private struct PresentationNavigationDestinationModifier< | ||
State, | ||
Action, | ||
DestinationState, | ||
DestinationAction, | ||
CoverContent: View | ||
>: ViewModifier { | ||
let store: Store<PresentationState<State>, PresentationAction<Action>> | ||
@StateObject var viewStore: ViewStore<Bool, PresentationAction<Action>> | ||
let toDestinationState: (State) -> DestinationState? | ||
let fromDestinationAction: (DestinationAction) -> Action | ||
let coverContent: (Store<DestinationState, DestinationAction>) -> CoverContent | ||
|
||
init( | ||
store: Store<PresentationState<State>, PresentationAction<Action>>, | ||
state toDestinationState: @escaping (State) -> DestinationState?, | ||
action fromDestinationAction: @escaping (DestinationAction) -> Action, | ||
content coverContent: @escaping (Store<DestinationState, DestinationAction>) -> CoverContent | ||
) { | ||
self.store = store | ||
self._viewStore = StateObject( | ||
wrappedValue: ViewStore( | ||
store | ||
.filter { state, _ in state.wrappedValue != nil } | ||
.scope(state: { $0.wrappedValue.flatMap(toDestinationState) != nil }) | ||
init( | ||
store: Store<PresentationState<State>, PresentationAction<Action>>, | ||
state toDestinationState: @escaping (State) -> DestinationState?, | ||
action fromDestinationAction: @escaping (DestinationAction) -> Action, | ||
content coverContent: @escaping (Store<DestinationState, DestinationAction>) -> CoverContent | ||
) { | ||
self.store = store | ||
self._viewStore = StateObject( | ||
wrappedValue: ViewStore( | ||
store | ||
.filter { state, _ in state.wrappedValue != nil } | ||
.scope(state: { $0.wrappedValue.flatMap(toDestinationState) != nil }) | ||
) | ||
) | ||
) | ||
self.toDestinationState = toDestinationState | ||
self.fromDestinationAction = fromDestinationAction | ||
self.coverContent = coverContent | ||
} | ||
self.toDestinationState = toDestinationState | ||
self.fromDestinationAction = fromDestinationAction | ||
self.coverContent = coverContent | ||
} | ||
|
||
func body(content: Content) -> some View { | ||
content.navigationDestination(isPresented: self.viewStore.binding(send: .dismiss)) { | ||
IfLetStore( | ||
self.store.scope( | ||
state: returningLastNonNilValue { $0.wrappedValue.flatMap(self.toDestinationState) }, | ||
action: { .presented(self.fromDestinationAction($0)) } | ||
), | ||
then: self.coverContent | ||
) | ||
func body(content: Content) -> some View { | ||
content.navigationDestination(isPresented: self.viewStore.binding(send: .dismiss)) { | ||
IfLetStore( | ||
self.store.scope( | ||
state: returningLastNonNilValue { $0.wrappedValue.flatMap(self.toDestinationState) }, | ||
action: { .presented(self.fromDestinationAction($0)) } | ||
), | ||
then: self.coverContent | ||
) | ||
} | ||
} | ||
} | ||
} | ||
#endif |