Skip to content

Instantly share code, notes, and snippets.

@lammertw
Created March 8, 2021 17:58
Show Gist options
  • Save lammertw/7f05c9bfc4206652ed9757f2162f861c to your computer and use it in GitHub Desktop.
Save lammertw/7f05c9bfc4206652ed9757f2162f861c to your computer and use it in GitHub Desktop.
import Combine
import SwiftUI
import shared
class ObservableRestaurantDetailViewModel: ObservableObject {
@Published var viewModelOutput: RestaurantDetailViewModelOutput?
init(viewModel: RestaurantDetailViewModel) {
asPublisher(viewModel.output)
.compactMap { $0 }
.receive(on: DispatchQueue.main)
.assign(to: &$viewModelOutput)
}
}
struct RestaurantDetailView: View {
@ObservedObject var viewModelOutput: ObservableRestaurantDetailViewModel
init(viewModel: RestaurantDetailViewModel) {
viewModelOutput = ObservableRestaurantDetailViewModel(viewModel: viewModel)
}
var body: some View {
if let output = viewModelOutput.viewModelOutput {
VStack {
Text(output.name)
Text(output.priceCategory)
// etc
}
} else {
EmptyView()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment