Skip to content

Commit

Permalink
use BindableStore to generate bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
square-tomb committed Dec 21, 2023
1 parent 2be92b5 commit f1ae774
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
12 changes: 3 additions & 9 deletions Samples/SwiftUITestbed/Sources/MainScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extension MainWorkflow.Rendering: SwiftUIScreen, Screen {
}

private struct MainScreenView: View {
var model: Store<MainWorkflow.State>
@BindableStore var model: Store<MainWorkflow.State>

@Environment(\.viewEnvironment.marketStylesheet) private var styles: MarketStylesheet
@Environment(\.viewEnvironment.marketContext) private var context: MarketContext
Expand All @@ -49,10 +49,7 @@ private struct MainScreenView: View {

TextField(
"Text",
text: Binding(
get: { model.title },
set: { _ in fatalError("TODO") }
)
text: $model.title
)
.focused($focusedField, equals: .title)
.onAppear { focusedField = .title }
Expand All @@ -61,10 +58,7 @@ private struct MainScreenView: View {
style: context.stylesheets.testbed.toggleRow,
label: "All Caps",
isEnabled: model.allCapsToggleIsEnabled,
isOn: Binding(
get: { model.allCapsToggleIsOn },
set: { _ in fatalError("TODO") }
)
isOn: $model.allCapsToggleIsOn
)

Spacer(minLength: styles.spacings.spacing50)
Expand Down
3 changes: 2 additions & 1 deletion Samples/SwiftUITestbed/Sources/MainWorkflow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ struct MainWorkflow: Workflow {

extension MainWorkflow.State {
var allCapsToggleIsOn: Bool {
isAllCaps
get { isAllCaps }
set { fatalError("TODO") }
}

var allCapsToggleIsEnabled: Bool {
Expand Down
38 changes: 38 additions & 0 deletions Samples/SwiftUITestbed/Sources/Observation/BindableStore.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copied from https://github.com/pointfreeco/swift-composable-architecture/blob/acfbab4290adda4e47026d059db36361958d495c/Sources/ComposableArchitecture/Observation/BindableStore.swift

import ComposableArchitecture
import SwiftUI

/// A property wrapper type that supports creating bindings to the mutable properties of a
/// ``Store``.
///
/// Use this property wrapper in iOS 16, macOS 13, tvOS 16, watchOS 9, and earlier, when `@Bindable`
/// is unavailable, to derive bindings to properties of your features.
///
/// If you are targeting iOS 17, macOS 14, tvOS 17, watchOS 9, or later, then you can replace
/// ``BindableStore`` with SwiftUI's `@Bindable`.
@available(iOS, deprecated: 17, renamed: "Bindable")
@available(macOS, deprecated: 14, renamed: "Bindable")
@available(tvOS, deprecated: 17, renamed: "Bindable")
@available(watchOS, deprecated: 10, renamed: "Bindable")
@propertyWrapper
@dynamicMemberLookup
public struct BindableStore<State: ObservableState> {
public var wrappedValue: Store<State>
public init(wrappedValue: Store<State>) {
self.wrappedValue = wrappedValue
}

public var projectedValue: BindableStore<State> {
self
}

public subscript<Subject>(
dynamicMember keyPath: ReferenceWritableKeyPath<Store<State>, Subject>
) -> Binding<Subject> {
Binding(
get: { self.wrappedValue[keyPath: keyPath] },
set: { self.wrappedValue[keyPath: keyPath] = $0 }
)
}
}
7 changes: 7 additions & 0 deletions Samples/SwiftUITestbed/Sources/Observation/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ public extension Store {
subscript<T>(dynamicMember keyPath: KeyPath<State, T>) -> T {
state[keyPath: keyPath]
}

subscript<T>(
dynamicMember keyPath: WritableKeyPath<State, T>
) -> T {
get { state[keyPath: keyPath] }
set { state[keyPath: keyPath] = newValue }
}
}

extension Store: Equatable {
Expand Down

0 comments on commit f1ae774

Please sign in to comment.