DI: Optimize AppModule

This commit is contained in:
Han Yin 2025-04-14 12:53:24 -07:00
parent d60bba9b8f
commit aedf442632
4 changed files with 20 additions and 12 deletions

View File

@ -4,10 +4,12 @@ import android.content.Context
import androidx.room.Database import androidx.room.Database
import androidx.room.Room import androidx.room.Room
import androidx.room.RoomDatabase import androidx.room.RoomDatabase
import javax.inject.Singleton
/** /**
* Main database for the application. * Main database for the application.
*/ */
@Singleton
@Database(entities = [SystemPromptEntity::class], version = 1, exportSchema = false) @Database(entities = [SystemPromptEntity::class], version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase() { abstract class AppDatabase : RoomDatabase() {

View File

@ -7,13 +7,19 @@ import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.longPreferencesKey import androidx.datastore.preferences.core.longPreferencesKey
import androidx.datastore.preferences.preferencesDataStore import androidx.datastore.preferences.preferencesDataStore
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import javax.inject.Inject
import javax.inject.Singleton
/** /**
* Manages user preferences for the application. * Manages user preferences for the application.
*/ */
class UserPreferences(private val context: Context) { @Singleton
class UserPreferences @Inject constructor (
@ApplicationContext private val context: Context
) {
companion object { companion object {
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings") private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")

View File

@ -1,20 +1,22 @@
package com.example.llama.revamp.data.repository package com.example.llama.revamp.data.repository
import android.content.Context import com.example.llama.revamp.data.local.SystemPromptDao
import com.example.llama.revamp.data.local.AppDatabase
import com.example.llama.revamp.data.local.SystemPromptEntity import com.example.llama.revamp.data.local.SystemPromptEntity
import com.example.llama.revamp.data.model.SystemPrompt import com.example.llama.revamp.data.model.SystemPrompt
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import java.util.UUID import java.util.UUID
import javax.inject.Inject
import javax.inject.Singleton
/** /**
* Repository for managing system prompts. * Repository for managing system prompts.
*/ */
class SystemPromptRepository(context: Context) { @Singleton
class SystemPromptRepository @Inject constructor(
private val systemPromptDao = AppDatabase.getDatabase(context).systemPromptDao() private val systemPromptDao: SystemPromptDao,
) {
/** /**
* Get all preset prompts. * Get all preset prompts.

View File

@ -1,8 +1,7 @@
package com.example.llama.revamp.di package com.example.llama.revamp.di
import android.content.Context import android.content.Context
import com.example.llama.revamp.data.preferences.UserPreferences import com.example.llama.revamp.data.local.AppDatabase
import com.example.llama.revamp.data.repository.SystemPromptRepository
import com.example.llama.revamp.engine.InferenceEngine import com.example.llama.revamp.engine.InferenceEngine
import com.example.llama.revamp.monitoring.PerformanceMonitor import com.example.llama.revamp.monitoring.PerformanceMonitor
import dagger.Module import dagger.Module
@ -15,6 +14,7 @@ import javax.inject.Singleton
@Module @Module
@InstallIn(SingletonComponent::class) @InstallIn(SingletonComponent::class)
object AppModule { object AppModule {
@Provides @Provides
@Singleton @Singleton
fun provideInferenceEngine() = InferenceEngine() fun provideInferenceEngine() = InferenceEngine()
@ -24,10 +24,8 @@ object AppModule {
fun providePerformanceMonitor(@ApplicationContext context: Context) = PerformanceMonitor(context) fun providePerformanceMonitor(@ApplicationContext context: Context) = PerformanceMonitor(context)
@Provides @Provides
@Singleton fun provideAppDatabase(@ApplicationContext context: Context) = AppDatabase.getDatabase(context)
fun provideUserPreferences(@ApplicationContext context: Context) = UserPreferences(context)
@Provides @Provides
@Singleton fun providesSystemPromptDao(appDatabase: AppDatabase) = appDatabase.systemPromptDao()
fun provideSystemPromptRepository(@ApplicationContext context: Context) = SystemPromptRepository(context)
} }