Bimba.git

commit 8fd68aae52dcb40b9d44a2cd09064f588a05e55b

Author: Adam Evyčędo <git@apiote.xyz>

show transitous stops on map

%!v(PANIC=String method: strings: negative Repeat count)


diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/api/transitousQueryables.kt b/app/src/main/java/xyz/apiote/bimba/czwek/api/transitousQueryables.kt
index 1397e0c211c2b4109a72c05b554404717cdb9874..71d32872c65e42a1c27a8115ccf99bb3e4cdb819 100644
--- a/app/src/main/java/xyz/apiote/bimba/czwek/api/transitousQueryables.kt
+++ b/app/src/main/java/xyz/apiote/bimba/czwek/api/transitousQueryables.kt
@@ -14,6 +14,8 @@ import xyz.apiote.bimba.czwek.repo.Position
 import xyz.apiote.bimba.czwek.repo.Queryable
 import xyz.apiote.bimba.czwek.repo.Stop
 import xyz.apiote.bimba.czwek.repo.TrafficResponseException
+import xyz.apiote.bimba.czwek.units.DistanceUnit
+import xyz.apiote.bimba.czwek.units.Metre
 
 suspend fun getTransitousQueryables(query: String, context: Context): List<Queryable> {
 	if (!isNetworkAvailable(context)) {
@@ -103,7 +105,7 @@ 		}
 	}
 }
 
-suspend fun locateTransitousQueryables(position: Position, context: Context): List<Queryable> {
+suspend fun locateTransitousQueryables(position: Position, context: Context, radius: DistanceUnit = Metre(500.0)): List<Queryable> {
 	if (!isNetworkAvailable(context)) {
 		throw TrafficResponseException(0, "", Error(0, R.string.error_offline, R.drawable.error_net))
 	}
@@ -116,7 +118,7 @@ 				"target": "/lookup/geo_station"
 			},
 			"content_type": "LookupGeoStationRequest",
 			"content": {
-				"max_radius": 500,
+				"max_radius": ${radius.meters().toInt()},
 				"pos": {
 					"lat": ${position.latitude},
 					"lng": ${position.longitude}




diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/repo/OnlineRepository.kt b/app/src/main/java/xyz/apiote/bimba/czwek/repo/OnlineRepository.kt
index b7d2e006eaff96892534f2e72bf447aa6cb96d54..89f7f8d5ccc84cde130f7c6a92ded75a16e30170 100644
--- a/app/src/main/java/xyz/apiote/bimba/czwek/repo/OnlineRepository.kt
+++ b/app/src/main/java/xyz/apiote/bimba/czwek/repo/OnlineRepository.kt
@@ -50,6 +50,7 @@ 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 xyz.apiote.bimba.czwek.api.responses.QueryablesResponseV4
+import xyz.apiote.bimba.czwek.units.Metre
 import java.time.LocalDate
 
 // todo [3.2] in Repository check if responses are BARE or HTML
@@ -172,56 +173,81 @@ 		context: Context,
 		bl: Position,
 		tr: Position,
 	): List<Locatable>? {
-		val result = xyz.apiote.bimba.czwek.api.getLocatablesIn(
-			context,
-			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)
+		val transitousQueryables = if (Server.get(context).feeds.transitousEnabled()) {
+			val centre = Position(
+				latitude = (bl.latitude + tr.latitude) / 2,
+				longitude = (bl.longitude + tr.longitude) / 2
+			)
+			locateTransitousQueryables(
+				centre, context, Metre(centre.distanceTo(bl).coerceAtMost(5000f).toDouble())
+			).map {
+				when (it) {
+					is Stop -> it
+					else -> null
+				}
+			}.filterNotNull()
+		} else {
+			null
+		}
+		val bimbaQueryables = if (Server.get(context).feeds.bimbaEnabled()) {
+			val result = xyz.apiote.bimba.czwek.api.getLocatablesIn(
+				context,
+				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 {
-				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 StopV3 -> Stop(it)
-						is VehicleV3 -> Vehicle(it)
-						else -> throw UnknownResourceException("locatables", it::class)
+				when (val response =
+					withContext(Dispatchers.IO) { LocatablesResponse.unmarshal(result.stream!!) }) {
+					is LocatablesResponseDev -> response.locatables.map {
+						when (it) {
+							is StopV3 -> 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 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 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)
+					is LocatablesResponseV1 -> response.locatables.map {
+						when (it) {
+							is StopV1 -> Stop(it)
+							is VehicleV1 -> Vehicle(it)
+							else -> throw UnknownResourceException("locatables", it::class)
+						}
 					}
+
+					else -> null
 				}
-
-				else -> null
 			}
+		} else {
+			null
+		}
+		return if (transitousQueryables == null && bimbaQueryables == null) {
+			null
+		} else {
+			(bimbaQueryables ?: listOf()) + (transitousQueryables ?: listOf())
 		}
 	}