Skip to content

Commit

Permalink
Abstract away storage
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentTreguier committed Jul 31, 2024
1 parent 04c1193 commit d6dcb6e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,8 +15,6 @@ object ConnectionSerializer : Serializer<Connection> {
.setEnvironment(BuildConfig.ENVIRONMENT_DEFAULT)
.build()

val corruptionHandler = ReplaceFileCorruptionHandler { defaultValue }

override suspend fun readFrom(input: InputStream): Connection {
try {
return Connection.parseFrom(input)
Expand All @@ -30,9 +25,3 @@ object ConnectionSerializer : Serializer<Connection> {

override suspend fun writeTo(t: Connection, output: OutputStream) = t.writeTo(output)
}

val Context.connectionStore by dataStore(
fileName = "connection.pb",
serializer = ConnectionSerializer,
corruptionHandler = ConnectionSerializer.corruptionHandler
)
14 changes: 14 additions & 0 deletions app/src/main/kotlin/app/fyreplace/fyreplace/data/Module.kt
Original file line number Diff line number Diff line change
@@ -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
}
33 changes: 33 additions & 0 deletions app/src/main/kotlin/app/fyreplace/fyreplace/data/StoreResolver.kt
Original file line number Diff line number Diff line change
@@ -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<Connection>
}

class StoreResolverImpl @Inject constructor(
@ApplicationContext private val context: Context
) : StoreResolver {
override fun connectionStoreIn(scope: CoroutineScope) =
StoreWrapper(context, ConnectionSerializer, scope).store

class StoreWrapper<T>(context: Context, serializer: Serializer<T>, scope: CoroutineScope) {
private val Context.store by dataStore(
fileName = "connection.pb",
serializer = serializer,
corruptionHandler = ReplaceFileCorruptionHandler { serializer.defaultValue },
scope = scope
)

val store = context.store
}
}
Original file line number Diff line number Diff line change
@@ -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()
}
}
Expand Down

0 comments on commit d6dcb6e

Please sign in to comment.