DI: Optimize AppModule
This commit is contained in:
parent
d60bba9b8f
commit
aedf442632
|
|
@ -4,10 +4,12 @@ import android.content.Context
|
|||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
import javax.inject.Singleton
|
||||
|
||||
/**
|
||||
* Main database for the application.
|
||||
*/
|
||||
@Singleton
|
||||
@Database(entities = [SystemPromptEntity::class], version = 1, exportSchema = false)
|
||||
abstract class AppDatabase : RoomDatabase() {
|
||||
|
||||
|
|
|
|||
|
|
@ -7,13 +7,19 @@ import androidx.datastore.preferences.core.booleanPreferencesKey
|
|||
import androidx.datastore.preferences.core.edit
|
||||
import androidx.datastore.preferences.core.longPreferencesKey
|
||||
import androidx.datastore.preferences.preferencesDataStore
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
/**
|
||||
* Manages user preferences for the application.
|
||||
*/
|
||||
class UserPreferences(private val context: Context) {
|
||||
@Singleton
|
||||
class UserPreferences @Inject constructor (
|
||||
@ApplicationContext private val context: Context
|
||||
) {
|
||||
|
||||
companion object {
|
||||
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")
|
||||
|
|
|
|||
|
|
@ -1,20 +1,22 @@
|
|||
package com.example.llama.revamp.data.repository
|
||||
|
||||
import android.content.Context
|
||||
import com.example.llama.revamp.data.local.AppDatabase
|
||||
import com.example.llama.revamp.data.local.SystemPromptDao
|
||||
import com.example.llama.revamp.data.local.SystemPromptEntity
|
||||
import com.example.llama.revamp.data.model.SystemPrompt
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.map
|
||||
import java.util.UUID
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
/**
|
||||
* Repository for managing system prompts.
|
||||
*/
|
||||
class SystemPromptRepository(context: Context) {
|
||||
|
||||
private val systemPromptDao = AppDatabase.getDatabase(context).systemPromptDao()
|
||||
@Singleton
|
||||
class SystemPromptRepository @Inject constructor(
|
||||
private val systemPromptDao: SystemPromptDao,
|
||||
) {
|
||||
|
||||
/**
|
||||
* Get all preset prompts.
|
||||
|
|
|
|||
|
|
@ -1,8 +1,7 @@
|
|||
package com.example.llama.revamp.di
|
||||
|
||||
import android.content.Context
|
||||
import com.example.llama.revamp.data.preferences.UserPreferences
|
||||
import com.example.llama.revamp.data.repository.SystemPromptRepository
|
||||
import com.example.llama.revamp.data.local.AppDatabase
|
||||
import com.example.llama.revamp.engine.InferenceEngine
|
||||
import com.example.llama.revamp.monitoring.PerformanceMonitor
|
||||
import dagger.Module
|
||||
|
|
@ -15,6 +14,7 @@ import javax.inject.Singleton
|
|||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
object AppModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideInferenceEngine() = InferenceEngine()
|
||||
|
|
@ -24,10 +24,8 @@ object AppModule {
|
|||
fun providePerformanceMonitor(@ApplicationContext context: Context) = PerformanceMonitor(context)
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideUserPreferences(@ApplicationContext context: Context) = UserPreferences(context)
|
||||
fun provideAppDatabase(@ApplicationContext context: Context) = AppDatabase.getDatabase(context)
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideSystemPromptRepository(@ApplicationContext context: Context) = SystemPromptRepository(context)
|
||||
fun providesSystemPromptDao(appDatabase: AppDatabase) = appDatabase.systemPromptDao()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue