Files
swift-composable-architectu…/Sources/ComposableArchitecture/SwiftUI/ForEachStore.swift
Brandon Williams 2c93195c23 Prerelease 1.0 (#1929)
* Converted voice memos back to identified array

* update deps

* update docs for DismissEffect

* wip

* Add Sendable conformance to PresentationState (#2086)

* wip

* swift-format

* wip

* wip

* fix some warnings

* docs

* wip

* wip

* Catch some typos in Articles (#2088)

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* docs

* wip

* wip

* docs

* wip

* wip

* wip

* wip

* docs

* docs

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Fix invalid states count for 3 optionals and typos (#2094)

* wip

* wip

* more dismisseffect docs

* fixed some references

* navigation doc corrections

* more nav docs

* fix cancellation tests in release mode

* wrap some tests in #if DEBUG since they are testing expected failures

* update UUIDs in tests to use shorter initializer

* fixed a todo

* wip

* fix merge errors

* wip

* fix

* wip

* wip

* fixing a bunch of todos

* get rid of rawvalue in StackElementID

* more todos

* NavLinkStore docs

* fix swift 5.6 stuff

* fix some standups tests

* fix

* clean up

* docs fix

* fixes

* wip

* 5.6 fix

* wip

* wip

* dont parallelize tests

* updated demo readmes

* wip

* Use ObservedObject instead of StateObject for alert/dialog modifiers.

* integration tests for bad dismissal behavior

* check for runtime warnings in every integration test

* wip

* wip

* wip

* fix

* wip

* wip

* wip

* wip

* wip

* wip

* Drop a bunch of Hashables.

* some nav bug fixes

* wip

* wip

* wip

* fix

* fix

* wip

* wip

* Simplify recording test.

* add concurrent async test

* fix

* wip

* Refact how detail dismisses itself.

* fix

* 5.6 fix

* wip

* wip

* wip

* wip

* Add TestStore.assert.

* Revert "Add TestStore.assert."

This reverts commit a892cccc66.

* add Ukrainian Readme.md (#2121)

* Add TestStore.assert. (#2123)

* Add TestStore.assert.

* wip

* Update Sources/ComposableArchitecture/TestStore.swift

Co-authored-by: Stephen Celis <stephen@stephencelis.com>

* Update Sources/ComposableArchitecture/Documentation.docc/Extensions/TestStore.md

Co-authored-by: Stephen Celis <stephen@stephencelis.com>

* fix tests

---------

Co-authored-by: Stephen Celis <stephen@stephencelis.com>

* Run swift-format

* push for store.finish and presentation

* wip

* move docs around

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Add case subscripts

* wip

* wip

* wip

* 5.7-only

* wip

* wip

* wip

* wip

* fix

* revert store.finish task cancellation

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* add test for presentation scope

* wip

* wip

* wip

* wip

* wip

* cleanup

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Rename ReducerProtocol.swift to Reducer.swift (#2206)

* Hard-deprecate old SwitchStore initializers/overloads

* wip

* wip

* Resolve CaseStudies crash (#2258)

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Bump timeout for CI

* wip

* wip

---------

Co-authored-by: Jackson Utsch <jutechs@gmail.com>
Co-authored-by: Stephen Celis <stephen@stephencelis.com>
Co-authored-by: 유재호 <y73447jh@gmail.com>
Co-authored-by: Dmytro <barabashdmyto@gmail.com>
Co-authored-by: mbrandonw <mbrandonw@users.noreply.github.com>
2023-07-27 17:35:07 -07:00

133 lines
3.6 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
/// struct Todo: Reducer {
/// struct State: Equatable, Identifiable {
/// let id: UUID
/// var description = ""
/// var isComplete = false
/// }
///
/// enum Action {
/// case isCompleteToggled(Bool)
/// case descriptionChanged(String)
/// }
///
/// func reduce(into state: inout State, action: Action) -> Effect<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
/// struct Todos: Reducer {
/// struct State: Equatable {
/// var todos: IdentifiedArrayOf<Todo.State> = []
/// }
/// // ...
/// }
/// ```
///
/// Define a case to handle actions sent to the child domain:
///
/// ```swift
/// enum Action {
/// case todo(id: Todo.State.ID, action: Todo.Action)
/// }
/// ```
///
/// Enhance its core reducer using ``Reducer/forEach(_:action:element:fileID:line:)``:
///
/// ```swift
/// var body: some Reducer<State, Action> {
/// Reduce { state, action in
/// // ...
/// }
/// .forEach(\.todos, action: /Action.todo(id:action:)) {
/// Todo()
/// }
/// }
/// ```
///
/// And finally render a list of `TodoView`s using ``ForEachStore``:
///
/// ```swift
/// ForEachStore(
/// self.store.scope(state: \.todos, action: AppAction.todo(id:action:))
/// ) { 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>, (ID, EachAction)>,
@ViewBuilder content: @escaping (_ store: Store<EachState, EachAction>) -> EachContent
)
where
Data == IdentifiedArray<ID, EachState>,
Content == WithViewStore<
IdentifiedArray<ID, EachState>, (ID, EachAction),
ForEach<IdentifiedArray<ID, EachState>, ID, EachContent>
>
{
self.data = store.state.value
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
}
}