Cześć.
Czy dobrym pomysłem jest używanie LiveData w ten sposób? Trzymanie pól typu LiveData w companion object zagnieżdżonego w TrackerService? Przeniesienie timera do bazy danych i załadowanie go do ViewModel powodowało opóźnienie. Jeśli miałoby to powodować jakieś problemy, to jak inaczej mogę przenieść dane z Service do ViewModel i umieścić je w LiveData?
@AndroidEntryPoint
class TrackerService: LifecycleService() {
@Inject
lateinit var notification: NotificationCompat.Builder
@Inject
lateinit var notificationManager: NotificationManager
private lateinit var fusedLocationProviderClient: FusedLocationProviderClient
private val timer = Timer()
companion object {
val started = MutableLiveData<Boolean>()
val distanceMeters = MutableLiveData<Double>()
val runningTime = MutableLiveData<Int>()
val date = MutableLiveData<Long>()
val locationList = MutableLiveData<MutableList<LatLng>>()
val kilometerReached = MutableLiveData<Boolean>()
val paceTimes = MutableLiveData<MutableList<Int>>()
val avgPaceTime = MutableLiveData<Double>()
val burnedKcal = MutableLiveData<Int>()
fun timerReset() {
runTime.postValue(0)
}
}
[...]
Druga rzecz, z którą się zmagam, to filtrowanie listy z obserwowanych liveData. Czy dobrym pomysłem jest filtrowanie go w metodzie observe? Może powinienem to zrobić w inny sposób ponieważ np. grozi to zatrzymywaniem UI?
mainViewModel.readRuns.observe(viewLifecycleOwner) { runs ->
mAdapter.setData(runs.sortedWith (
when(historyViewModel.selectedChip){
binding.dateChip.id ->
if(desc) compareByDescending {it.date}
else compareBy {it.date}
binding.timeChip.id -> if(desc) compareByDescending {it.runTime}
else compareBy {it.runTime}
binding.distanceChip.id -> if(desc) compareByDescending {it.distanceMeters}
else compareBy {it.distanceMeters}
binding.caloriesChip.id -> if(desc) compareByDescending {it.burnedKcal}
else compareBy {it.burnedKcal}
else -> if(desc) compareByDescending {it.date} else compareBy {it.burnedKcal}
}
))
}