-
-
Save jasdev/68a3968a5b96b471c7d1020dce9439cb to your computer and use it in GitHub Desktop.
Sample `ContentView` using `RandomNumberViewModel`.
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 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