Skip to content

Commit

Permalink
Merge pull request #171 from skedgo/feature/23125-support-real-time-v…
Browse files Browse the repository at this point in the history
…ehicle-location

[23125] Support real time vehicle location indicator
  • Loading branch information
sg-jsonjuliane authored Jan 20, 2025
2 parents 88715e0 + 8485fe8 commit e5633bc
Show file tree
Hide file tree
Showing 10 changed files with 515 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.skedgo.tripkit.ui.map.servicestop

import android.annotation.SuppressLint
import android.content.Context
import com.google.android.gms.maps.model.MarkerOptions
import com.jakewharton.rxrelay2.BehaviorRelay
import com.jakewharton.rxrelay2.PublishRelay
import com.skedgo.tripkit.common.model.realtimealert.RealTimeStatus
import com.skedgo.tripkit.common.model.region.Region
import com.skedgo.tripkit.common.model.stop.ScheduledStop
Expand All @@ -24,8 +26,10 @@ import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.BiFunction
import io.reactivex.rxkotlin.Observables
import io.reactivex.schedulers.Schedulers
import timber.log.Timber
import javax.inject.Inject

@SuppressLint("CheckResult")
class ServiceStopMapViewModel @Inject constructor(
val context: Context,
val fetchAndLoadServices: FetchAndLoadServices,
Expand All @@ -34,9 +38,10 @@ class ServiceStopMapViewModel @Inject constructor(
) : RxViewModel() {

val service = BehaviorRelay.create<TimetableEntry>()

val stop = BehaviorRelay.create<ScheduledStop>()

private val stopRealtimeRelay = PublishRelay.create<Unit>() // To stop real-time updates

private val serviceStop = Observable
.combineLatest(
stop.hide(),
Expand All @@ -49,6 +54,43 @@ class ServiceStopMapViewModel @Inject constructor(
lateinit var realtimeViewModel: RealTimeChoreographerViewModel
lateinit var serviceStopMarkerCreator: ServiceStopMarkerCreator

init {
Observables.combineLatest(
service,
serviceStop.hide().flatMap { regionService.getRegionByLocationAsync(it) }
) { service, region -> service to region }
.distinctUntilChanged()
.observeOn(Schedulers.io())
.switchMap { (service, region) ->
if (service.realTimeStatus in listOf(
RealTimeStatus.IS_REAL_TIME,
RealTimeStatus.CAPABLE
)
) {
realtimeViewModel.getRealTimeVehicles(region, listOf(service))
.takeUntil(stopRealtimeRelay) // Stop when stopRealtimeRelay emits
.doOnNext { vehicles ->
Timber.d("Fetched real-time vehicles: $vehicles")
}
.onErrorResumeNext { throwable: Throwable ->
Timber.e(throwable, "Error fetching real-time vehicles")
Observable.empty() // Emit nothing in case of an error
}
} else {
Timber.d("Service not real-time capable")
Observable.just(service to region)
}
}
.replay(1)
.refCount()
.subscribe()
.autoClear()
}

fun stopRealtimeUpdates() {
stopRealtimeRelay.accept(Unit)
}

private val serviceStopsAndLines =
Observable.combineLatest(
service,
Expand Down Expand Up @@ -138,4 +180,5 @@ class ServiceStopMapViewModel @Inject constructor(
}
return stop
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ class ServiceDetailFragment : BaseTripKitFragment() {
mapContributor.setStop(stop)
mapContributor.setService(timetableEntry)
}

(mapContributor as? TimetableMapContributor)?.let { contributor ->
contributor.formattedElapsedTime.observe(viewLifecycleOwner) { formattedTime ->
// Handle the formatted time in the fragment
handleFormattedElapsedTime(formattedTime)
}
}
}

private fun handleFormattedElapsedTime(formattedTime: String) {
viewModel.apply {
lastUpdatedText.set(formattedTime)
}
}

class Builder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class ServiceDetailViewModel @Inject constructor(

val showOccupancyInfo = ObservableBoolean(false)

val lastUpdatedText = ObservableField<String>()

val itemBinding = ItemBinding.of<ServiceDetailItemViewModel>(
BR.viewModel,
R.layout.service_detail_fragment_list_item
Expand Down
Loading

0 comments on commit e5633bc

Please sign in to comment.