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

Fix Incorect InitialValue Usage in SwiftUI Wrapper #2357

Merged
merged 3 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 10 additions & 2 deletions Example/Example/AnimationPreviewView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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") {
Expand Down
18 changes: 12 additions & 6 deletions Sources/Public/Animation/LottieView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public struct LottieView<Placeholder: View>: 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
}

Expand All @@ -28,7 +28,7 @@ public struct LottieView<Placeholder: View>: UIViewConfiguringSwiftUIView {
/// }
/// ```
public init(dotLottieFile: DotLottieFile?) where Placeholder == EmptyView {
_animationSource = State(initialValue: dotLottieFile.map(LottieAnimationSource.dotLottieFile))
localAnimation = dotLottieFile.map(LottieAnimationSource.dotLottieFile)
placeholder = nil
}

Expand Down Expand Up @@ -106,9 +106,10 @@ public struct LottieView<Placeholder: View>: UIViewConfiguringSwiftUIView {
_ loadAnimation: @escaping () async throws -> LottieAnimationSource?,
@ViewBuilder placeholder: @escaping () -> Placeholder)
{

self.loadAnimation = loadAnimation
self.localAnimation = nil
calda marked this conversation as resolved.
Show resolved Hide resolved
self.placeholder = placeholder
_animationSource = State(initialValue: nil)
}

// MARK: Public
Expand Down Expand Up @@ -453,7 +454,8 @@ public struct LottieView<Placeholder: View>: 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?
Expand All @@ -473,12 +475,16 @@ public struct LottieView<Placeholder: View>: 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)")
}
Expand All @@ -489,7 +495,7 @@ public struct LottieView<Placeholder: View>: UIViewConfiguringSwiftUIView {
guard loadAnimation != nil else { return }

if showPlaceholderWhileReloading {
animationSource = nil
remoteAnimation = nil
}

loadAnimationIfNecessary()
Expand Down
2 changes: 1 addition & 1 deletion Sources/Public/Controls/LottieViewType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading