-
Notifications
You must be signed in to change notification settings - Fork 70
Add View.onPreferenceChange #926
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // | ||
| // PreferenceActionModifierExample.swift | ||
| // Shared | ||
|
|
||
| #if OPENSWIFTUI | ||
| import OpenSwiftUI | ||
| #else | ||
| import SwiftUI | ||
| #endif | ||
|
|
||
| struct MyKey: PreferenceKey { | ||
| static let defaultValue = "" | ||
|
|
||
| static func reduce(value: inout String, nextValue: () -> String) { | ||
| value = nextValue() | ||
| } | ||
| } | ||
|
|
||
| struct PreferenceActionModifierExample: View { | ||
| var body: some View { | ||
| VStack { | ||
| Color.red | ||
| .preference(key: MyKey.self, value: "changed") | ||
| } | ||
| .onPreferenceChange(MyKey.self) { | ||
| print("onPreferenceChange: \($0)") | ||
| } | ||
| } | ||
| } |
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing TransactionalPreferenceActionModifier stuff |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| // | ||
| // PreferenceActionModifier.swift | ||
| // OpenSwiftUICore | ||
| // | ||
| // Audited for 6.5.4 | ||
| // Status: Complete | ||
| // ID: 264234112339315C9A664F0B7F8B50C1 (SwiftUICore) | ||
|
|
||
| import OpenAttributeGraphShims | ||
|
|
||
| extension View { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing available statement |
||
| /// Adds an action to perform when the specified preference key's value | ||
| /// changes. | ||
| /// | ||
| /// - Parameters: | ||
| /// - key: The key to monitor for value changes. | ||
| /// - action: The action to perform when the value for `key` changes. The | ||
| /// `action` closure passes the new value as its parameter. | ||
| /// | ||
| /// - Returns: A view that triggers `action` when the value for `key` | ||
| /// changes. | ||
| @inlinable | ||
| nonisolated public func onPreferenceChange<K>( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Severity: low 🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage. |
||
| _ key: K.Type = K.self, | ||
| perform action: @escaping (K.Value) -> Void | ||
| ) -> some View where K: PreferenceKey, K.Value: Equatable { | ||
| return modifier(_PreferenceActionModifier<K>(action: action)) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - PreferenceActionModifier | ||
|
|
||
| @frozen | ||
| public struct _PreferenceActionModifier<K>: MultiViewModifier, PrimitiveViewModifier where K: PreferenceKey, K.Value: Equatable { | ||
| public var action: (_ value: K.Value) -> Void | ||
|
|
||
| @inlinable | ||
| public init(action: @escaping (_ value: K.Value) -> Void) { | ||
| self.action = action | ||
| } | ||
|
|
||
| nonisolated public static func _makeView( | ||
| modifier: _GraphValue<Self>, | ||
| inputs: _ViewInputs, | ||
| body: @escaping (_Graph, _ViewInputs) -> _ViewOutputs | ||
| ) -> _ViewOutputs { | ||
| var inputs = inputs | ||
| inputs.preferences.add(K.self) | ||
| let outputs = body(_Graph(), inputs) | ||
| guard let keyValue = outputs[K.self] else { | ||
| return outputs | ||
| } | ||
| let binder = Attribute(PreferenceBinder<K>( | ||
| modifier: modifier.value, | ||
| keyValue: keyValue, | ||
| phase: inputs.viewPhase, | ||
| lastResetSeed: .zero, | ||
| lastValue: nil | ||
| )) | ||
| binder.flags = .transactional | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return outputs | ||
| } | ||
| } | ||
|
|
||
| @available(*, unavailable) | ||
| extension _PreferenceActionModifier: Sendable {} | ||
|
|
||
| // MARK: - PreferenceBinder | ||
|
|
||
| private struct PreferenceBinder<K>: StatefulRule, AsyncAttribute where K: PreferenceKey, K.Value: Equatable { | ||
| @Attribute var modifier: _PreferenceActionModifier<K> | ||
| @Attribute var keyValue: K.Value | ||
| @Attribute var phase: _GraphInputs.Phase | ||
| var cycleDetector: UpdateCycleDetector | ||
| var lastResetSeed: UInt32 | ||
| var lastValue: K.Value? | ||
|
|
||
| init( | ||
| modifier: Attribute<_PreferenceActionModifier<K>>, | ||
| keyValue: Attribute<K.Value>, | ||
| phase: Attribute<_GraphInputs.Phase>, | ||
| cycleDetector: UpdateCycleDetector = .init(), | ||
| lastResetSeed: UInt32, | ||
| lastValue: K.Value? | ||
| ) { | ||
| self._modifier = modifier | ||
| self._keyValue = keyValue | ||
| self._phase = phase | ||
| self.cycleDetector = cycleDetector | ||
| self.lastResetSeed = lastResetSeed | ||
| self.lastValue = lastValue | ||
| } | ||
|
|
||
| typealias Value = Void | ||
|
|
||
| mutating func updateValue() { | ||
| if lastResetSeed != phase.resetSeed { | ||
| lastResetSeed = phase.resetSeed | ||
| cycleDetector.reset() | ||
| lastValue = nil | ||
| } | ||
| let (newValue, changed) = $keyValue.changedValue() | ||
| guard changed || (lastValue == nil && _SemanticFeature_v6_1.isEnabled) else { | ||
| return | ||
| } | ||
| guard lastValue != newValue else { | ||
| return | ||
| } | ||
| lastValue = newValue | ||
|
|
||
| guard cycleDetector.dispatch( | ||
| label: "Bound preference \(K.self)", | ||
| isDebug: true | ||
| ) else { | ||
| return | ||
| } | ||
| let action = Graph.withoutUpdate { | ||
| modifier.action | ||
| } | ||
| Update.enqueueAction(reason: nil) { | ||
| action(newValue) | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing test case for this