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

Switch from viewModelScope to lifecycleScope for external collections #3045

Merged
merged 5 commits into from
Nov 12, 2022
Merged
Show file tree
Hide file tree
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 @@ -6,12 +6,16 @@ import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.home.views.LoadHomePage
import io.homeassistant.companion.android.onboarding.OnboardingActivity
import io.homeassistant.companion.android.onboarding.integration.MobileAppIntegrationActivity
import io.homeassistant.companion.android.sensors.SensorReceiver
import io.homeassistant.companion.android.sensors.SensorWorker
import kotlinx.coroutines.launch
import javax.inject.Inject

@AndroidEntryPoint
Expand Down Expand Up @@ -41,6 +45,15 @@ class HomeActivity : ComponentActivity(), HomeView {
}

mainViewModel.init(presenter)

lifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.RESUMED) {
launch { mainViewModel.entityUpdates() }
launch { mainViewModel.areaUpdates() }
launch { mainViewModel.deviceUpdates() }
launch { mainViewModel.entityRegistryUpdates() }
}
}
}

override fun onResume() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,45 +154,55 @@ class MainViewModel @Inject constructor(
} else {
LoadingState.ERROR
}

// Listen for updates
viewModelScope.launch {
homePresenter.getEntityUpdates()?.collect {
if (supportedDomains().contains(it.domain)) {
entities[it.entityId] = it
updateEntityDomains()
}
}
}
viewModelScope.launch {
homePresenter.getAreaRegistryUpdates()?.collect {
areaRegistry = homePresenter.getAreaRegistry()
areas.clear()
areaRegistry?.let {
areas.addAll(it)
}
updateEntityDomains()
}
}
viewModelScope.launch {
homePresenter.getDeviceRegistryUpdates()?.collect {
deviceRegistry = homePresenter.getDeviceRegistry()
updateEntityDomains()
}
}
viewModelScope.launch {
homePresenter.getEntityRegistryUpdates()?.collect {
entityRegistry = homePresenter.getEntityRegistry()
updateEntityDomains()
}
}
} catch (e: Exception) {
Log.e(TAG, "Exception while loading entities", e)
loadingState.value = LoadingState.ERROR
}
}
}

suspend fun entityUpdates() {
if (!homePresenter.isConnected())
return
homePresenter.getEntityUpdates()?.collect {
if (supportedDomains().contains(it.domain)) {
entities[it.entityId] = it
updateEntityDomains()
}
}
}

suspend fun areaUpdates() {
if (!homePresenter.isConnected())
return
homePresenter.getAreaRegistryUpdates()?.collect {
areaRegistry = homePresenter.getAreaRegistry()
areas.clear()
areaRegistry?.let {
areas.addAll(it)
}
updateEntityDomains()
}
}

suspend fun deviceUpdates() {
if (!homePresenter.isConnected())
return
homePresenter.getDeviceRegistryUpdates()?.collect {
deviceRegistry = homePresenter.getDeviceRegistry()
updateEntityDomains()
}
}

suspend fun entityRegistryUpdates() {
if (!homePresenter.isConnected())
return
homePresenter.getEntityRegistryUpdates()?.collect {
entityRegistry = homePresenter.getEntityRegistry()
updateEntityDomains()
}
}

fun updateEntityDomains() {
val entitiesList = entities.values.toList().sortedBy { it.entityId }
val areasList = areaRegistry.orEmpty().sortedBy { it.name }
Expand Down