Skip to content

Commit

Permalink
Add a third collect view modifier.
Browse files Browse the repository at this point in the history
  • Loading branch information
TadeasKriz committed May 21, 2024
1 parent be539af commit 28907b4
Showing 1 changed file with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ object SwiftUIFlowObservingGenerator {
let viewModel = SharedViewModel()
@State
var counter: Int = 0
var counter: KotlinInt = 0
var body: some View {
Text("Tick #\(counter)")
Expand All @@ -65,6 +65,46 @@ object SwiftUIFlowObservingGenerator {
}
}
/**
A view modifier used to collect a SKIE-bridged Flow into a SwiftUI Binding, transforming the value before assigning.
The flow is being collected using the `task` modifier,
sharing the same lifecycle.
In the following example we collect a `Flow<Int>` property `counter`
from the `SharedViewModel` into a `@State` property in our view.
```swift
struct ExampleView: View {
let viewModel = SharedViewModel()
@State
var counter: Int = 0
var body: some View {
Text("Tick #\(counter)")
.collect(flow: viewModel.counter, into: ${'$'}counter)
}
}
```
- parameter flow: A SKIE-bridged Flow you with to collect.
- parameter binding: A binding to a property where each new value will be set to.
- parameter transform: An async closure to transform any value emitted by the flow into a one expected by the binding.
Returning `nil` from this closure will reject the value.
*/
public func collect<Flow: SkieSwiftFlowProtocol, U>(
flow: Flow,
into binding: SwiftUI.Binding<U>,
transform: @escaping (Flow.Element) async -> U?
) -> some SwiftUI.View {
collect(flow: flow) { newValue in
if let newTransformedValue = await transform(newValue) {
binding.wrappedValue = newTransformedValue
}
}
}
/**
A view modifier used to collect a SKIE-bridged Flow and perform a closere with each received value.
Expand Down

0 comments on commit 28907b4

Please sign in to comment.