Christopher Mederos
11/18/2023, 5:22 AMSam
11/20/2023, 3:22 PMAsyncButton
. It's rather simple to do.
public struct AsyncButton<Label: View>: View {
let action: () async -> Void
@ViewBuilder var label: () -> Label
@State var inProgress: Bool = false
public init(action: @escaping () async -> Void, label: @escaping () -> Label) {
self.action = action
self.label = label
}
public var body: some View {
Button(
action: {
inProgress = true
Task {
await action()
await MainActor.run {
inProgress = false
}
}
},
label: label)
.disabled(inProgress)
}
}
You can then apply whatever modifiers to it that you want and the Button
struct will pick them up.
struct SkipButton: View {
let action: () async -> Void
var body: some View {
AsyncButton(action: action, label: {
Text("Skip this step")
}).buttonStyle(.textButton)
}
}
disabled
view modifier at an appropriately high enough level in your hierarchy to disable buttons and form fields while a form is processing.
VStack {
// Children Views
}.disabled(viewModel.processing))
Due to the way SwiftUI works this cascades down view modifiers anything in that VStack can disable itself.Christopher Mederos
11/21/2023, 3:00 AM