Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lorenzofiamingo/swiftui-async-button
AsyncButton is the simple way to run concurrent code in your views.
https://github.com/lorenzofiamingo/swiftui-async-button
Last synced: about 2 months ago
JSON representation
AsyncButton is the simple way to run concurrent code in your views.
- Host: GitHub
- URL: https://github.com/lorenzofiamingo/swiftui-async-button
- Owner: lorenzofiamingo
- Created: 2022-06-27T20:17:34.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-21T13:07:10.000Z (9 months ago)
- Last Synced: 2024-05-09T15:18:34.570Z (8 months ago)
- Language: Swift
- Size: 11.7 KB
- Stars: 27
- Watchers: 3
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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:
```swift
Button("Run") { run() }
```
to this:
```swift
AsyncButton("Run") { try await run() }
```In addition to `Button` initializers, you have the possibilities to specify special behaviours via `AsyncButtonOptions`:
```swift
AsyncButton("Ciao", options: [.showProgressViewOnLoading, .showAlertOnError], transaction: Transaction(animation: .default)) {
try await run()
}
```For heavy customizations you can have access to the `AsyncButtonOperation`s:
```swift
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 VariadicViews π₯](https://github.com/lorenzofiamingo/swiftui-variadic-views)
[SwiftUI CachedAsyncImage ποΈ](https://github.com/lorenzofiamingo/swiftui-cached-async-image)
[SwiftUI MapItemPicker πΊοΈ](https://github.com/lorenzofiamingo/swiftui-map-item-picker)
[SwiftUI PhotosPicker π](https://github.com/lorenzofiamingo/swiftui-photos-picker)
[SwiftUI VerticalTabView π](https://github.com/lorenzofiamingo/swiftui-vertical-tab-view)
[SwiftUI SharedObject π±](https://github.com/lorenzofiamingo/swiftui-shared-object)