mirror of
https://github.com/home-assistant/android
synced 2024-09-06 17:23:32 +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) {
|
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
|
||||||
entitiesFlow.collect {
|
entitiesFlow.collect {
|
||||||
loading = false
|
loading = false
|
||||||
|
val hasChanged = entities.size != it.size || entities.toSet() != it.toSet()
|
||||||
entities = it
|
entities = it
|
||||||
invalidate()
|
if (hasChanged) invalidate()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -103,13 +103,16 @@ class MainVehicleScreen(
|
||||||
invalidate()
|
invalidate()
|
||||||
}
|
}
|
||||||
allEntities.collect { entities ->
|
allEntities.collect { entities ->
|
||||||
domains.clear()
|
val newDomains = entities.values
|
||||||
entities.values.forEach {
|
.map { it.domain }
|
||||||
if (it.domain in SUPPORTED_DOMAINS) {
|
.distinct()
|
||||||
domains.add(it.domain)
|
.filter { it in SUPPORTED_DOMAINS }
|
||||||
}
|
.toSet()
|
||||||
|
if (newDomains.size != domains.size || newDomains != domains) {
|
||||||
|
domains.clear()
|
||||||
|
domains.addAll(newDomains)
|
||||||
|
invalidate()
|
||||||
}
|
}
|
||||||
invalidate()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,15 +43,30 @@ class MapVehicleScreen(
|
||||||
}
|
}
|
||||||
|
|
||||||
var loading = true
|
var loading = true
|
||||||
var entities: List<Entity<*>> = listOf()
|
var entities: Set<Entity<*>> = setOf()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch {
|
||||||
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
|
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
|
||||||
entitiesFlow.collect {
|
entitiesFlow.collect {
|
||||||
loading = false
|
loading = false
|
||||||
entities = it
|
val newSet = it
|
||||||
invalidate()
|
.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 {
|
override fun onGetTemplate(): Template {
|
||||||
val listBuilder = ItemList.Builder()
|
val listBuilder = ItemList.Builder()
|
||||||
entities
|
entities
|
||||||
.mapNotNull {
|
.map { // Null checks handled during collection
|
||||||
val attrs = it.attributes as? Map<*, *>
|
val attrs = it.attributes as Map<*, *>
|
||||||
if (it.domain == "device_tracker" && it.state == "home") {
|
val lat = attrs["latitude"] as Double
|
||||||
return@mapNotNull null
|
val lon = attrs["longitude"] as Double
|
||||||
}
|
Pair(it, listOf(lat, lon))
|
||||||
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
|
|
||||||
}
|
}
|
||||||
.sortedBy { it.first.friendlyName }
|
.sortedBy { it.first.friendlyName }
|
||||||
.forEach { (entity, location) ->
|
.forEach { (entity, location) ->
|
||||||
|
|
Loading…
Reference in a new issue