Skip to content

Commit

Permalink
Add demo for remote url animation load
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Jimenez committed Jul 26, 2023
1 parent e76ed0c commit d971bac
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 11 deletions.
4 changes: 4 additions & 0 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
2E9E77AF27602BD400C84BA3 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E9E77AE27602BD400C84BA3 /* AppDelegate.swift */; };
2EC6E5082763D981002E091C /* LinkView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EC6E5072763D981002E091C /* LinkView.swift */; };
2EC6E5102763E79F002E091C /* AnimationPreviewViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EC6E50F2763E79F002E091C /* AnimationPreviewViewController.swift */; };
AB82EA9B2A7090B400AEBB48 /* RemoteAnimationDemoView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB82EA9A2A7090B400AEBB48 /* RemoteAnimationDemoView.swift */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -67,6 +68,7 @@
2E9E77BC27602BD400C84BA3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
2EC6E5072763D981002E091C /* LinkView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkView.swift; sourceTree = "<group>"; };
2EC6E50F2763E79F002E091C /* AnimationPreviewViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnimationPreviewViewController.swift; sourceTree = "<group>"; };
AB82EA9A2A7090B400AEBB48 /* RemoteAnimationDemoView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoteAnimationDemoView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -101,6 +103,7 @@
08E359932A55FFC400141956 /* LottieViewLayoutDemoView.swift */,
08E359972A55FFC600141956 /* Example.entitlements */,
607FACD11AFB9204008FA782 /* Products */,
AB82EA9A2A7090B400AEBB48 /* RemoteAnimationDemoView.swift */,
);
path = Example;
sourceTree = "<group>";
Expand Down Expand Up @@ -295,6 +298,7 @@
2E1670CC2784F9C1009CDED3 /* AnimatedSwitchRow.swift in Sources */,
2E97E3052767E7C600FE22C3 /* Configuration.swift in Sources */,
2E1670C32784F009009CDED3 /* ControlsDemoViewController.swift in Sources */,
AB82EA9B2A7090B400AEBB48 /* RemoteAnimationDemoView.swift in Sources */,
2E1670CA2784F123009CDED3 /* AnimatedButtonRow.swift in Sources */,
2E362A1E2762BA06006AE7D2 /* SampleListViewController.swift in Sources */,
2E9E77AF27602BD400C84BA3 /* AppDelegate.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Example/Example/AnimationListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct AnimationListView: View {
.navigationDestination(for: Item.self) { item in
switch item {
case .animation(_, let animationPath):
AnimationPreviewView(animationName: animationPath)
AnimationPreviewView(animationSource: .local(animationPath: animationPath))
case .subdirectory(let subdirectoryURL):
AnimationListView(directory: "\(directory)/\(subdirectoryURL.lastPathComponent)")
}
Expand Down
86 changes: 77 additions & 9 deletions Example/Example/AnimationPreviewView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,25 @@ struct AnimationPreviewView: View {

// MARK: Internal

let animationName: String
enum AnimationSource {
case local(animationPath: String)
case remote(url: URL)

var name: String {
switch self {
case .local(let name):
return name
case .remote:
return "Remote animation"
}
}
}

let animationSource: AnimationSource

var body: some View {
VStack {
LottieView(animation: .named(animationName))
.imageProvider(.exampleAppSampleImages)
.resizable()
.looping()
.currentProgress(animationPlaying ? nil : sliderValue)
.getRealtimeAnimationProgress(animationPlaying ? $sliderValue : nil)

lottieView
Spacer()

#if !os(tvOS)
Expand All @@ -33,7 +41,7 @@ struct AnimationPreviewView: View {
.padding(.all, 16)
#endif
}
.navigationTitle(animationName.components(separatedBy: "/").last!)
.navigationTitle(animationSource.name.components(separatedBy: "/").last!)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.secondaryBackground)
}
Expand All @@ -43,6 +51,47 @@ struct AnimationPreviewView: View {
@State private var animationPlaying = true
@State private var sliderValue: AnimationProgressTime = 0

@ViewBuilder
private var lottieView: some View {
switch animationSource {
case .local(let name):
localAnimation(animationName: name)
case .remote(let url):
LottieView {
await LottieAnimation.loadedFrom(url: url)
} placeholder: {
LoadingIndicator()
}
.imageProvider(.exampleAppSampleImages)
.resizable()
.looping()
.currentProgress(animationPlaying ? nil : sliderValue)
.getRealtimeAnimationProgress(animationPlaying ? $sliderValue : nil)
}
}

@ViewBuilder
private func localAnimation(animationName: String) -> some View {
if let animation = LottieAnimation.named(animationName) {
LottieView(animation: animation)
.imageProvider(.exampleAppSampleImages)
.resizable()
.looping()
.currentProgress(animationPlaying ? nil : sliderValue)
.getRealtimeAnimationProgress(animationPlaying ? $sliderValue : nil)
} else {
LottieView {
try await DotLottieFile.named(animationName)
} placeholder: {
LoadingIndicator()
}
.imageProvider(.exampleAppSampleImages)
.resizable()
.looping()
.currentProgress(animationPlaying ? nil : sliderValue)
.getRealtimeAnimationProgress(animationPlaying ? $sliderValue : nil)
}
}
}

extension Color {
Expand All @@ -60,3 +109,22 @@ extension AnimationImageProvider where Self == FilepathImageProvider {
FilepathImageProvider(filepath: Bundle.main.resourceURL!.appending(path: "Samples/Images"))
}
}

// MARK: - LoadingIndicator

struct LoadingIndicator: View {
@State private var animating = false

var body: some View {
Image(systemName: "rays")
.rotationEffect(animating ? Angle.degrees(360) : .zero)
.animation(
Animation
.linear(duration: 2)
.repeatForever(autoreverses: false),
value: animating)
.onAppear {
animating = true
}
}
}
49 changes: 49 additions & 0 deletions Example/Example/RemoteAnimationDemoView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Created by miguel_jimenez on 7/25/23.
// Copyright © 2023 Airbnb Inc. All rights reserved.

import Lottie
import SwiftUI

// MARK: - AnimationListView

struct RemoteAnimationsDemoView: View {

struct Item: Hashable {
let name: String
let url: URL
}

var body: some View {
NavigationStack {
List {
ForEach(items, id: \.self) { item in
NavigationLink(value: item.url) {
HStack {
LottieView {
await LottieAnimation.loadedFrom(url: item.url)
}
.currentProgress(0.5)
.imageProvider(.exampleAppSampleImages)
.frame(width: 50, height: 50)
.padding(EdgeInsets(top: 8, leading: 8, bottom: 8, trailing: 8))

Text(item.name)
}
}
.navigationDestination(for: URL.self) { url in
AnimationPreviewView(animationSource: .remote(url: url))
}
}
}.navigationTitle("Remote Animations")
}
}

var items: [Item] {
[
Item(
name: "Rooms Animation",
url: URL(string: "https://a0.muscache.com/pictures/96699af6-b73e-499f-b0f5-3c862ae7d126.json")!),
]
}

}
14 changes: 13 additions & 1 deletion Example/iOS/ViewControllers/SampleListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ final class SampleListViewController: CollectionViewController {

if isTopLevel {
demoLinks
remoteAnimationLinks
}
}

Expand Down Expand Up @@ -87,7 +88,7 @@ final class SampleListViewController: CollectionViewController {
switch Configuration.previewImplementation {
case .swiftUI:
previewViewController = UIHostingController(
rootView: AnimationPreviewView(animationName: animationPath))
rootView: AnimationPreviewView(animationSource: .local(animationPath: animationPath)))

case .uiKit:
previewViewController = AnimationPreviewViewController(animationPath)
Expand Down Expand Up @@ -124,6 +125,17 @@ final class SampleListViewController: CollectionViewController {
}
}

@ItemModelBuilder
private var remoteAnimationLinks: [ItemModeling] {
LinkView.itemModel(
dataID: "Remote animations",
content: .init(animationName: nil, title: "Remote animations"))
.didSelect { [weak self] _ in
let remoteAnimationsDemo = UIHostingController(rootView: RemoteAnimationsDemoView())
self?.present(remoteAnimationsDemo, animated: true)
}
}

private func configureSettingsMenu() {
navigationItem.rightBarButtonItem = UIBarButtonItem(
title: "Settings",
Expand Down

0 comments on commit d971bac

Please sign in to comment.