A rootless Android app that boots Alpine Linux: run containers (Podman/Docker/LXC) and GUI desktop apps.
fix(ui): move blocking reads off the main thread
ExTv63979dce9f6d
diff --git a/app/src/main/java/com/excp/podroid/ui/screens/backup/ContainerBackupViewModel.kt b/app/src/main/java/com/excp/podroid/ui/screens/backup/ContainerBackupViewModel.ktindex dbd93cd..61f1d9d 100644--- a/app/src/main/java/com/excp/podroid/ui/screens/backup/ContainerBackupViewModel.kt+++ b/app/src/main/java/com/excp/podroid/ui/screens/backup/ContainerBackupViewModel.kt@@ -12,11 +12,14 @@ import com.excp.podroid.engine.VmEngineimport com.excp.podroid.engine.VmStateimport dagger.hilt.android.lifecycle.HiltViewModelimport dagger.hilt.android.qualifiers.ApplicationContext+import kotlinx.coroutines.Dispatchersimport kotlinx.coroutines.flow.MutableStateFlowimport kotlinx.coroutines.flow.SharingStartedimport kotlinx.coroutines.flow.StateFlowimport kotlinx.coroutines.flow.combineimport kotlinx.coroutines.flow.stateIn+import kotlinx.coroutines.launch+import kotlinx.coroutines.withContextimport javax.inject.Injectdata class ContainerBackupUiState(@@ -24,6 +27,7 @@ data class ContainerBackupUiState(val storageAccessEnabled: Boolean = false,val guestPath: String = "/var/backups/podroid",val backupFiles: List<ContainerBackupFile> = emptyList(),+ val backupListError: Boolean = false,val containerName: String = "",val imageRef: String = "",)@@ -39,22 +43,28 @@ class ContainerBackupViewModel @Inject constructor(private val _containerName = MutableStateFlow("")private val _imageRef = MutableStateFlow("")private val _backupFiles = MutableStateFlow<List<ContainerBackupFile>>(emptyList())+ private val _backupListError = MutableStateFlow(false)val uiState: StateFlow<ContainerBackupUiState> = combine(- engine.state,- settingsRepository.storageAccessEnabled,- _containerName,- _imageRef,- _backupFiles,- ) { vmState, storageAccess, container, image, files ->- ContainerBackupUiState(- vmRunning = vmState is VmState.Running,- storageAccessEnabled = storageAccess,- guestPath = repository.guestBackupPathLabel(storageAccess),- backupFiles = files,- containerName = container,- imageRef = image,- )+ combine(+ engine.state,+ settingsRepository.storageAccessEnabled,+ _containerName,+ _imageRef,+ _backupFiles,+ ) { vmState, storageAccess, container, image, files ->+ ContainerBackupUiState(+ vmRunning = vmState is VmState.Running,+ storageAccessEnabled = storageAccess,+ guestPath = repository.guestBackupPathLabel(storageAccess),+ backupFiles = files,+ containerName = container,+ imageRef = image,+ )+ },+ _backupListError,+ ) { state, listError ->+ state.copy(backupListError = listError)}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), ContainerBackupUiState())init {@@ -62,7 +72,17 @@ class ContainerBackupViewModel @Inject constructor(}fun refresh() {- _backupFiles.value = repository.listBackupFiles()+ viewModelScope.launch {+ val result = withContext(Dispatchers.IO) {+ runCatching { repository.listBackupFiles() }+ }+ result.onSuccess { files ->+ _backupFiles.value = files+ _backupListError.value = false+ }.onFailure {+ _backupListError.value = true+ }+ }}fun setContainerName(value: String) {diff --git a/app/src/main/java/com/excp/podroid/ui/screens/status/StatusViewModel.kt b/app/src/main/java/com/excp/podroid/ui/screens/status/StatusViewModel.ktindex 14bf0b2..11d088a 100644--- a/app/src/main/java/com/excp/podroid/ui/screens/status/StatusViewModel.kt+++ b/app/src/main/java/com/excp/podroid/ui/screens/status/StatusViewModel.kt@@ -15,6 +15,7 @@ import com.excp.podroid.util.UptimeFormatterimport com.excp.podroid.util.VmLoadSamplerimport dagger.hilt.android.lifecycle.HiltViewModelimport dagger.hilt.android.qualifiers.ApplicationContext+import kotlinx.coroutines.Dispatchersimport kotlinx.coroutines.delayimport kotlinx.coroutines.flow.MutableStateFlowimport kotlinx.coroutines.flow.SharingStarted@@ -23,6 +24,7 @@ import kotlinx.coroutines.flow.combineimport kotlinx.coroutines.flow.stateInimport kotlinx.coroutines.isActiveimport kotlinx.coroutines.launch+import kotlinx.coroutines.withContextimport java.io.Fileimport javax.inject.Inject@@ -102,13 +104,18 @@ class StatusViewModel @Inject constructor() { a, b, c, backendId ->val vmState = a[0] as VmStateval tick = b[4] as Long+ // NetworkUtils enumerates NetworkInterface, which is blocking I/O; keep+ // it off the main thread that this combine transform otherwise runs on.+ val (phoneIp, phoneAddresses) = withContext(Dispatchers.IO) {+ NetworkUtils.localIpv4(context) to NetworkUtils.allLocalIpv4(context)+ }StatusUiState(vmState = vmState,backendId = backendId,engineSelection = b[1] as EngineSelection,uptimeLabel = uptimeLabel(vmState, tick),- phoneIp = NetworkUtils.localIpv4(context),- phoneAddresses = NetworkUtils.allLocalIpv4(context),+ phoneIp = phoneIp,+ phoneAddresses = phoneAddresses,vmRamMb = a[1] as Int,vmCpus = a[2] as Int,storageSizeGb = a[3] as Int,@@ -127,8 +134,10 @@ class StatusViewModel @Inject constructor(viewModelScope.launch {while (isActive) {delay(2_000)- refreshMetrics()- sampleVmLoad()+ withContext(Dispatchers.IO) {+ refreshMetrics()+ sampleVmLoad()+ }if (engine.state.value is VmState.Running) {_uptimeTick.value = System.currentTimeMillis()}