This repository has been archived by the owner on Dec 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from andreas16700/main
BIG Stability improvements
- Loading branch information
Showing
7 changed files
with
320 additions
and
25 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
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,44 @@ | ||
// | ||
// AsyncButton.swift | ||
// | ||
// Borrowed from https://www.swiftbysundell.com/articles/building-an-async-swiftui-button/ | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
struct AsyncButton<Label: View>: View { | ||
var action: () async -> Void | ||
@ViewBuilder var label: () -> Label | ||
|
||
@State private var isPerformingTask = false | ||
|
||
var body: some View { | ||
Button( | ||
action: { | ||
withAnimation{ | ||
isPerformingTask = true | ||
} | ||
Task { | ||
await action() | ||
withAnimation{ | ||
isPerformingTask = false | ||
} | ||
} | ||
}, | ||
label: { | ||
ZStack { | ||
// We hide the label by setting its opacity | ||
// to zero, since we don't want the button's | ||
// size to change while its task is performed: | ||
label().opacity(isPerformingTask ? 0 : 1) | ||
|
||
if isPerformingTask { | ||
ProgressView() | ||
} | ||
} | ||
} | ||
) | ||
.disabled(isPerformingTask) | ||
} | ||
} |
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
Oops, something went wrong.