Bimba.git

ref: 009e40933320a46978bb4175a788e8a77e8390c9

app/src/main/java/xyz/apiote/bimba/czwek/repo/OnlineRepository.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
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package xyz.apiote.bimba.czwek.repo

import android.content.Context
import android.net.ConnectivityManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import xyz.apiote.bimba.czwek.api.DeparturesResponse
import xyz.apiote.bimba.czwek.api.DeparturesResponseDev
import xyz.apiote.bimba.czwek.api.DeparturesResponseV1
import xyz.apiote.bimba.czwek.api.ErrorResponse
import xyz.apiote.bimba.czwek.api.LocatablesResponse
import xyz.apiote.bimba.czwek.api.LocatablesResponseDev
import xyz.apiote.bimba.czwek.api.LocatablesResponseV1
import xyz.apiote.bimba.czwek.api.PositionV1
import xyz.apiote.bimba.czwek.api.QueryablesResponse
import xyz.apiote.bimba.czwek.api.QueryablesResponseDev
import xyz.apiote.bimba.czwek.api.QueryablesResponseV1
import xyz.apiote.bimba.czwek.api.Server
import xyz.apiote.bimba.czwek.api.StopV1
import xyz.apiote.bimba.czwek.api.StopV2
import xyz.apiote.bimba.czwek.api.VehicleV1
import xyz.apiote.bimba.czwek.api.VehicleV2

// todo [3.1] in Repository check if responses are BARE or HTML

// todo add feedID
class OnlineRepository : Repository {
	override suspend fun getDepartures(
		cm: ConnectivityManager,
		stop: String,
		line: String?,
		context: Context
	): StopDepartures? {
		val result = xyz.apiote.bimba.czwek.api.getDepartures(cm, Server.get(context), stop, line)
		if (result.error != null) {
			if (result.stream != null) {
				val response = withContext(Dispatchers.IO) { ErrorResponse.unmarshal(result.stream) }
				throw ErrorResponseError(result.error.statusCode, response.message, result.error)
			} else {
				throw ErrorResponseError(result.error.statusCode, "", result.error)
			}
		} else {
			return when (val response =
				withContext(Dispatchers.IO) { DeparturesResponse.unmarshal(result.stream!!) }) {
				is DeparturesResponseDev -> StopDepartures(response.departures.map { Departure(it) }, Stop(response.stop), response.alerts.map{Alert(it)})
				is DeparturesResponseV1 -> StopDepartures(response.departures.map { Departure(it) }, Stop(response.stop), response.alerts.map{Alert(it)})
				else -> null
			}
		}
	}

	override suspend fun getLocatablesIn(
		cm: ConnectivityManager,
		bl: Position,
		tr: Position,
		context: Context
	): List<Locatable>? {
		val result = xyz.apiote.bimba.czwek.api.getLocatablesIn(
			cm,
			Server.get(context),
			PositionV1(bl.latitude, bl.longitude),
			PositionV1(tr.latitude, tr.longitude)
		)
		if (result.error != null) {
			if (result.stream != null) {
				val response = withContext(Dispatchers.IO) { ErrorResponse.unmarshal(result.stream) }
				throw ErrorResponseError(result.error.statusCode, response.message, result.error)
			} else {
				throw ErrorResponseError(result.error.statusCode, "", result.error)
			}
		} else {
			return when (val response =
				withContext(Dispatchers.IO) { LocatablesResponse.unmarshal(result.stream!!) }) {
				is LocatablesResponseDev -> response.locatables.map {
					when (it) {
						is StopV2 -> Stop(it)
						is VehicleV2 -> Vehicle(it)
						else -> TODO("nothing else")
					}
				}

				is LocatablesResponseV1 -> response.locatables.map {
					when (it) {
						is StopV1 -> Stop(it)
						is VehicleV1 -> Vehicle(it)
						else -> TODO("nothing else")
					}
				}

				else -> null
			}
		}
	}

	override suspend fun queryQueryables(
		cm: ConnectivityManager, query: String, context: Context
	): List<Queryable>? {
		return getQueryables(cm, query, null, context, "query")
	}

	override suspend fun locateQueryables(
		cm: ConnectivityManager, position: Position, context: Context
	): List<Queryable>? {
		return getQueryables(cm, null, position, context, "locate")
	}

	private suspend fun getQueryables(
		cm: ConnectivityManager, query: String?, position: Position?, context: Context, type: String
	): List<Queryable>? {
		val result = when (type) {
			"query" -> {
				xyz.apiote.bimba.czwek.api.queryQueryables(cm, Server.get(context), query!!, limit = 12)
			}

			"locate" -> xyz.apiote.bimba.czwek.api.locateQueryables(
				cm, Server.get(context), PositionV1(position!!.latitude, position.longitude)
			)

			else -> TODO("Throw")
		}
		if (result.error != null) {
			if (result.stream != null) {
				val response = withContext(Dispatchers.IO) { ErrorResponse.unmarshal(result.stream) }
				throw ErrorResponseError(result.error.statusCode, response.message, result.error)
			} else {
				throw ErrorResponseError(result.error.statusCode, "", result.error)
			}
		} else {
			return when (val response =
				withContext(Dispatchers.IO) { QueryablesResponse.unmarshal(result.stream!!) }) {
				is QueryablesResponseDev -> response.queryables.map {
					when (it) {
						is StopV2 -> Stop(it)
						// todo[lines] is LineV1 -> Line(it)
						else -> TODO("nothing else")
					}
				}

				is QueryablesResponseV1 -> response.queryables.map {
					when (it) {
						is StopV1 -> Stop(it)
						else -> TODO("nothing else")
					}
				}

				else -> null
			}
		}
	}
}