refactor: use injected ioDispatcher and ApplicationCoroutineScope

Replaces two coroutine anti-patterns flagged by the architecture audit:

* FirmwareUpdateViewModel.onCleared() was using GlobalScope.launch to
  run cleanup work that must outlive the ViewModel scope. Introduce a
  new ApplicationCoroutineScope marker interface + Koin @Single binding
  in core/common, backed by SupervisorJob() + ioDispatcher, and inject
  it into the VM. NonCancellable is preserved so shutdown work still
  cannot be interrupted. Verified end-to-end by the KoinVerificationTest
  full-graph check.
* Six sites in feature/settings (android+jvm LogExporter/PrefExporter)
  and core/ui (android+jvm PlatformUtils) were hardcoding Dispatchers.IO.
  Switch to the project's injectable ioDispatcher property so tests
  can substitute a test dispatcher without reflection.

Dispatchers.Main sites in LogExporter are intentionally left alone —
they are fully test-swappable via Dispatchers.setMain() and there is
no existing mainDispatcher property to inject.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
James Rich 2026-04-17 10:46:48 -05:00
parent df3b5365f9
commit 092238cb55
12 changed files with 86 additions and 17 deletions

View file

@ -37,12 +37,12 @@ import androidx.lifecycle.compose.LifecycleEventEffect
import co.touchlab.kermit.Logger
import com.eygraber.uri.toAndroidUri
import com.eygraber.uri.toKmpUri
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.jetbrains.compose.resources.StringResource
import org.jetbrains.compose.resources.getString
import org.meshtastic.core.common.gpsDisabled
import org.meshtastic.core.common.util.CommonUri
import org.meshtastic.core.common.util.ioDispatcher
import java.net.URLEncoder
@Composable
@ -146,7 +146,7 @@ actual fun rememberReadTextFromUri(): suspend (uri: CommonUri, maxChars: Int) ->
val context = LocalContext.current
return remember(context) {
{ uri, maxChars ->
withContext(Dispatchers.IO) {
withContext(ioDispatcher) {
@Suppress("TooGenericExceptionCaught")
try {
val androidUri = uri.toAndroidUri()

View file

@ -20,10 +20,10 @@ package org.meshtastic.core.ui.util
import androidx.compose.runtime.Composable
import co.touchlab.kermit.Logger
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.jetbrains.compose.resources.StringResource
import org.meshtastic.core.common.util.CommonUri
import org.meshtastic.core.common.util.ioDispatcher
import java.awt.Desktop
import java.awt.FileDialog
import java.awt.Frame
@ -89,7 +89,7 @@ actual fun rememberOpenFileLauncher(onUriReceived: (CommonUri?) -> Unit): (mimeT
/** JVM — Reads text from a file URI. */
@Composable
actual fun rememberReadTextFromUri(): suspend (uri: CommonUri, maxChars: Int) -> String? = { uri, maxChars ->
withContext(Dispatchers.IO) {
withContext(ioDispatcher) {
@Suppress("TooGenericExceptionCaught")
try {
val file = File(URI(uri.toString()))