Skip to content

Commit af6c393

Browse files
committed
Update README.md
1 parent 5f1c35f commit af6c393

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,55 @@ actor CoreUserProvider: UserProvider {
8787
}
8888
```
8989

90+
### Using a coordinator
91+
Coordinators can be very helpful in making your code and logic solid, composable, and scalable. The coordinators can be as simple or complex as you need. Typically, you'll need a coordinator where the view or feature has complexities such as delegate callbacks or other transactions that must be completed as part of the feature or view.
92+
93+
```swift
94+
@Observable
95+
final class AppCoordinator: Coordinator {
96+
var state: CoordinatorState = .idle
97+
98+
enum Actions {
99+
case getPresets
100+
}
101+
102+
func perform(action: Actions) async throws {
103+
switch action {
104+
case .getPresets:
105+
...
106+
}
107+
}
108+
}
109+
```
110+
111+
A view coordinator can be great for integrating into legacy (UIKit/AppKit) projects that use legacy architectures, such as MVVM, MVC, or some mixture, as well as for just generally to make sure a complex feature view has easy coordination between networking, the model, and the view.
112+
113+
```swift
114+
@Observable
115+
final class FeatureDetailCoordinator: ViewCoordinator {
116+
var state: CoordinatorState = .idle
117+
var viewModel: FeatureDetailViewModel
118+
119+
var view: some View {
120+
SomeView(viewModel: self.viewModel)
121+
.environment(\.error, self.viewModel.error)
122+
// Add other environment properties and values…
123+
}
124+
125+
enum Actions {
126+
case fetchDetails
127+
}
128+
129+
func perform(action: Actions) async throws {
130+
switch action {
131+
case .fetchDetails:
132+
let details = self.provider.fetchDetails(for: id)
133+
self.viewModel.update(from: details)
134+
}
135+
}
136+
}
137+
```
138+
90139
### Composability with views
91140
Making SwiftUI views composable is somewhat of an art. There's a few ways to accomplish this:
92141

0 commit comments

Comments
 (0)