Skip to content

Commit

Permalink
caught datastore.data
Browse files Browse the repository at this point in the history
Signed-off-by: Basler182 <[email protected]>
  • Loading branch information
Basler182 committed Jun 22, 2024
1 parent 4b2f01b commit 6ee145d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class LocalKeyValueStorageTest {
localStorage.saveData(key, initialData)
localStorage.saveData(key, newData)


// Then
val readData = localStorage.readDataBlocking(key)
assertThat(readData).isEqualTo(newData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.emptyPreferences
import androidx.datastore.preferences.preferencesDataStore
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.map
import javax.inject.Inject
Expand All @@ -29,13 +31,17 @@ class LocalKeyValueStorage @Inject constructor(
}

override fun <T> readData(key: PreferenceKey<T>): Flow<T?> {
return dataStore.data.map { preferences ->
preferences[key.key]
}
return dataStore.data
.catch { emit(emptyPreferences()) }
.map { preferences ->
preferences[key.key]
}
}

override suspend fun <T> readDataBlocking(key: PreferenceKey<T>): T? {
return dataStore.data.firstOrNull()?.get(key.key)
return dataStore.data
.catch { emit(emptyPreferences()) }
.firstOrNull()?.get(key.key)
}

override suspend fun <T> deleteData(key: PreferenceKey<T>) {
Expand Down

0 comments on commit 6ee145d

Please sign in to comment.