Bimba.git

commit 3ee6d240b0cbb515f01ab0c00b735a66c2eee043

Author: Adam <git@apiote.xyz>

api: add ID in Departure and change position to lat,lon

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


diff --git a/app/src/main/java/ml/adamsprogs/bimba/api/Api.kt b/app/src/main/java/ml/adamsprogs/bimba/api/Api.kt
index 4036f5b7617d2f894b516e53bb1026a8b256f92d..019ce0dc48a04d71befc67fc281b27a0be5db554 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/api/Api.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/api/Api.kt
@@ -51,12 +51,12 @@ 	}
 	return request(server, "items", params, cm)
 }
 
-suspend fun locateItems(cm: ConnectivityManager, server: Server, plusCode: String): Result {
-	return request(server, "items", mapOf("near" to plusCode), cm)
+suspend fun locateItems(cm: ConnectivityManager, server: Server, near: Position): Result {
+	return request(server, "items", mapOf("near" to near.toString()), cm)
 }
 
-suspend fun getLocatablesIn(cm: ConnectivityManager, server: Server, bl: String, tr: String): Result {
-	return request(server, "locatables", mapOf("lb" to bl, "rt" to tr), cm)
+suspend fun getLocatablesIn(cm: ConnectivityManager, server: Server, bl: Position, tr: Position): Result {
+	return request(server, "locatables", mapOf("lb" to bl.toString(), "rt" to tr.toString()), cm)
 }
 
 suspend fun getDepartures(




diff --git a/app/src/main/java/ml/adamsprogs/bimba/api/Structs.kt b/app/src/main/java/ml/adamsprogs/bimba/api/Structs.kt
index 2f6d8ce34d7a26be330aaaae1a8ac95c965b65b0..7bf00e8e16c98a6999c0950d44aefc999d561e58 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/api/Structs.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/api/Structs.kt
@@ -17,6 +17,22 @@ import java.util.zip.Adler32
 import kotlin.math.abs
 import kotlin.math.pow
 
+data class Position(
+	val latitude: Double,
+	val longitude: Double
+) {
+	override fun toString(): String = "$latitude,$longitude"
+	companion object {
+		fun unmarshal(stream: InputStream): Position {
+			val reader = Reader(stream)
+			return Position(
+				reader.readFloat64(),
+				reader.readFloat64()
+			)
+		}
+	}
+}
+
 data class DateTime(
 	val year: Int,
 	val month: Int,
@@ -139,7 +155,7 @@ }
 
 data class Vehicle(
 	val ID: String,
-	val Position: String,
+	val Position: Position,
 	val Capabilities: UShort,
 	val Speed: Float,
 	val Line: LineStub,
@@ -165,14 +181,14 @@ 	override fun icon(context: Context): Drawable {
 		return BitmapDrawable(context.resources, Line.icon(context, 1f))
 	}
 
-	override fun location(): String = Position
+	override fun location(): Position = Position
 
 	companion object {
 		fun unmarshal(stream: InputStream): Vehicle {
 			val reader = Reader(stream)
 			return Vehicle(
 				reader.readString(),
-				reader.readString(),
+				Position.unmarshal(stream),
 				reader.readU16(),
 				reader.readFloat32(),
 				LineStub.unmarshal(stream),
@@ -209,6 +225,7 @@ 	}
 }
 
 data class Departure(
+	val ID: String,
 	val line: LineStub,
 	val headsign: String,
 	val time: Time,
@@ -221,6 +238,7 @@ ) {
 	companion object {
 		fun unmarshal(stream: InputStream): Departure {
 			val reader = Reader(stream)
+			val id = reader.readString()
 			val line = LineStub.unmarshal(stream)
 			val headsign = reader.readString()
 			val time = Time.unmarshal(stream)
@@ -229,7 +247,7 @@ 			val isRealtime = reader.readBoolean()
 			val stopOrder = reader.readString()
 			val vehicle = Vehicle.unmarshal(stream)
 			val boarding = reader.readU8()
-			return Departure(line, headsign, time, status, isRealtime, stopOrder, vehicle, boarding)
+			return Departure(id, line, headsign, time, status, isRealtime, stopOrder, vehicle, boarding)
 		}
 	}
 }
@@ -237,14 +255,14 @@
 interface Item
 interface Locatable {
 	fun icon(context: Context): Drawable
-	fun location(): String
+	fun location(): Position
 	fun id(): String
 }
 
 data class Stop(
 	val code: String,
 	val zone: String,
-	val position: String,
+	val position: Position,
 	val changeOptions: List<ChangeOption>,
 	val name: String
 ) : Item, Locatable {
@@ -271,7 +289,7 @@ 	}
 
 	override fun id(): String = code
 
-	override fun location(): String = position
+	override fun location(): Position = position
 
 	override fun toString(): String {
 		var result = "$name ($code) [$zone] $position\n"
@@ -291,7 +309,7 @@ 		fun unmarshal(stream: InputStream): Stop {
 			val reader = Reader(stream)
 			val code = reader.readString()
 			val zone = reader.readString()
-			val position = reader.readString()
+			val position = Position.unmarshal(stream)
 			val chOptionsNum = reader.readUInt()
 			val changeOptions = mutableListOf<ChangeOption>()
 			for (i in 0UL until chOptionsNum) {




diff --git a/app/src/main/java/ml/adamsprogs/bimba/dashboard/ui/map/MapFragment.kt b/app/src/main/java/ml/adamsprogs/bimba/dashboard/ui/map/MapFragment.kt
index da970dc91871e1157c601ebcfb4d832cafe01c26..7b5fac52304e9b3dba3df0f0232d35bcba485d27 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/dashboard/ui/map/MapFragment.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/dashboard/ui/map/MapFragment.kt
@@ -19,8 +19,8 @@ import androidx.core.graphics.drawable.toBitmap
 import androidx.fragment.app.Fragment
 import androidx.lifecycle.ViewModelProvider
 import com.google.android.material.snackbar.Snackbar
-import com.google.openlocationcode.OpenLocationCode
 import ml.adamsprogs.bimba.R
+import ml.adamsprogs.bimba.api.Position
 import ml.adamsprogs.bimba.api.Server
 import ml.adamsprogs.bimba.dashboard.MainActivity
 import ml.adamsprogs.bimba.databinding.FragmentMapBinding
@@ -150,9 +150,7 @@ 			}
 
 			it.forEach { locatable ->
 				val marker = Marker(binding.map)
-				marker.position = OpenLocationCode.decode(locatable.location()).let { area ->
-					GeoPoint(area.centerLatitude, area.centerLongitude)
-				}
+				marker.position = GeoPoint(locatable.location().latitude, locatable.location().longitude)
 				marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_CENTER)
 				marker.icon = context?.let { ctx -> locatable.icon(ctx) }
 
@@ -191,8 +189,8 @@ 	private fun getLocatables() {
 		maybeBinding?.let { binding ->
 			val (bl, tr) = binding.map.boundingBox.let {
 				Pair(
-					OpenLocationCode.encode(it.latSouth, it.lonWest),
-					OpenLocationCode.encode(it.latNorth, it.lonEast)
+					Position(it.latSouth, it.lonWest),
+					Position(it.latNorth, it.lonEast)
 				)
 			}
 			context?.let {




diff --git a/app/src/main/java/ml/adamsprogs/bimba/dashboard/ui/map/MapViewModel.kt b/app/src/main/java/ml/adamsprogs/bimba/dashboard/ui/map/MapViewModel.kt
index 2f520a17e16ebfa05701fd737638632dcb4a0285..def30777abd8ca73bb7e73a16581c5818d124dfb 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/dashboard/ui/map/MapViewModel.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/dashboard/ui/map/MapViewModel.kt
@@ -17,7 +17,6 @@ import androidx.lifecycle.MutableLiveData
 import androidx.lifecycle.ViewModel
 import androidx.lifecycle.viewModelScope
 import com.google.android.material.bottomsheet.BottomSheetDialogFragment
-import com.google.openlocationcode.OpenLocationCode
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.launch
 import kotlinx.coroutines.withContext
@@ -31,7 +30,7 @@
 	private val _items = MutableLiveData<List<Locatable>>()
 	val items: MutableLiveData<List<Locatable>> = _items
 
-	fun getLocatablesIn(cm: ConnectivityManager, server: Server, bl: String, tr: String) {
+	fun getLocatablesIn(cm: ConnectivityManager, server: Server, bl: Position, tr: Position) {
 		viewModelScope.launch {
 			val locatablesResult = ml.adamsprogs.bimba.api.getLocatablesIn(cm, server, bl, tr)
 			val response = if (locatablesResult.stream != null) {
@@ -149,17 +148,15 @@ 						}
 						startActivity(intent)
 					}
 					findViewById<Button>(R.id.navigation_button).setOnClickListener {
-						OpenLocationCode.decode(locatable.position).let { position ->
-							try {
-								startActivity(
-									Intent(
-										Intent.ACTION_VIEW,
-										Uri.parse("geo:${position.centerLatitude},${position.centerLongitude}")
-									)
+						try {
+							startActivity(
+								Intent(
+									Intent.ACTION_VIEW,
+									Uri.parse("geo:${locatable.location().latitude},${locatable.location().longitude}")
 								)
-							} catch (_: ActivityNotFoundException) {
-								Toast.makeText(context, "No maps app installed", Toast.LENGTH_SHORT).show()
-							}
+							)
+						} catch (_: ActivityNotFoundException) {
+							Toast.makeText(context, "No maps app installed", Toast.LENGTH_SHORT).show()
 						}
 					}
 




diff --git a/app/src/main/java/ml/adamsprogs/bimba/search/ResultsActivity.kt b/app/src/main/java/ml/adamsprogs/bimba/search/ResultsActivity.kt
index 623226b31bda88008c0f4050b719a1ad687501d4..3f188ad8b534f373ba15da1e7da6c8eeae58506f 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/search/ResultsActivity.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/search/ResultsActivity.kt
@@ -16,7 +16,6 @@ import androidx.appcompat.app.AppCompatActivity
 import androidx.appcompat.content.res.AppCompatResources
 import androidx.core.view.WindowCompat
 import androidx.recyclerview.widget.LinearLayoutManager
-import com.google.openlocationcode.OpenLocationCode
 import kotlinx.coroutines.*
 import kotlinx.coroutines.Runnable
 import ml.adamsprogs.bimba.R
@@ -94,8 +93,7 @@ 	}
 
 	override fun onLocationChanged(location: Location) {
 		handler.removeCallbacks(runnable)
-		val code = OpenLocationCode.encode(location.latitude, location.longitude)
-		getItemsByLocation(Server.get(this), code)
+		getItemsByLocation(Server.get(this), Position(location.latitude, location.longitude))
 	}
 
 	override fun onResume() {
@@ -139,10 +137,10 @@ 			}
 		}
 	}
 
-	private fun getItemsByLocation(server: Server, plusCode: String) {
+	private fun getItemsByLocation(server: Server, position: Position) {
 		val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
 		MainScope().launch {
-			val itemsResult = locateItems(cm, server, plusCode)
+			val itemsResult = locateItems(cm, server, position)
 			val response = if (itemsResult.stream != null) {
 				unmarshallItemResponse(itemsResult.stream)
 			} else {