-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
123 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// | ||
// AsyncButton.swift | ||
// MySalesforceAccounts | ||
// | ||
// Created by Michael Epstein on 3/27/22. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
// Borrowed from https://www.swiftbysundell.com/articles/building-an-async-swiftui-button/ | ||
struct AsyncButton<Label: View>: View { | ||
|
||
var action: () async -> Void | ||
var actionOptions = Set(ActionOption.allCases) | ||
@ViewBuilder var label: () -> Label | ||
|
||
@State private var isDisabled = false | ||
@State private var showProgressView = false | ||
|
||
var body: some View { | ||
Button( | ||
action: { | ||
if actionOptions.contains(.disableButton) { | ||
isDisabled = true | ||
} | ||
|
||
Task { | ||
var progressViewTask: Task<Void, Error>? | ||
|
||
if actionOptions.contains(.showProgressView) { | ||
progressViewTask = Task { | ||
try await Task.sleep(nanoseconds: 150_000_000) | ||
showProgressView = true | ||
} | ||
} | ||
|
||
await action() | ||
progressViewTask?.cancel() | ||
|
||
isDisabled = false | ||
showProgressView = false | ||
} | ||
}, | ||
label: { | ||
ZStack { | ||
label().opacity(showProgressView ? 0 : 1) | ||
|
||
if showProgressView { | ||
ProgressView() | ||
} | ||
} | ||
} | ||
) | ||
.disabled(isDisabled) | ||
} | ||
} | ||
|
||
extension AsyncButton { | ||
enum ActionOption: CaseIterable { | ||
case disableButton | ||
case showProgressView | ||
} | ||
} | ||
|
||
extension AsyncButton where Label == Text { | ||
init(_ label: String, | ||
actionOptions: Set<ActionOption> = Set(ActionOption.allCases), | ||
action: @escaping () async -> Void) { | ||
self.init(action: action) { | ||
Text(label) | ||
} | ||
} | ||
} | ||
|
||
extension AsyncButton where Label == Image { | ||
init(systemImageName: String, | ||
actionOptions: Set<ActionOption> = Set(ActionOption.allCases), | ||
action: @escaping () async -> Void) { | ||
self.init(action: action) { | ||
Image(systemName: systemImageName) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters