Files
swift-composable-architectu…/Sources/ComposableArchitecture/SwiftUI/ForEachStore.swift
Stephen Celis 57e804f1cc Macro bonanza (#2553)
* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Silence test warnings

* wip

* wip

* wip

* update a bunch of docs

* 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

* fixes

* wip

* wip

* wip

* wip

* wip

* fix

* wip

* remove for now

* wip

* wip

* updated some docs

* migration guides

* more migration guide

* fix ci

* fix

* soft deprecate all apis using AnyCasePath

* wip

* Fix

* fix 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 commit f9bdf2f04b.

* disable flakey tests on CI

* wip

* wip

* Revert "Revert "move integration tests to cron""

This reverts commit 66aafa7327.

* fix

* wip

* fix

---------

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>
2023-11-13 12:57:35 -08:00

192 lines
5.7 KiB
Swift

import OrderedCollections
import SwiftUI
/// A Composable Architecture-friendly wrapper around `ForEach` that simplifies working with
/// collections of state.
///
/// ``ForEachStore`` loops over a store's collection with a store scoped to the domain of each
/// element. This allows you to extract and modularize an element's view and avoid concerns around
/// collection index math and parent-child store communication.
///
/// For example, a todos app may define the domain and logic associated with an individual todo:
///
/// ```swift
/// @Reducer
/// struct Todo {
/// struct State: Equatable, Identifiable {
/// let id: UUID
/// var description = ""
/// var isComplete = false
/// }
///
/// enum Action {
/// case isCompleteToggled(Bool)
/// case descriptionChanged(String)
/// }
///
/// var body: some Reducer<State, Action> {
/// // ...
/// }
/// }
/// ```
///
/// As well as a view with a domain-specific store:
///
/// ```swift
/// struct TodoView: View {
/// let store: StoreOf<Todo>
/// var body: some View { /* ... */ }
/// }
/// ```
///
/// For a parent domain to work with a collection of todos, it can hold onto this collection in
/// state:
///
/// ```swift
/// @Reducer
/// struct Todos {
/// struct State: Equatable {
/// var todos: IdentifiedArrayOf<Todo.State> = []
/// }
/// // ...
/// }
/// ```
///
/// Define a case to handle actions sent to the child domain:
///
/// ```swift
/// enum Action {
/// case todos(IdentifiedActionOf<Todo>)
/// }
/// ```
///
/// Enhance its core reducer using ``Reducer/forEach(_:action:element:fileID:line:)-247po``:
///
/// ```swift
/// var body: some Reducer<State, Action> {
/// Reduce { state, action in
/// // ...
/// }
/// .forEach(\.todos, action: \.todos) {
/// Todo()
/// }
/// }
/// ```
///
/// And finally render a list of `TodoView`s using ``ForEachStore``:
///
/// ```swift
/// ForEachStore(
/// self.store.scope(state: \.todos, action: \.todos)
/// ) { todoStore in
/// TodoView(store: todoStore)
/// }
/// ```
///
public struct ForEachStore<
EachState, EachAction, Data: Collection, ID: Hashable, Content: View
>: DynamicViewContent {
public let data: Data
let content: Content
/// Initializes a structure that computes views on demand from a store on a collection of data and
/// an identified action.
///
/// - Parameters:
/// - store: A store on an identified array of data and an identified action.
/// - content: A function that can generate content given a store of an element.
public init<EachContent>(
_ store: Store<IdentifiedArray<ID, EachState>, IdentifiedAction<ID, EachAction>>,
@ViewBuilder content: @escaping (_ store: Store<EachState, EachAction>) -> EachContent
)
where
Data == IdentifiedArray<ID, EachState>,
Content == WithViewStore<
IdentifiedArray<ID, EachState>, IdentifiedAction<ID, EachAction>,
ForEach<IdentifiedArray<ID, EachState>, ID, EachContent>
>
{
self.data = store.withState { $0 }
self.content = WithViewStore(
store,
observe: { $0 },
removeDuplicates: { areOrderedSetsDuplicates($0.ids, $1.ids) }
) { viewStore in
ForEach(viewStore.state, id: viewStore.state.id) { element in
var element = element
let id = element[keyPath: viewStore.state.id]
content(
store.scope(
state: {
element = $0[id: id] ?? element
return element
},
action: { .element(id: id, action: $0) }
)
)
}
}
}
@available(
iOS,
deprecated: 9999,
message:
"Use an 'IdentifiedAction', instead. See the following migration guide for more information:\n\nhttps://pointfreeco.github.io/swift-composable-architecture/main/documentation/composablearchitecture/Migratingto14#Identified-actions"
)
@available(
macOS,
deprecated: 9999,
message:
"Use an 'IdentifiedAction', instead. See the following migration guide for more information:\n\nhttps://pointfreeco.github.io/swift-composable-architecture/main/documentation/composablearchitecture/Migratingto14#Identified-actions"
)
@available(
tvOS,
deprecated: 9999,
message:
"Use an 'IdentifiedAction', instead. See the following migration guide for more information:\n\nhttps://pointfreeco.github.io/swift-composable-architecture/main/documentation/composablearchitecture/Migratingto14#Identified-actions"
)
@available(
watchOS,
deprecated: 9999,
message:
"Use an 'IdentifiedAction', instead. See the following migration guide for more information:\n\nhttps://pointfreeco.github.io/swift-composable-architecture/main/documentation/composablearchitecture/Migratingto14#Identified-actions"
)
public init<EachContent>(
_ store: Store<IdentifiedArray<ID, EachState>, (id: ID, action: EachAction)>,
@ViewBuilder content: @escaping (_ store: Store<EachState, EachAction>) -> EachContent
)
where
Data == IdentifiedArray<ID, EachState>,
Content == WithViewStore<
IdentifiedArray<ID, EachState>, (id: ID, action: EachAction),
ForEach<IdentifiedArray<ID, EachState>, ID, EachContent>
>
{
self.data = store.withState { $0 }
self.content = WithViewStore(
store,
observe: { $0 },
removeDuplicates: { areOrderedSetsDuplicates($0.ids, $1.ids) }
) { viewStore in
ForEach(viewStore.state, id: viewStore.state.id) { element in
var element = element
let id = element[keyPath: viewStore.state.id]
content(
store.scope(
state: {
element = $0[id: id] ?? element
return element
},
action: { (id, $0) }
)
)
}
}
}
public var body: some View {
self.content
}
}