Skip to content

Instantly share code, notes, and snippets.

@jasdev

jasdev/content_view.swift Secret

Last active Apr 7, 2020
Embed
What would you like to do?
Sample `ContentView` using `RandomNumberViewModel`.
struct ContentView: View {
@ObservedObject var viewModel = RandomNumberViewModel()
var body: some View {
Button(
action: viewModel.randomNumberPing.send, /// (1) Pipe button taps over to our view model’s `PassthroughSubject`.
label: { Text("Fetch a random number.") }
)
.disabled(viewModel.dataLoadState.isLoading)
.sheet(isPresented: viewModel.modalShown) {
view(for: self.viewModel.dataLoadState)
}
}
}
private func view(for dataLoadState: DataLoadState<Int, RandomDotOrgError>) -> some View {
switch dataLoadState {
case let .loaded(randomNumber):
return Text("\(randomNumber)")
case .error:
return Text("An error occured or an odd was returned!")
case .initial, .loading:
fatalError("We shouldn’t be displaying a modal in these cases.")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment