Skip to content

Commit

Permalink
Add vehicle position display
Browse files Browse the repository at this point in the history
  • Loading branch information
dellisd committed Apr 26, 2024
1 parent 03546af commit f076fd5
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ca.derekellis.reroute.realtime.RealtimeMessage
import com.soywiz.klock.DateFormat
import com.soywiz.klock.DateTime
import com.soywiz.klock.parse
import io.github.dellisd.spatialk.geojson.FeatureCollection
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.plugins.websocket.receiveDeserialized
Expand Down Expand Up @@ -39,4 +40,8 @@ class RerouteClient(private val client: HttpClient) {
}

suspend fun nextTripsSingle(code: String): RealtimeMessage = client.get("/api/realtime/$code").body()

suspend fun vehicles(): FeatureCollection {
return client.get("/api/realtime/vehicles").body()
}
}
15 changes: 14 additions & 1 deletion web/src/jsMain/kotlin/ca/derekellis/reroute/map/MapPresenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,27 @@ import androidx.compose.runtime.setValue
import app.cash.sqldelight.coroutines.asFlow
import app.cash.sqldelight.coroutines.mapToList
import ca.derekellis.reroute.data.DataSource
import ca.derekellis.reroute.data.RerouteClient
import ca.derekellis.reroute.db.DatabaseHelper
import ca.derekellis.reroute.ui.CollectEffect
import ca.derekellis.reroute.ui.Navigator
import ca.derekellis.reroute.ui.Presenter
import io.github.dellisd.spatialk.geojson.Feature
import io.github.dellisd.spatialk.geojson.FeatureCollection
import io.github.dellisd.spatialk.geojson.dsl.feature
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import me.tatarka.inject.annotations.Assisted
import me.tatarka.inject.annotations.Inject
import kotlin.time.Duration.Companion.seconds
import ca.derekellis.reroute.stops.Stop as StopScreen

@Inject
class MapPresenter(
private val dataSource: DataSource,
private val interactionsManager: MapInteractionsManager,
private val withDatabase: DatabaseHelper,
private val client: RerouteClient,
@Assisted private val navigator: Navigator,
@Assisted private val args: Map,
) : Presenter<MapViewModel, MapViewEvent> {
Expand Down Expand Up @@ -60,7 +65,15 @@ class MapPresenter(

val targetStop by interactionsManager.targetStop.collectAsState(null)

return MapViewModel(targetStop, routeFeatures)
var vehiclePositions by remember { mutableStateOf(FeatureCollection(emptyList())) }
LaunchedEffect(Unit) {
while (true) {
vehiclePositions = client.vehicles()
delay(25.seconds)
}
}

return MapViewModel(targetStop, routeFeatures, vehiclePositions)
}

// TODO: Extract route colours into dataset
Expand Down
44 changes: 43 additions & 1 deletion web/src/jsMain/kotlin/ca/derekellis/reroute/map/MapView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class MapView : View<MapViewModel, MapViewEvent> {
}
}

MapContent(mapState, onEvent = emit, model.routeFeatures)
MapContent(mapState, onEvent = emit, model.routeFeatures, model.vehicles)
}
}

Expand All @@ -54,6 +54,7 @@ private fun MapContent(
mapState: MapboxState,
onEvent: (MapViewEvent) -> Unit,
routeFeatures: Set<io.github.dellisd.spatialk.geojson.Feature>,
vehicles: FeatureCollection,
) {
Div {
ca.derekellis.mapbox.MapboxMap(
Expand Down Expand Up @@ -106,6 +107,47 @@ private fun MapContent(
)
}
}

val vehiclesGeojson = remember(vehicles) {
JSON.parse<GeoJsonObject>(vehicles.json())
}
GeoJsonSource("vehicles", vehiclesGeojson) {
CircleLayer("vehicles-circles") {
circleColor("#212121")
circleRadius(
interpolate(
exponential(2.0),
expression("zoom"),
12 to 3,
15.5 to 10,
),
)
circleOpacity(
interpolate(
exponential(2.0),
expression("zoom"),
12 to 1,
15.5 to 0,
),
)
}
SymbolLayer("vehicles-labels") {
textField(get("id"))
textHaloWidth(50.0)
textColor("#FFFFFF")
textHaloColor("#212121")
textAllowOverlap(true)

textOpacity(
interpolate(
exponential(2.0),
expression("zoom"),
12 to 0,
15.5 to 1,
),
)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package ca.derekellis.reroute.map

import ca.derekellis.reroute.models.Stop
import io.github.dellisd.spatialk.geojson.Feature
import io.github.dellisd.spatialk.geojson.FeatureCollection

data class MapViewModel(
val targetStop: Stop?,
val routeFeatures: Set<Feature>,
val vehicles: FeatureCollection,
)

0 comments on commit f076fd5

Please sign in to comment.