mirror of
https://github.com/bbara04/Purefin.git
synced 2026-07-24 03:36:51 +00:00
refactor: restructure SettingsRepository and implement SettingsRepositoryImpl
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package hu.bbara.purefin.data.settings
|
||||
|
||||
import android.content.Context
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.core.DataStoreFactory
|
||||
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler
|
||||
import androidx.datastore.dataStoreFile
|
||||
import dagger.Binds
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import hu.bbara.purefin.core.settings.SettingsRepository
|
||||
import hu.bbara.purefin.model.Settings
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
abstract class SettingsBindingsModule {
|
||||
|
||||
@Binds
|
||||
@Singleton
|
||||
abstract fun bindSettingsRepository(
|
||||
settingsRepositoryImpl: SettingsRepositoryImpl
|
||||
): SettingsRepository
|
||||
}
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
class SettingsModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideSettingsDataStore(
|
||||
@ApplicationContext context: Context
|
||||
): DataStore<Settings> {
|
||||
return DataStoreFactory.create(
|
||||
serializer = SettingsSerializer,
|
||||
produceFile = { context.dataStoreFile("settings.json") },
|
||||
corruptionHandler = ReplaceFileCorruptionHandler(
|
||||
produceNewData = { SettingsSerializer.defaultValue }
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package hu.bbara.purefin.data.settings
|
||||
|
||||
import androidx.datastore.core.CorruptionException
|
||||
import androidx.datastore.core.Serializer
|
||||
import hu.bbara.purefin.model.Settings
|
||||
import kotlinx.serialization.SerializationException
|
||||
import kotlinx.serialization.json.Json
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
|
||||
object SettingsSerializer : Serializer<Settings> {
|
||||
override val defaultValue: Settings
|
||||
get() = Settings()
|
||||
|
||||
override suspend fun readFrom(input: InputStream): Settings {
|
||||
try {
|
||||
return Json.decodeFromString<Settings>(
|
||||
input.readBytes().decodeToString()
|
||||
)
|
||||
} catch (serialization: SerializationException) {
|
||||
throw CorruptionException("Unable to read Settings", serialization)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun writeTo(t: Settings, output: OutputStream) {
|
||||
output.write(
|
||||
Json.encodeToString(Settings.serializer(), t)
|
||||
.encodeToByteArray()
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user