Bimba.git

ref: ece2d164cf08f2544955a5a1b367e0030707302a

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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// SPDX-FileCopyrightText: Adam Evyčędo
//
// SPDX-License-Identifier: GPL-3.0-or-later

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.LineV1
import xyz.apiote.bimba.czwek.api.LineV2
import xyz.apiote.bimba.czwek.api.PositionV1
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.UnknownResourceException
import xyz.apiote.bimba.czwek.api.VehicleV1
import xyz.apiote.bimba.czwek.api.VehicleV2
import xyz.apiote.bimba.czwek.api.VehicleV3
import xyz.apiote.bimba.czwek.api.responses.DeparturesResponse
import xyz.apiote.bimba.czwek.api.responses.DeparturesResponseDev
import xyz.apiote.bimba.czwek.api.responses.DeparturesResponseV1
import xyz.apiote.bimba.czwek.api.responses.DeparturesResponseV2
import xyz.apiote.bimba.czwek.api.responses.DeparturesResponseV3
import xyz.apiote.bimba.czwek.api.responses.ErrorResponse
import xyz.apiote.bimba.czwek.api.responses.FeedsResponse
import xyz.apiote.bimba.czwek.api.responses.FeedsResponseDev
import xyz.apiote.bimba.czwek.api.responses.FeedsResponseV1
import xyz.apiote.bimba.czwek.api.responses.FeedsResponseV2
import xyz.apiote.bimba.czwek.api.responses.LineResponse
import xyz.apiote.bimba.czwek.api.responses.LineResponseDev
import xyz.apiote.bimba.czwek.api.responses.LineResponseV1
import xyz.apiote.bimba.czwek.api.responses.LineResponseV2
import xyz.apiote.bimba.czwek.api.responses.LocatablesResponse
import xyz.apiote.bimba.czwek.api.responses.LocatablesResponseDev
import xyz.apiote.bimba.czwek.api.responses.LocatablesResponseV1
import xyz.apiote.bimba.czwek.api.responses.LocatablesResponseV2
import xyz.apiote.bimba.czwek.api.responses.LocatablesResponseV3
import xyz.apiote.bimba.czwek.api.responses.QueryablesResponse
import xyz.apiote.bimba.czwek.api.responses.QueryablesResponseDev
import xyz.apiote.bimba.czwek.api.responses.QueryablesResponseV1
import xyz.apiote.bimba.czwek.api.responses.QueryablesResponseV2
import xyz.apiote.bimba.czwek.api.responses.QueryablesResponseV3
import java.io.File
import java.net.URLEncoder

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

class OnlineRepository : Repository {
	private fun saveFeedCache(server: Server, context: Context, rawResponse: ByteArray) {
		val file = File(
			context.cacheDir, URLEncoder.encode(server.apiPath, "utf-8")
		)
		file.writeBytes(rawResponse)
	}

	override suspend fun getFeeds(
		server: Server,
		context: Context
	): Map<String, FeedInfo>? {
		val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
		val result =
			xyz.apiote.bimba.czwek.api.getFeeds(cm, server)
		if (result.error != null) {
			if (result.stream != null) {
				val response = withContext(Dispatchers.IO) { ErrorResponse.unmarshal(result.stream) }
				throw TrafficResponseException(result.error.statusCode, response.message, result.error)
			} else {
				throw TrafficResponseException(result.error.statusCode, "", result.error)
			}
		} else {
			val rawResponse = result.stream!!.readBytes()
			saveFeedCache(server, context, rawResponse)
			return when (val response =
				withContext(Dispatchers.IO) { FeedsResponse.unmarshal(rawResponse.inputStream()) }) {
				is FeedsResponseDev -> response.feeds.associate { Pair(it.id, FeedInfo(it)) }
				is FeedsResponseV2 -> response.feeds.associate { Pair(it.id, FeedInfo(it)) }
				is FeedsResponseV1 -> response.feeds.associate { Pair(it.id, FeedInfo(it)) }

				else -> null
			}
		}
	}

