-
Notifications
You must be signed in to change notification settings - Fork 1
PredicateControllerDelegate #3
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
base: main
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -13,22 +13,27 @@ | |
| /// Notified when the predicate of the `UIPredicateEditor`will change. | ||
| /// | ||
| /// The `object` on the `Notification` will be the editor. | ||
| @available(*, deprecated, message: "Utilize `PredicateControllerDelegate`") | ||
| static let predicateWillChange = Notification.Name(rawValue: "UIPredicateEditor.predicateWillChange") | ||
|
|
||
| /// Notified when the predicate of the `UIPredicateEditor` changes. | ||
| /// | ||
| /// The `object` on the `Notification` will be the editor. | ||
| @available(*, deprecated, message: "Utilize `PredicateControllerDelegate`") | ||
| static let predicateDidChange = Notification.Name(rawValue: "UIPredicateEditor.predicateDidChange") | ||
| } | ||
|
|
||
| /// Concrete final class that manages predicates, row templates, updating and notifying of predicate changes to its managing view. | ||
| /// | ||
| /// The `UIPRedicateEditorViewController` class uses it internally for its predicate operations. | ||
| /// The `UIPredicateEditorViewController` class uses it internally for its predicate operations. | ||
| /// | ||
| /// You may choose to write your own view and use the `PredicateController` as its driving model. | ||
| @MainActor | ||
| @Observable | ||
| public final class PredicateController { | ||
| /// The delegate which will receive updates as the controller modified the `predicate`. | ||
| public var delegate: PredicateControllerDelegate? | ||
|
|
||
| /// contains the predicate evaluated by the editor. | ||
| /// | ||
| /// If one or more parts cannot be queried from the row templates, the property evaluates to `nil`. | ||
|
|
@@ -68,7 +73,7 @@ | |
| /// Each row will have its own predicate which is used to form the predicate on the `UIPredicateEditor`. | ||
| public var requiredRowTemplates: [UIPredicateEditorRowTemplate] = [] | ||
|
|
||
| /// Created on-demand everytime as the formatting dictionary may change during runtime | ||
| /// Created on-demand every time as the formatting dictionary may change during runtime | ||
| var formattingHelper: FormattingDictionaryHelper { | ||
| FormattingDictionaryHelper(formattingDictionary: formattingDictionary ?? [:]) | ||
| } | ||
|
|
@@ -144,17 +149,13 @@ | |
| /// The receiver internally updates the derived predicate and notifies subscribers. | ||
| /// - Parameter logicalType: logical type of the predicate. | ||
| public func updatePredicate(for _: NSCompoundPredicate.LogicalType) { | ||
| Task { @MainActor in | ||
| notifyPredicateWillChange() | ||
| } | ||
| notifyPredicateWillChange() | ||
|
|
||
| // We assume the first row is the root container if available. | ||
| // If we have no rows, we have no predicate. | ||
| guard !requiredRowTemplates.isEmpty else { | ||
| guard !requiredRowTemplates.isEmpty, !rowTemplates.isEmpty else { | ||
|
Contributor
Author
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. Additional check here for the |
||
| predicate = nil | ||
| Task { @MainActor in | ||
| notifyPredicateDidChange() | ||
| } | ||
| notifyPredicateDidChange() | ||
| return | ||
| } | ||
|
|
||
|
|
@@ -163,11 +164,8 @@ | |
| buildRecursivePredicate(from: $0) | ||
| } | ||
|
|
||
| predicate = NSCompoundPredicate(type: self.rowTemplates[0].logicalType, subpredicates: predicates) | ||
|
|
||
| Task { @MainActor in | ||
| notifyPredicateDidChange() | ||
| } | ||
| predicate = NSCompoundPredicate(type: rowTemplates[0].logicalType, subpredicates: predicates) | ||
| notifyPredicateDidChange() | ||
| } | ||
|
|
||
| /// Add a new row template on the receiver for the given LHS expression title. | ||
|
|
@@ -291,14 +289,14 @@ | |
|
|
||
| // MARK: Notify | ||
|
|
||
| @MainActor func notifyPredicateWillChange() { | ||
| // @TODO: Refactor to call delegate | ||
| func notifyPredicateWillChange() { | ||
| delegate?.predicateWillChangeForPredicateController(self) | ||
|
Comment on lines
+292
to
+293
Contributor
Author
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. Since the class was already I'm still coming up to speed on the Swift 6 concurrency changes, so I might be missing something. |
||
| NotificationCenter.default.post(name: .predicateWillChange, object: self) | ||
|
Check warning on line 294 in Sources/UIPredicateEditor/Public/PredicateController.swift
|
||
| } | ||
|
|
||
| @MainActor func notifyPredicateDidChange() { | ||
| // @TODO: Refactor to call delegate | ||
| func notifyPredicateDidChange() { | ||
| delegate?.predicateDidChangeForPredicateController(self) | ||
| NotificationCenter.default.post(name: .predicateDidChange, object: self) | ||
|
Check warning on line 299 in Sources/UIPredicateEditor/Public/PredicateController.swift
|
||
| } | ||
|
|
||
| // MARK: Internal | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| #if os(iOS) | ||
| import Foundation | ||
|
|
||
| /// Optional methods implemented by parties interested in updates to a `PredicateController` | ||
| /// | ||
| /// - note: Not marked `@objc`, so conforming types do not need to also conform to `NSObjectProtocol`. | ||
| /// The default implementation makes it so the implementer is only required to respond to 'didChange' | ||
| /// events. | ||
| @MainActor public protocol PredicateControllerDelegate { | ||
| /// Informs the receiver that a predicate is about to change. | ||
| func predicateWillChangeForPredicateController(_ predicateController: PredicateController) | ||
|
|
||
| /// Informs the receiver that a predicate has been changed. | ||
| func predicateDidChangeForPredicateController(_ predicateController: PredicateController) | ||
| } | ||
|
|
||
| // MARK: - Default Implementation | ||
|
|
||
| public extension PredicateControllerDelegate { | ||
| func predicateWillChangeForPredicateController(_: PredicateController) {} | ||
| } | ||
| #endif |
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.
I made the assumption that you wanted to switch to a delegate in place of using Notifications. I can easily remove the deprecation if that wasn't the intent.