Skip to content

Instantly share code, notes, and snippets.

@dmytro-anokhin
Last active August 20, 2020 17:47
Show Gist options
  • Save dmytro-anokhin/c4b183d2d7545d50fd16c0067ee47716 to your computer and use it in GitHub Desktop.
Save dmytro-anokhin/c4b183d2d7545d50fd16c0067ee47716 to your computer and use it in GitHub Desktop.
struct RemoteContentView<Value, Empty, Progress, Failure, Content> : View where Empty : View,
Progress : View,
Failure : View,
Content : View
{
let empty: () -> Empty
let progress: () -> Progress
let failure: (_ error: Error, _ retry: @escaping () -> Void) -> Failure
let content: (_ value: Value) -> Content
init<R: RemoteContent>(remoteContent: R,
empty: @escaping () -> Empty,
progress: @escaping () -> Progress,
failure: @escaping (_ error: Error, _ retry: @escaping () -> Void) -> Failure,
content: @escaping (_ value: Value) -> Content) where R.ObjectWillChangePublisher == ObservableObjectPublisher,
R.Value == Value
{
self.remoteContent = AnyRemoteContent(remoteContent)
self.empty = empty
self.progress = progress
self.failure = failure
self.content = content
}
var body: some View {
ZStack {
switch remoteContent.loadingState {
case .initial:
empty()
case .inProgress:
progress()
case .success(let value):
content(value)
case .failure(let error):
failure(error) {
remoteContent.load()
}
}
}
.onAppear {
remoteContent.load()
}
.onDisappear {
remoteContent.cancel()
}
}
@ObservedObject private var remoteContent: AnyRemoteContent<Value>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment