Skip to content

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.kt
index 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.VmEngine
import com.excp.podroid.engine.VmState
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
+import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.stateIn
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
import javax.inject.Inject
data 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.kt
index 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.UptimeFormatter
import com.excp.podroid.util.VmLoadSampler
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
+import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
@@ -23,6 +24,7 @@ import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
import java.io.File
import javax.inject.Inject
@@ -102,13 +104,18 @@ class StatusViewModel @Inject constructor(
) { a, b, c, backendId ->
val vmState = a[0] as VmState
val 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()
}