-
Notifications
You must be signed in to change notification settings - Fork 5
/
WeatherViewModel.kt
37 lines (33 loc) · 1.31 KB
/
WeatherViewModel.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package presentation.model
import arrow.core.raise.either
import arrow.core.raise.ensureNotNull
import arrow.core.raise.withError
import domain.location.LocationTracker
import domain.weather.WeatherRepository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
// lifecycle-aware == scope-aware == CoroutineScope as dependency
context(WeatherRepository, LocationTracker, CoroutineScope)
class WeatherViewModel {
private val _state: MutableStateFlow<WeatherState> = MutableStateFlow(WeatherState.Loading)
val state: StateFlow<WeatherState> = _state
fun loadWeatherInfo() {
launch(Dispatchers.IO) {
_state.value = WeatherState.Loading
val result = either<String, _> {
val location =
ensureNotNull(getCurrentLocation()) { "Couldn't retrieve location" }
val weather =
withError({ it.message }) { getWeatherData(location.latitude, location.longitude) }
WeatherState.Ok(location.name, weather)
}
_state.value = result.fold(
ifLeft = { WeatherState.Error(it) },
ifRight = { it }
)
}
}
}