mirror of
https://github.com/bbara04/Purefin.git
synced 2026-07-23 19:26:50 +00:00
refactor: restructure SettingsRepository and implement SettingsRepositoryImpl
This commit is contained in:
@@ -1,56 +1,20 @@
|
|||||||
package hu.bbara.purefin.core.settings
|
package hu.bbara.purefin.core.settings
|
||||||
|
|
||||||
import androidx.datastore.core.DataStore
|
|
||||||
import hu.bbara.purefin.model.Settings
|
import hu.bbara.purefin.model.Settings
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
|
||||||
import kotlinx.coroutines.flow.map
|
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
class SettingsRepository @Inject constructor(
|
interface SettingsRepository {
|
||||||
private val settingsDataStore: DataStore<Settings>
|
val settings: Flow<Settings>
|
||||||
) {
|
|
||||||
val settings: Flow<Settings> = settingsDataStore.data
|
|
||||||
|
|
||||||
fun value(option: NumberSetting): Flow<Double> {
|
fun value(option: NumberSetting): Flow<Double>
|
||||||
return settings
|
|
||||||
.map { it.numberSettings[option.key] ?: option.defaultValue }
|
|
||||||
.distinctUntilChanged()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun value(option: BooleanSetting): Flow<Boolean> {
|
fun value(option: BooleanSetting): Flow<Boolean>
|
||||||
return settings
|
|
||||||
.map { it.booleanSettings[option.key] ?: option.defaultValue }
|
|
||||||
.distinctUntilChanged()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun value(option: StringSetting): Flow<String> {
|
fun value(option: StringSetting): Flow<String>
|
||||||
return settings
|
|
||||||
.map { it.stringSettings[option.key] ?: option.defaultValue }
|
|
||||||
.distinctUntilChanged()
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun set(option: NumberSetting, value: Double) {
|
suspend fun set(option: NumberSetting, value: Double)
|
||||||
settingsDataStore.updateData { current ->
|
|
||||||
current.copy(
|
|
||||||
numberSettings = current.numberSettings + (option.key to value)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun set(option: BooleanSetting, value: Boolean) {
|
suspend fun set(option: BooleanSetting, value: Boolean)
|
||||||
settingsDataStore.updateData { current ->
|
|
||||||
current.copy(
|
|
||||||
booleanSettings = current.booleanSettings + (option.key to value)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun set(option: StringSetting, value: String) {
|
suspend fun set(option: StringSetting, value: String)
|
||||||
settingsDataStore.updateData { current ->
|
|
||||||
current.copy(
|
|
||||||
stringSettings = current.stringSettings + (option.key to value)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,31 @@
|
|||||||
package hu.bbara.purefin.core.settings
|
package hu.bbara.purefin.data.settings
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import androidx.datastore.core.DataStore
|
import androidx.datastore.core.DataStore
|
||||||
import androidx.datastore.core.DataStoreFactory
|
import androidx.datastore.core.DataStoreFactory
|
||||||
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler
|
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler
|
||||||
import androidx.datastore.dataStoreFile
|
import androidx.datastore.dataStoreFile
|
||||||
|
import dagger.Binds
|
||||||
import dagger.Module
|
import dagger.Module
|
||||||
import dagger.Provides
|
import dagger.Provides
|
||||||
import dagger.hilt.InstallIn
|
import dagger.hilt.InstallIn
|
||||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||||
import dagger.hilt.components.SingletonComponent
|
import dagger.hilt.components.SingletonComponent
|
||||||
|
import hu.bbara.purefin.core.settings.SettingsRepository
|
||||||
import hu.bbara.purefin.model.Settings
|
import hu.bbara.purefin.model.Settings
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
@Module
|
||||||
|
@InstallIn(SingletonComponent::class)
|
||||||
|
abstract class SettingsBindingsModule {
|
||||||
|
|
||||||
|
@Binds
|
||||||
|
@Singleton
|
||||||
|
abstract fun bindSettingsRepository(
|
||||||
|
settingsRepositoryImpl: SettingsRepositoryImpl
|
||||||
|
): SettingsRepository
|
||||||
|
}
|
||||||
|
|
||||||
@Module
|
@Module
|
||||||
@InstallIn(SingletonComponent::class)
|
@InstallIn(SingletonComponent::class)
|
||||||
class SettingsModule {
|
class SettingsModule {
|
||||||
@@ -30,12 +43,4 @@ class SettingsModule {
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
|
||||||
@Singleton
|
|
||||||
fun provideSettingsRepository(
|
|
||||||
settingsDataStore: DataStore<Settings>
|
|
||||||
): SettingsRepository {
|
|
||||||
return SettingsRepository(settingsDataStore)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
package hu.bbara.purefin.data.settings
|
||||||
|
|
||||||
|
import androidx.datastore.core.DataStore
|
||||||
|
import hu.bbara.purefin.core.settings.BooleanSetting
|
||||||
|
import hu.bbara.purefin.core.settings.NumberSetting
|
||||||
|
import hu.bbara.purefin.core.settings.SettingsRepository
|
||||||
|
import hu.bbara.purefin.core.settings.StringSetting
|
||||||
|
import hu.bbara.purefin.model.Settings
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
class SettingsRepositoryImpl @Inject constructor(
|
||||||
|
private val settingsDataStore: DataStore<Settings>
|
||||||
|
) : SettingsRepository {
|
||||||
|
|
||||||
|
override val settings: Flow<Settings> = settingsDataStore.data
|
||||||
|
|
||||||
|
override fun value(option: NumberSetting): Flow<Double> {
|
||||||
|
return settings
|
||||||
|
.map { it.numberSettings[option.key] ?: option.defaultValue }
|
||||||
|
.distinctUntilChanged()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun value(option: BooleanSetting): Flow<Boolean> {
|
||||||
|
return settings
|
||||||
|
.map { it.booleanSettings[option.key] ?: option.defaultValue }
|
||||||
|
.distinctUntilChanged()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun value(option: StringSetting): Flow<String> {
|
||||||
|
return settings
|
||||||
|
.map { it.stringSettings[option.key] ?: option.defaultValue }
|
||||||
|
.distinctUntilChanged()
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun set(option: NumberSetting, value: Double) {
|
||||||
|
settingsDataStore.updateData { current ->
|
||||||
|
current.copy(
|
||||||
|
numberSettings = current.numberSettings + (option.key to value)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun set(option: BooleanSetting, value: Boolean) {
|
||||||
|
settingsDataStore.updateData { current ->
|
||||||
|
current.copy(
|
||||||
|
booleanSettings = current.booleanSettings + (option.key to value)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun set(option: StringSetting, value: String) {
|
||||||
|
settingsDataStore.updateData { current ->
|
||||||
|
current.copy(
|
||||||
|
stringSettings = current.stringSettings + (option.key to value)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package hu.bbara.purefin.core.settings
|
package hu.bbara.purefin.data.settings
|
||||||
|
|
||||||
import androidx.datastore.core.CorruptionException
|
import androidx.datastore.core.CorruptionException
|
||||||
import androidx.datastore.core.Serializer
|
import androidx.datastore.core.Serializer
|
||||||
Reference in New Issue
Block a user