-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix builds, tests, warnings and run swift-format
- Loading branch information
Showing
10 changed files
with
868 additions
and
869 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
196 changes: 98 additions & 98 deletions
196
Sources/ComposableArchitecture/SwiftUI/IfLetStore.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,112 +1,112 @@ | ||
#if canImport(SwiftUI) | ||
import SwiftUI | ||
import SwiftUI | ||
|
||
/// A view that safely unwraps a store of optional state in order to show one of two views. | ||
/// | ||
/// When the underlying state is non-`nil`, the `then` closure will be performed with a ``Store`` | ||
/// that holds onto non-optional state, and otherwise the `else` closure will be performed. | ||
/// | ||
/// This is useful for deciding between two views to show depending on an optional piece of state: | ||
/// | ||
/// ```swift | ||
/// IfLetStore( | ||
/// store.scope(state: \SearchState.results, action: SearchAction.results), | ||
/// ) { | ||
/// SearchResultsView(store: $0) | ||
/// } else: { | ||
/// Text("Loading search results...") | ||
/// } | ||
/// ``` | ||
/// | ||
/// And for showing a sheet when a piece of state becomes non-`nil`: | ||
/// | ||
/// ```swift | ||
/// .sheet( | ||
/// isPresented: viewStore.binding( | ||
/// get: \.isGameActive, | ||
/// send: { $0 ? .startButtonTapped : .detailDismissed } | ||
/// ) | ||
/// ) { | ||
/// IfLetStore( | ||
/// self.store.scope(state: \.detail, action: AppAction.detail) | ||
/// ) { | ||
/// DetailView(store: $0) | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// A view that safely unwraps a store of optional state in order to show one of two views. | ||
/// | ||
/// When the underlying state is non-`nil`, the `then` closure will be performed with a ``Store`` | ||
/// that holds onto non-optional state, and otherwise the `else` closure will be performed. | ||
/// | ||
/// This is useful for deciding between two views to show depending on an optional piece of state: | ||
/// | ||
/// ```swift | ||
/// IfLetStore( | ||
/// store.scope(state: \SearchState.results, action: SearchAction.results), | ||
/// ) { | ||
/// SearchResultsView(store: $0) | ||
/// } else: { | ||
/// Text("Loading search results...") | ||
/// } | ||
/// ``` | ||
/// | ||
/// And for showing a sheet when a piece of state becomes non-`nil`: | ||
/// | ||
/// ```swift | ||
/// .sheet( | ||
/// isPresented: viewStore.binding( | ||
/// get: \.isGameActive, | ||
/// send: { $0 ? .startButtonTapped : .detailDismissed } | ||
/// ) | ||
/// ) { | ||
/// IfLetStore( | ||
/// self.store.scope(state: \.detail, action: AppAction.detail) | ||
/// ) { | ||
/// DetailView(store: $0) | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
public struct IfLetStore<State, Action, Content>: View where Content: View { | ||
private let content: (ViewStore<State?, Action>) -> Content | ||
private let store: Store<State?, Action> | ||
private let content: (ViewStore<State?, Action>) -> Content | ||
private let store: Store<State?, Action> | ||
|
||
/// Initializes an ``IfLetStore`` view that computes content depending on if a store of optional | ||
/// state is `nil` or non-`nil`. | ||
/// | ||
/// - Parameters: | ||
/// - store: A store of optional state. | ||
/// - ifContent: A function that is given a store of non-optional state and returns a view that | ||
/// is visible only when the optional state is non-`nil`. | ||
/// - elseContent: A view that is only visible when the optional state is `nil`. | ||
public init<IfContent, ElseContent>( | ||
_ store: Store<State?, Action>, | ||
@ViewBuilder then ifContent: @escaping (Store<State, Action>) -> IfContent, | ||
@ViewBuilder else elseContent: () -> ElseContent | ||
) where Content == _ConditionalContent<IfContent, ElseContent> { | ||
self.store = store | ||
let elseContent = elseContent() | ||
self.content = { viewStore in | ||
if var state = viewStore.state { | ||
return ViewBuilder.buildEither( | ||
first: ifContent( | ||
/// Initializes an ``IfLetStore`` view that computes content depending on if a store of optional | ||
/// state is `nil` or non-`nil`. | ||
/// | ||
/// - Parameters: | ||
/// - store: A store of optional state. | ||
/// - ifContent: A function that is given a store of non-optional state and returns a view that | ||
/// is visible only when the optional state is non-`nil`. | ||
/// - elseContent: A view that is only visible when the optional state is `nil`. | ||
public init<IfContent, ElseContent>( | ||
_ store: Store<State?, Action>, | ||
@ViewBuilder then ifContent: @escaping (Store<State, Action>) -> IfContent, | ||
@ViewBuilder else elseContent: () -> ElseContent | ||
) where Content == _ConditionalContent<IfContent, ElseContent> { | ||
self.store = store | ||
let elseContent = elseContent() | ||
self.content = { viewStore in | ||
if var state = viewStore.state { | ||
return ViewBuilder.buildEither( | ||
first: ifContent( | ||
store | ||
.filter { state, _ in state == nil ? !BindingLocal.isActive : true } | ||
.scope { | ||
state = $0 ?? state | ||
return state | ||
} | ||
) | ||
) | ||
} else { | ||
return ViewBuilder.buildEither(second: elseContent) | ||
} | ||
} | ||
} | ||
|
||
/// Initializes an ``IfLetStore`` view that computes content depending on if a store of optional | ||
/// state is `nil` or non-`nil`. | ||
/// | ||
/// - Parameters: | ||
/// - store: A store of optional state. | ||
/// - ifContent: A function that is given a store of non-optional state and returns a view that | ||
/// is visible only when the optional state is non-`nil`. | ||
public init<IfContent>( | ||
_ store: Store<State?, Action>, | ||
@ViewBuilder then ifContent: @escaping (Store<State, Action>) -> IfContent | ||
) where Content == IfContent? { | ||
self.store = store | ||
self.content = { viewStore in | ||
if var state = viewStore.state { | ||
return ifContent( | ||
store | ||
.filter { state, _ in state == nil ? !BindingLocal.isActive : true } | ||
.scope { | ||
state = $0 ?? state | ||
return state | ||
} | ||
state = $0 ?? state | ||
return state | ||
} | ||
) | ||
) | ||
} else { | ||
return ViewBuilder.buildEither(second: elseContent) | ||
} else { | ||
return nil | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Initializes an ``IfLetStore`` view that computes content depending on if a store of optional | ||
/// state is `nil` or non-`nil`. | ||
/// | ||
/// - Parameters: | ||
/// - store: A store of optional state. | ||
/// - ifContent: A function that is given a store of non-optional state and returns a view that | ||
/// is visible only when the optional state is non-`nil`. | ||
public init<IfContent>( | ||
_ store: Store<State?, Action>, | ||
@ViewBuilder then ifContent: @escaping (Store<State, Action>) -> IfContent | ||
) where Content == IfContent? { | ||
self.store = store | ||
self.content = { viewStore in | ||
if var state = viewStore.state { | ||
return ifContent( | ||
store | ||
.filter { state, _ in state == nil ? !BindingLocal.isActive : true } | ||
.scope { | ||
state = $0 ?? state | ||
return state | ||
} | ||
) | ||
} else { | ||
return nil | ||
} | ||
public var body: some View { | ||
WithViewStore( | ||
self.store, | ||
observe: { $0 }, | ||
removeDuplicates: { ($0 != nil) == ($1 != nil) }, | ||
content: self.content | ||
) | ||
} | ||
} | ||
|
||
public var body: some View { | ||
WithViewStore( | ||
self.store, | ||
observe: { $0 }, | ||
removeDuplicates: { ($0 != nil) == ($1 != nil) }, | ||
content: self.content | ||
) | ||
} | ||
} | ||
#endif |
Oops, something went wrong.