Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move binding warning suppression to dynamic member lookup #2740

Merged
merged 7 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 11 additions & 7 deletions Sources/ComposableArchitecture/Internal/Binding+IsPresent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ import SwiftUI

extension Binding {
func isPresent<Wrapped>() -> Binding<Bool> where Value == Wrapped? {
.init(
get: { self.wrappedValue != nil },
set: { isPresent, transaction in
guard !isPresent else { return }
self.transaction(transaction).wrappedValue = nil
}
)
self._isPresent
}
}

extension Optional {
fileprivate var _isPresent: Bool {
get { self != nil }
set {
guard !newValue else { return }
self = nil
}
}
}
254 changes: 104 additions & 150 deletions Sources/ComposableArchitecture/Observation/Binding+Observation.swift
Original file line number Diff line number Diff line change
@@ -1,41 +1,13 @@
#if canImport(Perception)
import SwiftUI

@dynamicMemberLookup
public struct _StoreBinding<State, Action> {
fileprivate let wrappedValue: Store<State, Action>

public subscript<Member>(
dynamicMember keyPath: KeyPath<State, Member>
) -> _StoreBinding<Member, Action> {
_StoreBinding<Member, Action>(
wrappedValue: self.wrappedValue.scope(state: keyPath, action: \.self)
)
}

/// Creates a binding to the value by sending new values through the given action.
///
/// - Parameter action: An action for the binding to send values through.
/// - Returns: A binding.
public func sending(_ action: CaseKeyPath<Action, State>) -> Binding<State> {
Binding(
get: { self.wrappedValue.withState { $0 } },
set: { newValue, transaction in
BindingLocal.$isActive.withValue(true) {
_ = self.wrappedValue.send(action(newValue), transaction: transaction)
}
}
)
}
}

extension Binding {
@_disfavoredOverload
public subscript<State: ObservableState, Action, Member>(
dynamicMember keyPath: KeyPath<State, Member>
) -> _StoreBinding<Member, Action>
) -> _StoreBinding<State, Action, Member>
where Value == Store<State, Action> {
_StoreBinding(wrappedValue: self.wrappedValue.scope(state: keyPath, action: \.self))
_StoreBinding(binding: self, keyPath: keyPath)
}
}

Expand All @@ -44,9 +16,9 @@
@_disfavoredOverload
public subscript<State: ObservableState, Action, Member>(
dynamicMember keyPath: KeyPath<State, Member>
) -> _StoreBinding<Member, Action>
) -> _StoreBindable_SwiftUI<State, Action, Member>
where Value == Store<State, Action> {
_StoreBinding(wrappedValue: self.wrappedValue.scope(state: keyPath, action: \.self))
_StoreBindable_SwiftUI(bindable: self, keyPath: keyPath)
}
}

Expand All @@ -58,9 +30,9 @@
@_disfavoredOverload
public subscript<State: ObservableState, Action, Member>(
dynamicMember keyPath: KeyPath<State, Member>
) -> _StoreBinding<Member, Action>
) -> _StoreBindable_Perception<State, Action, Member>
where Value == Store<State, Action> {
_StoreBinding(wrappedValue: self.wrappedValue.scope(state: keyPath, action: \.self))
_StoreBindable_Perception(bindable: self, keyPath: keyPath)
}
}

Expand Down Expand Up @@ -100,7 +72,11 @@
dynamicMember keyPath: WritableKeyPath<State, Value>
) -> Value {
get { self.state[keyPath: keyPath] }
set { self.send(.binding(.set(keyPath, newValue))) }
set {
BindingLocal.$isActive.withValue(true) {
self.send(.binding(.set(keyPath, newValue)))
}
}
}
}

Expand All @@ -114,7 +90,11 @@
@_disfavoredOverload
public var state: State {
get { self.state }
set { self.send(.binding(.set(\.self, newValue))) }
set {
BindingLocal.$isActive.withValue(true) {
self.send(.binding(.set(\.self, newValue)))
}
}
}
}

