Skip to content

Commit 00f3a23

Browse files
committed
Convert nearby data sources and contract to Kotlin
1 parent 61b8b4e commit 00f3a23

File tree

10 files changed

+206
-424
lines changed

10 files changed

+206
-424
lines changed

app/src/main/java/fr/free/nrw/commons/nearby/MarkerPlaceGroup.java

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package fr.free.nrw.commons.nearby
2+
3+
/**
4+
* Groups a map marker with its corresponding [Place] and bookmark state.
5+
*/
6+
data class MarkerPlaceGroup(
7+
var isBookmarked: Boolean,
8+
var place: Place
9+
)

app/src/main/java/fr/free/nrw/commons/nearby/PlaceDao.java

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package fr.free.nrw.commons.nearby
2+
3+
import androidx.room.Dao
4+
import androidx.room.Insert
5+
import androidx.room.OnConflictStrategy
6+
import androidx.room.Query
7+
import io.reactivex.Completable
8+
9+
/**
10+
* DAO for storing and retrieving [Place] objects, used for Nearby map caching.
11+
*/
12+
@Dao
13+
abstract class PlaceDao {
14+
15+
@Insert(onConflict = OnConflictStrategy.REPLACE)
16+
abstract fun saveSynchronous(place: Place)
17+
18+
@Query("SELECT * from place WHERE entityID=:entity")
19+
abstract fun getPlace(entity: String): Place
20+
21+
@Query(
22+
"SELECT * from place WHERE name!='' AND latitude>=:latBegin AND longitude>=:lngBegin " +
23+
"AND latitude<:latEnd AND longitude<:lngEnd"
24+
)
25+
abstract fun fetchPlaces(
26+
latBegin: Double,
27+
lngBegin: Double,
28+
latEnd: Double,
29+
lngEnd: Double
30+
): List<Place>
31+
32+
fun save(place: Place): Completable =
33+
Completable.fromAction { saveSynchronous(place) }
34+
35+
@Query("DELETE FROM place")
36+
abstract fun deleteAllSynchronous()
37+
38+
fun deleteAll(): Completable =
39+
Completable.fromAction { deleteAllSynchronous() }
40+
}

app/src/main/java/fr/free/nrw/commons/nearby/PlacesLocalDataSource.java

Lines changed: 0 additions & 121 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package fr.free.nrw.commons.nearby
2+
3+
import fr.free.nrw.commons.location.LatLng
4+
import io.reactivex.Completable
5+
import javax.inject.Inject
6+
7+
/**
8+
* Local data source handling cached [Place] objects.
9+
*/
10+
class PlacesLocalDataSource @Inject constructor(
11+
private val placeDao: PlaceDao
12+
) {
13+
14+
fun fetchPlace(entityID: String): Place = placeDao.getPlace(entityID)
15+
16+
fun fetchPlaces(mapBottomLeft: LatLng, mapTopRight: LatLng): List<Place> {
17+
data class Constraint(val latBegin: Double, val lngBegin: Double, val latEnd: Double, val lngEnd: Double)
18+
val constraints = mutableListOf<Constraint>()
19+
20+
if (mapTopRight.latitude < mapBottomLeft.latitude) {
21+
if (mapTopRight.longitude < mapBottomLeft.longitude) {
22+
constraints += Constraint(mapBottomLeft.latitude, mapBottomLeft.longitude, 90.0, 180.0)
23+
constraints += Constraint(mapBottomLeft.latitude, -180.0, 90.0, mapTopRight.longitude)
24+
constraints += Constraint(-90.0, mapBottomLeft.longitude, mapTopRight.latitude, 180.0)
25+
constraints += Constraint(-90.0, -180.0, mapTopRight.latitude, mapTopRight.longitude)
26+
} else {
27+
constraints += Constraint(mapBottomLeft.latitude, mapBottomLeft.longitude, 90.0, mapTopRight.longitude)
28+
constraints += Constraint(-90.0, mapBottomLeft.longitude, mapTopRight.latitude, mapTopRight.longitude)
29+
}
30+
} else {
31+
if (mapTopRight.longitude < mapBottomLeft.longitude) {
32+
constraints += Constraint(mapBottomLeft.latitude, mapBottomLeft.longitude, mapTopRight.latitude, 180.0)
33+
constraints += Constraint(mapBottomLeft.latitude, -180.0, mapTopRight.latitude, mapTopRight.longitude)
34+
} else {
35+
constraints += Constraint(mapBottomLeft.latitude, mapBottomLeft.longitude, mapTopRight.latitude, mapTopRight.longitude)
36+
}
37+
}
38+
39+
val cachedPlaces = mutableListOf<Place>()
40+
for (c in constraints) {
41+
cachedPlaces += placeDao.fetchPlaces(c.latBegin, c.lngBegin, c.latEnd, c.lngEnd)
42+
}
43+
return cachedPlaces
44+
}
45+
46+
fun savePlace(place: Place): Completable = placeDao.save(place)
47+
48+
fun clearCache(): Completable = placeDao.deleteAll()
49+
}

app/src/main/java/fr/free/nrw/commons/nearby/PlacesRepository.java

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)