Bimba.git

ref: 3dc642a878cddf6ffa50a59e992a3f0945447d84

app/src/main/java/xyz/apiote/bimba/czwek/journeys/JourneysViewModel.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
// SPDX-FileCopyrightText: Adam Evyčędo
//
// SPDX-License-Identifier: GPL-3.0-or-later
package xyz.apiote.bimba.czwek.journeys

import android.content.Context
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import xyz.apiote.bimba.czwek.repo.Journey
import xyz.apiote.bimba.czwek.repo.JourneyParams
import xyz.apiote.bimba.czwek.repo.Place
import java.net.SocketTimeoutException

class JourneysViewModel : ViewModel() {

	private val _journeys = MutableLiveData<List<Journey>>()
	val journeys: LiveData<List<Journey>> = _journeys

	// FIXME when not in foreground, throws java.net.SocketException: Software caused connection abort
	fun getJourneys(context: Context, origin: Place, destination: Place, params: JourneyParams) {
		viewModelScope.launch {
			try {
				_journeys.value =
					xyz.apiote.bimba.czwek.api.getJourney(origin, destination, params, context)
			} catch (e: SocketTimeoutException) {
				_journeys.value = emptyList<Journey>()
				Log.e("Journeys", "timeout: $e")
			}
		}
	}
}