make sure all stops are requested
This commit is contained in:
parent
ced8e8173d
commit
93148fcf36
@ -10,13 +10,11 @@ import androidx.compose.ui.Modifier
|
|||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.res.painterResource
|
import androidx.compose.ui.res.painterResource
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.compose.ui.unit.sp
|
|
||||||
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.addLazyLoader
|
|
||||||
import ovh.plrapps.mapcompose.api.addMarker
|
import ovh.plrapps.mapcompose.api.addMarker
|
||||||
|
import ovh.plrapps.mapcompose.api.removeMarker
|
||||||
import ovh.plrapps.mapcompose.ui.state.markers.model.RenderingStrategy
|
import ovh.plrapps.mapcompose.ui.state.markers.model.RenderingStrategy
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
|
|
||||||
@ -37,9 +35,10 @@ data class GTFSRoute(
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Type alias for array of GTFS routes
|
// Type alias for array of GTFS routes
|
||||||
typealias GTFSRoutes = List<GTFSRoute>
|
typealias GTFSRoutes = MutableList<GTFSRoute>
|
||||||
|
|
||||||
|
var gtfsRoutes: GTFSRoutes = mutableListOf()
|
||||||
|
|
||||||
var gtfsRoutes: GTFSRoutes? = null
|
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data class GTFSStopGeometry(
|
data class GTFSStopGeometry(
|
||||||
@ -68,11 +67,11 @@ data class GTFSStop(
|
|||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data class GTFSStops(
|
data class GTFSStops(
|
||||||
val features: List<GTFSStop>,
|
val features: MutableList<GTFSStop>,
|
||||||
val type: String?
|
val type: String?
|
||||||
)
|
)
|
||||||
|
|
||||||
var gtfsStops: GTFSStops? = null
|
var gtfsStops: GTFSStops = GTFSStops(mutableListOf<GTFSStop>(), "FeatureCollection")
|
||||||
|
|
||||||
|
|
||||||
// OkHttpClient for making HTTP requests
|
// OkHttpClient for making HTTP requests
|
||||||
@ -108,19 +107,24 @@ private fun fetchGTFSPosition(gtfsUrl: URL,api_key: String, callee: Activity) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Start the timer with a URL:
|
// Start the timer with a URL:
|
||||||
fun startGTFSTracking(url: String, api_key: String, callee: Activity) {
|
fun startGTFSTracking(url: String, api_key: String, callee: Activity) {
|
||||||
if (GTFSIsTracking) return
|
if (GTFSIsTracking) return
|
||||||
|
|
||||||
GTFSIsTracking = true
|
GTFSIsTracking = true
|
||||||
GTFSTrackingThread = Thread {
|
GTFSTrackingThread = Thread {
|
||||||
var gtfsUrl = URL(url);
|
val gtfsUrl = URL(url);
|
||||||
try {
|
try {
|
||||||
if(gtfsRoutes == null) {
|
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 offset = 0;
|
||||||
|
do {
|
||||||
val request = Request.Builder()
|
val request = Request.Builder()
|
||||||
.url("${gtfsUrl.protocol}://${gtfsUrl.host}${callee.getString(R.string.routes_path)}")
|
.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)"
|
||||||
@ -133,13 +137,20 @@ fun startGTFSTracking(url: String, api_key: String, callee: Activity) {
|
|||||||
} else {
|
} else {
|
||||||
"Error: ${response.code} ${response.message}"
|
"Error: ${response.code} ${response.message}"
|
||||||
}
|
}
|
||||||
gtfsRoutes = json.decodeFromString<GTFSRoutes>(message)
|
messageParsed = json.decodeFromString<GTFSRoutes>(message)
|
||||||
}}
|
gtfsRoutes.addAll(messageParsed)
|
||||||
if(gtfsStops == null) {
|
offset += messageParsed.size
|
||||||
|
}} while (messageParsed.isNotEmpty())
|
||||||
|
}
|
||||||
|
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 offset = 0;
|
||||||
|
do {
|
||||||
val request = Request.Builder()
|
val request = Request.Builder()
|
||||||
.url("${gtfsUrl.protocol}://${gtfsUrl.host}${callee.getString(R.string.stops_path)}")
|
.url("${gtfsUrl.protocol}://${gtfsUrl.host}${callee.getString(R.string.stops_path)}?offset=${offset}")
|
||||||
.header(
|
.header(
|
||||||
"User-Agent",
|
"User-Agent",
|
||||||
"${BuildConfig.APPLICATION_ID}/${BuildConfig.VERSION_NAME} (Android)"
|
"${BuildConfig.APPLICATION_ID}/${BuildConfig.VERSION_NAME} (Android)"
|
||||||
@ -152,8 +163,13 @@ fun startGTFSTracking(url: String, api_key: String, callee: Activity) {
|
|||||||
} else {
|
} else {
|
||||||
"Error: ${response.code} ${response.message}"
|
"Error: ${response.code} ${response.message}"
|
||||||
}
|
}
|
||||||
gtfsStops = json.decodeFromString<GTFSStops>(message)
|
messageParsed = json.decodeFromString<GTFSStops>(message)
|
||||||
|
gtfsStops.features.addAll(messageParsed.features)
|
||||||
|
}
|
||||||
|
|
||||||
|
offset += messageParsed.features.size;
|
||||||
|
}while(messageParsed.features.isNotEmpty())
|
||||||
|
}
|
||||||
// put all stops on the map
|
// 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()}
|
||||||
@ -168,13 +184,10 @@ fun startGTFSTracking(url: String, api_key: String, callee: Activity) {
|
|||||||
tint = Color(0xFF0000FF)
|
tint = Color(0xFF0000FF)
|
||||||
)
|
)
|
||||||
}}
|
}}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
GTFSMainHandler.post {
|
GTFSMainHandler.post {
|
||||||
Toast.makeText(callee, "Done drawing stops", Toast.LENGTH_SHORT).show()}
|
Toast.makeText(callee, "Done drawing stops", Toast.LENGTH_SHORT).show()}
|
||||||
}}
|
|
||||||
} 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()
|
||||||
@ -195,7 +208,11 @@ fun startGTFSTracking(url: String, api_key: String, callee: Activity) {
|
|||||||
|
|
||||||
fun stopGTFSTracking() {
|
fun stopGTFSTracking() {
|
||||||
GTFSIsTracking = false
|
GTFSIsTracking = false
|
||||||
|
for (stop in gtfsStops!!.features) {
|
||||||
|
if (stop.properties.parent_station == null) {
|
||||||
|
mapState.removeMarker(stop.properties.stop_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
GTFSTrackingThread?.interrupt()
|
GTFSTrackingThread?.interrupt()
|
||||||
GTFSTrackingThread = null
|
GTFSTrackingThread = null
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user