add badge showing how many filters are active

This commit is contained in:
Johan von Forstner
2020-05-17 14:17:41 +02:00
parent 9ad2f86b39
commit ea94f67187
5 changed files with 75 additions and 3 deletions

View File

@@ -10,6 +10,7 @@ import android.content.res.Configuration
import android.os.Bundle
import android.view.*
import android.view.inputmethod.InputMethodManager
import android.widget.TextView
import androidx.activity.OnBackPressedCallback
import androidx.core.app.SharedElementCallback
import androidx.core.content.ContextCompat
@@ -505,7 +506,21 @@ class MapFragment : Fragment(), OnMapReadyCallback, MapsActivity.FragmentCallbac
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.map, menu)
super.onCreateOptionsMenu(menu, inflater)
val filterItem = menu.findItem(R.id.menu_filter)
val filterView = filterItem.actionView
val filterBadge = filterView?.findViewById<TextView>(R.id.filter_badge)
if (filterBadge != null) {
// set up badge showing number of active filters
vm.filtersCount.observe(viewLifecycleOwner, Observer {
filterBadge.visibility = if (it > 0) View.VISIBLE else View.GONE
filterBadge.text = it.toString()
})
}
filterView?.setOnClickListener {
onOptionsItemSelected(filterItem)
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {

View File

@@ -169,7 +169,18 @@ data class MultipleChoiceFilterValue(
@PrimaryKey override val key: String,
var values: MutableSet<String>,
var all: Boolean
) : FilterValue()
) : FilterValue() {
override fun equals(other: Any?): Boolean {
if (other == null || other !is MultipleChoiceFilterValue) return false
if (key != other.key) return false
return if (all) {
other.all
} else {
!other.all && values == other.values
}
}
}
@Entity
data class SliderFilterValue(

View File

@@ -45,6 +45,17 @@ class MapViewModel(application: Application, geApiKey: String) : AndroidViewMode
private val filtersWithValue: LiveData<List<FilterWithValue<out FilterValue>>> by lazy {
filtersWithValue(filters, filterValues)
}
val filtersCount: LiveData<Int> by lazy {
MediatorLiveData<Int>().apply {
value = 0
addSource(filtersWithValue) { filtersWithValue ->
value = filtersWithValue.count {
it.filter.defaultValue() != it.value
}
}
}
}
val chargepoints: MediatorLiveData<Resource<List<ChargepointListItem>>> by lazy {
MediatorLiveData<Resource<List<ChargepointListItem>>>()
.apply {