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

[Idea] Add async await snippets to read me or lib #43

Open
tscholze opened this issue Jul 29, 2022 · 0 comments
Open

[Idea] Add async await snippets to read me or lib #43

tscholze opened this issue Jul 29, 2022 · 0 comments
Assignees

Comments

@tscholze
Copy link
Member

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) }
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants