mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
Uses Hilt to get the database initialization off of the main thread. The initial introduction always has a disproportionate fan-out of boilerplate. In this case, all entry points which were using UIViewModel needed to be annotated in order to let the code gen know that they needed to support it. The PacketRepository is injected into things via the main thread (e.g., the MeshService) but due to the lazy declaration, the database isn't hydrated until the DAO is access while on an IO thread.
29 lines
No EOL
791 B
Kotlin
29 lines
No EOL
791 B
Kotlin
package com.geeksville.mesh.database
|
|
|
|
import android.app.Application
|
|
import androidx.room.Room
|
|
import com.geeksville.mesh.database.dao.PacketDao
|
|
import dagger.Module
|
|
import dagger.Provides
|
|
import dagger.hilt.InstallIn
|
|
import dagger.hilt.components.SingletonComponent
|
|
|
|
@InstallIn(SingletonComponent::class)
|
|
@Module
|
|
class DatabaseModule {
|
|
@Provides
|
|
fun provideDatabase(application: Application): MeshtasticDatabase {
|
|
return Room.databaseBuilder(
|
|
application.applicationContext,
|
|
MeshtasticDatabase::class.java,
|
|
"meshtastic_database"
|
|
)
|
|
.fallbackToDestructiveMigration()
|
|
.build()
|
|
}
|
|
|
|
@Provides
|
|
fun providePacketDao(database: MeshtasticDatabase): PacketDao {
|
|
return database.packetDao()
|
|
}
|
|
} |