Expand All @@ -130,7 +110,11 @@
dynamicMember keyPath: WritableKeyPath<State, Value>
) -> Value {
get { self.state[keyPath: keyPath] }
set { self.send(.view(.binding(.set(keyPath, newValue)))) }
set {
BindingLocal.$isActive.withValue(true) {
self.send(.view(.binding(.set(keyPath, newValue))))
}
}
}
}

Expand All @@ -145,129 +129,99 @@
@_disfavoredOverload
public var state: State {
get { self.state }
set { self.send(.view(.binding(.set(\.self, newValue)))) }
set {
BindingLocal.$isActive.withValue(true) {
self.send(.view(.binding(.set(\.self, newValue))))
}
}
}
}

// NB: These overloads ensure runtime warnings aren't emitted for errant SwiftUI bindings.
#if DEBUG
extension Binding {
public subscript<State: ObservableState, Action: BindableAction, Member: Equatable>(
dynamicMember keyPath: WritableKeyPath<State, Member>
) -> Binding<Member>
where Value == Store<State, Action>, Action.State == State {
Binding<Member>(
get: { self.wrappedValue.state[keyPath: keyPath] },
set: { newValue, transaction in
BindingLocal.$isActive.withValue(true) {
_ = self.wrappedValue.send(
.binding(.set(keyPath, newValue)), transaction: transaction
)
}
}
)
}
@dynamicMemberLookup
public struct _StoreBinding<State: ObservableState, Action, Value> {
fileprivate let binding: Binding<Store<State, Action>>
fileprivate let keyPath: KeyPath<State, Value>

public subscript<State: ObservableState, Action: ViewAction, Member: Equatable>(
dynamicMember keyPath: WritableKeyPath<State, Member>
) -> Binding<Member>
where
Value == Store<State, Action>,
Action.ViewAction: BindableAction,
Action.ViewAction.State == State
{
Binding<Member>(
get: { self.wrappedValue.state[keyPath: keyPath] },
set: { newValue, transaction in
BindingLocal.$isActive.withValue(true) {
_ = self.wrappedValue.send(
.view(.binding(.set(keyPath, newValue))), transaction: transaction
)
}
}
)
}
public subscript<Member>(
dynamicMember keyPath: KeyPath<Value, Member>
) -> _StoreBinding<State, Action, Member> {
_StoreBinding<State, Action, Member>(
binding: self.binding,
keyPath: self.keyPath.appending(path: keyPath)
)
}

@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
extension SwiftUI.Bindable {
public subscript<State: ObservableState, Action: BindableAction, Member: Equatable>(
dynamicMember keyPath: WritableKeyPath<State, Member>
) -> Binding<Member>
where Value == Store<State, Action>, Action.State == State {
Binding<Member>(
get: { self.wrappedValue.state[keyPath: keyPath] },
set: { newValue, transaction in
BindingLocal.$isActive.withValue(true) {
_ = self.wrappedValue.send(
.binding(.set(keyPath, newValue)), transaction: transaction
)
}
}
)
}
/// Creates a binding to the value by sending new values through the given action.
///
/// - Parameter action: An action for the binding to send values through.
/// - Returns: A binding.
public func sending(_ action: CaseKeyPath<Action, Value>) -> Binding<Value> {
self.binding[state: self.keyPath, action: action]
}
}

public subscript<State: ObservableState, Action: ViewAction, Member: Equatable>(
dynamicMember keyPath: WritableKeyPath<State, Member>
) -> Binding<Member>
where
Value == Store<State, Action>,
Action.ViewAction: BindableAction,
Action.ViewAction.State == State
{
Binding<Member>(
get: { self.wrappedValue.state[keyPath: keyPath] },
set: { newValue, transaction in
BindingLocal.$isActive.withValue(true) {
_ = self.wrappedValue.send(
.view(.binding(.set(keyPath, newValue))), transaction: transaction
)
}
}
)
}
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@dynamicMemberLookup
public struct _StoreBindable_SwiftUI<State: ObservableState, Action, Value> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I think it might be possible to abstract these 3 "store binding" types, I'm not sure it's worth it or a good idea.

fileprivate let bindable: SwiftUI.Bindable<Store<State, Action>>
fileprivate let keyPath: KeyPath<State, Value>

public subscript<Member>(
dynamicMember keyPath: KeyPath<Value, Member>
) -> _StoreBindable_SwiftUI<State, Action, Member> {
_StoreBindable_SwiftUI<State, Action, Member>(
bindable: self.bindable,
keyPath: self.keyPath.appending(path: keyPath)
)
}

@available(iOS, introduced: 13, obsoleted: 17)
@available(macOS, introduced: 10.15, obsoleted: 14)
@available(tvOS, introduced: 13, obsoleted: 17)
@available(watchOS, introduced: 6, obsoleted: 10)
extension Perception.Bindable {
public subscript<State: ObservableState, Action: BindableAction, Member: Equatable>(
dynamicMember keyPath: WritableKeyPath<State, Member>
) -> Binding<Member>
where Value == Store<State, Action>, Action.State == State {
Binding<Member>(
get: { self.wrappedValue.state[keyPath: keyPath] },
set: { newValue, transaction in
BindingLocal.$isActive.withValue(true) {
_ = self.wrappedValue.send(
.binding(.set(keyPath, newValue)), transaction: transaction
)
}
}
)
}
/// Creates a binding to the value by sending new values through the given action.
///
/// - Parameter action: An action for the binding to send values through.
/// - Returns: A binding.
public func sending(_ action: CaseKeyPath<Action, Value>) -> Binding<Value> {
self.bindable[state: self.keyPath, action: action]
}
}

@available(iOS, introduced: 13, obsoleted: 17)
@available(macOS, introduced: 10.15, obsoleted: 14)
@available(tvOS, introduced: 13, obsoleted: 17)
@available(watchOS, introduced: 6, obsoleted: 10)
@dynamicMemberLookup
public struct _StoreBindable_Perception<State: ObservableState, Action, Value> {
fileprivate let bindable: Perception.Bindable<Store<State, Action>>
fileprivate let keyPath: KeyPath<State, Value>

public subscript<State: ObservableState, Action: ViewAction, Member: Equatable>(
dynamicMember keyPath: WritableKeyPath<State, Member>
) -> Binding<Member>
where
Value == Store<State, Action>,
Action.ViewAction: BindableAction,
Action.ViewAction.State == State
{
Binding<Member>(
get: { self.wrappedValue.state[keyPath: keyPath] },
set: { newValue, transaction in
BindingLocal.$isActive.withValue(true) {
_ = self.wrappedValue.send(
.view(.binding(.set(keyPath, newValue))), transaction: transaction
)
}
}
)
public subscript<Member>(
dynamicMember keyPath: KeyPath<Value, Member>
) -> _StoreBindable_Perception<State, Action, Member> {
_StoreBindable_Perception<State, Action, Member>(
bindable: self.bindable,
keyPath: self.keyPath.appending(path: keyPath)
)
}

/// Creates a binding to the value by sending new values through the given action.
///
/// - Parameter action: An action for the binding to send values through.
/// - Returns: A binding.
public func sending(_ action: CaseKeyPath<Action, Value>) -> Binding<Value> {
self.bindable[state: self.keyPath, action: action]
}
}

extension Store where State: ObservableState {
fileprivate subscript<Value>(
state state: KeyPath<State, Value>,
action action: CaseKeyPath<Action, Value>
) -> Value {
get { self.state[keyPath: state] }
set {
BindingLocal.$isActive.withValue(true) {
self.send(action(newValue))
}
}
}
#endif
}
#endif