From 7dabde78b39fee4d22b993a2c301d099f871a5e4 Mon Sep 17 00:00:00 2001 From: Bryn Bodayle Date: Tue, 2 Apr 2024 11:26:21 -0700 Subject: [PATCH 1/3] Fix for incorrect initial value usage in SwiftUI --- Sources/Public/Animation/LottieView.swift | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Sources/Public/Animation/LottieView.swift b/Sources/Public/Animation/LottieView.swift index 6b3b86d870..7ad6eeb4a8 100644 --- a/Sources/Public/Animation/LottieView.swift +++ b/Sources/Public/Animation/LottieView.swift @@ -13,7 +13,7 @@ public struct LottieView: UIViewConfiguringSwiftUIView { /// Creates a `LottieView` that displays the given animation public init(animation: LottieAnimation?) where Placeholder == EmptyView { - _animationSource = State(initialValue: animation.map(LottieAnimationSource.lottieAnimation)) + localAnimation = animation.map(LottieAnimationSource.lottieAnimation) placeholder = nil } @@ -28,7 +28,7 @@ public struct LottieView: UIViewConfiguringSwiftUIView { /// } /// ``` public init(dotLottieFile: DotLottieFile?) where Placeholder == EmptyView { - _animationSource = State(initialValue: dotLottieFile.map(LottieAnimationSource.dotLottieFile)) + localAnimation = dotLottieFile.map(LottieAnimationSource.dotLottieFile) placeholder = nil } @@ -106,9 +106,10 @@ public struct LottieView: UIViewConfiguringSwiftUIView { _ loadAnimation: @escaping () async throws -> LottieAnimationSource?, @ViewBuilder placeholder: @escaping () -> Placeholder) { + self.loadAnimation = loadAnimation + self.localAnimation = nil self.placeholder = placeholder - _animationSource = State(initialValue: nil) } // MARK: Public @@ -453,7 +454,8 @@ public struct LottieView: UIViewConfiguringSwiftUIView { // MARK: Private - @State private var animationSource: LottieAnimationSource? + private let localAnimation: LottieAnimationSource? + @State private var remoteAnimation: LottieAnimationSource? private var playbackMode: LottiePlaybackMode? private var animationSpeed: Double? private var reloadAnimationTrigger: AnyEquatable? @@ -473,12 +475,16 @@ public struct LottieView: UIViewConfiguringSwiftUIView { imageProvider: AnimationImageProvider, imageProvidersAreEqual: (AnimationImageProvider, AnimationImageProvider) -> Bool)? + private var animationSource: LottieAnimationSource? { + localAnimation ?? remoteAnimation + } + private func loadAnimationIfNecessary() { guard let loadAnimation else { return } Task { do { - animationSource = try await loadAnimation() + remoteAnimation = try await loadAnimation() } catch { logger.warn("Failed to load asynchronous Lottie animation with error: \(error)") } @@ -489,7 +495,7 @@ public struct LottieView: UIViewConfiguringSwiftUIView { guard loadAnimation != nil else { return } if showPlaceholderWhileReloading { - animationSource = nil + remoteAnimation = nil } loadAnimationIfNecessary() From 9325fb90317ae24fcf06b03b41adc5e2ee8c6155 Mon Sep 17 00:00:00 2001 From: Bryn Bodayle Date: Tue, 2 Apr 2024 11:26:37 -0700 Subject: [PATCH 2/3] Fix for new Swift and Xcode version --- Example/Example/AnimationPreviewView.swift | 12 ++++++++++-- Sources/Public/Controls/LottieViewType.swift | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Example/Example/AnimationPreviewView.swift b/Example/Example/AnimationPreviewView.swift index d2af929766..b100af81fd 100644 --- a/Example/Example/AnimationPreviewView.swift +++ b/Example/Example/AnimationPreviewView.swift @@ -44,7 +44,10 @@ struct AnimationPreviewView: View { var body: some View { VStack { LottieView { - try await loadAnimation() + try await Self.loadAnimation( + from: animationSource, + urls: urls, + currentURLIndex: currentURLIndex) } placeholder: { LoadingIndicator() .frame(width: 50, height: 50) @@ -172,7 +175,12 @@ struct AnimationPreviewView: View { #endif } - private func loadAnimation() async throws -> LottieAnimationSource? { + private static func loadAnimation( + from animationSource: AnimationSource, + urls: [URL], + currentURLIndex: Int) + async throws -> LottieAnimationSource? + { switch animationSource { case .local(let name): if name.hasSuffix(".lottie") { diff --git a/Sources/Public/Controls/LottieViewType.swift b/Sources/Public/Controls/LottieViewType.swift index 363183f711..ec97c97df0 100644 --- a/Sources/Public/Controls/LottieViewType.swift +++ b/Sources/Public/Controls/LottieViewType.swift @@ -44,7 +44,7 @@ public enum LottieNSControlState: UInt, RawRepresentable { /// - `UIControl.Event` on iOS / tvOS and `LottieNSControlEvent` on macOS. public typealias LottieControlEvent = LottieNSControlEvent -public struct LottieNSControlEvent: Equatable { +public struct LottieNSControlEvent: Equatable, Sendable { // MARK: Lifecycle From 7555a6c1c866c64815c64da57790f2c93520c4e6 Mon Sep 17 00:00:00 2001 From: Cal Stephens Date: Tue, 2 Apr 2024 13:36:52 -0700 Subject: [PATCH 3/3] Update Sources/Public/Animation/LottieView.swift --- Sources/Public/Animation/LottieView.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sources/Public/Animation/LottieView.swift b/Sources/Public/Animation/LottieView.swift index 7ad6eeb4a8..dc0e59e882 100644 --- a/Sources/Public/Animation/LottieView.swift +++ b/Sources/Public/Animation/LottieView.swift @@ -106,9 +106,8 @@ public struct LottieView: UIViewConfiguringSwiftUIView { _ loadAnimation: @escaping () async throws -> LottieAnimationSource?, @ViewBuilder placeholder: @escaping () -> Placeholder) { - + localAnimation = nil self.loadAnimation = loadAnimation - self.localAnimation = nil self.placeholder = placeholder }