Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper method to initialise DotLottie with data #2090

Merged
merged 4 commits into from
Jun 26, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions Sources/Public/DotLottie/DotLottieFileHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,65 @@ extension DotLottieFile {
}
}

/// 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(
kirti-swiggy marked this conversation as resolved.
Show resolved Hide resolved
data: Data,
filename: String)
throws -> DotLottieFile
{
return try DotLottieFile(data: data, filename: filename)
}

/// 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)
}
}
}

}