mirror of
https://github.com/pointfreeco/swift-composable-architecture.git
synced 2025-12-20 09:11:33 +01:00
* wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * Silence test warnings * wip * wip * wip * wip * wip * wip * wip * Revert "wip" This reverts commited6cc24adb. * Revert "Revert "wip"" This reverts commit8358990b1a. * wip * wip * wip * wip * wip * update a bunch of docs * wip * wip * wip * fix * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * Kill integration tests for now * wip * wip * wip * wip * updating docs for @Reducer macro * replaced more Reducer protocols with @Reducer * Fixed some broken docc references * wip * Some @Reducer docs * more docs * convert some old styles to new style * wip * wip * wip * wip * wip * wip * wip * bump * update tutorials to use body * update tutorials to use DML on destination state enum * Add diagnostic * wip * updated a few more tests * wip * wip * Add another gotcha * wip * wip * wip * Add dynamic lookup to presentation state/action * wip * wip * Better lookup * wip * wip * wip * IdentifiedAction * wip * wip * wip * wip * wip * wip * wip * wip * wip * fixes * wip * wip * added migration guide for new scope operation * migration guide for new navigation view modifiers * wip * fix * wip * wip * wip * wip * wip * wip * wip * wip * wip * fix * fix * wip * wip * remove for now * wip * wip * simplify scope * wip * updated some docs * migration guides * more migration guide * fix ci * fix * soft deprecate all apis using AnyCasePath * wip * Fix * fix tests * updated tests * swift-format 509 compatibility * wip * wip * Update Sources/ComposableArchitecture/Macros.swift Co-authored-by: Mateusz Bąk <bakmatthew@icloud.com> * wip * wip * update optional state case study * remove initializer * Don't use @State for BasicsView integration demo * fix tests * remove reduce diagnostics for now * diagnose error not warning * Update Sources/ComposableArchitecture/Macros.swift Co-authored-by: Jesse Tipton <jesse@jessetipton.com> * wip * move integration tests to cron * Revert "move integration tests to cron" This reverts commitf9bdf2f04b. * disable flakey tests on CI * wip * wip * fix migration guide * fix docs * fix deprecation messages * wip * wip * missing deprecation * soft * wip * update migration guide * Fix resolved * update migration guide * fix test * format * wip * fix * wip * wip * wip * wip * wip * wip * wip * wip * fix * wip * wip --------- Co-authored-by: Brandon Williams <mbrandonw@hey.com> Co-authored-by: Mateusz Bąk <bakmatthew@icloud.com> Co-authored-by: Brandon Williams <135203+mbrandonw@users.noreply.github.com> Co-authored-by: Jesse Tipton <jesse@jessetipton.com>
270 lines
8.2 KiB
Swift
270 lines
8.2 KiB
Swift
import SwiftUI
|
|
|
|
extension View {
|
|
@_spi(Presentation)
|
|
public func presentation<State, Action, Content: View>(
|
|
store: Store<PresentationState<State>, PresentationAction<Action>>,
|
|
@ViewBuilder body: @escaping (
|
|
_ content: Self,
|
|
_ isPresented: Binding<Bool>,
|
|
_ destination: DestinationContent<State, Action>
|
|
) -> Content
|
|
) -> some View {
|
|
self.presentation(store: store) { `self`, $item, destination in
|
|
body(self, $item.isPresent(), destination)
|
|
}
|
|
}
|
|
|
|
@_disfavoredOverload
|
|
@_spi(Presentation)
|
|
public func presentation<State, Action, Content: View>(
|
|
store: Store<PresentationState<State>, PresentationAction<Action>>,
|
|
@ViewBuilder body: @escaping (
|
|
_ content: Self,
|
|
_ item: Binding<AnyIdentifiable?>,
|
|
_ destination: DestinationContent<State, Action>
|
|
) -> Content
|
|
) -> some View {
|
|
self.presentation(
|
|
store: store,
|
|
state: { $0 },
|
|
id: { $0.wrappedValue.map { _ in ObjectIdentifier(State.self) } },
|
|
action: { $0 },
|
|
body: body
|
|
)
|
|
}
|
|
|
|
@_spi(Presentation)
|
|
public func presentation<
|
|
State,
|
|
Action,
|
|
DestinationState,
|
|
DestinationAction,
|
|
Content: View
|
|
>(
|
|
store: Store<PresentationState<State>, PresentationAction<Action>>,
|
|
state toDestinationState: @escaping (_ state: State) -> DestinationState?,
|
|
action fromDestinationAction: @escaping (_ destinationAction: DestinationAction) -> Action,
|
|
@ViewBuilder body: @escaping (
|
|
_ content: Self,
|
|
_ isPresented: Binding<Bool>,
|
|
_ destination: DestinationContent<DestinationState, DestinationAction>
|
|
) -> Content
|
|
) -> some View {
|
|
self.presentation(
|
|
store: store, state: toDestinationState, action: fromDestinationAction
|
|
) { `self`, $item, destination in
|
|
body(self, $item.isPresent(), destination)
|
|
}
|
|
}
|
|
|
|
@_disfavoredOverload
|
|
@_spi(Presentation)
|
|
public func presentation<
|
|
State,
|
|
Action,
|
|
DestinationState,
|
|
DestinationAction,
|
|
Content: View
|
|
>(
|
|
store: Store<PresentationState<State>, PresentationAction<Action>>,
|
|
state toDestinationState: @escaping (_ state: State) -> DestinationState?,
|
|
action fromDestinationAction: @escaping (_ destinationAction: DestinationAction) -> Action,
|
|
@ViewBuilder body: @escaping (
|
|
_ content: Self,
|
|
_ item: Binding<AnyIdentifiable?>,
|
|
_ destination: DestinationContent<DestinationState, DestinationAction>
|
|
) -> Content
|
|
) -> some View {
|
|
self.presentation(
|
|
store: store,
|
|
state: toDestinationState,
|
|
id: { $0.id },
|
|
action: fromDestinationAction,
|
|
body: body
|
|
)
|
|
}
|
|
|
|
@_spi(Presentation)
|
|
@ViewBuilder
|
|
public func presentation<
|
|
State,
|
|
Action,
|
|
DestinationState,
|
|
DestinationAction,
|
|
Content: View
|
|
>(
|
|
store: Store<PresentationState<State>, PresentationAction<Action>>,
|
|
state toDestinationState: @escaping (State) -> DestinationState?,
|
|
id toID: @escaping (PresentationState<State>) -> AnyHashable?,
|
|
action fromDestinationAction: @escaping (DestinationAction) -> Action,
|
|
@ViewBuilder body: @escaping (
|
|
Self,
|
|
Binding<AnyIdentifiable?>,
|
|
DestinationContent<DestinationState, DestinationAction>
|
|
) -> Content
|
|
) -> some View {
|
|
PresentationStore(
|
|
store, state: toDestinationState, id: toID, action: fromDestinationAction
|
|
) { $item, destination in
|
|
body(self, $item, destination)
|
|
}
|
|
}
|
|
}
|
|
|
|
@_spi(Presentation)
|
|
public struct PresentationStore<
|
|
State, Action, DestinationState, DestinationAction, Content: View
|
|
>: View {
|
|
let store: Store<PresentationState<State>, PresentationAction<Action>>
|
|
let toDestinationState: (State) -> DestinationState?
|
|
let toID: (PresentationState<State>) -> AnyHashable?
|
|
let fromDestinationAction: (DestinationAction) -> Action
|
|
let content:
|
|
(
|
|
Binding<AnyIdentifiable?>,
|
|
DestinationContent<DestinationState, DestinationAction>
|
|
) -> Content
|
|
|
|
@ObservedObject var viewStore: ViewStore<PresentationState<State>, PresentationAction<Action>>
|
|
|
|
public init(
|
|
_ store: Store<PresentationState<State>, PresentationAction<Action>>,
|
|
@ViewBuilder content: @escaping (
|
|
_ isPresented: Binding<Bool>,
|
|
_ destination: DestinationContent<DestinationState, DestinationAction>
|
|
) -> Content
|
|
) where State == DestinationState, Action == DestinationAction {
|
|
self.init(store) { $item, destination in
|
|
content($item.isPresent(), destination)
|
|
}
|
|
}
|
|
|
|
@_disfavoredOverload
|
|
public init(
|
|
_ store: Store<PresentationState<State>, PresentationAction<Action>>,
|
|
@ViewBuilder content: @escaping (
|
|
_ item: Binding<AnyIdentifiable?>,
|
|
_ destination: DestinationContent<DestinationState, DestinationAction>
|
|
) -> Content
|
|
) where State == DestinationState, Action == DestinationAction {
|
|
self.init(
|
|
store,
|
|
state: { $0 },
|
|
action: { $0 },
|
|
content: content
|
|
)
|
|
}
|
|
|
|
public init(
|
|
_ store: Store<PresentationState<State>, PresentationAction<Action>>,
|
|
state toDestinationState: @escaping (_ state: State) -> DestinationState?,
|
|
action fromDestinationAction: @escaping (_ destinationAction: DestinationAction) -> Action,
|
|
@ViewBuilder content: @escaping (
|
|
_ isPresented: Binding<Bool>,
|
|
_ destination: DestinationContent<DestinationState, DestinationAction>
|
|
) -> Content
|
|
) {
|
|
self.init(
|
|
store, state: toDestinationState, action: fromDestinationAction
|
|
) { $item, destination in
|
|
content($item.isPresent(), destination)
|
|
}
|
|
}
|
|
|
|
@_disfavoredOverload
|
|
public init(
|
|
_ store: Store<PresentationState<State>, PresentationAction<Action>>,
|
|
state toDestinationState: @escaping (_ state: State) -> DestinationState?,
|
|
action fromDestinationAction: @escaping (_ destinationAction: DestinationAction) -> Action,
|
|
@ViewBuilder content: @escaping (
|
|
_ item: Binding<AnyIdentifiable?>,
|
|
_ destination: DestinationContent<DestinationState, DestinationAction>
|
|
) -> Content
|
|
) {
|
|
self.init(
|
|
store,
|
|
state: toDestinationState,
|
|
id: { $0.id },
|
|
action: fromDestinationAction,
|
|
content: content
|
|
)
|
|
}
|
|
|
|
fileprivate init<ID: Hashable>(
|
|
_ store: Store<PresentationState<State>, PresentationAction<Action>>,
|
|
state toDestinationState: @escaping (State) -> DestinationState?,
|
|
id toID: @escaping (PresentationState<State>) -> ID?,
|
|
action fromDestinationAction: @escaping (DestinationAction) -> Action,
|
|
content: @escaping (
|
|
_ item: Binding<AnyIdentifiable?>,
|
|
_ destination: DestinationContent<DestinationState, DestinationAction>
|
|
) -> Content
|
|
) {
|
|
let store = store.scope(
|
|
state: { $0 },
|
|
id: nil,
|
|
action: { $0 },
|
|
isInvalid: { $0.wrappedValue.flatMap(toDestinationState) == nil },
|
|
removeDuplicates: nil
|
|
)
|
|
let viewStore = ViewStore(store, observe: { $0 }, removeDuplicates: { toID($0) == toID($1) })
|
|
|
|
self.store = store
|
|
self.toDestinationState = toDestinationState
|
|
self.toID = toID
|
|
self.fromDestinationAction = fromDestinationAction
|
|
self.content = content
|
|
self.viewStore = viewStore
|
|
}
|
|
|
|
public var body: some View {
|
|
let id = self.toID(self.viewStore.state)
|
|
self.content(
|
|
self.viewStore.binding(
|
|
get: {
|
|
$0.wrappedValue.flatMap(toDestinationState) != nil
|
|
? toID($0).map { AnyIdentifiable(Identified($0) { $0 }) }
|
|
: nil
|
|
},
|
|
compactSend: {
|
|
guard
|
|
$0 == nil,
|
|
self.viewStore.wrappedValue != nil,
|
|
id == nil || self.toID(self.viewStore.state) == id
|
|
else { return nil }
|
|
return .dismiss
|
|
}
|
|
),
|
|
DestinationContent(
|
|
store: self.store.scope(
|
|
state: { $0.wrappedValue.flatMap(self.toDestinationState) },
|
|
action: { .presented(fromDestinationAction($0)) }
|
|
)
|
|
)
|
|
)
|
|
}
|
|
}
|
|
|
|
@_spi(Presentation)
|
|
public struct AnyIdentifiable: Identifiable {
|
|
public let id: AnyHashable
|
|
|
|
public init<Base: Identifiable>(_ base: Base) {
|
|
self.id = base.id
|
|
}
|
|
}
|
|
|
|
@_spi(Presentation)
|
|
public struct DestinationContent<State, Action> {
|
|
let store: Store<State?, Action>
|
|
|
|
public func callAsFunction<Content: View>(
|
|
@ViewBuilder _ body: @escaping (_ store: Store<State, Action>) -> Content
|
|
) -> some View {
|
|
IfLetStore(
|
|
self.store.scope(state: returningLastNonNilValue { $0 }, action: { $0 }), then: body
|
|
)
|
|
}
|
|
}
|