-
-
Save laevandus/d5002fafec2503e35fcb8fc1393a88c4 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
struct ContentPrepareView<Content, Failure, Loading>: View where Content: View, Failure: View, Loading: View { | |
@State private var viewContent: ViewContent = .loading | |
@ViewBuilder let content: () -> Content | |
@ViewBuilder let failure: (Error, @escaping () async -> Void) -> Failure | |
@ViewBuilder let loading: () -> Loading | |
let task: () async throws -> Void | |
init(content: @escaping () -> Content, | |
failure: @escaping (Error, @escaping () async -> Void) -> Failure = { FailureView(error: $0, retryTask: $1) }, | |
loading: @escaping () -> Loading = { ProgressView() }, | |
task: @escaping () async throws -> Void) { | |
self.content = content | |
self.failure = failure | |
self.loading = loading | |
self.task = task | |
} | |
var body: some View { | |
Group { | |
switch viewContent { | |
case .content: | |
content() | |
case .failure(let error): | |
failure(error, loadTask) | |
case .loading: | |
loading() | |
} | |
} | |
.onLoad(perform: loadTask) | |
} | |
// redacted | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment