Skip to content
This repository has been archived by the owner on Dec 16, 2024. It is now read-only.

Commit

Permalink
[FEAT] Adds ability to create dotLottie files from JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
eharrison committed Jun 22, 2021
1 parent f68154f commit 6633c7e
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 5 deletions.
15 changes: 15 additions & 0 deletions Example/dotLottieLoader/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,27 @@
//

import UIKit
import dotLottieLoader

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

DotLottieUtils.isLogEnabled = true
DotLottieLoader.dotLottie(fromJsonLottieAt: URL(string: "https://assets7.lottiefiles.com/packages/lf20_6k4jsmai.json")!) { url in
// file compressed into dotLottie
guard let url = url else { return }
DotLottieLoader.load(from: url) { dotLottieFile in
// file decompressed from dotLottie
guard let dotLottieFile = dotLottieFile else {
print("invalid dotLottie file")
return
}
print("dotLottieFile decompressed successfuly with \(dotLottieFile.animations.count) animation\(dotLottieFile.animations.count == 1 ? "" : "s")")
}
}
}

override func didReceiveMemoryWarning() {
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pod 'dotLottieLoader'
### Swift Package Manager

```swift
.package(url: "https://github.com/dotlottie/dotLottieLoader-ios.git", from: "0.1.3")
.package(url: "https://github.com/dotlottie/dotLottieLoader-ios.git", from: "0.1.4")
```

## Using dotLottie
Expand Down Expand Up @@ -74,6 +74,14 @@ DotLottieLoader.load(from: URL(string:"https://dotlottie.io/sample_files/animati
}
```

##### Creating .lottie file from JSON animation file

```swift
DotLottieLoader.dotLottie(fromJsonLottieAt: URL(string: "https://assets7.lottiefiles.com/packages/lf20_6k4jsmai.json")!) { dotLottieFileUrl in
// share or play `dotLottieFileUrl` using [DotLottie library](https://github.com/dotlottie/dotlottie-ios)
}
```

## Author

[Evandro Harrison Hoffmann](https://github.com/eharrison) | [email protected]
Expand Down
40 changes: 40 additions & 0 deletions Sources/dotLottieLoader/DotLottieFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,44 @@ public struct DotLottieFile {
}
}

/// Creates dotLottieFile from animation json
/// - Parameters:
/// - url: url of JSON lottie animation
/// - directory: directory to save file
/// - loop: loop enabled
/// - themeColor: theme color
/// - Returns: URL of .lottie file
static func compress(jsonLottieAt url: URL, in directory: URL = DotLottieUtils.tempDirectoryURL, loop: Bool = true, themeColor: String = "#ffffff") -> URL? {
Zip.addCustomFileExtension(DotLottieUtils.dotLottieExtension)

do {
let fileName = url.deletingPathExtension().lastPathComponent
let dotLottieDirectory = directory.appendingPathComponent(fileName)
try FileManager.default.createDirectory(at: dotLottieDirectory, withIntermediateDirectories: true, attributes: nil)

let animationsDirectory = dotLottieDirectory.appendingPathComponent("animations")
try FileManager.default.createDirectory(at: animationsDirectory, withIntermediateDirectories: true, attributes: nil)

let animationData = try Data(contentsOf: url)
try animationData.write(to: animationsDirectory.appendingPathComponent(fileName).appendingPathExtension("json"))

let manifest = DotLottieManifest(animations: [
DotLottieAnimation(loop: loop, themeColor: themeColor, speed: 1.0, id: fileName)
], version: "1.0", author: "LottieFiles", generator: "LottieFiles dotLottieLoader-iOS 0.1.4")
let manifestUrl = dotLottieDirectory.appendingPathComponent("manifest").appendingPathExtension("json")
let manifestData = try manifest.encode()
try manifestData.write(to: manifestUrl)

let dotLottieUrl = directory.appendingPathComponent(fileName).appendingPathExtension("lottie")
try Zip.zipFiles(paths: [animationsDirectory, manifestUrl], zipFilePath: dotLottieUrl, password: nil, compression: .DefaultCompression, progress: { progress in
DotLottieUtils.log("Compressing dotLottie file: \(progress)")
})

return dotLottieUrl
} catch {
DotLottieUtils.log("Extraction of dotLottie archive failed with error: \(error)")
return nil
}
}

}
22 changes: 22 additions & 0 deletions Sources/dotLottieLoader/DotLottieLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,26 @@ public class DotLottieLoader {
}
}).resume()
}

/// Creates .lottie file from a json animation
/// - Parameters:
/// - jsonUrl: URL to JSON lottie animation
/// - loop: loop enabled
/// - themeColor: theme color in HEX
/// - completion: URL to .lottie file
public static func dotLottie(fromJsonLottieAt jsonUrl: URL, loop: Bool = true, themeColor: String = "#ffffff", completion: @escaping (URL?) -> Void) {
guard jsonUrl.isJsonFile else {
DotLottieUtils.log("Not a json file")
return
}

guard let dotLottieUrl = DotLottieFile.compress(jsonLottieAt: jsonUrl, loop: loop, themeColor: themeColor) else {
DotLottieUtils.log("Failed to create dotLottie file")
completion(nil)
return
}

DotLottieUtils.log("Created dotLottie file at \(dotLottieUrl.absoluteString)")
completion(dotLottieUrl)
}
}
15 changes: 12 additions & 3 deletions Sources/dotLottieLoader/DotLottieManifest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

/// Manifest model for .lottie File
public struct DotLottieManifest: Decodable {
public struct DotLottieManifest: Codable {
public var animations: [DotLottieAnimation]
public var version: String
public var author: String
Expand All @@ -20,7 +20,15 @@ public struct DotLottieManifest: Decodable {
/// - Throws: Error
/// - Returns: .lottie Manifest model
public static func decode(from data: Data) throws -> DotLottieManifest? {
return try? JSONDecoder().decode(DotLottieManifest.self, from: data)
try? JSONDecoder().decode(DotLottieManifest.self, from: data)
}

/// Encodes to data
/// - Parameter encoder: JSONEncoder
/// - Throws: Error
/// - Returns: encoded Data
func encode(with encoder: JSONEncoder = JSONEncoder()) throws -> Data {
try encoder.encode(self)
}

/// Loads manifest from given URL
Expand All @@ -30,10 +38,11 @@ public struct DotLottieManifest: Decodable {
guard let data = try? Data(contentsOf: url) else { return nil }
return try? decode(from: data)
}

}

/// Animation model for .lottie File
public struct DotLottieAnimation: Decodable {
public struct DotLottieAnimation: Codable {
public var loop: Bool
public var themeColor: String
public var speed: Float
Expand Down
2 changes: 1 addition & 1 deletion dotLottieLoader.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'dotLottieLoader'
s.version = '0.1.3'
s.version = '0.1.4'
s.summary = 'An iOS library to natively load .lottie files https://dotlottie.io/'

s.description = <<-DESC
Expand Down

0 comments on commit 6633c7e

Please sign in to comment.