Skip to content

Commit

Permalink
Add helper method to initialize DotLottieFile with Data (#2090)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirti-swiggy committed Jun 26, 2023
1 parent 598b677 commit 4ff8174
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions Sources/Public/DotLottie/DotLottieFileHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,26 @@ extension DotLottieFile {
return .failure(error)
}
}

/// Loads an DotLottie from a data synchronously. Returns a `Result<DotLottieFile, Error>`
///
/// Please use the asynchronous methods whenever possible. This operation will block the Thread it is running in.
///
/// - Parameters:
/// - data: The data(`Foundation.Data`) object to load DotLottie from
/// - filename: The name of the lottie file without the lottie extension. eg. "StarAnimation"
public static func loadedFrom(
data: Data,
filename: String)
-> Result<DotLottieFile, Error>
{
do {
let dotLottieFile = try DotLottieFile(data: data, filename: filename)
return .success(dotLottieFile)
} catch {
return .failure(error)
}
}
}

/// Loads a DotLottie model from a bundle by its name. Returns `nil` if a file is not found.
Expand Down Expand Up @@ -289,4 +309,51 @@ extension DotLottieFile {
}
}

/// Loads an DotLottie from a data asynchronously.
///
/// - Parameters:
/// - data: The data(`Foundation.Data`) object to load DotLottie from
/// - filename: The name of the lottie file without the lottie extension. eg. "StarAnimation"
/// - dispatchQueue: A dispatch queue used to load animations. Defaults to `DispatchQueue.global()`. Optional.
/// - handleResult: A closure to be called when the file has loaded.
public static func loadedFrom(
data: Data,
filename: String,
dispatchQueue: DispatchQueue = .global(),
handleResult: @escaping (Result<DotLottieFile, Error>) -> Void)
{
dispatchQueue.async {
do {
let dotLottie = try DotLottieFile(data: data, filename: filename)
DispatchQueue.main.async {
handleResult(.success(dotLottie))
}
} catch {
DispatchQueue.main.async {
handleResult(.failure(error))
}
}
}
}

/// Loads an DotLottie from a data asynchronously.
///
/// - Parameters:
/// - data: The data(`Foundation.Data`) object to load DotLottie from
/// - filename: The name of the lottie file without the lottie extension. eg. "StarAnimation"
/// - dispatchQueue: A dispatch queue used to load animations. Defaults to `DispatchQueue.global()`. Optional.
@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
public static func loadedFrom(
data: Data,
filename: String,
dispatchQueue: DispatchQueue = .global())
async throws -> DotLottieFile
{
try await withCheckedThrowingContinuation { continuation in
loadedFrom(data: data, filename: filename, dispatchQueue: dispatchQueue) { result in
continuation.resume(with: result)
}
}
}

}

0 comments on commit 4ff8174

Please sign in to comment.