Hide non-primary entities on Wear home screen (#2216)

This commit is contained in:
Joris Pelgröm 2022-01-31 20:58:01 +01:00 committed by GitHub
parent cee8eace15
commit d2e944023e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 8 deletions

View file

@ -3,5 +3,6 @@ package io.homeassistant.companion.android.common.data.websocket.impl.entities
data class EntityRegistryResponse(
val areaId: String?,
val deviceId: String?,
val entityCategory: String?,
val entityId: String
)

View file

@ -26,4 +26,11 @@ object RegistriesDataHandler {
}
return null
}
fun getCategoryForEntity(
entityId: String,
entityRegistry: List<EntityRegistryResponse>?
): String? {
return entityRegistry?.firstOrNull { it.entityId == entityId }?.entityCategory
}
}

View file

@ -193,6 +193,9 @@ class MainViewModel @Inject constructor(application: Application) : AndroidViewM
fun getAreaForEntity(entityId: String): AreaRegistryResponse? =
RegistriesDataHandler.getAreaForEntity(entityId, areaRegistry, deviceRegistry, entityRegistry)
fun getCategoryForEntity(entityId: String): String? =
RegistriesDataHandler.getCategoryForEntity(entityId, entityRegistry)
private fun getFavorites() {
viewModelScope.launch {
favorites()?.collect {

View file

@ -143,13 +143,17 @@ fun MainView(
}
}
if (mainViewModel.entitiesByArea.values.any { it.isNotEmpty() }) {
if (mainViewModel.entitiesByArea.values.any {
it.isNotEmpty() && it.any { entity -> mainViewModel.getCategoryForEntity(entity.entityId) == null }
}
) {
item {
ListHeader(id = commonR.string.areas)
}
for (id in mainViewModel.entitiesByAreaOrder) {
val entities = mainViewModel.entitiesByArea[id]
if (!entities.isNullOrEmpty()) {
val entitiesToShow = entities?.filter { mainViewModel.getCategoryForEntity(it.entityId) == null }
if (!entitiesToShow.isNullOrEmpty()) {
val area = mainViewModel.areas.first { it.areaId == id }
item {
Chip(
@ -161,7 +165,7 @@ fun MainView(
onTestClicked(
mapOf(area.name to entities),
listOf(area.name)
) { true }
) { mainViewModel.getCategoryForEntity(it.entityId) == null }
},
colors = ChipDefaults.primaryChipColors()
)
@ -170,7 +174,9 @@ fun MainView(
}
}
if (mainViewModel.entities.values.any { mainViewModel.getAreaForEntity(it.entityId) == null }) {
val domainEntitiesFilter: (entity: Entity<*>) -> Boolean =
{ mainViewModel.getAreaForEntity(it.entityId) == null && mainViewModel.getCategoryForEntity(it.entityId) == null }
if (mainViewModel.entities.values.any(domainEntitiesFilter)) {
item {
ListHeader(id = commonR.string.more_entities)
}
@ -178,8 +184,8 @@ fun MainView(
// Buttons for each existing category
for (domain in mainViewModel.entitiesByDomainOrder) {
val domainEntities = mainViewModel.entitiesByDomain[domain]!!
val domainEntitiesNotInArea = domainEntities.filter { mainViewModel.getAreaForEntity(it.entityId) == null }
if (domainEntitiesNotInArea.isNotEmpty()) {
val domainEntitiesToShow = domainEntities.filter(domainEntitiesFilter)
if (domainEntitiesToShow.isNotEmpty()) {
item {
Chip(
modifier = Modifier.fillMaxWidth(),
@ -194,8 +200,9 @@ fun MainView(
mapOf(
mainViewModel.stringForDomain(domain)!! to domainEntities
),
listOf(mainViewModel.stringForDomain(domain)!!)
) { mainViewModel.getAreaForEntity(it.entityId) == null }
listOf(mainViewModel.stringForDomain(domain)!!),
domainEntitiesFilter
)
},
colors = ChipDefaults.primaryChipColors()
)