Skip to content

Instantly share code, notes, and snippets.

@laevandus
Last active November 8, 2023 10:16
Show Gist options
  • Save laevandus/d5002fafec2503e35fcb8fc1393a88c4 to your computer and use it in GitHub Desktop.
Save laevandus/d5002fafec2503e35fcb8fc1393a88c4 to your computer and use it in GitHub Desktop.
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