Skip to content

Commit

Permalink
Merge pull request #388 from theleftbit/use-structured-concurrency
Browse files Browse the repository at this point in the history
Use structured concurrency
  • Loading branch information
piercifani authored Jul 23, 2024
2 parents 5d1334d + d044638 commit 82aff46
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ private struct ContentView: View {

public extension View {

@available(iOS, deprecated: 16.4, message: "Use presentationCompactAdaptation(horizontal:vertical:) instead.")
func alwaysPopover<Content: View, Value: Identifiable>(id: Value.ID, isPresented: Binding<Value?>, @ViewBuilder content: @escaping () -> Content) -> some View {
self.modifier(AlwaysPopoverModifier(id: id, isPresented: isPresented, contentBlock: content))
}
Expand Down
29 changes: 9 additions & 20 deletions Sources/BSWInterfaceKit/SwiftUI/Views/AsyncButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,8 @@ public struct AsyncButton<Label: View>: View {
public var body: some View {
Button(
action: {
if #available(iOS 17.0, macOS 14, *) {
withAnimation {
self.state = .loading
} completion: {
Task {
await performAction()
}
}
} else {
Task {
await performAction(forIOS16: true)
}
withAnimation {
self.state = .loading
}
},
label: {
Expand All @@ -54,24 +44,23 @@ public struct AsyncButton<Label: View>: View {
)
.disabled((state == .loading) || (error != nil))
.errorAlert(error: $error)
.task(id: state) {
if state == .loading {
await performAction()
}
}
}

@MainActor
private func performAction(forIOS16: Bool = false) async {
private func performAction() async {

#if canImport(UIKit)
var hudVC: UIViewController?
if loadingConfiguration.isBlocking {
hudVC = await presentHUDViewController()
}
#endif

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


let result = await Swift.Result(catching: {
try await action()
})
Expand Down

0 comments on commit 82aff46

Please sign in to comment.