Exception handling for when reading datastore.

pull/1710/head
Shuvo 10 months ago
parent 1c2fe163a0
commit b405e5a484

@ -21,6 +21,7 @@ import androidx.datastore.core.DataStore
import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig
import com.google.samples.apps.nowinandroid.core.model.data.ThemeBrand
import com.google.samples.apps.nowinandroid.core.model.data.UserData
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.map
import java.io.IOException
@ -30,6 +31,14 @@ class NiaPreferencesDataSource @Inject constructor(
private val userPreferences: DataStore<UserPreferences>,
) {
val userData = userPreferences.data
.catch { exception ->
if (exception is IOException) {
Log.e("NiaPreferences", "Error reading user preferences.", exception)
emit(UserPreferences.getDefaultInstance())
} else {
throw exception
}
}
.map {
UserData(
bookmarkedNewsResources = it.bookmarkedNewsResourceIdsMap.keys,

Loading…
Cancel
Save