Only invalidate vehicle template UI when contents change (#3641)

* Don't invalidate Auto main screen if domains don't change

* Don't invalidate Auto domain/map screen if contents don't change
This commit is contained in:
Joris Pelgröm 2023-07-07 17:43:36 +02:00 committed by GitHub
parent 774e89c58e
commit ef469fb977
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 23 deletions

View file

@ -49,8 +49,9 @@ class EntityGridVehicleScreen(
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
entitiesFlow.collect {
loading = false
val hasChanged = entities.size != it.size || entities.toSet() != it.toSet()
entities = it
invalidate()
if (hasChanged) invalidate()
}
}
}

View file

@ -103,13 +103,16 @@ class MainVehicleScreen(
invalidate()
}
allEntities.collect { entities ->
domains.clear()
entities.values.forEach {
if (it.domain in SUPPORTED_DOMAINS) {
domains.add(it.domain)
}
val newDomains = entities.values
.map { it.domain }
.distinct()
.filter { it in SUPPORTED_DOMAINS }
.toSet()
if (newDomains.size != domains.size || newDomains != domains) {
domains.clear()
domains.addAll(newDomains)
invalidate()
}
invalidate()
}
}
}

View file

@ -43,15 +43,30 @@ class MapVehicleScreen(
}
var loading = true
var entities: List<Entity<*>> = listOf()
var entities: Set<Entity<*>> = setOf()
init {
lifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
entitiesFlow.collect {
loading = false
entities = it
invalidate()
val newSet = it
.filter { entity ->
if (entity.domain == "device_tracker" && entity.state == "home") {
return@filter false
}
val attrs = entity.attributes as? Map<*, *>
if (attrs != null) {
val lat = attrs["latitude"] as? Double
val lon = attrs["longitude"] as? Double
return@filter lat != null && lon != null
}
return@filter false
}
.toSet()
val hasChanged = entities.size != newSet.size || entities != newSet
entities = newSet
if (hasChanged) invalidate()
}
}
}
@ -60,19 +75,11 @@ class MapVehicleScreen(
override fun onGetTemplate(): Template {
val listBuilder = ItemList.Builder()
entities
.mapNotNull {
val attrs = it.attributes as? Map<*, *>
if (it.domain == "device_tracker" && it.state == "home") {
return@mapNotNull null
}
if (attrs != null) {
val lat = attrs["latitude"] as? Double
val lon = attrs["longitude"] as? Double
if (lat != null && lon != null) {
return@mapNotNull Pair(it, listOf(lat, lon))
}
}
return@mapNotNull null
.map { // Null checks handled during collection
val attrs = it.attributes as Map<*, *>
val lat = attrs["latitude"] as Double
val lon = attrs["longitude"] as Double
Pair(it, listOf(lat, lon))
}
.sortedBy { it.first.friendlyName }
.forEach { (entity, location) ->