-
Notifications
You must be signed in to change notification settings - Fork 9
refactor(ios): shared property listener implementation #55
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
Merged
HayesGordon
merged 3 commits into
main
from
refactor/shared-view-model-property-listener
Dec 1, 2025
Merged
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
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,97 @@ | ||
| import Foundation | ||
| import RiveRuntime | ||
|
|
||
| /// Protocol for Rive property types that support listener management | ||
| protocol RivePropertyWithListeners: AnyObject { | ||
| associatedtype ListenerValueType | ||
|
|
||
| func addListener(_ callback: @escaping (ListenerValueType) -> Void) -> UUID | ||
| func removeListener(_ id: UUID) | ||
| } | ||
|
|
||
| typealias BooleanPropertyType = RiveDataBindingViewModel.Instance.BooleanProperty | ||
| typealias NumberPropertyType = RiveDataBindingViewModel.Instance.NumberProperty | ||
| typealias StringPropertyType = RiveDataBindingViewModel.Instance.StringProperty | ||
| typealias EnumPropertyType = RiveDataBindingViewModel.Instance.EnumProperty | ||
| typealias ColorPropertyType = RiveDataBindingViewModel.Instance.ColorProperty | ||
| typealias TriggerPropertyType = RiveDataBindingViewModel.Instance.TriggerProperty | ||
| typealias ImagePropertyType = RiveDataBindingViewModel.Instance.ImageProperty | ||
|
|
||
| // Make all Rive property types conform to the protocol | ||
| extension BooleanPropertyType: RivePropertyWithListeners { | ||
| typealias ListenerValueType = Bool // Native: Bool → Bool (no conversion) | ||
| } | ||
| extension NumberPropertyType: RivePropertyWithListeners { | ||
| typealias ListenerValueType = Float // Native: Float → Double (needs conversion) | ||
| } | ||
| extension StringPropertyType: RivePropertyWithListeners { | ||
| typealias ListenerValueType = String // Native: String → String (no conversion) | ||
| } | ||
| extension EnumPropertyType: RivePropertyWithListeners { | ||
| typealias ListenerValueType = String // Native: String → String (no conversion) | ||
| } | ||
| extension ColorPropertyType: RivePropertyWithListeners { | ||
| typealias ListenerValueType = UIColor // Native: UIColor → Double (needs conversion) | ||
| } | ||
| // Note: TriggerProperty doesn't fit the pattern - it has () -> Void listeners, not (Void) -> Void | ||
|
|
||
| /// Helper class for managing ViewModel property listeners | ||
| class PropertyListenerHelper<PropertyType: RivePropertyWithListeners> { | ||
| private var listenerIds: [UUID] = [] | ||
| weak var property: PropertyType? | ||
|
|
||
| init(property: PropertyType) { | ||
| self.property = property | ||
| } | ||
|
|
||
| /// Adds a listener to the property and automatically tracks its ID for cleanup | ||
| func addListener(_ callback: @escaping (PropertyType.ListenerValueType) -> Void) { | ||
| guard let property = property else { return } | ||
| let id = property.addListener(callback) | ||
| listenerIds.append(id) | ||
| } | ||
|
|
||
| func removeListeners() throws { | ||
| guard let property = property else { return } | ||
| for id in listenerIds { | ||
| property.removeListener(id) | ||
| } | ||
| listenerIds.removeAll() | ||
| } | ||
|
|
||
| func dispose() throws { | ||
| try? removeListeners() | ||
| } | ||
| } | ||
|
|
||
| /// Protocol for properties that have typed values (Bool, String, Double, etc.) | ||
| /// Provides a default addListener implementation | ||
| protocol ValuedPropertyProtocol<ValueType> { | ||
| associatedtype PropertyType: RivePropertyWithListeners | ||
| associatedtype ValueType | ||
|
|
||
| var property: PropertyType! { get } | ||
| var helper: PropertyListenerHelper<PropertyType> { get } | ||
|
|
||
| func addListener(onChanged: @escaping (ValueType) -> Void) throws | ||
| func removeListeners() throws | ||
| func dispose() throws | ||
| } | ||
|
|
||
| /// Default implementations for lifecycle methods (always available) | ||
| extension ValuedPropertyProtocol { | ||
| func removeListeners() throws { | ||
| try helper.removeListeners() | ||
| } | ||
|
|
||
| func dispose() throws { | ||
| try helper.dispose() | ||
| } | ||
| } | ||
|
|
||
| /// Automatic addListener() ONLY when ListenerValueType == ValueType (no conversion needed) | ||
| extension ValuedPropertyProtocol where PropertyType.ListenerValueType == ValueType { | ||
|
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. We now automatically also handle adding a listener if the native type and the converted type is the same. If not, you have to provide the |
||
| func addListener(onChanged: @escaping (ValueType) -> Void) throws { | ||
| helper.addListener(onChanged) // Types match, just forward directly! | ||
| } | ||
| } | ||
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
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
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.
For some classes we won't be able to make use of this abstraction. But I think that is okay, we'll have 80% shared code, and things like Triggers and Lists will need to be custom.