feat(desktop): implement DI auto-wiring and validation (#4782)

Signed-off-by: James Rich <2199651+jamesarich@users.noreply.github.com>
This commit is contained in:
James Rich 2026-03-13 13:08:55 -05:00 committed by GitHub
parent 8bb1e86511
commit f45993ede2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 160 additions and 28 deletions

View file

@ -44,11 +44,14 @@ import org.meshtastic.core.repository.ServiceRepository
import org.meshtastic.desktop.radio.DesktopMeshServiceController
import org.meshtastic.desktop.radio.DesktopRadioInterfaceService
import org.meshtastic.desktop.stub.NoopAppWidgetUpdater
import org.meshtastic.desktop.stub.NoopCompassHeadingProvider
import org.meshtastic.desktop.stub.NoopLocationRepository
import org.meshtastic.desktop.stub.NoopMQTTRepository
import org.meshtastic.desktop.stub.NoopMagneticFieldProvider
import org.meshtastic.desktop.stub.NoopMeshLocationManager
import org.meshtastic.desktop.stub.NoopMeshServiceNotifications
import org.meshtastic.desktop.stub.NoopMeshWorkerManager
import org.meshtastic.desktop.stub.NoopPhoneLocationProvider
import org.meshtastic.desktop.stub.NoopPlatformAnalytics
import org.meshtastic.desktop.stub.NoopServiceBroadcasts
import org.meshtastic.core.common.di.module as coreCommonModule
@ -71,7 +74,7 @@ import org.meshtastic.feature.settings.di.module as featureSettingsModule
/**
* Koin module for the Desktop target.
*
* Includes the generated KSP modules from core KMP libraries (which provide real implementations of prefs, data
* Includes the generated Koin K2 modules from core KMP libraries (which provide real implementations of prefs, data
* repositories, managers, datastore data sources, use cases, and ViewModels from `commonMain`).
*
* Only truly platform-specific interfaces are stubbed here things that require Android APIs (BLE/USB transport,
@ -80,7 +83,7 @@ import org.meshtastic.feature.settings.di.module as featureSettingsModule
* Platform infrastructure (DataStores, Room database, Lifecycle) is provided by [desktopPlatformModule].
*/
fun desktopModule() = module {
// Include generated KSP modules from core KMP libraries (commonMain implementations)
// Include generated Koin K2 modules from core KMP libraries (commonMain implementations)
includes(
org.meshtastic.core.di.di.CoreDiModule().coreDiModule(),
org.meshtastic.core.common.di.CoreCommonModule().coreCommonModule(),
@ -131,6 +134,9 @@ private fun desktopPlatformStubsModule() = module {
single<MeshLocationManager> { NoopMeshLocationManager() }
single<LocationRepository> { NoopLocationRepository() }
single<MQTTRepository> { NoopMQTTRepository() }
single<org.meshtastic.feature.node.compass.CompassHeadingProvider> { NoopCompassHeadingProvider() }
single<org.meshtastic.feature.node.compass.PhoneLocationProvider> { NoopPhoneLocationProvider() }
single<org.meshtastic.feature.node.compass.MagneticFieldProvider> { NoopMagneticFieldProvider() }
// Desktop mesh service controller — replaces Android's MeshService lifecycle
single {

View file

@ -0,0 +1,38 @@
/*
* 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.desktop.stub
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
import org.meshtastic.feature.node.compass.CompassHeadingProvider
import org.meshtastic.feature.node.compass.HeadingState
import org.meshtastic.feature.node.compass.MagneticFieldProvider
import org.meshtastic.feature.node.compass.PhoneLocationProvider
import org.meshtastic.feature.node.compass.PhoneLocationState
class NoopCompassHeadingProvider : CompassHeadingProvider {
override fun headingUpdates(): Flow<HeadingState> = flowOf(HeadingState(hasSensor = false))
}
class NoopPhoneLocationProvider : PhoneLocationProvider {
override fun locationUpdates(): Flow<PhoneLocationState> =
flowOf(PhoneLocationState(permissionGranted = false, providerEnabled = false))
}
class NoopMagneticFieldProvider : MagneticFieldProvider {
override fun getDeclination(latitude: Double, longitude: Double, altitude: Double, timeMillis: Long): Float = 0f
}

View file

@ -52,7 +52,7 @@ import org.meshtastic.proto.Position as ProtoPosition
*
* These stubs exist ONLY for interfaces that have no `commonMain` implementation and require Android-specific APIs
* (BLE/USB transport, notifications, WorkManager, location services, broadcasts, widgets). All other interfaces use
* real `commonMain` implementations wired through the generated KSP Koin modules.
* real `commonMain` implementations wired through the generated Koin K2 modules.
*
* As real desktop implementations become available (e.g., serial transport, TCP transport), they replace individual
* stubs in [desktopModule].

View file

@ -0,0 +1,47 @@
/*
* 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.desktop.di
import androidx.lifecycle.SavedStateHandle
import io.ktor.client.HttpClient
import io.ktor.client.engine.HttpClientEngine
import kotlinx.coroutines.CoroutineDispatcher
import org.koin.core.annotation.KoinExperimentalAPI
import org.koin.dsl.module
import org.koin.test.verify.verify
import kotlin.test.Test
@OptIn(KoinExperimentalAPI::class)
class DesktopKoinTest {
@Test
fun `verify desktop koin modules`() {
// This test validates the full Koin DI graph for the Desktop target.
// It includes the main desktopModule (repositories, use cases, ViewModels, stubs)
// and the desktopPlatformModule (DataStores, Room database, lifecycle).
module { includes(desktopModule(), desktopPlatformModule()) }
.verify(
extraTypes =
listOf(
SavedStateHandle::class,
CoroutineDispatcher::class,
HttpClient::class,
HttpClientEngine::class,
),
)
}
}