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 {

View File

@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
style="?attr/actionButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:srcCompat="@drawable/ic_filter" />
<TextView
android:id="@+id/filter_badge"
android:layout_width="wrap_content"
android:layout_height="16dp"
android:minWidth="15dp"
android:layout_gravity="right|end|bottom"
android:layout_marginEnd="-5dp"
android:layout_marginBottom="6dp"
android:background="@drawable/rounded_rect_4dp"
android:backgroundTint="?colorPrimary"
android:gravity="center"
android:padding="0dp"
android:textColor="@android:color/white"
android:visibility="gone"
tools:text="0"
android:textSize="10sp" />
</FrameLayout>

View File

@@ -6,5 +6,6 @@
android:id="@+id/menu_filter"
android:title="@string/menu_filter"
android:icon="@drawable/ic_filter"
app:showAsAction="ifRoom" />
app:showAsAction="ifRoom"
app:actionLayout="@layout/action_filter" />
</menu>