Ignore load errors if wasn't loading
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package gq.kirmanak.mealient.extensions
|
||||
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.FlowCollector
|
||||
|
||||
fun <T> Flow<T>.valueUpdatesOnly(): Flow<T> = when (this) {
|
||||
is ValueUpdateOnlyFlowImpl<T> -> this
|
||||
else -> ValueUpdateOnlyFlowImpl(this)
|
||||
}
|
||||
|
||||
private class ValueUpdateOnlyFlowImpl<T>(private val upstream: Flow<T>) : Flow<T> {
|
||||
|
||||
override suspend fun collect(collector: FlowCollector<T>) {
|
||||
var previousValue: T? = null
|
||||
upstream.collect { value ->
|
||||
if (previousValue != null && previousValue != value) {
|
||||
collector.emit(value)
|
||||
}
|
||||
previousValue = value
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,9 +8,10 @@ import androidx.paging.cachedIn
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import gq.kirmanak.mealient.data.auth.AuthRepo
|
||||
import gq.kirmanak.mealient.data.recipes.RecipeRepo
|
||||
import gq.kirmanak.mealient.extensions.valueUpdatesOnly
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.runningReduce
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
@@ -26,12 +27,9 @@ class RecipeViewModel @Inject constructor(
|
||||
val isAuthorized: LiveData<Boolean?> = _isAuthorized
|
||||
|
||||
init {
|
||||
authRepo.isAuthorizedFlow.runningReduce { wasAuthorized, isAuthorized ->
|
||||
logger.v { "Authorization state changed from $wasAuthorized to $isAuthorized" }
|
||||
if (wasAuthorized != isAuthorized) {
|
||||
_isAuthorized.postValue(isAuthorized)
|
||||
}
|
||||
isAuthorized
|
||||
authRepo.isAuthorizedFlow.valueUpdatesOnly().onEach {
|
||||
logger.v { "Authorization state changed to $it" }
|
||||
_isAuthorized.postValue(it)
|
||||
}.launchIn(viewModelScope)
|
||||
}
|
||||
|
||||
|
||||
@@ -19,10 +19,14 @@ import gq.kirmanak.mealient.datasource.NetworkError
|
||||
import gq.kirmanak.mealient.extensions.collectWhenViewResumed
|
||||
import gq.kirmanak.mealient.extensions.refreshRequestFlow
|
||||
import gq.kirmanak.mealient.extensions.showLongToast
|
||||
import gq.kirmanak.mealient.extensions.valueUpdatesOnly
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
import gq.kirmanak.mealient.ui.activity.MainActivityViewModel
|
||||
import gq.kirmanak.mealient.ui.recipes.images.RecipePreloaderFactory
|
||||
import kotlinx.coroutines.flow.*
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.filter
|
||||
import kotlinx.coroutines.flow.filterIsInstance
|
||||
import kotlinx.coroutines.flow.map
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
@@ -133,7 +137,7 @@ private fun Throwable.toLoadErrorReasonText(): Int? = when (this) {
|
||||
private fun <T : Any, VH : RecyclerView.ViewHolder> PagingDataAdapter<T, VH>.refreshErrors(): Flow<Throwable> {
|
||||
return loadStateFlow
|
||||
.map { it.refresh }
|
||||
.distinctUntilChanged()
|
||||
.valueUpdatesOnly()
|
||||
.filterIsInstance<LoadState.Error>()
|
||||
.map { it.error }
|
||||
}
|
||||
@@ -141,7 +145,7 @@ private fun <T : Any, VH : RecyclerView.ViewHolder> PagingDataAdapter<T, VH>.ref
|
||||
private fun <T : Any, VH : RecyclerView.ViewHolder> PagingDataAdapter<T, VH>.appendPaginationEnd(): Flow<Unit> {
|
||||
return loadStateFlow
|
||||
.map { it.append.endOfPaginationReached }
|
||||
.distinctUntilChanged()
|
||||
.valueUpdatesOnly()
|
||||
.filter { it }
|
||||
.map { }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user