Skip to content

Instantly share code, notes, and snippets.

@jasdev
Last active April 7, 2020 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasdev/68a3968a5b96b471c7d1020dce9439cb to your computer and use it in GitHub Desktop.
Save jasdev/68a3968a5b96b471c7d1020dce9439cb to your computer and use it in GitHub Desktop.
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