mirror of
https://github.com/home-assistant/android
synced 2024-09-06 17:23:32 +00:00
Update filter in Manage Sensors to all/enabled/disabled (#2950)
* Update filter in Manage Sensors to all/enabled/disabled * Cleanup when (enums are exhaustive)
This commit is contained in:
parent
dd800d00ea
commit
0ea8821193
|
@ -51,9 +51,13 @@ class SensorSettingsFragment : Fragment() {
|
|||
viewModel.setSensorsSearchQuery(null)
|
||||
}
|
||||
|
||||
if (viewModel.showOnlyEnabledSensors.value) {
|
||||
val checkable = menu.findItem(R.id.action_show_only_enabled_sensors)
|
||||
checkable?.isChecked = true
|
||||
when (viewModel.sensorFilter) {
|
||||
SensorSettingsViewModel.SensorFilter.ALL ->
|
||||
menu.findItem(R.id.action_show_sensors_all)?.isChecked = true
|
||||
SensorSettingsViewModel.SensorFilter.ENABLED ->
|
||||
menu.findItem(R.id.action_show_sensors_enabled)?.isChecked = true
|
||||
SensorSettingsViewModel.SensorFilter.DISABLED ->
|
||||
menu.findItem(R.id.action_show_sensors_disabled)?.isChecked = true
|
||||
}
|
||||
|
||||
menu.findItem(R.id.get_help)?.let {
|
||||
|
@ -64,9 +68,15 @@ class SensorSettingsFragment : Fragment() {
|
|||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
when (item.itemId) {
|
||||
R.id.action_show_only_enabled_sensors -> {
|
||||
item.isChecked = !item.isChecked
|
||||
viewModel.setShowOnlyEnabledSensors(item.isChecked)
|
||||
R.id.action_show_sensors_all, R.id.action_show_sensors_enabled, R.id.action_show_sensors_disabled -> {
|
||||
item.isChecked = true
|
||||
viewModel.setSensorFilterChoice(
|
||||
when (item.itemId) {
|
||||
R.id.action_show_sensors_enabled -> SensorSettingsViewModel.SensorFilter.ENABLED
|
||||
R.id.action_show_sensors_disabled -> SensorSettingsViewModel.SensorFilter.DISABLED
|
||||
else -> SensorSettingsViewModel.SensorFilter.ALL
|
||||
}
|
||||
)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
package io.homeassistant.companion.android.settings.sensor
|
||||
|
||||
import android.app.Application
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateListOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
|
@ -19,16 +21,21 @@ class SensorSettingsViewModel @Inject constructor(
|
|||
) :
|
||||
AndroidViewModel(application) {
|
||||
|
||||
enum class SensorFilter {
|
||||
ALL,
|
||||
ENABLED,
|
||||
DISABLED
|
||||
}
|
||||
|
||||
private var sensorsList = emptyList<Sensor>()
|
||||
var sensors = mutableStateListOf<Sensor>()
|
||||
|
||||
var searchQuery: String? = null
|
||||
var showOnlyEnabledSensors = mutableStateOf(false)
|
||||
var sensorFilter by mutableStateOf(SensorFilter.ALL)
|
||||
private set
|
||||
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
// TODO: For some reason we can't inject the sensor dao into this view model.
|
||||
sensorDao.getAllFlow().collect {
|
||||
sensorsList = it
|
||||
filterSensorsList()
|
||||
|
@ -41,8 +48,8 @@ class SensorSettingsViewModel @Inject constructor(
|
|||
filterSensorsList()
|
||||
}
|
||||
|
||||
fun setShowOnlyEnabledSensors(onlyEnabled: Boolean) {
|
||||
showOnlyEnabledSensors.value = onlyEnabled
|
||||
fun setSensorFilterChoice(filter: SensorFilter) {
|
||||
sensorFilter = filter
|
||||
filterSensorsList()
|
||||
}
|
||||
|
||||
|
@ -61,7 +68,11 @@ class SensorSettingsViewModel @Inject constructor(
|
|||
app.getString(manager.name).contains(searchQuery!!, true)
|
||||
)
|
||||
) &&
|
||||
(!showOnlyEnabledSensors.value || manager.isEnabled(app.applicationContext, sensor.id))
|
||||
(
|
||||
sensorFilter == SensorFilter.ALL ||
|
||||
(sensorFilter == SensorFilter.ENABLED && manager.isEnabled(app.applicationContext, sensor.id)) ||
|
||||
(sensorFilter == SensorFilter.DISABLED && !manager.isEnabled(app.applicationContext, sensor.id))
|
||||
)
|
||||
}
|
||||
.mapNotNull { sensor -> sensorsList.firstOrNull { it.id == sensor.id } }
|
||||
)
|
||||
|
|
|
@ -1,11 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:autoMirrored="true"
|
||||
android:height="24dp"
|
||||
android:viewportHeight="24.0"
|
||||
android:viewportWidth="24.0"
|
||||
android:width="24dp">
|
||||
<path android:fillColor="@color/colorIcon"
|
||||
android:fillType="evenOdd"
|
||||
android:pathData="M20,3H4C2.9,3 2,3.9 2,5v14c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2V5C22,3.9 21.1,3 20,3zM10,17H5v-2h5V17zM10,13H5v-2h5V13zM10,9H5V7h5V9zM14.82,15L12,12.16l1.41,-1.41l1.41,1.42L17.99,9l1.42,1.42L14.82,15z"/>
|
||||
</vector>
|
|
@ -23,10 +23,15 @@
|
|||
android:id="@+id/filter_toolbar_group"
|
||||
android:checkableBehavior="single">
|
||||
<item
|
||||
android:id="@+id/action_show_only_enabled_sensors"
|
||||
android:checkable="true"
|
||||
android:icon="@drawable/ic_fact_check_toolbar_filter"
|
||||
android:title="@string/filter_show_only_enabled_sensors" />
|
||||
android:id="@+id/action_show_sensors_all"
|
||||
android:title="@string/filter_sensors_all"
|
||||
android:checked="true" />
|
||||
<item
|
||||
android:id="@+id/action_show_sensors_enabled"
|
||||
android:title="@string/filter_sensors_enabled" />
|
||||
<item
|
||||
android:id="@+id/action_show_sensors_disabled"
|
||||
android:title="@string/filter_sensors_disabled" />
|
||||
</group>
|
||||
</menu>
|
||||
</item>
|
||||
|
|
|
@ -222,7 +222,9 @@
|
|||
<string name="feedback_settings">Feedback Settings</string>
|
||||
<string name="filter_notifications">Filter Notifications</string>
|
||||
<string name="filter_sensors">Filter sensors</string>
|
||||
<string name="filter_show_only_enabled_sensors">Only enabled sensors</string>
|
||||
<string name="filter_sensors_all">All sensors</string>
|
||||
<string name="filter_sensors_enabled">Enabled sensors</string>
|
||||
<string name="filter_sensors_disabled">Disabled sensors</string>
|
||||
<string name="finish">Finish</string>
|
||||
<string name="firebase_error_message">This means you will be unable to receive cloud notifications.\n\nLocal notifications will still be available via persistent connection settings in companion app settings</string>
|
||||
<string name="firebase_error_title">Firebase Error</string>
|
||||
|
|
Loading…
Reference in a new issue