Fix absent recipe refresh on authorization
This commit is contained in:
@@ -15,4 +15,6 @@ interface RecipeRepo {
|
||||
suspend fun loadRecipeInfo(recipeId: String): FullRecipeEntity?
|
||||
|
||||
fun updateNameQuery(name: String?)
|
||||
|
||||
suspend fun refreshRecipes()
|
||||
}
|
||||
@@ -25,7 +25,11 @@ class RecipeRepoImpl @Inject constructor(
|
||||
|
||||
override fun createPager(): Pager<Int, RecipeSummaryEntity> {
|
||||
logger.v { "createPager() called" }
|
||||
val pagingConfig = PagingConfig(pageSize = LOAD_PAGE_SIZE, enablePlaceholders = true)
|
||||
val pagingConfig = PagingConfig(
|
||||
pageSize = LOAD_PAGE_SIZE,
|
||||
enablePlaceholders = true,
|
||||
initialLoadSize = INITIAL_LOAD_PAGE_SIZE,
|
||||
)
|
||||
return Pager(
|
||||
config = pagingConfig,
|
||||
remoteMediator = mediator,
|
||||
@@ -59,7 +63,17 @@ class RecipeRepoImpl @Inject constructor(
|
||||
pagingSourceFactory.setQuery(name)
|
||||
}
|
||||
|
||||
override suspend fun refreshRecipes() {
|
||||
logger.v { "refreshRecipes() called" }
|
||||
runCatchingExceptCancel {
|
||||
storage.refreshAll(dataSource.requestRecipes(0, INITIAL_LOAD_PAGE_SIZE))
|
||||
}.onFailure {
|
||||
logger.e(it) { "Can't refresh recipes" }
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val LOAD_PAGE_SIZE = 50
|
||||
private const val INITIAL_LOAD_PAGE_SIZE = LOAD_PAGE_SIZE * 3
|
||||
}
|
||||
}
|
||||
@@ -123,15 +123,6 @@ class RecipesListFragment : Fragment(R.layout.fragment_recipes_list) {
|
||||
logger.v { "setupRecipeAdapter: received refresh request" }
|
||||
recipesAdapter.refresh()
|
||||
}
|
||||
|
||||
viewModel.isAuthorized.observe(viewLifecycleOwner) { isAuthorized ->
|
||||
logger.v { "setupRecipeAdapter: isAuthorized changed to $isAuthorized" }
|
||||
if (isAuthorized != null) {
|
||||
if (isAuthorized) recipesAdapter.refresh()
|
||||
// else is ignored to avoid the removal of the non-public recipes
|
||||
viewModel.onAuthorizationChangeHandled()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun onLoadFailure(error: Throwable) {
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package gq.kirmanak.mealient.ui.recipes
|
||||
|
||||
import androidx.lifecycle.*
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.liveData
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import androidx.paging.cachedIn
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import gq.kirmanak.mealient.data.auth.AuthRepo
|
||||
@@ -20,21 +23,13 @@ class RecipesListViewModel @Inject constructor(
|
||||
|
||||
val pagingData = recipeRepo.createPager().flow.cachedIn(viewModelScope)
|
||||
|
||||
private val _isAuthorized = MutableLiveData<Boolean?>(null)
|
||||
val isAuthorized: LiveData<Boolean?> = _isAuthorized
|
||||
|
||||
init {
|
||||
authRepo.isAuthorizedFlow.valueUpdatesOnly().onEach {
|
||||
logger.v { "Authorization state changed to $it" }
|
||||
_isAuthorized.postValue(it)
|
||||
authRepo.isAuthorizedFlow.valueUpdatesOnly().onEach { hasAuthorized ->
|
||||
logger.v { "Authorization state changed to $hasAuthorized" }
|
||||
if (hasAuthorized) recipeRepo.refreshRecipes()
|
||||
}.launchIn(viewModelScope)
|
||||
}
|
||||
|
||||
fun onAuthorizationChangeHandled() {
|
||||
logger.v { "onAuthorizationSuccessHandled() called" }
|
||||
_isAuthorized.postValue(null)
|
||||
}
|
||||
|
||||
fun refreshRecipeInfo(recipeSlug: String): LiveData<Result<Unit>> {
|
||||
logger.v { "refreshRecipeInfo called with: recipeSlug = $recipeSlug" }
|
||||
return liveData {
|
||||
|
||||
@@ -25,29 +25,24 @@ class RecipesListViewModelTest : BaseUnitTest() {
|
||||
lateinit var recipeRepo: RecipeRepo
|
||||
|
||||
@Test
|
||||
fun `when authRepo isAuthorized changes to true expect isAuthorized update`() {
|
||||
fun `when authRepo isAuthorized changes to true expect that recipes are refreshed`() {
|
||||
every { authRepo.isAuthorizedFlow } returns flowOf(false, true)
|
||||
assertThat(createSubject().isAuthorized.value).isTrue()
|
||||
createSubject()
|
||||
coVerify { recipeRepo.refreshRecipes() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `when authRepo isAuthorized changes to false expect isAuthorized update`() {
|
||||
fun `when authRepo isAuthorized changes to false expect that recipes are not refreshed`() {
|
||||
every { authRepo.isAuthorizedFlow } returns flowOf(true, false)
|
||||
assertThat(createSubject().isAuthorized.value).isFalse()
|
||||
createSubject()
|
||||
coVerify(inverse = true) { recipeRepo.refreshRecipes() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `when authRepo isAuthorized doesn't change expect isAuthorized null`() {
|
||||
fun `when authRepo isAuthorized doesn't change expect that recipes are not refreshed`() {
|
||||
every { authRepo.isAuthorizedFlow } returns flowOf(true)
|
||||
assertThat(createSubject().isAuthorized.value).isNull()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `when isAuthorization change is handled expect isAuthorized null`() {
|
||||
every { authRepo.isAuthorizedFlow } returns flowOf(true, false)
|
||||
val subject = createSubject()
|
||||
subject.onAuthorizationChangeHandled()
|
||||
assertThat(subject.isAuthorized.value).isNull()
|
||||
createSubject()
|
||||
coVerify(inverse = true) { recipeRepo.refreshRecipes() }
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user