Skip to content

Commit

Permalink
Add SwiftUI LottieView (#2092)
Browse files Browse the repository at this point in the history
  • Loading branch information
calda authored Jun 28, 2023
1 parent c4dc4d7 commit 5189094
Show file tree
Hide file tree
Showing 36 changed files with 403 additions and 586 deletions.
324 changes: 165 additions & 159 deletions Example/Example.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Example/iOS/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright © 2021 Airbnb Inc. All rights reserved.

import Lottie
import SwiftUI
import UIKit

@UIApplicationMain
Expand Down
19 changes: 19 additions & 0 deletions Example/iOS/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,29 @@ final class Configuration {
}
}

/// The rendering engine to use
static var previewImplementation: AnimationPreviewImplementation {
get {
AnimationPreviewImplementation(rawValue: UserDefaults.standard.string(forKey: #function) ?? "SwiftUI") ?? .swiftUI
}
set {
UserDefaults.standard.set(newValue.rawValue, forKey: #function)
}
}

/// Applies the current configuration (stored in UserDefaults)
/// to the singleton `LottieConfiguration.shared`
static func applyCurrentConfiguration() {
LottieConfiguration.shared.renderingEngine = renderingEngineOption
}

}

// MARK: - AnimationPreviewImplementation

enum AnimationPreviewImplementation: String, RawRepresentable {
/// Preview animations using the UIKit `AnimationPreviewViewController`
case uiKit = "UIKit"
/// Preview animations using the SwiftUI `AnimationPreviewView`
case swiftUI = "SwiftUI"
}
25 changes: 25 additions & 0 deletions Example/iOS/SwiftUI/AnimationPreviewView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Created by Cal Stephens on 6/23/23.
// Copyright © 2023 Airbnb Inc. All rights reserved.

import Lottie
import SwiftUI

/// TODO: Implement functionality from UIKit `AnimationPreviewViewController`
struct AnimationPreviewView: View {

let animationName: String

var body: some View {
VStack {
// TODO: Should `LottieView` take an optional `LottieAnimation` so it can support
// this sort of spelling without a forced-unwrap?
LottieView(animation: LottieAnimation.named(animationName)!)
.resizable()
.looping()
}
.navigationTitle(animationName.components(separatedBy: "/").last!)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color(uiColor: .secondarySystemBackground))
}

}
49 changes: 39 additions & 10 deletions Example/iOS/ViewControllers/SampleListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import Epoxy
import EpoxyCollectionView
import Lottie
import SwiftUI
import UIKit

/// Displays a list of all of the sample Lottie animations
Expand Down Expand Up @@ -82,9 +83,17 @@ final class SampleListViewController: CollectionViewController {
animationName: animationPath,
title: animationName))
.didSelect { [weak self] context in
self?.show(
AnimationPreviewViewController(animationPath),
sender: context.view)
let previewViewController: UIViewController
switch Configuration.previewImplementation {
case .swiftUI:
previewViewController = UIHostingController(
rootView: AnimationPreviewView(animationName: animationPath))

case .uiKit:
previewViewController = AnimationPreviewViewController(animationPath)
}

self?.show(previewViewController, sender: context.view)
}
}
}
Expand Down Expand Up @@ -120,13 +129,23 @@ final class SampleListViewController: CollectionViewController {
title: "Settings",
image: UIImage(systemName: "gear")!,
primaryAction: nil,
menu: UIMenu(
title: "Rendering Engine",
children: [
action(for: .automatic),
action(for: .mainThread),
action(for: .coreAnimation),
]))
menu: UIMenu(children: [
UIMenu(
title: "Rendering Engine",
options: .displayInline,
children: [
action(for: .automatic),
action(for: .mainThread),
action(for: .coreAnimation),
]),
UIMenu(
title: "Framework",
options: .displayInline,
children: [
action(for: .swiftUI),
action(for: .uiKit),
]),
]))
}

private func action(for renderingEngineOption: RenderingEngineOption) -> UIAction {
Expand All @@ -139,4 +158,14 @@ final class SampleListViewController: CollectionViewController {
})
}

private func action(for previewImplementation: AnimationPreviewImplementation) -> UIAction {
UIAction(
title: previewImplementation.rawValue,
state: Configuration.previewImplementation == previewImplementation ? .on : .off,
handler: { [weak self] _ in
Configuration.previewImplementation = previewImplementation
self?.configureSettingsMenu()
})
}

}
14 changes: 8 additions & 6 deletions Example/macOS/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="11134" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="21701" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="11134"/>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="21701"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Application-->
Expand Down Expand Up @@ -618,7 +620,7 @@
<menuItem title="Show Sidebar" keyEquivalent="s" id="kIP-vf-haE">
<modifierMask key="keyEquivalentModifierMask" control="YES" command="YES"/>
<connections>
<action selector="toggleSourceList:" target="Ady-hI-5gd" id="iwa-gc-5KM"/>
<action selector="toggleSidebar:" target="Ady-hI-5gd" id="iwa-gc-5KM"/>
</connections>
</menuItem>
<menuItem title="Enter Full Screen" keyEquivalent="f" id="4J7-dP-txa">
Expand Down Expand Up @@ -673,7 +675,7 @@
<outlet property="delegate" destination="Voe-Tx-rLC" id="PrD-fu-P6m"/>
</connections>
</application>
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModuleProvider="target"/>
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="Example__macOS_" customModuleProvider="target"/>
<customObject id="YLy-65-1bz" customClass="NSFontManager"/>
<customObject id="Ady-hI-5gd" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
Expand Down Expand Up @@ -703,7 +705,7 @@
<!--View Controller-->
<scene sceneID="hIz-AP-VOD">
<objects>
<viewController id="XfG-lQ-9wD" customClass="ViewController" customModuleProvider="target" sceneMemberID="viewController">
<viewController id="XfG-lQ-9wD" customClass="ViewController" customModule="Example__macOS_" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" wantsLayer="YES" id="m2S-Jp-Qdl">
<rect key="frame" x="0.0" y="0.0" width="480" height="270"/>
<autoresizingMask key="autoresizingMask"/>
Expand Down
24 changes: 0 additions & 24 deletions Example/tvOS/AppDelegate.swift

This file was deleted.

11 changes: 0 additions & 11 deletions Example/tvOS/Assets.xcassets/AccentColor.colorset/Contents.json

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 5189094

Please sign in to comment.