From af0ea8c54e592ba6eb2471659806af1a55211e27 Mon Sep 17 00:00:00 2001 From: Miguel Jimenez Date: Wed, 26 Jul 2023 19:39:10 -0400 Subject: [PATCH] Renaming and preview simplification --- Example/Example/AnimationPreviewView.swift | 9 ++--- Sources/Public/Animation/LottieView.swift | 43 ++++++++++++---------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/Example/Example/AnimationPreviewView.swift b/Example/Example/AnimationPreviewView.swift index e7f96170ff..af264fd6ac 100644 --- a/Example/Example/AnimationPreviewView.swift +++ b/Example/Example/AnimationPreviewView.swift @@ -44,7 +44,7 @@ struct AnimationPreviewView: View { var body: some View { VStack { - LottieView(loadAnimationReference: loadAnimationReference) { + LottieView(loadAnimationTrigger: fetchTrigger) { try await lottieSource() } placeholder: { LoadingIndicator() @@ -85,15 +85,12 @@ struct AnimationPreviewView: View { @State private var sliderValue: AnimationProgressTime = 0 @State private var currentURLIndex: Int - private var loadAnimationReference: Binding { + private var fetchTrigger: Binding { switch animationSource { case .local: return .constant(currentURLIndex) case .remote: - return $currentURLIndex - .mapGetter { _ in - urls[currentURLIndex] - } + return $currentURLIndex.mapGetter(transform: AnyHashable.init) } } diff --git a/Sources/Public/Animation/LottieView.swift b/Sources/Public/Animation/LottieView.swift index 9779691633..6a5b11f088 100644 --- a/Sources/Public/Animation/LottieView.swift +++ b/Sources/Public/Animation/LottieView.swift @@ -15,41 +15,44 @@ public struct LottieView: UIViewConfiguringSwiftUIView { public init(animation: LottieAnimation?) where Placeholder == EmptyView { _animationSource = State(initialValue: animation.map(LottieAnimationSource.lottieAnimation)) placeholder = nil - loadAnimationReference = .constant(LoadReference.constantSource) + loadAnimationTrigger = .constant(LoadReference.constantSource) } /// Creates a `LottieView` that displays the given `DotLottieFile` public init(dotLottieFile: DotLottieFile?) where Placeholder == EmptyView { _animationSource = State(initialValue: dotLottieFile.map(LottieAnimationSource.dotLottieFile)) placeholder = nil - loadAnimationReference = .constant(LoadReference.constantSource) + loadAnimationTrigger = .constant(LoadReference.constantSource) } /// Creates a `LottieView` that asynchronously loads and displays the given `LottieAnimation`. /// The `loadAnimation` closure is called exactly once in `onAppear`. /// - Parameters: - /// - loadAnimationReference: `Binding` to indicate that a new remote animation should be fetched + /// - loadAnimationTrigger: `Binding` that triggers a new call to `loadAnimation` when a new value is propagated /// - loadAnimation: Closure that provides an animation asynchronously public init( - loadAnimationReference: Binding = .constant(LoadReference.constantSource), + loadAnimationTrigger: Binding = .constant(LoadReference.constantSource), _ loadAnimation: @escaping () async throws -> LottieAnimation?) where Placeholder == EmptyView { - self.init(loadAnimationReference: loadAnimationReference, loadAnimation, placeholder: EmptyView.init) + self.init( + loadAnimationTrigger: loadAnimationTrigger, + loadAnimation, + placeholder: EmptyView.init) } /// Creates a `LottieView` that asynchronously loads and displays the given `LottieAnimation`. /// The `loadAnimation` closure is called exactly once in `onAppear`. /// While the animation is loading, the `placeholder` view is shown in place of the `LottieAnimationView`. /// - Parameters: - /// - loadAnimationReference: `Binding` to indicate that a new remote animation should be fetched + /// - loadAnimationTrigger: `Binding` that triggers a new call to `loadAnimation` when a new value is propagated /// - loadAnimation: Closure that provides an animation asynchronously public init( - loadAnimationReference: Binding = .constant(LoadReference.constantSource), + loadAnimationTrigger: Binding = .constant(LoadReference.constantSource), _ loadAnimation: @escaping () async throws -> LottieAnimation?, @ViewBuilder placeholder: @escaping (() -> Placeholder)) { - self.init(loadAnimationReference: loadAnimationReference) { + self.init(loadAnimationTrigger: loadAnimationTrigger) { try await loadAnimation().map(LottieAnimationSource.lottieAnimation) } placeholder: { placeholder() @@ -59,14 +62,14 @@ public struct LottieView: UIViewConfiguringSwiftUIView { /// Creates a `LottieView` that asynchronously loads and displays the given `DotLottieFile`. /// The `loadDotLottieFile` closure is called exactly once in `onAppear`. /// - Parameters: - /// - loadAnimationReference: `Binding` to indicate that a new remote animation should be fetched + /// - loadAnimationTrigger: `Binding` that triggers a new call to `loadAnimation` when a new value is propagated /// - loadAnimation: Closure that provides an animation asynchronously public init( - loadAnimationReference: Binding = .constant(LoadReference.constantSource), + loadAnimationTrigger: Binding = .constant(LoadReference.constantSource), _ loadDotLottieFile: @escaping () async throws -> DotLottieFile?) where Placeholder == EmptyView { self.init( - loadAnimationReference: loadAnimationReference, + loadAnimationTrigger: loadAnimationTrigger, loadDotLottieFile, placeholder: EmptyView.init) } @@ -75,14 +78,14 @@ public struct LottieView: UIViewConfiguringSwiftUIView { /// The `loadDotLottieFile` closure is called exactly once in `onAppear`. /// While the animation is loading, the `placeholder` view is shown in place of the `LottieAnimationView`. /// - Parameters: - /// - loadAnimationReference: `Binding` to indicate that a new remote animation should be fetched + /// - loadAnimationTrigger: `Binding` that triggers a new call to `loadAnimation` when a new value is propagated /// - loadAnimation: Closure that provides an animation asynchronously public init( - loadAnimationReference: Binding = .constant(LoadReference.constantSource), + loadAnimationTrigger: Binding = .constant(LoadReference.constantSource), _ loadDotLottieFile: @escaping () async throws -> DotLottieFile?, @ViewBuilder placeholder: @escaping (() -> Placeholder)) { - self.init(loadAnimationReference: loadAnimationReference) { + self.init(loadAnimationTrigger: loadAnimationTrigger) { try await loadDotLottieFile().map(LottieAnimationSource.dotLottieFile) } placeholder: { placeholder() @@ -93,22 +96,22 @@ public struct LottieView: UIViewConfiguringSwiftUIView { /// The `loadAnimation` closure is called exactly once in `onAppear`. /// While the animation is loading, the `placeholder` view is shown in place of the `LottieAnimationView`. /// - Parameters: - /// - loadAnimationReference: `Binding` to indicate that a new remote animation should be fetched + /// - loadAnimationTrigger: `Binding` that triggers a new call to `loadAnimation` when a new value is propagated /// - loadAnimation: Closure that provides an animation asynchronously public init( - loadAnimationReference: Binding = .constant(LoadReference.constantSource), + loadAnimationTrigger: Binding = .constant(LoadReference.constantSource), loadAnimation: @escaping () async throws -> LottieAnimationSource?, @ViewBuilder placeholder: @escaping () -> Placeholder) { self.loadAnimation = loadAnimation self.placeholder = placeholder - self.loadAnimationReference = loadAnimationReference + self.loadAnimationTrigger = loadAnimationTrigger _animationSource = State(initialValue: nil) } // MARK: Public - /// Token used when no `loadAnimationReference` is provided + /// Token used when no `loadAnimationTrigger` is provided public enum LoadReference { case constantSource } @@ -141,7 +144,7 @@ public struct LottieView: UIViewConfiguringSwiftUIView { .onAppear { loadAnimationIfNecessary() } - .valueChanged(value: loadAnimationReference.wrappedValue) { _ in + .valueChanged(value: loadAnimationTrigger.wrappedValue) { _ in loadAnimationIfNecessary() } } @@ -372,7 +375,7 @@ public struct LottieView: UIViewConfiguringSwiftUIView { // MARK: Private @State private var animationSource: LottieAnimationSource? - private var loadAnimationReference: Binding + private var loadAnimationTrigger: Binding private var loadAnimation: (() async throws -> LottieAnimationSource?)? private var imageProvider: AnimationImageProvider? private var textProvider: AnimationTextProvider = DefaultTextProvider()