From d12dc6efc0d4da1946aecfed88863278bb5e7945 Mon Sep 17 00:00:00 2001 From: Kyle Corry Date: Thu, 18 Jan 2024 20:51:58 -0500 Subject: [PATCH] Expose method for getting beacons in a region --- .../infrastructure/persistence/BeaconDao.kt | 22 +++++++++++++++++++ .../infrastructure/persistence/BeaconRepo.kt | 13 +++++++++++ .../persistence/BeaconService.kt | 5 +++++ .../infrastructure/persistence/IBeaconRepo.kt | 3 +++ .../persistence/IBeaconService.kt | 3 +++ 5 files changed, 46 insertions(+) diff --git a/app/src/main/java/com/kylecorry/trail_sense/navigation/beacons/infrastructure/persistence/BeaconDao.kt b/app/src/main/java/com/kylecorry/trail_sense/navigation/beacons/infrastructure/persistence/BeaconDao.kt index 32a3aae31..5663bf088 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/navigation/beacons/infrastructure/persistence/BeaconDao.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/navigation/beacons/infrastructure/persistence/BeaconDao.kt @@ -37,4 +37,26 @@ interface BeaconDao { @Update suspend fun update(beacon: BeaconEntity) + + /** + * Get all beacons in a region. Does not work if the region crosses the 180 meridian, use getAllInRegionNear180Meridian instead. + */ + @Query("SELECT * FROM beacons WHERE `temporary` = 0 AND latitude BETWEEN :south AND :north AND longitude BETWEEN :west AND :east") + suspend fun getAllInRegion( + north: Double, + south: Double, + east: Double, + west: Double + ): List + + /** + * Get all beacons in a region that crosses the 180 meridian + */ + @Query("SELECT * FROM beacons WHERE `temporary` = 0 AND latitude BETWEEN :south AND :north AND (longitude >= :west OR longitude <= :east)") + suspend fun getAllInRegionNear180Meridian( + north: Double, + south: Double, + east: Double, + west: Double + ): List } \ No newline at end of file diff --git a/app/src/main/java/com/kylecorry/trail_sense/navigation/beacons/infrastructure/persistence/BeaconRepo.kt b/app/src/main/java/com/kylecorry/trail_sense/navigation/beacons/infrastructure/persistence/BeaconRepo.kt index fd5c7365d..946d7ffa8 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/navigation/beacons/infrastructure/persistence/BeaconRepo.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/navigation/beacons/infrastructure/persistence/BeaconRepo.kt @@ -1,6 +1,7 @@ package com.kylecorry.trail_sense.navigation.beacons.infrastructure.persistence import android.content.Context +import com.kylecorry.sol.science.geology.CoordinateBounds import com.kylecorry.trail_sense.navigation.beacons.domain.Beacon import com.kylecorry.trail_sense.navigation.beacons.domain.BeaconOwner import com.kylecorry.trail_sense.shared.database.AppDatabase @@ -72,6 +73,18 @@ class BeaconRepo private constructor(context: Context) : IBeaconRepo { override suspend fun getGroup(id: Long): BeaconGroupEntity? = beaconGroupDao.get(id) + override suspend fun getBeaconsInRegion(region: CoordinateBounds): List { + return if (region.east < region.west) { + beaconDao.getAllInRegionNear180Meridian( + region.north, + region.south, + region.east, + region.west + ) + } else { + beaconDao.getAllInRegion(region.north, region.south, region.east, region.west) + } + } companion object { private var instance: BeaconRepo? = null diff --git a/app/src/main/java/com/kylecorry/trail_sense/navigation/beacons/infrastructure/persistence/BeaconService.kt b/app/src/main/java/com/kylecorry/trail_sense/navigation/beacons/infrastructure/persistence/BeaconService.kt index 784532f98..239a55ee5 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/navigation/beacons/infrastructure/persistence/BeaconService.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/navigation/beacons/infrastructure/persistence/BeaconService.kt @@ -1,6 +1,7 @@ package com.kylecorry.trail_sense.navigation.beacons.infrastructure.persistence import android.content.Context +import com.kylecorry.sol.science.geology.CoordinateBounds import com.kylecorry.trail_sense.navigation.beacons.domain.Beacon import com.kylecorry.trail_sense.navigation.beacons.domain.BeaconGroup import com.kylecorry.trail_sense.navigation.beacons.domain.BeaconOwner @@ -89,6 +90,10 @@ class BeaconService(context: Context) : IBeaconService { } } + override suspend fun getBeaconsInRegion(region: CoordinateBounds): List { + return repo.getBeaconsInRegion(region).map { it.toBeacon() } + } + override suspend fun delete(group: BeaconGroup) { repo.deleteBeaconGroup(BeaconGroupEntity.from(group)) } diff --git a/app/src/main/java/com/kylecorry/trail_sense/navigation/beacons/infrastructure/persistence/IBeaconRepo.kt b/app/src/main/java/com/kylecorry/trail_sense/navigation/beacons/infrastructure/persistence/IBeaconRepo.kt index f35ece15c..c46904c61 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/navigation/beacons/infrastructure/persistence/IBeaconRepo.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/navigation/beacons/infrastructure/persistence/IBeaconRepo.kt @@ -1,5 +1,6 @@ package com.kylecorry.trail_sense.navigation.beacons.infrastructure.persistence +import com.kylecorry.sol.science.geology.CoordinateBounds import com.kylecorry.trail_sense.navigation.beacons.domain.Beacon import com.kylecorry.trail_sense.navigation.beacons.domain.BeaconOwner import kotlinx.coroutines.flow.Flow @@ -20,4 +21,6 @@ interface IBeaconRepo { suspend fun getGroupsWithParent(parent: Long?): List suspend fun getGroup(id: Long): BeaconGroupEntity? + suspend fun getBeaconsInRegion(region: CoordinateBounds): List + } \ No newline at end of file diff --git a/app/src/main/java/com/kylecorry/trail_sense/navigation/beacons/infrastructure/persistence/IBeaconService.kt b/app/src/main/java/com/kylecorry/trail_sense/navigation/beacons/infrastructure/persistence/IBeaconService.kt index a69de9da9..239d4fb70 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/navigation/beacons/infrastructure/persistence/IBeaconService.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/navigation/beacons/infrastructure/persistence/IBeaconService.kt @@ -1,5 +1,6 @@ package com.kylecorry.trail_sense.navigation.beacons.infrastructure.persistence +import com.kylecorry.sol.science.geology.CoordinateBounds import com.kylecorry.trail_sense.navigation.beacons.domain.Beacon import com.kylecorry.trail_sense.navigation.beacons.domain.BeaconGroup import com.kylecorry.trail_sense.navigation.beacons.domain.BeaconOwner @@ -30,6 +31,8 @@ interface IBeaconService { groupFilter: Long? ): List + suspend fun getBeaconsInRegion(region: CoordinateBounds): List + // Delete suspend fun delete(group: BeaconGroup) suspend fun delete(beacon: Beacon)