Skip to content
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

Add possibility to remove ValueProvider #2474

Merged
merged 8 commits into from
Sep 10, 2024

Conversation

batanus
Copy link
Contributor

@batanus batanus commented Sep 6, 2024

Summary

Previously, when the animation property was set, all ValueProviders for AnimatioView were reset (see #920). Now it's not so (see #2390)

@batanus batanus changed the title Add possibility to Remove ValueProvider Add possibility to remove ValueProvider Sep 6, 2024
@@ -673,6 +673,12 @@ open class LottieAnimationView: LottieAnimationViewBase {
lottieAnimationLayer.setValueProvider(valueProvider, keypath: keypath)
}

/// Sets a ValueProvider for the specified keypath. The value provider will be removed
/// on all properties that match the keypath.
public func removeValueProvider(keypath: AnimationKeypath) {
Copy link
Member

Choose a reason for hiding this comment

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

A better name for this would probably be:

Suggested change
public func removeValueProvider(keypath: AnimationKeypath) {
public func removeValueProvider(for keypath: AnimationKeypath) {

@@ -40,6 +40,11 @@ final class ValueProviderStore {
valueProviders.append((keypath: keypath, valueProvider: valueProvider))
}

/// Removes all ValueProviders for the given `AnimationKeypath`
func removeValueProvider(keypath: AnimationKeypath) {
valueProviders.removeAll(where: { $0.keypath == keypath })
Copy link
Member

Choose a reason for hiding this comment

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

What about if the given keypath is something like Layer.*? Should it remove key paths like Layer.Color? It seems like that probably wouldn't happen with the current implementation using ==.

Copy link
Member

Choose a reason for hiding this comment

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

Could you please add a test case for this to ValueProvidersTests.swift, with some cases like that where the the key path being removed isn't directly present in the list of providers and instead matches using wildcards?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is that behavior really expected? The method is named removeValueProvider, which suggests that it should only remove the exact value provider for the specified keyPath, and not multiple ValueProviders.
Another point - in setValueProvider the value providers are removed the same way as is implemented in this method (see here - https://github.com/airbnb/lottie-ios/blob/master/Sources/Private/CoreAnimation/ValueProviderStore.swift#L39). If you anyway would like to have functionality for wildcard case (like Layer.*) - I'm not sure how exactly it should work, so help would be wanted.
I added test for the current removeValueProvider implementation.

Thank you

Copy link
Member

Choose a reason for hiding this comment

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

Is that behavior really expected?

Good question, I can see it making sense for the behavior to stay as-is in the Core Animation rendering engine.

However, I'm pretty sure that this behavior that I describe is the current behavior implemented in this PR for the Main Thread rendering engine. We should make sure the two implementations are consistent.

@calda
Copy link
Member

calda commented Sep 8, 2024

You'll also need to run bundle exec rake format:swift, which automatically reformats the code to follow Airbnb's Swift Style Guide.

@@ -241,6 +241,17 @@ final class MainThreadAnimationLayer: CALayer, RootAnimationLayer {
}
}

func removeValueProvider(for keypath: AnimationKeypath) {
for layer in animationLayers {
if let foundProperties = layer.nodeProperties(for: keypath) {
Copy link
Member

Choose a reason for hiding this comment

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

Like I mentioned in #2474 (comment), I think this behavior is different than the behavior implemented for the Core Animation rendering engine in ValueProviderStore.

layer.nodeProperties(for: keypath) returns all of the properties that match the given key path, so for example if you add a provider with Layer.Color and then call removeValueProvider(for: "**.Color"), this implementation will remove the provider for Layer.Color (but the implementation in the Core Animation rendering engine will not).

Copy link
Member

Choose a reason for hiding this comment

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

I don't have a strong preference on which approach is better (removing all matches, or just removing exact matches) but I think the behavior of the Core Animation engine and Main Thread engine needs to be the same.

Copy link
Member

Choose a reason for hiding this comment

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

I have a feeling updating the behavior of ValueProviderStore to check for matches (using keypath.matches(...) instead of ==) would be easier than trying to change the implementation here to check for exact matches on the original input key path (which I don't think we have access to anymore).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, @calda, for your assistance!

context: animationContext)
XCTAssertNil(keyFramesQuery5)

// Test removing wildcard keypath
Copy link
Member

Choose a reason for hiding this comment

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

Thanks!

@@ -40,6 +40,11 @@ final class ValueProviderStore {
valueProviders.append((keypath: keypath, valueProvider: valueProvider))
}

/// Removes all ValueProviders for the given `AnimationKeypath`
func removeValueProvider(for keypath: AnimationKeypath) {
valueProviders.removeAll(where: { $0.keypath.matches(keypath) })
Copy link
Member

Choose a reason for hiding this comment

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

Perfect

public func removeValueProvider(for keypath: AnimationKeypath) {
guard let animationLayer = rootAnimationLayer else { return }

valueProviders[keypath] = nil
Copy link
Member

@calda calda Sep 10, 2024

Choose a reason for hiding this comment

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

I guess we should also do something like this here:

Suggested change
valueProviders[keypath] = nil
valueProviders.removeAll(where: { $0.key.matches(keypath) })

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The removeAll(where:) method isn't available for Dictionaries, so I used .forEach with a necessary check. Thank you 👍

Copy link
Member

@calda calda left a comment

Choose a reason for hiding this comment

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

Thanks for the update and test! Just one last comment before we merge this: https://github.com/airbnb/lottie-ios/pull/2474/files#r1752184422

Copy link
Member

@calda calda left a comment

Choose a reason for hiding this comment

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

Thank you!

@calda calda merged commit 7a8279b into airbnb:master Sep 10, 2024
12 checks passed
@batanus batanus deleted the add-possibility-to-remove-value-provider branch September 10, 2024 18:12
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.

3 participants