Skip to content

Latest commit

 

History

History
101 lines (81 loc) · 8.92 KB

README.md

File metadata and controls

101 lines (81 loc) · 8.92 KB

treastrain/AsyncSwiftUI

MIT License Swift: 5.9 Swift Package Manager compatible Platform: iOS 15.0+ | iPadOS 15.0+ | macOS 12.0+ | Mac Catalyst 15.0+ | tvOS 15.0+ | watchOS 8.0+ | visionOS 1.0+ Twitter: @treastrain

Wrappers for Swift Concurrency asynchronous actions on controls that enable user interaction specific to each platform and context provided by SwiftUI.

It's based on the following post: https://zenn.dev/treastrain/articles/3effccd39f4056

AsyncSwiftUI_README

Features

  • ✅ Designed for Swift Concurrency and SwiftUI.
  • ✅ Compatible with all Apple platforms (iPhone, iPod touch, iPad, Mac, Apple TV, Apple Watch, Apple Vision Pro).
  • ✅ Automatically cancels the task and performs a new action on new user interaction.
  • ✅ Automatically cancels the task after the view disappears before the action completes.
  • ✅ Supports Sendable and inherits the Actor context.
  • ✅ No dependencies.

Samples

import AsyncSwiftUI

struct ContentView: View {
    @State private var isRunning = false
    
    var body: some View {
        AsyncButton(isRunning ? "Running..." : "Run") {
            isRunning = true
            defer { isRunning = false }
            do {
                print("START")
                try await Task.sleep(for: .seconds(2))
            } catch {
                print("ERROR:", error)
            }
            print("FINISH")
        }
        .disabled(isRunning)
    }
}

Installation

To use this library in a Swift Package Manager project, add the following line to the dependencies in your Package.swift file:

.package(url: "https://github.com/treastrain/AsyncSwiftUI", from: "0.1.0"),

Include "AsyncSwiftUI" as a dependency for your executable target:

.target(name: "<target>", dependencies: [
    .product(name: "AsyncSwiftUI", package: "AsyncSwiftUI"),
]),

Finally, add import AsyncSwiftUI to your source code (or replace the existing import SwiftUI with it).

Components

Controls and indicators

SwiftUI AsyncSwiftUI Available on
Button AsyncButton iOS 15.0+, iPadOS 15.0+, macOS 12.0+, Mac Catalyst 15.0+, tvOS 15.0+, watchOS 8.0+, visionOS 1.0+
EditButton (No need) ―
PasteButton 🚧 ―
RenameButton 🚧 ―
Link (No need) ―
ShareLink (No need) ―
TextFieldLink AsyncTextFieldLink watchOS 9.0+
HelpLink 🚧 ―
Slider 🚧 ―
Stepper 🚧 ―
Toggle 🚧 ―
Picker (No need) ―
DatePicker (No need) ―
MultiDatePicker (No need) ―
ColorPicker (No need) ―
Gauge (No need) ―
ProgressView (No need) ―
ContentUnavailableView (No need) ―

Menus and commands

SwiftUI AsyncSwiftUI Available on
Menu 🚧 ―
MenuButton (No need) ―
PullDownButton (No need) ―
CommandMenu (No need) ―
CommandGroup (No need) ―

Note

The documentation comments included in this library, with some exceptions, are from the SwiftUI entry in the Apple Developer Documentation.