mirror of
https://github.com/home-assistant/android
synced 2024-10-15 12:32:54 +00:00
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:
parent
774e89c58e
commit
ef469fb977
|
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -103,16 +103,19 @@ class MainVehicleScreen(
|
|||
invalidate()
|
||||
}
|
||||
allEntities.collect { entities ->
|
||||
val newDomains = entities.values
|
||||
.map { it.domain }
|
||||
.distinct()
|
||||
.filter { it in SUPPORTED_DOMAINS }
|
||||
.toSet()
|
||||
if (newDomains.size != domains.size || newDomains != domains) {
|
||||
domains.clear()
|
||||
entities.values.forEach {
|
||||
if (it.domain in SUPPORTED_DOMAINS) {
|
||||
domains.add(it.domain)
|
||||
}
|
||||
}
|
||||
domains.addAll(newDomains)
|
||||
invalidate()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lifecycle.addObserver(object : DefaultLifecycleObserver {
|
||||
|
||||
|
|
|
@ -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) ->
|
||||
|
|
Loading…
Reference in a new issue