Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update filter in Manage Sensors to all/enabled/disabled #2950

Merged
merged 2 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand All @@ -41,8 +48,8 @@ class SensorSettingsViewModel @Inject constructor(
filterSensorsList()
}

fun setShowOnlyEnabledSensors(onlyEnabled: Boolean) {
showOnlyEnabledSensors.value = onlyEnabled
fun setSensorFilterChoice(filter: SensorFilter) {
sensorFilter = filter
filterSensorsList()
}

Expand All @@ -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 } }
)
Expand Down
11 changes: 0 additions & 11 deletions app/src/main/res/drawable/ic_fact_check_toolbar_filter.xml

This file was deleted.

13 changes: 9 additions & 4 deletions app/src/main/res/menu/menu_activity_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
4 changes: 3 additions & 1 deletion common/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down