You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We created an ION extension for BCGyou that wraps ION requests in async await function.
Maybe we should add it to the readme or an actual extension file inside the lib?
import UIKit
import Combine
import IONClient
// MARK: - ION -
extension ION
{
/// Downloads ION collection.
///
/// Async await wrapper for:
/// `ION.downloadCollection.onSuccess.onFailure`
///
/// - Throws: ION error if loading failed.
static func downloadCollection() async throws
{
try await withCheckedThrowingContinuation
{ continuation in
ION.downloadCollection()
.onSuccess { continuation.resume() }
.onFailure { continuation.resume(throwing: $0) }
}
}
/// Resets ION cache.
///
/// - Parameter includeDiskCache: Determines if disk cache should be also resetted
static func resetCache(includeDiskCache: Bool)
{
ION.config.lastOnlineUpdate.removeAll()
ION.resetMemCache()
includeDiskCache.onTrue { ION.resetDiskCache() }
}
/// Gets a fully loaded page by identifier.
///
/// Async await wrapper for:
/// `ION.page.onSuccess.onFailure`
///
/// - Parameter identifier: ION page identifier that should be loaded.
/// - Throws: ION error if loading failed.
/// - Returns: Found page object.
static func page(identifier: PageIdentifier) async throws -> Page
{
try await withCheckedThrowingContinuation
{ continuation in
ION.page(pageIdentifier: identifier, option: .full)
.onSuccess { continuation.resume(returning: $0) }
.onFailure { continuation.resume(throwing: $0) }
}
}
/// Gets a fully loaded pages by their identifiers.
///
/// Async await wrapper for:
/// `ION.pages.onSuccess.onFailure`
///
/// - Parameter identifier: ION page identifier that should be loaded.
/// - Throws: ION error if loading failed.
/// - Returns: Found page objects.
static func pages(identifiers: [PageIdentifier]) async throws -> [Page]
{
try await withCheckedThrowingContinuation
{ continuation in
ION.pages(pageIdentifiers: identifiers, option: .full)
.onSuccess { continuation.resume(returning: $0) }
.onFailure { continuation.resume(throwing: $0) }
}
}
/// Gets a fully loaded page and its parent by identifier.
///
/// - Parameter identifier: ION page identifier which itself with its parent should be loaded.
/// - Throws: ION error if loading failed.
/// - Returns: Touple of (parentPage, page)
static func pageWithParent(identifier: PageIdentifier) async throws -> (Page, Page)
{
// 1. Try to fully load page
let page = try await Self.page(identifier: identifier)
// 2. Check if page has a parent
guard let parentIdentifier = page.parent else
{ throw CustomError.custom(message: "No parent found for page: \(page.identifier)") }
// 3. Fully load parent page
let parent = try await Self.page(identifier: parentIdentifier)
// Return it as a touple
return (parent, page)
}
}
// MARK: - IONPage -
extension Page
{
/// Gets a fully loaded child pages by parent's identifier.
///
/// Async await wrapper for:
/// `IONPage.children.onSuccess.onFailure`
///
/// - Parameter identifier: ION page identifier which is the parent node.
/// - Throws: ION error if loading failed.
/// - Returns: Found page objects.
func children() async throws -> [Page]
{
try await withCheckedThrowingContinuation
{ continuation in
self.children
.onSuccess { continuation.resume(returning: $0) }
.onFailure { continuation.resume(throwing: $0) }
}
}
/// Gets an image from IONContent
///
/// Async await wrapper for:
/// `IONPage.content.o,age.onSuccess.onFailure`
///
/// - Parameter identifier:ION outlet identifier of the image
/// - Throws: ION error if loading failed.
/// - Returns: Found image
func image(for identifier: OutletIdentifier) async throws -> UIImage
{
try await withCheckedThrowingContinuation
{ continuation in
content.image(identifier)
.onSuccess
{ image in
Task
{
let scale = await UIScreen.main.scale
let size = CGSize(width: round(image.size.width / scale), height: round(image.size.height / scale))
let adjustedImage = image.resized(to: size, scale: scale)
continuation.resume(returning: adjustedImage ?? image)
}
}
.onFailure { continuation.resume(throwing: $0) }
}
}
}
The text was updated successfully, but these errors were encountered:
We created an ION extension for BCGyou that wraps ION requests in async await function.
Maybe we should add it to the readme or an actual extension file inside the lib?
The text was updated successfully, but these errors were encountered: