swiftui-async-button

1.1.0

AsyncButton is the simple way to run concurrent code in your views.
lorenzofiamingo/swiftui-async-button

What's New

SwiftUI AsyncButton 1.1.0

2022-06-29T09:17:10Z

SwiftUI AsyncButton πŸ–²οΈ

AsyncButton is a Button capable of running concurrent code.

Usage

AsyncButton has the exact same API as Button, so you just have to change this:

Button("Run") { run() }

to this:

AsyncButton("Run") { try await run() }

In addition to Button initializers, you have the possibilities to specify special behaviours via AsyncButtonOptions:

AsyncButton("Ciao", options: [.showProgressViewOnLoading, .showAlertOnError], transaction: Transaction(animation: .default)) {
    try await run()
}

For heavy customizations you can have access to the AsyncButtonOperations:

AsyncButton {
    try await run()
} label: { operations in
    if operations.contains { operation in
        if case .loading = operation {
            return true
        } else {
            return false
        }
    } {
        Text("Loading")
    } else if
        let last = operations.last,
        case .completed(_, let result) = last
    {
        switch result {
        case .failure:
            Text("Try again")
        case .success:
            Text("Run again")
        }
    } else {
        Text("Run")
    }
}

Installation

  1. In Xcode, open your project and navigate to File β†’ Swift Packages β†’ Add Package Dependency...
  2. Paste the repository URL (https://github.com/lorenzofiamingo/swiftui-async-button) and click Next.
  3. Click Finish.

Other projects

SwiftUI CachedAsyncImage πŸ—ƒοΈ

SwiftUI MapItemPicker πŸ—ΊοΈ

SwiftUI PhotosPicker πŸŒ‡

SwiftUI VerticalTabView πŸ”

SwiftUI SharedObject 🍱

Description

  • Swift Tools
View More Packages from this Author

Dependencies

  • None
Last updated: Tue Nov 08 2022 07:57:54 GMT-0500 (GMT-05:00)