Skip to content

Instantly share code, notes, and snippets.

@barefeettom
Created April 7, 2021 04:00
Show Gist options
  • Save barefeettom/a716bf19634c35e2f57013b9fee7f0e4 to your computer and use it in GitHub Desktop.
Save barefeettom/a716bf19634c35e2f57013b9fee7f0e4 to your computer and use it in GitHub Desktop.
WeatherScene view model. For article: https://medium.com/p/4ddf8710d1a0/
extension WeatherScene {
class ViewModel: ObservableObject {
@Published var city: String = ""
@Published var countryCode: String = ""
@Published var system: System = .metric
@Published var site: Site?
private var subscribers = Set<AnyCancellable>()
}
}
extension WeatherScene.ViewModel {
func fetch() {
Weather.publisher(
city: city,
countryCode: countryCode,
system: system
)
.receive(on: DispatchQueue.main)
.sink(
receiveCompletion: { _ in },
receiveValue: { self.site = $0 }
)
.store(in: &subscribers)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment