/
DashboardViewModel.kt
59 lines (49 loc) · 2.04 KB
/
DashboardViewModel.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package foo.bar.clean.ui.dashboard
import androidx.lifecycle.ViewModel
import co.early.fore.core.ui.SyncableView
import co.early.fore.kt.core.ui.ViewModelObservability
import co.early.fore.kt.core.ui.ViewModelObservabilityImp
import foo.bar.clean.domain.refresher.RefreshModel
import foo.bar.clean.domain.weather.WeatherModel
class DashboardViewModel(
private val weatherModel: WeatherModel,
private val refreshModel: RefreshModel,
) : ViewModel(), SyncableView, ViewModelObservability by ViewModelObservabilityImp(
weatherModel, refreshModel
) {
var viewState = DashboardViewState()
private set
init {
initSyncableView(this)
}
// this gets called whenever our domain models' state changes
override fun syncView() {
viewState = DashboardViewState(
WeatherViewState(
maxTempC = weatherModel.currentState.weatherReport.temperature.maxTempC ?: MIN_DIAL_TEMP,
minTempC = weatherModel.currentState.weatherReport.temperature.minTempC ?: MIN_DIAL_TEMP,
windSpeedKmpH = weatherModel.currentState.weatherReport.windSpeed.windSpeedKmpH,
pollenLevel = weatherModel.currentState.weatherReport.pollenCount.pollenLevel,
),
AutoRefreshViewState(
timeElapsedPcent = refreshModel.currentState.percentElapsedToNextUpdate(),
autoRefreshing = !refreshModel.currentState.updatesPaused
),
error = weatherModel.currentState.error,
isUpdating = weatherModel.currentState.isUpdating
)
notifyObservers()
}
fun updateNow() {
// these are fire and forget, the observers take care of any resulting changes
weatherModel.fetchWeatherReport()
}
fun startAutoRefresh() {
// these are fire and forget, the observers take care of any resulting changes
refreshModel.start()
}
fun stopAutoRefresh() {
// these are fire and forget, the observers take care of any resulting changes
refreshModel.stop()
}
}