Bimba.git

ref: 7b7c1299ce86a2cbc96c59f35995cae1b039f580

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

package xyz.apiote.bimba.czwek.api

import android.content.Context
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import xyz.apiote.bimba.czwek.R
import xyz.apiote.bimba.czwek.api.transitous.api.RoutingApi
import xyz.apiote.bimba.czwek.api.transitous.model.Mode
import xyz.apiote.bimba.czwek.repo.Journey
import xyz.apiote.bimba.czwek.repo.Leg
import xyz.apiote.bimba.czwek.repo.StopsLeg
import xyz.apiote.bimba.czwek.repo.TrafficResponseException
import xyz.apiote.bimba.czwek.repo.TransitLeg
import java.time.ZoneId

suspend fun getJourney(from: Double, to: Double, context: Context) {
	if (!isNetworkAvailable(context)) {
		throw TrafficResponseException(0, "", Error(0, R.string.error_offline, R.drawable.error_net))
	}

	return withContext(Dispatchers.IO) {
		val response = RoutingApi().plan(from.toString(), to.toString())
		val journeys = response.itineraries.map {
			val legs: List<Leg> = it.legs.map {
				if(it.mode in setOf(Mode.BUS)) {
					StopsLeg()
				} else {
					TransitLeg()
				}
			}
			Journey(it.startTime.atZoneSameInstant(ZoneId.systemDefault()), it.endTime.atZoneSameInstant(
				ZoneId.systemDefault()), legs)
		}
	}

}