Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto: reload entity registry, show loading when changing servers #3769

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -50,7 +51,9 @@ class MainVehicleScreen(
private var favoritesList = emptyList<String>()
private var isLoggedIn: Boolean? = null
private val domains = mutableSetOf<String>()
private var domainsJob: Job? = null
private var domainsAdded = false
private var domainsAddedFor: Int? = null

private val isAutomotive get() = carContext.packageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)

Expand All @@ -69,23 +72,34 @@ 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
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

if (invalidate) invalidate()
val newFavorites = getFavoritesList(entities)
invalidate = invalidate || newFavorites.size != favoritesEntities.size || newFavorites.toSet() != favoritesEntities.toSet()
favoritesEntities = newFavorites

if (invalidate) invalidate()
}
}
}
}
}
Expand Down