mirror of
https://github.com/ev-map/EVMap.git
synced 2026-04-29 02:24:20 -04:00
add badge showing how many filters are active
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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 {
|
||||
|
||||
34
app/src/main/res/layout/action_filter.xml
Normal file
34
app/src/main/res/layout/action_filter.xml
Normal 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>
|
||||
@@ -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>
|
||||
Reference in New Issue
Block a user