Skip to content

Commit

Permalink
[#159] Wrap flow with flowTransform and use the .first() to get only …
Browse files Browse the repository at this point in the history
…first emit from datastore
  • Loading branch information
lydiasama committed Jan 12, 2023
1 parent 2b5b0f6 commit 41c98c3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class HomeViewModel @Inject constructor(

init {
execute {
showLoading()
val getModelsFlow = getModelsUseCase()
val getFirstTimeLaunchPreferencesFlow = getFirstTimeLaunchPreferencesUseCase()

Expand All @@ -52,12 +51,13 @@ class HomeViewModel @Inject constructor(
if (firstTimeLaunch) {
updateFirstTimeLaunchPreferencesUseCase(false)
}
}.onStart {
showLoading()
}.onCompletion {
hideLoading()
}.catch {
_error.emit(it)
hideLoading()
}.collect {
hideLoading()
}
}.collect()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package co.nimblehq.sample.compose.data.repository
import android.util.Log
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.*
import co.nimblehq.sample.compose.data.extensions.flowTransform
import co.nimblehq.sample.compose.domain.repository.AppPreferencesRepository
import kotlinx.coroutines.flow.*
import java.io.IOException
Expand All @@ -16,8 +17,9 @@ class AppPreferencesRepositoryImpl @Inject constructor(
val FIRST_TIME_LAUNCH = booleanPreferencesKey("FIRST_TIME_LAUNCH")
}

override fun getFirstTimeLaunchPreferences(): Flow<Boolean> = appPreferencesDataStore.data
.catch { exception ->
override fun getFirstTimeLaunchPreferences(): Flow<Boolean> = flowTransform {
appPreferencesDataStore.data
.catch { exception ->
if (exception is IOException) {
Log.e(
AppPreferencesRepositoryImpl::class.simpleName,
Expand All @@ -30,7 +32,8 @@ class AppPreferencesRepositoryImpl @Inject constructor(
}
}.map { preferences ->
preferences[PreferencesKeys.FIRST_TIME_LAUNCH] ?: true
}
}.first()
}

override suspend fun updateFirstTimeLaunchPreferences(isFirstTimeLaunch: Boolean) {
appPreferencesDataStore.edit { preferences ->
Expand Down

0 comments on commit 41c98c3

Please sign in to comment.