Skip to content

Commit

Permalink
Switch from viewModelScope to lifecycleScope for external collect…
Browse files Browse the repository at this point in the history
…ions (#3045)

* Switch from viewmodelscope to lifecyclescope for external collections

* Check loading state before collecting

* Revert previous changes for checking loading state

* Skip collection if not connected

* Simplify launching collections
  • Loading branch information
dshokouhi authored Nov 12, 2022
1 parent 5bac66c commit 33d1e30
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ 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.SensorWorker
import kotlinx.coroutines.launch
import javax.inject.Inject

@AndroidEntryPoint
Expand Down Expand Up @@ -40,6 +44,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 @@ -167,45 +167,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

0 comments on commit 33d1e30

Please sign in to comment.