FilterFragment: add button to reset current settings

This commit is contained in:
johan12345
2023-01-20 19:12:52 +01:00
parent c972c871d4
commit 890eec4419
7 changed files with 76 additions and 11 deletions

View File

@@ -98,6 +98,12 @@ class FilterFragment : Fragment(), MenuProvider {
saveProfile()
true
}
R.id.menu_reset -> {
lifecycleScope.launch {
vm.resetValues()
}
true
}
else -> false
}
}

View File

@@ -1,29 +1,49 @@
package net.vonforst.evmap.storage
import androidx.lifecycle.liveData
import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.room.*
import net.vonforst.evmap.model.*
@Dao
abstract class FilterValueDao {
@Query("SELECT * FROM booleanfiltervalue WHERE profile = :profile AND dataSource = :dataSource")
protected abstract suspend fun getBooleanFilterValues(
protected abstract suspend fun getBooleanFilterValuesAsync(
profile: Long,
dataSource: String
): List<BooleanFilterValue>
@Query("SELECT * FROM multiplechoicefiltervalue WHERE profile = :profile AND dataSource = :dataSource")
protected abstract suspend fun getMultipleChoiceFilterValues(
protected abstract suspend fun getMultipleChoiceFilterValuesAsync(
profile: Long,
dataSource: String
): List<MultipleChoiceFilterValue>
@Query("SELECT * FROM sliderfiltervalue WHERE profile = :profile AND dataSource = :dataSource")
protected abstract suspend fun getSliderFilterValues(
protected abstract suspend fun getSliderFilterValuesAsync(
profile: Long,
dataSource: String
): List<SliderFilterValue>
@Query("SELECT * FROM booleanfiltervalue WHERE profile = :profile AND dataSource = :dataSource")
protected abstract fun getBooleanFilterValues(
profile: Long,
dataSource: String
): LiveData<List<BooleanFilterValue>>
@Query("SELECT * FROM multiplechoicefiltervalue WHERE profile = :profile AND dataSource = :dataSource")
protected abstract fun getMultipleChoiceFilterValues(
profile: Long,
dataSource: String
): LiveData<List<MultipleChoiceFilterValue>>
@Query("SELECT * FROM sliderfiltervalue WHERE profile = :profile AND dataSource = :dataSource")
protected abstract fun getSliderFilterValues(
profile: Long,
dataSource: String
): LiveData<List<SliderFilterValue>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
protected abstract suspend fun insert(vararg values: BooleanFilterValue)
@@ -58,15 +78,32 @@ abstract class FilterValueDao {
if (filterStatus == FILTERS_DISABLED || filterStatus == FILTERS_FAVORITES) {
emptyList()
} else {
getBooleanFilterValues(filterStatus, dataSource) +
getMultipleChoiceFilterValues(filterStatus, dataSource) +
getSliderFilterValues(filterStatus, dataSource)
getBooleanFilterValuesAsync(filterStatus, dataSource) +
getMultipleChoiceFilterValuesAsync(filterStatus, dataSource) +
getSliderFilterValuesAsync(filterStatus, dataSource)
}
open fun getFilterValues(filterStatus: Long, dataSource: String) = liveData {
emit(null)
emit(getFilterValuesAsync(filterStatus, dataSource))
}
open fun getFilterValues(filterStatus: Long, dataSource: String): LiveData<List<FilterValue>?> =
if (filterStatus == FILTERS_DISABLED || filterStatus == FILTERS_FAVORITES) {
MutableLiveData(emptyList())
} else {
MediatorLiveData<List<FilterValue>?>().apply {
value = null
val sources = listOf(
getBooleanFilterValues(filterStatus, dataSource),
getMultipleChoiceFilterValues(filterStatus, dataSource),
getSliderFilterValues(filterStatus, dataSource)
)
for (source in sources) {
addSource(source) {
val values = sources.map { it.value }
if (values.all { it != null }) {
value = values.filterNotNull().flatten()
}
}
}
}
}
@Transaction
open suspend fun insert(vararg values: FilterValue) {

View File

@@ -92,4 +92,8 @@ class FilterViewModel(application: Application) : AndroidViewModel(application)
prefs.filterStatus = FILTERS_DISABLED
}
}
suspend fun resetValues() {
db.filterValueDao().deleteFilterValuesForProfile(FILTERS_CUSTOM, prefs.dataSource)
}
}

View File

@@ -0,0 +1,10 @@
<vector android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportHeight="24"
android:viewportWidth="24"
android:width="24dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<path
android:fillColor="@android:color/white"
android:pathData="M10.83,8H21V6H8.83L10.83,8zM15.83,13H18v-2h-4.17L15.83,13zM14,16.83V18h-4v-2h3.17l-3,-3H6v-2h2.17l-3,-3H3V6h0.17L1.39,4.22l1.41,-1.41l18.38,18.38l-1.41,1.41L14,16.83z" />
</vector>

View File

@@ -1,6 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_reset"
android:title="@string/menu_reset"
android:icon="@drawable/ic_filter_no"
app:showAsAction="ifRoom" />
<item
android:id="@+id/menu_save_profile"
android:title="@string/menu_save_profile"

View File

@@ -143,6 +143,7 @@
<string name="category_caravan_site">Wohnmobilstellplatz</string>
<string name="menu_apply">Filter anwenden</string>
<string name="menu_save_profile">Als Profil speichern</string>
<string name="menu_reset">Filter zurücksetzen</string>
<string name="no_filters">Keine Filter</string>
<string name="filter_custom">Verändertes Filterprofil</string>
<string name="filter_favorites">Favoriten</string>

View File

@@ -142,6 +142,7 @@
<string name="category_caravan_site">Caravan site</string>
<string name="menu_apply">Apply filters</string>
<string name="menu_save_profile">Save as profile</string>
<string name="menu_reset">Reset filter settings</string>
<string name="no_filters">No filters</string>
<string name="filter_custom">Modified filter</string>
<string name="filter_favorites">Favorites</string>