mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
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:
parent
df3b5365f9
commit
092238cb55
12 changed files with 86 additions and 17 deletions
|
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* Copyright (c) 2026 Meshtastic LLC
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.meshtastic.core.common.di
|
||||
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import org.koin.core.annotation.Single
|
||||
import org.meshtastic.core.common.util.ioDispatcher
|
||||
|
||||
/**
|
||||
* A process-wide [CoroutineScope] that outlives individual ViewModels and UI components.
|
||||
*
|
||||
* Use this scope for fire-and-forget cleanup work that must continue after a ViewModel's own scope has been cancelled
|
||||
* (for example, deleting temporary files in `onCleared()`). Backed by a [SupervisorJob] so failures in one child do not
|
||||
* cancel siblings, and by [ioDispatcher] so work runs off the main thread.
|
||||
*
|
||||
* Prefer scoping work to a more specific scope (like `viewModelScope`) whenever possible; this scope is an escape hatch
|
||||
* and should be used sparingly.
|
||||
*/
|
||||
interface ApplicationCoroutineScope : CoroutineScope
|
||||
|
||||
@Single(binds = [ApplicationCoroutineScope::class])
|
||||
internal class ApplicationCoroutineScopeImpl : ApplicationCoroutineScope {
|
||||
override val coroutineContext = SupervisorJob() + ioDispatcher
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue