Skip to content

Commit

Permalink
Improve async button ux (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
michele-theleftbit authored May 15, 2024
1 parent cfef135 commit 8a19f9e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ jobs:
with:
lfs: true
- name: Test BSWInterfaceKit
run: set -o pipefail && xcodebuild -scheme BSWInterfaceKit -destination "platform=iOS Simulator,name=iPhone 15,OS=17.2" -resultBundlePath TestResults.xcresult test | xcbeautify
run: set -o pipefail && xcodebuild -scheme BSWInterfaceKit -destination "platform=iOS Simulator,name=iPhone 15,OS=17.5" -resultBundlePath TestResults.xcresult test | xcbeautify
35 changes: 22 additions & 13 deletions Sources/BSWInterfaceKit/SwiftUI/Views/AsyncButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,45 @@ public struct AsyncButton<Label: View>: View {
public var body: some View {
Button(
action: {
Task {
await performAction()
if #available(iOS 17.0, *) {
withAnimation {
self.state = .loading
} completion: {
Task {
await performAction()
}
}
} else {
Task {
await performAction(forIOS16: true)
}
}
},
label: {
if loadingConfiguration.isBlocking {
label
} else {
label
.opacity(state == .loading ? 0 : 1)
.overlay {
label
.opacity(state == .loading ? 0 : 1)
.overlay {
if loadingConfiguration.isBlocking == false, state == .loading {
loadingView
.opacity(state == .loading ? 1 : 0)
}
}
}
}
)
.disabled((state == .loading) || (error != nil))
.errorAlert(error: $error)
}

@MainActor
private func performAction() async {
private func performAction(forIOS16: Bool = false) async {
var hudVC: UIViewController?
if loadingConfiguration.isBlocking {
hudVC = await presentHUDViewController()
}

withAnimation {
self.state = .loading
if forIOS16 {
withAnimation {
self.state = .loading
}
}

let result = await Swift.Result(catching: {
Expand Down

0 comments on commit 8a19f9e

Please sign in to comment.