display current cache size in settings

This commit is contained in:
johan12345
2023-06-04 22:26:10 +02:00
committed by Johan von Forstner
parent 463ff61420
commit 2ba6a86b34
8 changed files with 83 additions and 4 deletions

View File

@@ -246,7 +246,10 @@ dependencies {
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-ktx:$room_version"
implementation 'com.github.anboralabs:spatia-room:0.2.6'
implementation('com.github.anboralabs:spatia-room:0.2.6') {
exclude group: 'com.github.dalgarins', module: 'android-spatialite'
}
implementation 'com.github.ev-map:android-spatialite:654dca2365'
// billing library
def billing_version = "6.0.0"

View File

@@ -41,8 +41,23 @@ class DataSettingsFragment : BaseSettingsFragment() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.settings_data, rootKey)
teslaAccountPreference = findPreference<Preference>("tesla_account")!!
teslaAccountPreference = findPreference("tesla_account")!!
refreshTeslaAccountStatus()
vm.chargerCacheCount.observe(this) {
updateCacheSizeSummary()
}
vm.chargerCacheSize.observe(this) {
updateCacheSizeSummary()
}
}
private fun updateCacheSizeSummary() {
val count = vm.chargerCacheCount.value ?: return
val size = vm.chargerCacheSize.value ?: return
val sizeMb = size.toFloat() / 1024 / 1024
findPreference<Preference>("cache_size")!!.summary =
getString(R.string.settings_cache_count_summary, count, sizeMb)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@@ -114,6 +129,11 @@ class DataSettingsFragment : BaseSettingsFragment() {
true
}
"cache_clear" -> {
vm.clearChargerCache()
true
}
else -> super.onPreferenceTreeClick(preference)
}
}

View File

@@ -53,6 +53,9 @@ abstract class ChargeLocationsDao {
@Query("DELETE FROM chargelocation WHERE dataSource == :dataSource AND timeRetrieved <= :before AND NOT EXISTS (SELECT 1 FROM favorite WHERE favorite.chargerId = chargelocation.id)")
abstract suspend fun deleteOutdatedIfNotFavorite(dataSource: String, before: Long)
@Query("DELETE FROM chargelocation WHERE NOT EXISTS (SELECT 1 FROM favorite WHERE favorite.chargerId = chargelocation.id)")
abstract suspend fun deleteAllIfNotFavorite()
@Query("SELECT * FROM chargelocation WHERE dataSource == :dataSource AND id == :id AND isDetailed == 1 AND timeRetrieved > :after")
abstract fun getChargeLocationById(
id: Long,
@@ -83,6 +86,13 @@ abstract class ChargeLocationsDao {
@RawQuery(observedEntities = [ChargeLocation::class])
abstract fun getChargeLocationsCustom(query: SupportSQLiteQuery): LiveData<List<ChargeLocation>>
@Query("SELECT COUNT(*) FROM chargelocation")
abstract fun getCount(): LiveData<Long>
@SkipQueryVerification
@Query("SELECT SUM(pgsize) FROM dbstat WHERE name == \"ChargeLocation\"")
abstract suspend fun getSize(): Long
}
/**

View File

@@ -97,6 +97,9 @@ abstract class SavedRegionDao {
@Query("DELETE FROM savedregion WHERE dataSource == :dataSource AND timeRetrieved <= :before")
abstract suspend fun deleteOutdated(dataSource: String, before: Long)
@Query("DELETE FROM savedregion")
abstract suspend fun deleteAll()
@SkipQueryVerification
@Query("SELECT MakeEllipse(:lng, :lat, :radiusLng, :radiusLat, 4326)")
protected abstract suspend fun makeEllipse(

View File

@@ -2,6 +2,7 @@ package net.vonforst.evmap.viewmodel
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
@@ -9,6 +10,7 @@ import net.vonforst.evmap.api.chargeprice.ChargepriceApi
import net.vonforst.evmap.api.chargeprice.ChargepriceCar
import net.vonforst.evmap.api.chargeprice.ChargepriceTariff
import net.vonforst.evmap.storage.AppDatabase
import net.vonforst.evmap.storage.PreferenceDataSource
import java.io.IOException
class SettingsViewModel(
@@ -17,8 +19,9 @@ class SettingsViewModel(
chargepriceApiUrl: String
) :
AndroidViewModel(application) {
private var api = ChargepriceApi.create(chargepriceApiKey, chargepriceApiUrl)
private var db = AppDatabase.getInstance(application)
private val api = ChargepriceApi.create(chargepriceApiKey, chargepriceApiUrl)
private val db = AppDatabase.getInstance(application)
private val prefs = PreferenceDataSource(application)
val vehicles: MutableLiveData<Resource<List<ChargepriceCar>>> by lazy {
MutableLiveData<Resource<List<ChargepriceCar>>>().apply {
@@ -34,6 +37,20 @@ class SettingsViewModel(
}
}
val chargerCacheCount: LiveData<Long> by lazy {
db.chargeLocationsDao().getCount()
}
val chargerCacheSize: LiveData<Long> by lazy {
MutableLiveData<Long>().apply {
chargerCacheCount.observeForever {
viewModelScope.launch {
value = db.chargeLocationsDao().getSize()
}
}
}
}
private fun loadVehicles() {
viewModelScope.launch {
try {
@@ -61,4 +78,11 @@ class SettingsViewModel(
db.recentAutocompletePlaceDao().deleteAll()
}
}
fun clearChargerCache() {
viewModelScope.launch {
db.savedRegionDao().deleteAll()
db.chargeLocationsDao().deleteAllIfNotFavorite()
}
}
}

View File

@@ -320,4 +320,9 @@
<string name="pref_map_scale_miles">Meilen</string>
<string name="pref_map_scale_off">aus</string>
<string name="data_retrieved_at">Daten abgerufen %s</string>
<string name="settings_caching">Cache</string>
<string name="settings_cache_count">Cache-Größe</string>
<string name="settings_cache_clear">Cache leeren</string>
<string name="settings_cache_clear_summary">Löscht alle gespeicherten Ladestationen außer Favoriten</string>
<string name="settings_cache_count_summary">%d Ladestationen gespeichert, %.1f MB</string>
</resources>

View File

@@ -320,4 +320,9 @@
<string name="pref_map_scale_miles">miles</string>
<string name="pref_map_scale_off">off</string>
<string name="data_retrieved_at">Data retrieved %s</string>
<string name="settings_caching">Caching</string>
<string name="settings_cache_count">Cache size</string>
<string name="settings_cache_clear">Clear cache</string>
<string name="settings_cache_clear_summary">Deletes all cached chargers except favorites</string>
<string name="settings_cache_count_summary">%d chargers cached, %.1f MB</string>
</resources>

View File

@@ -43,4 +43,13 @@
android:title="@string/pref_search_delete_recent" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/settings_caching">
<Preference
android:key="cache_size"
android:title="@string/settings_cache_count" />
<Preference
android:key="cache_clear"
android:title="@string/settings_cache_clear"
android:summary="@string/settings_cache_clear_summary" />
</PreferenceCategory>
</PreferenceScreen>