-
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
Open
Dark-Existed
wants to merge
1
commit into
OpenSwiftUIProject:main
Choose a base branch
from
Dark-Existed:feature/on_preference_change
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+159
−6
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
Example/Shared/ViewModifier/PreferenceActionModifierExample.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)") | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
Sources/OpenSwiftUICore/Data/Preference/PreferenceActionModifier.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
| /// 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>( | ||
| _ 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 | ||
| 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) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
onPreferenceChangeis being added as a public API, but it doesn’t appear to carry the@available(OpenSwiftUI_v1_0, *)availability annotation pattern used by other preference APIs (e.g.preference,transformPreference). Consider aligning the availability annotations to keep the public surface/versioning consistent.Severity: low
🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.