From f93e3ad1fd693b5183d07a5fd2f470676a116d58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joris=20Pelgr=C3=B6m?= Date: Mon, 7 Aug 2023 23:09:19 +0200 Subject: [PATCH 1/2] Reload entity registry, show loading when changing servers --- .../android/vehicle/MainVehicleScreen.kt | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/io/homeassistant/companion/android/vehicle/MainVehicleScreen.kt b/app/src/main/java/io/homeassistant/companion/android/vehicle/MainVehicleScreen.kt index 35ff2275a22..8de1e301b34 100644 --- a/app/src/main/java/io/homeassistant/companion/android/vehicle/MainVehicleScreen.kt +++ b/app/src/main/java/io/homeassistant/companion/android/vehicle/MainVehicleScreen.kt @@ -51,6 +51,7 @@ class MainVehicleScreen( private var isLoggedIn: Boolean? = null private val domains = mutableSetOf() private var domainsAdded = false + private var domainsAddedFor: Int? = null private val isAutomotive get() = carContext.packageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE) @@ -69,23 +70,31 @@ class MainVehicleScreen( .getSessionState() == SessionState.CONNECTED invalidate() } - entityRegistry = serverManager.webSocketRepository(serverId.value).getEntityRegistry() - allEntities.collect { entities -> - val newDomains = entities.values - .map { it.domain } - .distinct() - .filter { it in SUPPORTED_DOMAINS } - .toSet() - var invalidate = newDomains.size != domains.size || newDomains != domains || !domainsAdded - domains.clear() - domains.addAll(newDomains) - domainsAdded = true + serverId.collect { server -> + if (domainsAddedFor != server) { + domainsAdded = false + domainsAddedFor = server + invalidate() // Show loading state + entityRegistry = serverManager.webSocketRepository(server).getEntityRegistry() + } - val newFavorites = getFavoritesList(entities) - invalidate = invalidate || (newFavorites.size != favoritesEntities.size || newFavorites.toSet() != favoritesEntities.toSet()) - favoritesEntities = newFavorites + allEntities.collect { entities -> + val newDomains = entities.values + .map { it.domain } + .distinct() + .filter { it in SUPPORTED_DOMAINS } + .toSet() + var invalidate = newDomains.size != domains.size || newDomains != domains || !domainsAdded + domains.clear() + domains.addAll(newDomains) + domainsAdded = true - if (invalidate) invalidate() + val newFavorites = getFavoritesList(entities) + invalidate = invalidate || newFavorites.size != favoritesEntities.size || newFavorites.toSet() != favoritesEntities.toSet() + favoritesEntities = newFavorites + + if (invalidate) invalidate() + } } } } From b09075b731003f930f7a8a24351b56e79c8739f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joris=20Pelgr=C3=B6m?= Date: Tue, 8 Aug 2023 17:50:28 +0200 Subject: [PATCH 2/2] Don't block collecting server IDs by using a Job --- .../android/vehicle/MainVehicleScreen.kt | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/io/homeassistant/companion/android/vehicle/MainVehicleScreen.kt b/app/src/main/java/io/homeassistant/companion/android/vehicle/MainVehicleScreen.kt index 8de1e301b34..760f7910e8e 100644 --- a/app/src/main/java/io/homeassistant/companion/android/vehicle/MainVehicleScreen.kt +++ b/app/src/main/java/io/homeassistant/companion/android/vehicle/MainVehicleScreen.kt @@ -24,6 +24,7 @@ import io.homeassistant.companion.android.util.vehicle.getChangeServerGridItem import io.homeassistant.companion.android.util.vehicle.getDomainList import io.homeassistant.companion.android.util.vehicle.getNavigationGridItem import io.homeassistant.companion.android.util.vehicle.nativeModeActionStrip +import kotlinx.coroutines.Job import kotlinx.coroutines.delay import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.StateFlow @@ -50,6 +51,7 @@ class MainVehicleScreen( private var favoritesList = emptyList() private var isLoggedIn: Boolean? = null private val domains = mutableSetOf() + private var domainsJob: Job? = null private var domainsAdded = false private var domainsAddedFor: Int? = null @@ -78,22 +80,25 @@ class MainVehicleScreen( entityRegistry = serverManager.webSocketRepository(server).getEntityRegistry() } - allEntities.collect { entities -> - val newDomains = entities.values - .map { it.domain } - .distinct() - .filter { it in SUPPORTED_DOMAINS } - .toSet() - var invalidate = newDomains.size != domains.size || newDomains != domains || !domainsAdded - domains.clear() - domains.addAll(newDomains) - domainsAdded = true + if (domainsJob?.isActive == true) domainsJob?.cancel() + domainsJob = launch { + allEntities.collect { entities -> + val newDomains = entities.values + .map { it.domain } + .distinct() + .filter { it in SUPPORTED_DOMAINS } + .toSet() + var invalidate = newDomains.size != domains.size || newDomains != domains || !domainsAdded + domains.clear() + domains.addAll(newDomains) + domainsAdded = true - val newFavorites = getFavoritesList(entities) - invalidate = invalidate || newFavorites.size != favoritesEntities.size || newFavorites.toSet() != favoritesEntities.toSet() - favoritesEntities = newFavorites + val newFavorites = getFavoritesList(entities) + invalidate = invalidate || newFavorites.size != favoritesEntities.size || newFavorites.toSet() != favoritesEntities.toSet() + favoritesEntities = newFavorites - if (invalidate) invalidate() + if (invalidate) invalidate() + } } } }