Skip to content

Commit

Permalink
Merge pull request #422 from skedgo/feature/20911-facility-marker-han…
Browse files Browse the repository at this point in the history
…dling

[20911] Handle Facilities from location.json
  • Loading branch information
MichaelReyes authored Apr 15, 2024
2 parents b68bd5a + 3337d9a commit 74e2838
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import com.skedgo.tripkit.data.database.locations.carparks.*
import com.skedgo.tripkit.data.database.locations.carpods.CarPodDao
import com.skedgo.tripkit.data.database.locations.carpods.CarPodEntity
import com.skedgo.tripkit.data.database.locations.carpods.CarPodVehicle
import com.skedgo.tripkit.data.database.locations.facility.FacilityDao
import com.skedgo.tripkit.data.database.locations.facility.FacilityLocationEntity
import com.skedgo.tripkit.data.database.locations.freefloating.FreeFloatingLocationDao
import com.skedgo.tripkit.data.database.locations.freefloating.FreeFloatingLocationEntity
import com.skedgo.tripkit.data.database.locations.onstreetparking.OnStreetParkingDao
Expand Down Expand Up @@ -39,11 +41,13 @@ import com.skedgo.tripkit.data.database.timetables.ServiceAlertsEntity
ScheduledServiceRealtimeInfoEntity::class,
ParentStopEntity::class,
ServiceAlertsEntity::class,
TicketEntity::class], version = 6)
TicketEntity::class,
FacilityLocationEntity::class,], version = 7)
abstract class TripKitDatabase : RoomDatabase() {
abstract fun carParkDao(): CarParkDao
abstract fun carPodDao(): CarPodDao
abstract fun bikePodDao(): BikePodDao
abstract fun facilityDao(): FacilityDao
abstract fun freeFloatingLocationDao(): FreeFloatingLocationDao
abstract fun stopLocationDao(): StopLocationDao
abstract fun scheduledServiceRealtimeInfoDao(): ScheduledServiceRealtimeInfoDao
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.skedgo.tripkit.data.database.locations.facility

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import io.reactivex.Flowable

@Dao
abstract class FacilityDao {
@Query("SELECT * from facilities WHERE cellId IN (:cellIds)")
abstract fun getAllInCells(cellIds: List<String>): Flowable<List<FacilityLocationEntity>>

@Query("SELECT * from facilities WHERE cellId IN (:cellIds) AND :southWestLat < lat AND lat < :northEastLat AND :southWestLng < lng AND lng < :northEastLng")
abstract fun getAllByCellsAndLatLngBounds(
cellIds: List<String>,
southWestLat: Double,
southWestLng: Double,
northEastLat: Double,
northEastLng: Double): Flowable<List<FacilityLocationEntity>>

@Insert(onConflict = OnConflictStrategy.REPLACE)
abstract fun saveAll(facilities: List<FacilityLocationEntity>)

@Query("SELECT * from facilities WHERE identifier == :id")
abstract fun getById(id: String): FacilityLocationEntity
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.skedgo.tripkit.data.database.locations.facility


import androidx.annotation.Keep
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "facilities")
@Keep
class FacilityLocationEntity {
@PrimaryKey
var identifier: String = ""
var cellId: String? = null
var lat: Double = 0.0
var lng: Double = 0.0
var address: String? = null
var timezone: String? = null
var city: String? = null
var region: String? = null
var name: String? = null
var facilityType: String? = null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.skedgo.tripkit.data.database.locations.facility

import io.reactivex.Completable
import io.reactivex.Observable
import io.reactivex.Single
import com.skedgo.tripkit.location.GeoPoint

interface FacilityRepository {
fun saveFacilities(key: String, facilities: List<FacilityLocationEntity>): Completable
fun getFacilities(cellIds: List<String>): Observable<List<FacilityLocationEntity>>
fun getFacility(id: String): Single<FacilityLocationEntity>
fun getFacilitiesWithinBounds(cellIds: List<String>, southwest: GeoPoint, northEast: GeoPoint): Observable<List<FacilityLocationEntity>>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.skedgo.tripkit.data.database.locations.facility

import com.skedgo.tripkit.data.database.TripKitDatabase
import io.reactivex.Completable
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.schedulers.Schedulers
import com.skedgo.tripkit.location.GeoPoint

class FacilityRepositoryImpl(val tripGoDatabase2: TripKitDatabase) : FacilityRepository {
override fun saveFacilities(
key: String,
facilities: List<FacilityLocationEntity>
): Completable {
return Completable
.fromAction {
facilities.forEach {
it.cellId = key
it.identifier = it.identifier
}
tripGoDatabase2.facilityDao().saveAll(facilities)
}
.subscribeOn(Schedulers.io())
}

override fun getFacilities(cellIds: List<String>): Observable<List<FacilityLocationEntity>> {
return tripGoDatabase2.facilityDao()
.getAllInCells(cellIds)
.subscribeOn(Schedulers.io()).toObservable()
}

override fun getFacilitiesWithinBounds(
cellIds: List<String>,
southwest: GeoPoint,
northEast: GeoPoint
): Observable<List<FacilityLocationEntity>> {
return tripGoDatabase2.facilityDao().getAllInCells(cellIds)
.subscribeOn(Schedulers.io()).toObservable()
}

override fun getFacility(id: String): Single<FacilityLocationEntity> {
return Single
.fromCallable {
tripGoDatabase2.facilityDao().getById(id)
}
.subscribeOn(Schedulers.io())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.skedgo.tripkit.data.database.locations.carpods.CarPodLocation;
import com.skedgo.tripkit.data.database.locations.carrentals.CarRentalCompany;
import com.skedgo.tripkit.data.database.locations.carrentals.CarRentalLocation;
import com.skedgo.tripkit.data.database.locations.facility.FacilityLocationEntity;
import com.skedgo.tripkit.data.database.locations.freefloating.FreeFloatingLocationEntity;
import com.skedgo.tripkit.data.database.locations.onstreetparking.OnStreetParkingLocation;

Expand Down Expand Up @@ -53,6 +54,8 @@ public static class Group {
@SerializedName("carRentals")
List<CarRentalLocation> carRentals;

List<FacilityLocationEntity> facilities;

@SerializedName("hashCode")
private long hashCode;

Expand Down Expand Up @@ -98,5 +101,6 @@ public String getKey() {
public List<CarRentalLocation> getCarRentals() { return carRentals; }
public List<CarParkLocation> getCarParks() { return carParks; }
public List<CarPodLocation> getCarPods() { return carPods; }
public List<FacilityLocationEntity> getFacilities() { return facilities; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import io.reactivex.schedulers.Schedulers
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import org.apache.commons.collections4.CollectionUtils
import com.skedgo.tripkit.agenda.ConfigRepository
import com.skedgo.tripkit.data.database.locations.facility.FacilityRepository
import com.skedgo.tripkit.data.database.locations.freefloating.FreeFloatingRepository
import java.util.*

Expand All @@ -29,7 +30,9 @@ open class StopsFetcher(private val api: LocationsApi,
private val carParkMapper: CarParkMapper,
private val carPodMapper: CarPodMapper,
private val onStreetParkingMapper: OnStreetParkingMapper,
private val carPodRepository: CarPodRepository) {
private val carPodRepository: CarPodRepository,
private val facilityRepository: FacilityRepository,
) {

open fun fetchAsync(cellIds: List<String>,
region: Region,
Expand Down Expand Up @@ -162,6 +165,9 @@ open class StopsFetcher(private val api: LocationsApi,
).plus(
cells.filter { it.carPods != null && it.carPods.isNotEmpty() }
.map { carPodRepository.saveCarPods(carPodMapper.toEntity(it.key, it.carPods)) }
).plus(
cells.filter { it.facilities != null && it.facilities.isNotEmpty() }
.map { facilityRepository.saveFacilities(it.key, it.facilities) }
)
.toList()
.let {
Expand Down

0 comments on commit 74e2838

Please sign in to comment.