-
Notifications
You must be signed in to change notification settings - Fork 39
/
DashboardActivityViewModel.kt
92 lines (75 loc) · 3 KB
/
DashboardActivityViewModel.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.ruuvi.station.dashboard.ui
import androidx.lifecycle.*
import com.ruuvi.station.app.preferences.PreferencesRepository
import com.ruuvi.station.network.data.NetworkSyncResult
import com.ruuvi.station.network.data.NetworkSyncResultType
import com.ruuvi.station.network.data.NetworkSyncStatus
import com.ruuvi.station.network.domain.NetworkDataSyncInteractor
import com.ruuvi.station.network.domain.NetworkTokenRepository
import com.ruuvi.station.tag.domain.RuuviTag
import com.ruuvi.station.tag.domain.TagInteractor
import com.ruuvi.station.units.domain.UnitsConverter
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.collect
import timber.log.Timber
class DashboardActivityViewModel(
private val tagInteractor: TagInteractor,
val converter: UnitsConverter,
val networkDataSyncInteractor: NetworkDataSyncInteractor,
preferencesRepository: PreferencesRepository,
private val tokenRepository: NetworkTokenRepository
) : ViewModel() {
private val tags = MutableLiveData<List<RuuviTag>>(arrayListOf())
val observeTags: LiveData<List<RuuviTag>> = tags
private val syncResult = MutableLiveData<NetworkSyncResult>(NetworkSyncResult(NetworkSyncResultType.NONE))
val syncResultObserve: LiveData<NetworkSyncResult> = syncResult
private val syncInProgress = MutableLiveData<Boolean>(false)
val syncInProgressObserve: LiveData<Boolean> = syncInProgress
val userEmail = preferencesRepository.getUserEmailLiveData()
val lastSync = preferencesRepository.getLastSyncDateLiveData()
val syncStatus: MediatorLiveData<NetworkSyncStatus> = MediatorLiveData<NetworkSyncStatus>()
private val trigger = MutableLiveData<Int>(1)
init {
updateTags()
Timber.d("DashboardActivityViewModel-syncStatusFlow")
viewModelScope.launch {
networkDataSyncInteractor.syncResultFlow.collect {
syncResult.value = it
}
}
viewModelScope.launch {
networkDataSyncInteractor.syncInProgressFlow.collect{
syncInProgress.value = it
updateTags()
}
}
syncStatus.addSource(syncInProgress) { syncStatus.value = getSyncStatus() }
syncStatus.addSource(lastSync) { syncStatus.value = getSyncStatus() }
syncStatus.addSource(trigger) { syncStatus.value = getSyncStatus() }
}
private fun getSyncStatus(): NetworkSyncStatus = NetworkSyncStatus(
syncInProgress.value ?: false,
lastSync.value ?: Long.MIN_VALUE
)
fun updateTags() {
viewModelScope.launch {
val getTags = tagInteractor.getTags()
withContext(Dispatchers.Main) {
tags.value = getTags
}
}
}
fun syncResultShowed() {
networkDataSyncInteractor.syncStatusShowed()
}
fun updateNetworkStatus() {
CoroutineScope(Dispatchers.Main).launch {
trigger.value = -1
}
}
fun signOut() {
tokenRepository.signOut {
updateTags()
}
}
}