From d6dcb6e9c4f34bbd3f10fa18461144d3d27201f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laurent=20Tr=C3=A9guier?= Date: Wed, 31 Jul 2024 14:53:00 +0200 Subject: [PATCH] Abstract away storage --- .../fyreplace/data/ConnectionSerializer.kt | 11 ------- .../app/fyreplace/fyreplace/data/Module.kt | 14 ++++++++ .../fyreplace/fyreplace/data/StoreResolver.kt | 33 +++++++++++++++++++ .../settings/EnvironmentSelectorViewModel.kt | 12 +++---- 4 files changed, 53 insertions(+), 17 deletions(-) create mode 100644 app/src/main/kotlin/app/fyreplace/fyreplace/data/Module.kt create mode 100644 app/src/main/kotlin/app/fyreplace/fyreplace/data/StoreResolver.kt diff --git a/app/src/main/kotlin/app/fyreplace/fyreplace/data/ConnectionSerializer.kt b/app/src/main/kotlin/app/fyreplace/fyreplace/data/ConnectionSerializer.kt index 8a0ea80..1d7c59b 100644 --- a/app/src/main/kotlin/app/fyreplace/fyreplace/data/ConnectionSerializer.kt +++ b/app/src/main/kotlin/app/fyreplace/fyreplace/data/ConnectionSerializer.kt @@ -1,10 +1,7 @@ package app.fyreplace.fyreplace.data -import android.content.Context import androidx.datastore.core.CorruptionException import androidx.datastore.core.Serializer -import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler -import androidx.datastore.dataStore import app.fyreplace.fyreplace.BuildConfig import app.fyreplace.fyreplace.protos.Connection import java.io.IOException @@ -18,8 +15,6 @@ object ConnectionSerializer : Serializer { .setEnvironment(BuildConfig.ENVIRONMENT_DEFAULT) .build() - val corruptionHandler = ReplaceFileCorruptionHandler { defaultValue } - override suspend fun readFrom(input: InputStream): Connection { try { return Connection.parseFrom(input) @@ -30,9 +25,3 @@ object ConnectionSerializer : Serializer { override suspend fun writeTo(t: Connection, output: OutputStream) = t.writeTo(output) } - -val Context.connectionStore by dataStore( - fileName = "connection.pb", - serializer = ConnectionSerializer, - corruptionHandler = ConnectionSerializer.corruptionHandler -) diff --git a/app/src/main/kotlin/app/fyreplace/fyreplace/data/Module.kt b/app/src/main/kotlin/app/fyreplace/fyreplace/data/Module.kt new file mode 100644 index 0000000..cf8320d --- /dev/null +++ b/app/src/main/kotlin/app/fyreplace/fyreplace/data/Module.kt @@ -0,0 +1,14 @@ +package app.fyreplace.fyreplace.data + +import dagger.Binds +import dagger.Module +import dagger.hilt.InstallIn +import dagger.hilt.components.SingletonComponent + +@Suppress("unused") +@Module +@InstallIn(SingletonComponent::class) +interface Module { + @Binds + fun bindConnectionStoreResolver(resolver: StoreResolverImpl): StoreResolver +} diff --git a/app/src/main/kotlin/app/fyreplace/fyreplace/data/StoreResolver.kt b/app/src/main/kotlin/app/fyreplace/fyreplace/data/StoreResolver.kt new file mode 100644 index 0000000..c973b1e --- /dev/null +++ b/app/src/main/kotlin/app/fyreplace/fyreplace/data/StoreResolver.kt @@ -0,0 +1,33 @@ +package app.fyreplace.fyreplace.data + +import android.content.Context +import androidx.datastore.core.DataStore +import androidx.datastore.core.Serializer +import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler +import androidx.datastore.dataStore +import app.fyreplace.fyreplace.protos.Connection +import dagger.hilt.android.qualifiers.ApplicationContext +import kotlinx.coroutines.CoroutineScope +import javax.inject.Inject + +interface StoreResolver { + fun connectionStoreIn(scope: CoroutineScope): DataStore +} + +class StoreResolverImpl @Inject constructor( + @ApplicationContext private val context: Context +) : StoreResolver { + override fun connectionStoreIn(scope: CoroutineScope) = + StoreWrapper(context, ConnectionSerializer, scope).store + + class StoreWrapper(context: Context, serializer: Serializer, scope: CoroutineScope) { + private val Context.store by dataStore( + fileName = "connection.pb", + serializer = serializer, + corruptionHandler = ReplaceFileCorruptionHandler { serializer.defaultValue }, + scope = scope + ) + + val store = context.store + } +} diff --git a/app/src/main/kotlin/app/fyreplace/fyreplace/viewmodels/settings/EnvironmentSelectorViewModel.kt b/app/src/main/kotlin/app/fyreplace/fyreplace/viewmodels/settings/EnvironmentSelectorViewModel.kt index 7956731..4c2c5fa 100644 --- a/app/src/main/kotlin/app/fyreplace/fyreplace/viewmodels/settings/EnvironmentSelectorViewModel.kt +++ b/app/src/main/kotlin/app/fyreplace/fyreplace/viewmodels/settings/EnvironmentSelectorViewModel.kt @@ -1,27 +1,27 @@ package app.fyreplace.fyreplace.viewmodels.settings -import android.content.Context import androidx.lifecycle.viewModelScope -import app.fyreplace.fyreplace.data.connectionStore +import app.fyreplace.fyreplace.data.StoreResolver import app.fyreplace.fyreplace.protos.Environment import app.fyreplace.fyreplace.viewmodels.ViewModelBase import dagger.hilt.android.lifecycle.HiltViewModel -import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch import javax.inject.Inject @HiltViewModel class EnvironmentSelectorViewModel @Inject constructor( - @ApplicationContext private val context: Context + storeResolver: StoreResolver ) : ViewModelBase() { - val environment = context.connectionStore.data + private val connectionStore = storeResolver.connectionStoreIn(viewModelScope) + + val environment = connectionStore.data .map { it.environment } .asState(Environment.UNRECOGNIZED) fun updateEnvironment(environment: Environment) { viewModelScope.launch { - context.connectionStore.updateData { + connectionStore.updateData { it.toBuilder().setEnvironment(environment).build() } }