Skip to content

Commit

Permalink
Move binding warning suppression to dynamic member lookup (#2740)
Browse files Browse the repository at this point in the history
* Move binding warning suppression to dynamic member lookup

In TCA it is considered a bug to send an action to a child feature when
it is dismissed.

In SwiftUI, a focused text field binding will write to itself when the
view it is presented in is dismissed.

So if you derive a TCA binding to a text field in a dismissable child
feature, dismissing said feature will write to the text field and cause
a warning to be emitted.

To work around this, we set a task local, and are currently setting it
in debug-only methods that construct a binding from scratch. Creating
bindings from scratch unfortunately leads to issues with animations, so
we should avoid doing so.

Instead, we can leverage properties on store that bindings are derived
from and suppress the warnings from there.

* wip

* wip

* wip

* wip

* wip

* wip
  • Loading branch information
stephencelis committed Jan 30, 2024
1 parent 4d9f171 commit efc79f9
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 233 deletions.
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> {
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

0 comments on commit efc79f9

Please sign in to comment.