revert GTFSUtils.kt from 67fea519

This commit is contained in:
PoliEcho 2026-02-13 14:37:17 +01:00
parent 5970dce0d2
commit c3f8dd4066

View File

@ -13,7 +13,6 @@ import androidx.compose.ui.unit.dp
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
import ovh.plrapps.mapcompose.api.addClusterer
import ovh.plrapps.mapcompose.api.addMarker import ovh.plrapps.mapcompose.api.addMarker
import ovh.plrapps.mapcompose.api.removeMarker import ovh.plrapps.mapcompose.api.removeMarker
import ovh.plrapps.mapcompose.ui.state.markers.model.RenderingStrategy import ovh.plrapps.mapcompose.ui.state.markers.model.RenderingStrategy
@ -82,13 +81,12 @@ val gtfshttpClient = OkHttpClient.Builder()
private val GTFSMainHandler = Handler(Looper.getMainLooper()) private val GTFSMainHandler = Handler(Looper.getMainLooper())
private var GTFSTrackingThread: Thread? = null private var GTFSTrackingThread: Thread? = null
private var GTFSIsTracking = false private var GTFSIsTracking = false
private var GTFSIsInitialized = false // Prevent duplicate initialization
private fun fetchGTFSPosition(gtfsUrl: URL,api_key: String, callee: Activity) { private fun fetchGTFSPosition(gtfsUrl: URL,api_key: String, callee: Activity) {
try { try {
val request = Request.Builder() val request = Request.Builder()
.url("${gtfsUrl.protocol}://${gtfsUrl.host}${"/"}.pd") .url("${gtfsUrl.protocol}://${gtfsUrl.host}${"/"}")
.header("User-Agent", "${BuildConfig.APPLICATION_ID}/${BuildConfig.VERSION_NAME} (Android)") .header("User-Agent", "${BuildConfig.APPLICATION_ID}/${BuildConfig.VERSION_NAME} (Android)")
.build() .build()
@ -118,39 +116,39 @@ fun startGTFSTracking(url: String, api_key: String, callee: Activity) {
GTFSTrackingThread = Thread { GTFSTrackingThread = Thread {
val gtfsUrl = URL(url); val gtfsUrl = URL(url);
try { try {
if(gtfsRoutes.isEmpty() && !GTFSIsInitialized) { if(gtfsRoutes.isEmpty()) {
GTFSMainHandler.post { GTFSMainHandler.post {
Toast.makeText(callee, "Fetching routes...", Toast.LENGTH_SHORT).show()} Toast.makeText(callee, "Fetching routes...", Toast.LENGTH_SHORT).show()}
var messageParsed: GTFSRoutes; var messageParsed: GTFSRoutes;
var offset = 0; var offset = 0;
do { do {
val request = Request.Builder() val request = Request.Builder()
.url("${gtfsUrl.protocol}://${gtfsUrl.host}${callee.getString(R.string.routes_path)}?offset=${offset}") .url("${gtfsUrl.protocol}://${gtfsUrl.host}${callee.getString(R.string.routes_path)}?offset=${offset}")
.header( .header(
"User-Agent", "User-Agent",
"${BuildConfig.APPLICATION_ID}/${BuildConfig.VERSION_NAME} (Android)" "${BuildConfig.APPLICATION_ID}/${BuildConfig.VERSION_NAME} (Android)"
) )
.header("X-Access-Token", api_key).build(); .header("X-Access-Token", api_key).build();
gtfshttpClient.newCall(request).execute().use { response -> gtfshttpClient.newCall(request).execute().use { response ->
var message = if (response.isSuccessful) { var message = if (response.isSuccessful) {
response.body?.string() ?: "Empty response" response.body?.string() ?: "Empty response"
} else { } else {
"Error: ${response.code} ${response.message}" "Error: ${response.code} ${response.message}"
} }
messageParsed = json.decodeFromString<GTFSRoutes>(message) messageParsed = json.decodeFromString<GTFSRoutes>(message)
gtfsRoutes.addAll(messageParsed) gtfsRoutes.addAll(messageParsed)
offset += messageParsed.size offset += messageParsed.size
}} while (messageParsed.isNotEmpty()) }} while (messageParsed.isNotEmpty())
} }
if(gtfsStops.features.isEmpty()) { if(gtfsStops.features.isEmpty()) {
GTFSMainHandler.post { GTFSMainHandler.post {
Toast.makeText(callee, "Fetching stops...", Toast.LENGTH_SHORT).show()} Toast.makeText(callee, "Fetching stops...", Toast.LENGTH_SHORT).show()}
var messageParsed: GTFSStops; var messageParsed: GTFSStops;
var offset = 0; var offset = 0;
do { do {
val request = Request.Builder() val request = Request.Builder()
.url("${gtfsUrl.protocol}://${gtfsUrl.host}${callee.getString(R.string.stops_path)}?offset=${offset}") .url("${gtfsUrl.protocol}://${gtfsUrl.host}${callee.getString(R.string.stops_path)}?offset=${offset}")
.header( .header(
@ -170,58 +168,32 @@ fun startGTFSTracking(url: String, api_key: String, callee: Activity) {
} }
offset += messageParsed.features.size; offset += messageParsed.features.size;
}while(messageParsed.features.isNotEmpty()) }while(messageParsed.features.isNotEmpty())
} }
// put all stops on the map in batches to prevent ANR // put all stops on the map
GTFSMainHandler.post { GTFSMainHandler.post {
Toast.makeText(callee, "Drawing stops...", Toast.LENGTH_SHORT).show()} Toast.makeText(callee, "Drawing stops...", Toast.LENGTH_SHORT).show()}
for (stop in gtfsStops!!.features) {
val stopsToAdd = gtfsStops!!.features.filter { it.properties.parent_station == null } if (stop.properties.parent_station == null) {
val batchSize = 50 // Process 50 markers at a time mapState.addMarker(stop.properties.stop_id, x = longitudeToXNormalized(stop.geometry.coordinates[0]), y = latitudeToYNormalized(stop.geometry.coordinates[1]), renderingStrategy = RenderingStrategy.LazyLoading("default")) {
val totalStops = stopsToAdd.size Icon(
var processed = 0 painter = painterResource(id = R.drawable.outline_directions_bus_24),
contentDescription = null,
for (batch in stopsToAdd.chunked(batchSize)) { modifier = Modifier.size(12.dp),
if (!GTFSIsTracking) break // Stop if tracking was cancelled tint = Color(0xFF0000FF)
)
GTFSMainHandler.post { }}
for (stop in batch) {
try {
mapState.addMarker(
stop.properties.stop_id,
x = longitudeToXNormalized(stop.geometry.coordinates[0]),
y = latitudeToYNormalized(stop.geometry.coordinates[1]),
renderingStrategy = RenderingStrategy.LazyLoading("default")
) {
Icon(
painter = painterResource(id = R.drawable.outline_directions_bus_24),
contentDescription = null,
modifier = Modifier.size(12.dp),
tint = Color(0xFF0000FF)
)
}
} catch (e: Exception) {
// Skip markers that fail to add
}
}
}
processed += batch.size
// Small delay between batches to let UI breathe
Thread.sleep(50)
} }
GTFSMainHandler.post { GTFSMainHandler.post {
Toast.makeText(callee, "Done drawing ${totalStops} stops", Toast.LENGTH_SHORT).show()} Toast.makeText(callee, "Done drawing ${gtfsStops.features.size} stops", Toast.LENGTH_SHORT).show()}
GTFSIsInitialized = true // Mark as initialized to prevent duplicate additions } catch(e: Exception) {
} catch(e: Exception) {
GTFSMainHandler.post { GTFSMainHandler.post {
Toast.makeText(callee, e.message ?: "Unknown error", Toast.LENGTH_SHORT).show() Toast.makeText(callee, e.message ?: "Unknown error", Toast.LENGTH_SHORT).show()
} }
GTFSMainHandler.post { stopGTFSTracking() } GTFSMainHandler.post { stopGTFSTracking() }
return@Thread return@Thread
} }
while (GTFSIsTracking) { while (GTFSIsTracking) {
fetchGTFSPosition(gtfsUrl,api_key, callee) fetchGTFSPosition(gtfsUrl,api_key, callee)
@ -236,7 +208,6 @@ fun startGTFSTracking(url: String, api_key: String, callee: Activity) {
fun stopGTFSTracking() { fun stopGTFSTracking() {
GTFSIsTracking = false GTFSIsTracking = false
GTFSIsInitialized = false
for (stop in gtfsStops!!.features) { for (stop in gtfsStops!!.features) {
if (stop.properties.parent_station == null) { if (stop.properties.parent_station == null) {
mapState.removeMarker(stop.properties.stop_id); mapState.removeMarker(stop.properties.stop_id);