Skip to content

Commit

Permalink
Expose method for getting beacons in a region
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Jan 19, 2024
1 parent 304d190 commit d12dc6e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<BeaconEntity>

/**
* 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<BeaconEntity>
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<BeaconEntity> {
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -89,6 +90,10 @@ class BeaconService(context: Context) : IBeaconService {
}
}

override suspend fun getBeaconsInRegion(region: CoordinateBounds): List<Beacon> {
return repo.getBeaconsInRegion(region).map { it.toBeacon() }
}

override suspend fun delete(group: BeaconGroup) {
repo.deleteBeaconGroup(BeaconGroupEntity.from(group))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -20,4 +21,6 @@ interface IBeaconRepo {
suspend fun getGroupsWithParent(parent: Long?): List<BeaconGroupEntity>
suspend fun getGroup(id: Long): BeaconGroupEntity?

suspend fun getBeaconsInRegion(region: CoordinateBounds): List<BeaconEntity>

}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -30,6 +31,8 @@ interface IBeaconService {
groupFilter: Long?
): List<IBeacon>

suspend fun getBeaconsInRegion(region: CoordinateBounds): List<Beacon>

// Delete
suspend fun delete(group: BeaconGroup)
suspend fun delete(beacon: Beacon)
Expand Down

0 comments on commit d12dc6e

Please sign in to comment.