Created
April 7, 2021 04:00
-
-
Save barefeettom/a716bf19634c35e2f57013b9fee7f0e4 to your computer and use it in GitHub Desktop.
WeatherScene view model. For article: https://medium.com/p/4ddf8710d1a0/
This file contains hidden or 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
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