Skip to content

Commit

Permalink
Renaming and preview simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Jimenez committed Jul 26, 2023
1 parent 49a78fb commit af0ea8c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
9 changes: 3 additions & 6 deletions Example/Example/AnimationPreviewView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct AnimationPreviewView: View {

var body: some View {
VStack {
LottieView(loadAnimationReference: loadAnimationReference) {
LottieView(loadAnimationTrigger: fetchTrigger) {
try await lottieSource()
} placeholder: {
LoadingIndicator()
Expand Down Expand Up @@ -85,15 +85,12 @@ struct AnimationPreviewView: View {
@State private var sliderValue: AnimationProgressTime = 0
@State private var currentURLIndex: Int

private var loadAnimationReference: Binding<AnyHashable> {
private var fetchTrigger: Binding<AnyHashable> {
switch animationSource {
case .local:
return .constant(currentURLIndex)
case .remote:
return $currentURLIndex
.mapGetter { _ in
urls[currentURLIndex]
}
return $currentURLIndex.mapGetter(transform: AnyHashable.init)
}
}

Expand Down
43 changes: 23 additions & 20 deletions Sources/Public/Animation/LottieView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,44 @@ public struct LottieView<Placeholder: View>: 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<AnyHashable> = .constant(LoadReference.constantSource),
loadAnimationTrigger: Binding<AnyHashable> = .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<AnyHashable> = .constant(LoadReference.constantSource),
loadAnimationTrigger: Binding<AnyHashable> = .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()
Expand All @@ -59,14 +62,14 @@ public struct LottieView<Placeholder: View>: 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<AnyHashable> = .constant(LoadReference.constantSource),
loadAnimationTrigger: Binding<AnyHashable> = .constant(LoadReference.constantSource),
_ loadDotLottieFile: @escaping () async throws -> DotLottieFile?) where Placeholder == EmptyView
{
self.init(
loadAnimationReference: loadAnimationReference,
loadAnimationTrigger: loadAnimationTrigger,
loadDotLottieFile,
placeholder: EmptyView.init)
}
Expand All @@ -75,14 +78,14 @@ public struct LottieView<Placeholder: View>: 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<AnyHashable> = .constant(LoadReference.constantSource),
loadAnimationTrigger: Binding<AnyHashable> = .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()
Expand All @@ -93,22 +96,22 @@ public struct LottieView<Placeholder: View>: 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<AnyHashable> = .constant(LoadReference.constantSource),
loadAnimationTrigger: Binding<AnyHashable> = .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
}
Expand Down Expand Up @@ -141,7 +144,7 @@ public struct LottieView<Placeholder: View>: UIViewConfiguringSwiftUIView {
.onAppear {
loadAnimationIfNecessary()
}
.valueChanged(value: loadAnimationReference.wrappedValue) { _ in
.valueChanged(value: loadAnimationTrigger.wrappedValue) { _ in
loadAnimationIfNecessary()
}
}
Expand Down Expand Up @@ -372,7 +375,7 @@ public struct LottieView<Placeholder: View>: UIViewConfiguringSwiftUIView {
// MARK: Private

@State private var animationSource: LottieAnimationSource?
private var loadAnimationReference: Binding<AnyHashable>
private var loadAnimationTrigger: Binding<AnyHashable>
private var loadAnimation: (() async throws -> LottieAnimationSource?)?
private var imageProvider: AnimationImageProvider?
private var textProvider: AnimationTextProvider = DefaultTextProvider()
Expand Down

0 comments on commit af0ea8c

Please sign in to comment.