	override suspend fun getDepartures(
		feedID: String,
		stop: String,
		line: String?,
		context: Context,
		limit: Int?
	): StopDepartures? {
		val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
		val result =
			xyz.apiote.bimba.czwek.api.getDepartures(cm, Server.get(context), feedID, stop, line, limit)
		if (result.error != null) {
			if (result.stream != null) {
				val response = withContext(Dispatchers.IO) { ErrorResponse.unmarshal(result.stream) }
				throw TrafficResponseException(result.error.statusCode, response.message, result.error)
			} else {
				throw TrafficResponseException(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 DeparturesResponseV3 -> StopDepartures(
					response.departures.map { Departure(it) },
					Stop(response.stop),
					response.alerts.map { Alert(it) })

				is DeparturesResponseV2 -> 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 TrafficResponseException(result.error.statusCode, response.message, result.error)
			} else {
				throw TrafficResponseException(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 VehicleV3 -> Vehicle(it)
						else -> throw UnknownResourceException("locatables", it::class)
					}
				}

				is LocatablesResponseV3 -> response.locatables.map {
					when (it) {
						is StopV2 -> Stop(it)
						is VehicleV3 -> Vehicle(it)
						else -> throw UnknownResourceException("locatables", it::class)
					}
				}

				is LocatablesResponseV2 -> response.locatables.map {
					when (it) {
						is StopV2 -> Stop(it)
						is VehicleV2 -> Vehicle(it)
						else -> throw UnknownResourceException("locatables", it::class)
					}
				}

				is LocatablesResponseV1 -> response.locatables.map {
					when (it) {
						is StopV1 -> Stop(it)
						is VehicleV1 -> Vehicle(it)
						else -> throw UnknownResourceException("locatables", it::class)
					}
				}

				else -> null
			}
		}
	}

	override suspend fun getLine(
		cm: ConnectivityManager, feedID: String, line: String, context: Context
	): Line? {
		val result = xyz.apiote.bimba.czwek.api.getLine(cm, Server.get(context), feedID, line)
		if (result.error != null) {
			if (result.stream != null) {
				val response = withContext(Dispatchers.IO) { ErrorResponse.unmarshal(result.stream) }
				throw TrafficResponseException(result.error.statusCode, response.message, result.error)
			} else {
				throw TrafficResponseException(result.error.statusCode, "", result.error)
			}
		} else {
			return when (val response =
				withContext(Dispatchers.IO) { LineResponse.unmarshal(result.stream!!) }) {
				is LineResponseDev -> Line(response.line)
				is LineResponseV1 -> Line(response.line)
				is LineResponseV2 -> Line(response.line)
				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 -> throw RuntimeException("Unknown query type $type")
		}
		if (result.error != null) {
			if (result.stream != null) {
				val response = withContext(Dispatchers.IO) { ErrorResponse.unmarshal(result.stream) }
				throw TrafficResponseException(result.error.statusCode, response.message, result.error)
			} else {
				throw TrafficResponseException(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)
						is LineV2 -> Line(it)
						else -> throw UnknownResourceException("queryablesV2", it::class)
					}
				}

				is QueryablesResponseV1 -> response.queryables.map {
					when (it) {
						is StopV1 -> Stop(it)
						else -> throw UnknownResourceException("queryablesV1", it::class)
					}
				}

				is QueryablesResponseV2 -> response.queryables.map {
					when (it) {
						is StopV2 -> Stop(it)
						is LineV1 -> Line(it)
						else -> throw UnknownResourceException("queryablesV2", it::class)
					}
				}

				is QueryablesResponseV3 -> response.queryables.map {
					when (it) {
						is StopV2 -> Stop(it)
						is LineV2 -> Line(it)
						else -> throw UnknownResourceException("queryablesV2", it::class)
					}
				}

				else -> null
			}
		}
	}
}