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
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Sources/Private/CoreAnimation/CoreAnimationLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,14 @@ extension CoreAnimationLayer: RootAnimationLayer {
rebuildCurrentAnimation()
}

func removeValueProvider(for keypath: AnimationKeypath) {
valueProviderStore.removeValueProvider(for: keypath)

// We need to rebuild the current animation after removing a value provider,
// since any existing `CAAnimation`s could now be out of date.
rebuildCurrentAnimation()
}

func getValue(for _: AnimationKeypath, atFrame _: AnimationFrameTime?) -> Any? {
logger.assertionFailure("""
The Core Animation rendering engine doesn't support querying values for individual frames
Expand Down
5 changes: 5 additions & 0 deletions Sources/Private/CoreAnimation/ValueProviderStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

}

/// Retrieves the custom value keyframes for the given property,
/// if an `AnyValueProvider` was registered for the given keypath.
func customKeyframes<Value>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!

for property in foundProperties {
property.removeProvider()
}
layer.displayWithFrame(frame: presentation()?.currentFrame ?? currentFrame, forceUpdates: true)
}
}
}

func getValue(for keypath: AnimationKeypath, atFrame: CGFloat?) -> Any? {
for layer in animationLayers {
if
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class NodeProperty<T>: AnyNodeProperty {
valueContainer.setNeedsUpdate()
}

func removeProvider() {
valueProvider = originalValueProvider
valueContainer.setNeedsUpdate()
}

func update(frame: CGFloat) {
typedContainer.setValue(valueProvider.value(frame: frame), forFrame: frame)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ protocol AnyNodeProperty {

/// Sets the value provider for the property.
func setProvider(provider: AnyValueProvider)

/// Removes the value provider for the property.
func removeProvider()
}

extension AnyNodeProperty {
Expand Down
1 change: 1 addition & 0 deletions Sources/Private/RootAnimationLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ protocol RootAnimationLayer: CALayer {
func logHierarchyKeypaths()
func allHierarchyKeypaths() -> [String]
func setValueProvider(_ valueProvider: AnyValueProvider, keypath: AnimationKeypath)
func removeValueProvider(for keypath: AnimationKeypath)
func getValue(for keypath: AnimationKeypath, atFrame: AnimationFrameTime?) -> Any?
func getOriginalValue(for keypath: AnimationKeypath, atFrame: AnimationFrameTime?) -> Any?

Expand Down
7 changes: 7 additions & 0 deletions Sources/Public/Animation/LottieAnimationLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,13 @@ public class LottieAnimationLayer: CALayer {
animationLayer.setValueProvider(valueProvider, keypath: keypath)
}

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 👍

animationLayer.removeValueProvider(for: keypath)
}

/// Reads the value of a property specified by the Keypath.
/// Returns nil if no property is found.
///
Expand Down
6 changes: 6 additions & 0 deletions Sources/Public/Animation/LottieAnimationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(for keypath: AnimationKeypath) {
lottieAnimationLayer.removeValueProvider(for: keypath)
}

/// Reads the value of a property specified by the Keypath.
/// Returns nil if no property is found.
///
Expand Down
5 changes: 5 additions & 0 deletions Sources/Public/Controls/AnimatedControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ open class AnimatedControl: LottieControlType {
animationView.setValueProvider(valueProvider, keypath: keypath)
}

/// Removes a ValueProvider for the specified keypath
public func removeValueProvider(for keypath: AnimationKeypath) {
animationView.removeValueProvider(for: keypath)
}

// MARK: Internal

var stateMap: [UInt: String] = [:]
Expand Down
19 changes: 19 additions & 0 deletions Tests/ValueProvidersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,24 @@ final class ValueProvidersTests: XCTestCase {
for: "Layer.Shape Group.Stroke 1.Color",
context: animationContext)
XCTAssertEqual(keyFramesQuery4?.keyframes.map(\.value.components), [[0, 0, 0, 1]])

// Test removing specific keypath
let keypathToRemove: AnimationKeypath = "**.Color"
store.setValueProvider(ColorValueProvider(.black), keypath: keypathToRemove)
store.removeValueProvider(for: keypathToRemove)
let keyFramesQuery5 = try store.customKeyframes(
of: .color,
for: "Layer.Shape Group.Stroke 1.Color",
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!

store.setValueProvider(ColorValueProvider(.black), keypath: "**1.Color")
store.removeValueProvider(for: "**.Color")
let keyFramesQuery6 = try store.customKeyframes(
of: .color,
for: "Layer.Shape Group.Stroke 1.Color",
context: animationContext)
XCTAssertNil(keyFramesQuery6)
}
}
Loading