-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
encapsulates logic to load charging stations for future implementation of offline caching (#164, #97)
- Loading branch information
1 parent
3853536
commit 3c30481
Showing
11 changed files
with
235 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 96 additions & 21 deletions
117
app/src/main/java/net/vonforst/evmap/storage/ChargeLocationsDao.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,109 @@ | ||
package net.vonforst.evmap.storage | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.room.* | ||
import androidx.lifecycle.* | ||
import androidx.room.Dao | ||
import androidx.room.Delete | ||
import androidx.room.Insert | ||
import androidx.room.OnConflictStrategy | ||
import com.car2go.maps.model.LatLng | ||
import com.car2go.maps.model.LatLngBounds | ||
import kotlinx.coroutines.CoroutineScope | ||
import net.vonforst.evmap.api.ChargepointApi | ||
import net.vonforst.evmap.api.StringProvider | ||
import net.vonforst.evmap.api.goingelectric.GoingElectricApiWrapper | ||
import net.vonforst.evmap.api.openchargemap.OpenChargeMapApiWrapper | ||
import net.vonforst.evmap.model.ChargeLocation | ||
import net.vonforst.evmap.model.ChargepointListItem | ||
import net.vonforst.evmap.model.FilterValues | ||
import net.vonforst.evmap.model.ReferenceData | ||
import net.vonforst.evmap.viewmodel.Resource | ||
import net.vonforst.evmap.viewmodel.await | ||
|
||
@Dao | ||
interface ChargeLocationsDao { | ||
abstract class ChargeLocationsDao { | ||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
suspend fun insert(vararg locations: ChargeLocation) | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
fun insertBlocking(vararg locations: ChargeLocation) | ||
abstract suspend fun insert(vararg locations: ChargeLocation) | ||
|
||
@Delete | ||
suspend fun delete(vararg locations: ChargeLocation) | ||
abstract suspend fun delete(vararg locations: ChargeLocation) | ||
} | ||
|
||
/** | ||
* The ChargeLocationsRepository wraps the ChargepointApi and the DB to provide caching | ||
* functionality. | ||
*/ | ||
class ChargeLocationsRepository( | ||
api: ChargepointApi<ReferenceData>, private val scope: CoroutineScope, | ||
private val db: AppDatabase, private val prefs: PreferenceDataSource | ||
) { | ||
val api = MutableLiveData<ChargepointApi<ReferenceData>>().apply { value = api } | ||
|
||
val referenceData = this.api.switchMap { | ||
when (it) { | ||
is GoingElectricApiWrapper -> { | ||
GEReferenceDataRepository( | ||
it, | ||
scope, | ||
db.geReferenceDataDao(), | ||
prefs | ||
).getReferenceData() | ||
} | ||
is OpenChargeMapApiWrapper -> { | ||
OCMReferenceDataRepository( | ||
it, | ||
scope, | ||
db.ocmReferenceDataDao(), | ||
prefs | ||
).getReferenceData() | ||
} | ||
else -> { | ||
throw RuntimeException("no reference data implemented") | ||
} | ||
} | ||
} | ||
private val chargeLocationsDao = db.chargeLocationsDao() | ||
|
||
fun getChargepoints( | ||
bounds: LatLngBounds, | ||
zoom: Float, | ||
filters: FilterValues? | ||
): LiveData<Resource<List<ChargepointListItem>>> { | ||
val api = api.value!! | ||
return liveData { | ||
val refData = referenceData.await() | ||
val result = api.getChargepoints(refData, bounds, zoom, filters) | ||
|
||
emit(result) | ||
} | ||
} | ||
|
||
@Query("SELECT * FROM chargelocation") | ||
fun getAllChargeLocations(): LiveData<List<ChargeLocation>> | ||
fun getChargepointsRadius( | ||
location: LatLng, | ||
radius: Int, | ||
zoom: Float, | ||
filters: FilterValues? | ||
): LiveData<Resource<List<ChargepointListItem>>> { | ||
val api = api.value!! | ||
return liveData { | ||
val refData = referenceData.await() | ||
val result = | ||
api.getChargepointsRadius(refData, location, radius, zoom, filters) | ||
|
||
@Query("SELECT * FROM chargelocation") | ||
suspend fun getAllChargeLocationsAsync(): List<ChargeLocation> | ||
emit(result) | ||
} | ||
} | ||
|
||
@Query("SELECT * FROM chargelocation") | ||
fun getAllChargeLocationsBlocking(): List<ChargeLocation> | ||
fun getChargepointDetail( | ||
id: Long | ||
): LiveData<Resource<ChargeLocation>> { | ||
return liveData { | ||
val refData = referenceData.await() | ||
val result = api.value!!.getChargepointDetail(refData, id) | ||
emit(result) | ||
} | ||
} | ||
|
||
@Query("SELECT * FROM chargelocation WHERE lat >= :lat1 AND lat <= :lat2 AND lng >= :lng1 AND lng <= :lng2") | ||
suspend fun getChargeLocationsInBoundsAsync( | ||
lat1: Double, | ||
lat2: Double, | ||
lng1: Double, | ||
lng2: Double | ||
): List<ChargeLocation> | ||
fun getFilters(sp: StringProvider) = referenceData.map { data -> | ||
api.value!!.getFilters(data, sp) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.