mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
feat: per device DB manager (#3641)
This commit is contained in:
parent
f0b9a0ff75
commit
cb8d1871c9
16 changed files with 643 additions and 86 deletions
|
|
@ -18,7 +18,16 @@
|
|||
package com.geeksville.mesh
|
||||
|
||||
import android.app.Application
|
||||
import dagger.hilt.EntryPoint
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.android.EntryPointAccessors
|
||||
import dagger.hilt.android.HiltAndroidApp
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import org.meshtastic.core.database.DatabaseManager
|
||||
import org.meshtastic.core.prefs.mesh.MeshPrefs
|
||||
import timber.log.Timber
|
||||
|
||||
/**
|
||||
|
|
@ -33,9 +42,22 @@ class MeshUtilApplication : Application() {
|
|||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
initializeMaps(this)
|
||||
// Initialize DatabaseManager asynchronously with current device address so DAO consumers have an active DB
|
||||
val entryPoint = EntryPointAccessors.fromApplication(this, AppEntryPoint::class.java)
|
||||
CoroutineScope(Dispatchers.Default).launch {
|
||||
entryPoint.databaseManager().init(entryPoint.meshPrefs().deviceAddress)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EntryPoint
|
||||
@InstallIn(SingletonComponent::class)
|
||||
interface AppEntryPoint {
|
||||
fun databaseManager(): DatabaseManager
|
||||
|
||||
fun meshPrefs(): MeshPrefs
|
||||
}
|
||||
|
||||
fun logAssert(executeReliableWrite: Boolean) {
|
||||
if (!executeReliableWrite) {
|
||||
val ex = AssertionError("Assertion failed")
|
||||
|
|
|
|||
|
|
@ -58,6 +58,7 @@ import org.meshtastic.core.data.repository.MeshLogRepository
|
|||
import org.meshtastic.core.data.repository.NodeRepository
|
||||
import org.meshtastic.core.data.repository.PacketRepository
|
||||
import org.meshtastic.core.data.repository.RadioConfigRepository
|
||||
import org.meshtastic.core.database.DatabaseManager
|
||||
import org.meshtastic.core.database.entity.MeshLog
|
||||
import org.meshtastic.core.database.entity.MetadataEntity
|
||||
import org.meshtastic.core.database.entity.MyNodeEntity
|
||||
|
|
@ -138,6 +139,8 @@ class MeshService : Service() {
|
|||
|
||||
@Inject lateinit var nodeRepository: NodeRepository
|
||||
|
||||
@Inject lateinit var databaseManager: DatabaseManager
|
||||
|
||||
@Inject lateinit var mqttRepository: MQTTRepository
|
||||
|
||||
@Inject lateinit var serviceNotifications: MeshServiceNotifications
|
||||
|
|
@ -2008,8 +2011,14 @@ class MeshService : Service() {
|
|||
"SetDeviceAddress: Device address changed from ${currentAddr.anonymize} to ${deviceAddr.anonymize}",
|
||||
)
|
||||
meshPrefs.deviceAddress = deviceAddr
|
||||
clearDatabases()
|
||||
clearNotifications()
|
||||
serviceScope.handledLaunch {
|
||||
// Clear only in-memory caches to avoid cross-device bleed
|
||||
discardNodeDB()
|
||||
// Switch active on-disk DB to device-specific database
|
||||
databaseManager.switchActiveDatabase(deviceAddr)
|
||||
// Do not clear packet DB here; messages are per-device and should persist
|
||||
clearNotifications()
|
||||
}
|
||||
} else {
|
||||
Timber.d("SetDeviceAddress: Device address is unchanged, ignoring.")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue