diff --git a/app/build.gradle b/app/build.gradle index af35727c..43b96dbc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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" diff --git a/app/src/main/java/net/vonforst/evmap/fragment/preference/DataSettingsFragment.kt b/app/src/main/java/net/vonforst/evmap/fragment/preference/DataSettingsFragment.kt index 4a04fd97..16bde0e8 100644 --- a/app/src/main/java/net/vonforst/evmap/fragment/preference/DataSettingsFragment.kt +++ b/app/src/main/java/net/vonforst/evmap/fragment/preference/DataSettingsFragment.kt @@ -41,8 +41,23 @@ class DataSettingsFragment : BaseSettingsFragment() { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { setPreferencesFromResource(R.xml.settings_data, rootKey) - teslaAccountPreference = findPreference("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("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) } } diff --git a/app/src/main/java/net/vonforst/evmap/storage/ChargeLocationsDao.kt b/app/src/main/java/net/vonforst/evmap/storage/ChargeLocationsDao.kt index 272cc5f8..961e49bd 100644 --- a/app/src/main/java/net/vonforst/evmap/storage/ChargeLocationsDao.kt +++ b/app/src/main/java/net/vonforst/evmap/storage/ChargeLocationsDao.kt @@ -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> + + @Query("SELECT COUNT(*) FROM chargelocation") + abstract fun getCount(): LiveData + + @SkipQueryVerification + @Query("SELECT SUM(pgsize) FROM dbstat WHERE name == \"ChargeLocation\"") + abstract suspend fun getSize(): Long } /** diff --git a/app/src/main/java/net/vonforst/evmap/storage/SavedRegionDao.kt b/app/src/main/java/net/vonforst/evmap/storage/SavedRegionDao.kt index c2953790..7a0611c0 100644 --- a/app/src/main/java/net/vonforst/evmap/storage/SavedRegionDao.kt +++ b/app/src/main/java/net/vonforst/evmap/storage/SavedRegionDao.kt @@ -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( diff --git a/app/src/main/java/net/vonforst/evmap/viewmodel/SettingsViewModel.kt b/app/src/main/java/net/vonforst/evmap/viewmodel/SettingsViewModel.kt index 6c952b4c..1bd58e84 100644 --- a/app/src/main/java/net/vonforst/evmap/viewmodel/SettingsViewModel.kt +++ b/app/src/main/java/net/vonforst/evmap/viewmodel/SettingsViewModel.kt @@ -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>> by lazy { MutableLiveData>>().apply { @@ -34,6 +37,20 @@ class SettingsViewModel( } } + val chargerCacheCount: LiveData by lazy { + db.chargeLocationsDao().getCount() + } + + val chargerCacheSize: LiveData by lazy { + MutableLiveData().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() + } + } } \ No newline at end of file diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 4b7b5a47..5599869b 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -320,4 +320,9 @@ Meilen aus Daten abgerufen %s + Cache + Cache-Größe + Cache leeren + Löscht alle gespeicherten Ladestationen außer Favoriten + %d Ladestationen gespeichert, %.1f MB \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 13cc7fa3..239deaad 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -320,4 +320,9 @@ miles off Data retrieved %s + Caching + Cache size + Clear cache + Deletes all cached chargers except favorites + %d chargers cached, %.1f MB \ No newline at end of file diff --git a/app/src/main/res/xml/settings_data.xml b/app/src/main/res/xml/settings_data.xml index 7f8d9bc9..d74578d0 100644 --- a/app/src/main/res/xml/settings_data.xml +++ b/app/src/main/res/xml/settings_data.xml @@ -43,4 +43,13 @@ android:title="@string/pref_search_delete_recent" /> + + + + \ No newline at end of file