Created
May 25, 2024 11:04
-
-
Save bannzai/35e4fbd4b6dd72ee8dafda0c2683fb98 to your computer and use it in GitHub Desktop.
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
import SwiftUI | |
struct AsyncAction<Content: View>: View { | |
@State var isLoading = false | |
@State var task: @Sendable @MainActor () async -> Void | |
@ViewBuilder let content: (Bool, @escaping () -> Void) -> Content | |
var body: some View { | |
content(isLoading, { | |
if isLoading { | |
return | |
} | |
isLoading = true | |
Task { @MainActor in | |
await task() | |
isLoading = false | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: