diff --git a/TripKitData/src/main/java/com/skedgo/tripkit/data/database/TripKitDatabase.kt b/TripKitData/src/main/java/com/skedgo/tripkit/data/database/TripKitDatabase.kt index 530930de..19d55c66 100644 --- a/TripKitData/src/main/java/com/skedgo/tripkit/data/database/TripKitDatabase.kt +++ b/TripKitData/src/main/java/com/skedgo/tripkit/data/database/TripKitDatabase.kt @@ -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 @@ -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 diff --git a/TripKitData/src/main/java/com/skedgo/tripkit/data/database/locations/facility/FacilityDao.kt b/TripKitData/src/main/java/com/skedgo/tripkit/data/database/locations/facility/FacilityDao.kt new file mode 100644 index 00000000..e444e0d7 --- /dev/null +++ b/TripKitData/src/main/java/com/skedgo/tripkit/data/database/locations/facility/FacilityDao.kt @@ -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): Flowable> + + @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, + southWestLat: Double, + southWestLng: Double, + northEastLat: Double, + northEastLng: Double): Flowable> + + @Insert(onConflict = OnConflictStrategy.REPLACE) + abstract fun saveAll(facilities: List) + + @Query("SELECT * from facilities WHERE identifier == :id") + abstract fun getById(id: String): FacilityLocationEntity +} \ No newline at end of file diff --git a/TripKitData/src/main/java/com/skedgo/tripkit/data/database/locations/facility/FacilityLocationEntity.kt b/TripKitData/src/main/java/com/skedgo/tripkit/data/database/locations/facility/FacilityLocationEntity.kt new file mode 100644 index 00000000..6392b0ca --- /dev/null +++ b/TripKitData/src/main/java/com/skedgo/tripkit/data/database/locations/facility/FacilityLocationEntity.kt @@ -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 +} \ No newline at end of file diff --git a/TripKitData/src/main/java/com/skedgo/tripkit/data/database/locations/facility/FacilityRepository.kt b/TripKitData/src/main/java/com/skedgo/tripkit/data/database/locations/facility/FacilityRepository.kt new file mode 100644 index 00000000..dab5c6fd --- /dev/null +++ b/TripKitData/src/main/java/com/skedgo/tripkit/data/database/locations/facility/FacilityRepository.kt @@ -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): Completable + fun getFacilities(cellIds: List): Observable> + fun getFacility(id: String): Single + fun getFacilitiesWithinBounds(cellIds: List, southwest: GeoPoint, northEast: GeoPoint): Observable> +} \ No newline at end of file diff --git a/TripKitData/src/main/java/com/skedgo/tripkit/data/database/locations/facility/FacilityRepositoryImpl.kt b/TripKitData/src/main/java/com/skedgo/tripkit/data/database/locations/facility/FacilityRepositoryImpl.kt new file mode 100644 index 00000000..c3b0bed5 --- /dev/null +++ b/TripKitData/src/main/java/com/skedgo/tripkit/data/database/locations/facility/FacilityRepositoryImpl.kt @@ -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 + ): 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): Observable> { + return tripGoDatabase2.facilityDao() + .getAllInCells(cellIds) + .subscribeOn(Schedulers.io()).toObservable() + } + + override fun getFacilitiesWithinBounds( + cellIds: List, + southwest: GeoPoint, + northEast: GeoPoint + ): Observable> { + return tripGoDatabase2.facilityDao().getAllInCells(cellIds) + .subscribeOn(Schedulers.io()).toObservable() + } + + override fun getFacility(id: String): Single { + return Single + .fromCallable { + tripGoDatabase2.facilityDao().getById(id) + } + .subscribeOn(Schedulers.io()) + } +} \ No newline at end of file diff --git a/TripKitData/src/main/java/com/skedgo/tripkit/data/locations/LocationsResponse.java b/TripKitData/src/main/java/com/skedgo/tripkit/data/locations/LocationsResponse.java index c2c48db1..808c5605 100644 --- a/TripKitData/src/main/java/com/skedgo/tripkit/data/locations/LocationsResponse.java +++ b/TripKitData/src/main/java/com/skedgo/tripkit/data/locations/LocationsResponse.java @@ -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; @@ -53,6 +54,8 @@ public static class Group { @SerializedName("carRentals") List carRentals; + List facilities; + @SerializedName("hashCode") private long hashCode; @@ -98,5 +101,6 @@ public String getKey() { public List getCarRentals() { return carRentals; } public List getCarParks() { return carParks; } public List getCarPods() { return carPods; } + public List getFacilities() { return facilities; } } } \ No newline at end of file diff --git a/TripKitData/src/main/java/com/skedgo/tripkit/data/locations/StopsFetcher.kt b/TripKitData/src/main/java/com/skedgo/tripkit/data/locations/StopsFetcher.kt index 37a16500..99b6c816 100644 --- a/TripKitData/src/main/java/com/skedgo/tripkit/data/locations/StopsFetcher.kt +++ b/TripKitData/src/main/java/com/skedgo/tripkit/data/locations/StopsFetcher.kt @@ -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.* @@ -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, region: Region, @@ -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 {