Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class WeatherService {
private struct Constants {
static let apiKey = "z74X7aKpZlip02a5Wlrq9R70JkDnOZsq"
}
static let shared: WeatherService = WeatherService()
fileprivate lazy var sessionManager: SessionManager = self.setupSessionManager()
fileprivate let appStore: AppStore = AppStore.shared
private init() {
setupObserving()
}
private func setupObserving() {
let locationProducer = self.appStore.producer
.map { $0.location.locationRequestState }
.filter { $0.isSuccess }
.skipRepeats()
.observe(on: QueueScheduler.main)
locationProducer.startWithValues { [weak self] _ in self?.appStore.consume(event: .geopositionRequest )}
let geopositionProducer = self.appStore.producer
.map { $0.weather.geopositionRequestState }
.skipRepeats() // To use skipRepeats we have to implement equatable for GeopositionRequestState
.observe(on: QueueScheduler.main)
geopositionProducer
.filter { $0 == .updating }
.startWithValues { [weak self] _ in self?.performGeopositionRequest() }
geopositionProducer
.filter { $0.isSuccess }
.startWithValues { [weak self] _ in self?.appStore.consume(event: .weatherRequest )}
let weatherProducer = self.appStore.producer
.map { $0.weather.weatherRequestState }
.filter { $0 == .updating }
.skipRepeats() // To use skipRepeats we have to implement equatable for WeatherRequestState
.observe(on: QueueScheduler.main)
weatherProducer.startWithValues { [weak self] _ in self?.performWeatherRequest() }
}
private func setupSessionManager() -> SessionManager {
return SessionManager.default // let's use very default for simplicity
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.