Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion driver-app/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ android {

buildTypes {
debug {
buildConfigField("String", "BASE_URL", "\"https://prima-staging.motis-project.de\"")
//buildConfigField("String", "BASE_URL", "\"https://prima-staging.motis-project.de\"")
buildConfigField("String", "BASE_URL", "\"http://82.165.178.73:7777\"")
}
release {
isMinifyEnabled = false
Expand Down
81 changes: 67 additions & 14 deletions driver-app/app/src/main/java/de/motis/prima/data/DataRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import android.content.res.Configuration
import android.util.Log
import com.google.firebase.messaging.FirebaseMessaging
import de.motis.prima.services.ApiService
import de.motis.prima.services.Leg
import de.motis.prima.services.Tour
import de.motis.prima.services.Vehicle
import de.motis.prima.ui.TimeBlock
import io.realm.kotlin.query.RealmResults
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -63,6 +63,9 @@ class DataRepository @Inject constructor(
private val _networkError = MutableStateFlow(false)
val networkError = _networkError.asStateFlow()

private val _updateError = MutableStateFlow(false)
val updateError = _updateError.asStateFlow()

val selectedVehicle: Flow<Vehicle> = dataStoreManager.selectedVehicleFlow
private var _vehicleId = 0

Expand All @@ -71,6 +74,16 @@ class DataRepository @Inject constructor(
private val _markedTour = MutableStateFlow(-1)
val markedTour: StateFlow<Int> = _markedTour.asStateFlow()

private val _ptLegs = MutableStateFlow<HashMap<Int, Leg>>(hashMapOf())
val ptLegs: StateFlow<HashMap<Int, Leg>> = _ptLegs.asStateFlow()

private val _updateRequestIDs = MutableStateFlow(emptySet<Int>())
private val updateRequestIDs = _updateRequestIDs.asStateFlow()

fun setUpdateIds(ids: Set<Int>) {
_updateRequestIDs.value = ids
}

private val _eventObjectGroups = MutableStateFlow<List<EventObjectGroup>>(emptyList())
val eventObjectGroups: StateFlow<List<EventObjectGroup>> = _eventObjectGroups.asStateFlow()

Expand Down Expand Up @@ -429,19 +442,8 @@ class DataRepository @Inject constructor(
return tourStore.getTour(id)
}

fun hasPendingValidations(tourId: Int): Boolean {
for (id in tourStore.getPickupRequestIDs(tourId)) {
if (_pendingValidationTickets.value.find { e -> e.requestId == id } != null ) {
return true
}
}
return false
}

fun hasInvalidatedTickets(tourId: Int): Boolean {
val pickupEvents = tourStore.getEventsForTour(tourId).filter { e -> e.isPickup }
val invalidated = pickupEvents.filter { e -> e.ticketChecked.not() }
return invalidated.isNotEmpty()
fun getEvent(id: Int): EventObject? {
return tourStore.getEvent(id)
}

fun getTourSpecialInfo(tourId: Int): TourSpecialInfo {
Expand Down Expand Up @@ -472,4 +474,55 @@ class DataRepository @Inject constructor(
fun removeMarker() {
_markedTour.value = -1
}

fun stopPolling() {
_updateRequestIDs.value = emptySet()
_realTimePolling.value = false
}

private val _realTimePolling = MutableStateFlow(false)

private suspend fun updateLegs(requestId: Int): Int {
var error: Int
try {
val res = apiService.getItinerary(requestId)
if (res.isSuccessful) {
val leg = res.body()
_ptLegs.value = HashMap(_ptLegs.value).apply {
put(requestId, leg)
}
return 0
}
error = -1
} catch (e: Exception) {
Log.e("error", "${e.message}")
error = -2
}
_ptLegs.value.remove(requestId) // invalidate previously fetched data
return error
}

fun getItinerary(requestId: Int) {
CoroutineScope(Dispatchers.IO).launch {
updateLegs(requestId)
}
}

private fun updateItinerariesFlow() = flow {
while (_realTimePolling.value) {
for (requestId in updateRequestIDs.value) {
emit(updateLegs(requestId))
}
delay(120000)
}
}.flowOn(Dispatchers.IO)

fun initRealTimePolling() {
_realTimePolling.value = true
CoroutineScope(Dispatchers.IO).launch {
updateItinerariesFlow().collect { e ->
_updateError.value = e == -2
}
}
}
}
4 changes: 4 additions & 0 deletions driver-app/app/src/main/java/de/motis/prima/data/TourStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ class TourStore @Inject constructor(
return realm.query<TourObject>("tourId == $0", id).first().find()
}

fun getEvent(id: Int): EventObject? {
return realm.query<EventObject>("id == $0", id).first().find()
}

fun getPickupRequestIDs(tourId: Int): Set<Int> {
val res: MutableSet<Int> = mutableSetOf()
val pickupEvents = getEventsForTour(tourId).filter { e -> e.isPickup }
Expand Down
82 changes: 82 additions & 0 deletions driver-app/app/src/main/java/de/motis/prima/services/Api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ interface ApiService {
@Query("from") from: String,
@Query("to") to: String
): Response<Void>

@GET("api/driver/journey")
suspend fun getItinerary(
@Query("requestId") requestId: Int
): Response<Leg?>
}

data class AvailabilityRequest(
Expand Down Expand Up @@ -130,3 +135,80 @@ data class Tour(
val licensePlate: String,
val events: List<Event>
)

data class Itinerary(
val duration: Long,
val startTime: String,
val endTime: String,
val transfers: Int,
val legs: List<Leg>,
//val fareTransfers: List<FareTransfer>
)

data class Leg(
val mode: String,
val from: Place,
val to: Place,
val duration: Long,
val startTime: String,
val endTime: String,
var scheduledStartTime: String?,
val scheduledEndTime: String?,
val realTime: Boolean,
val scheduled: Boolean,
//val distance: Double,
//val interlineWithPreviousLeg: Boolean,
val headsign: String?,
val tripTo: Place?,
//val routeId: String?,
//val directionId: String?,
val routeColor: String?,
val routeTextColor: String?,
//val routeType: Int?,
//val agencyName: String?,
//val agencyUrl: String?,
//val agencyId: String?,
val tripId: String?,
val routeShortName: String?,
val routeLongName: String?,
val tripShortName: String?,
val displayName: String?,
val cancelled: Boolean,
//val source: String?,
val intermediateStops: List<Place>,
//val legGeometry: Polyline?,
//val steps: List<Step>,
//val rental: Rental?,
//val fareTransferIndex: Int?,
//val effectiveFareLegIndex: Int?,
//val alerts: List<Alert>,
//val loopedCalendarSince: String?
)

data class Place(
val name: String?,
val stopId: String?,
val parentId: String?,
val importance: Double?,
val lat: Double,
val lon: Double,
val level: Int?,
val tz: String?,
val arrival: String?,
val departure: String?,
val scheduledArrival: String?,
val scheduledDeparture: String?,
val scheduledTrack: String?,
val track: String?,
val description: String?,
//val vertexType: VertexType?,
//val pickupType: PickupDropoffType?,
//val dropoffType: PickupDropoffType?,
val cancelled: Boolean,
//val alerts: List<Alert>,
//val flex: String?,
//val flexId: String?,
//val flexStartPickupDropOffWindow: String?,
//val flexEndPickupDropOffWindow: String?,
//val modes: List<Mode>
)
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ class AvailabilityViewModel @Inject constructor(
}

val intervals = mergeModifications(fetchDate)
Log.d("merge", "merged: $intervals") //TODO: fix merging
val from = mutableListOf<Long>()
val to = mutableListOf<Long>()
val add = mutableListOf<Boolean>()
Expand Down
2 changes: 0 additions & 2 deletions driver-app/app/src/main/java/de/motis/prima/ui/DayTimeline.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import android.util.Log
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectDragGestures
Expand Down Expand Up @@ -41,7 +40,6 @@ import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import de.motis.prima.R
import de.motis.prima.ui.AvailabilityViewModel
import java.time.LocalDate

@Composable
fun DayTimeline(
Expand Down
Loading
Loading