diff --git a/core/datasource/src/main/java/com/tangem/datasource/local/preferences/utils/AppPreferencesStoreExt.kt b/core/datasource/src/main/java/com/tangem/datasource/local/preferences/utils/AppPreferencesStoreExt.kt index 862962e88d..5622eb2351 100644 --- a/core/datasource/src/main/java/com/tangem/datasource/local/preferences/utils/AppPreferencesStoreExt.kt +++ b/core/datasource/src/main/java/com/tangem/datasource/local/preferences/utils/AppPreferencesStoreExt.kt @@ -12,7 +12,15 @@ import kotlinx.coroutines.flow.map /** Get flow of nullable data [T] by string [key] */ inline fun AppPreferencesStore.getObject(key: Preferences.Key): Flow { val adapter = moshi.adapter(T::class.java) - return data.map { it[key]?.let(adapter::fromJson) } + return data.map { preferences -> + preferences[key]?.let { + try { + adapter.fromJson(it) + } catch (e: JsonDataException) { + null + } + } + } } /** @@ -44,7 +52,13 @@ suspend inline fun AppPreferencesStore.getObjectSyncOrNull(key: Pref val adapter = moshi.adapter(T::class.java) // TODO: Support parameterized types return data.firstOrNull() ?.get(key) - ?.let(adapter::fromJson) + ?.let { + try { + adapter.fromJson(it) + } catch (e: JsonDataException) { + null + } + } } /** Get data [T] by string [key]. If data is not found, it returns [default] */ @@ -55,7 +69,13 @@ suspend inline fun AppPreferencesStore.getObjectSyncOrDefault( val adapter = moshi.adapter(T::class.java) return data.firstOrNull() ?.get(key) - ?.let(adapter::fromJson) + ?.let { + try { + adapter.fromJson(it) + } catch (e: JsonDataException) { + default + } + } ?: default }