ref: 610865e8df2cf3b90fa1ee401ce15fd043ae7460
app/src/main/java/xyz/apiote/bimba/czwek/departures/DeparturesViewModel.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 |
// SPDX-FileCopyrightText: Adam Evyčędo // // SPDX-License-Identifier: GPL-3.0-or-later package xyz.apiote.bimba.czwek.departures import android.content.Context import android.util.Log import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import kotlinx.coroutines.MainScope import kotlinx.coroutines.launch import xyz.apiote.bimba.czwek.api.Error import xyz.apiote.bimba.czwek.api.mapHttpError import xyz.apiote.bimba.czwek.repo.OnlineRepository import xyz.apiote.bimba.czwek.repo.StopDepartures import xyz.apiote.bimba.czwek.repo.TrafficResponseException class DeparturesViewModel: ViewModel() { private val _departures = MutableLiveData<StopDepartures>() val departures: LiveData<StopDepartures> = _departures private val _error = MutableLiveData<Error>() val error: LiveData<Error> = _error var requestedItemsNumber = 12 var allItemsRequested = false fun getDepartures(context: Context, feedID: String, code: String, line: String?) { MainScope().launch { try { val repository = OnlineRepository() val stopDepartures = repository.getDepartures( feedID, code, line, context, requestedItemsNumber ) stopDepartures?.let { if (stopDepartures.departures.isEmpty()) { val (string, image) = mapHttpError(404) throw TrafficResponseException(404, "", Error(404, string, image)) } _departures.value = it } } catch (e: TrafficResponseException) { if (!departures.isInitialized) { _error.value = e.error } Log.w("Departures", "$e") } } } } |