Skip to content

refactor(ios): shared property listener implementation#55

Merged
HayesGordon merged 3 commits into
mainfrom
refactor/shared-view-model-property-listener
Dec 1, 2025
Merged

refactor(ios): shared property listener implementation#55
HayesGordon merged 3 commits into
mainfrom
refactor/shared-view-model-property-listener

Conversation

@HayesGordon
Copy link
Copy Markdown
Contributor

This might be overkill. But I noticed in this PR, how much nicer the Kotlin code is.

I tried back in the day to abstract the Swift code but the properties don't inherit from the same base. This is another attempt by adding protocol extensions + composition with a helper class.

mfazekas
mfazekas previously approved these changes Nov 29, 2025
Copy link
Copy Markdown
Collaborator

@mfazekas mfazekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

func removeListeners() throws {
for id in listenerIds {
property.removeListener(id)
helper.trackListener { property in
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Btw can't we make a helper.addListener?! that does call trackListener and property.addListener?!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm cleaning this up!

}

/// Automatic addListener() ONLY when ListenerValueType == ValueType (no conversion needed)
extension ValuedPropertyProtocol where PropertyType.ListenerValueType == ValueType {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 addListener implementation.

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
Copy link
Copy Markdown
Contributor Author

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.

@HayesGordon HayesGordon merged commit 0a58796 into main Dec 1, 2025
7 checks passed
@HayesGordon HayesGordon deleted the refactor/shared-view-model-property-listener branch December 9, 2025 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants