From 32d366b8fd0876ae12f467e647c229f8590c6fb0 Mon Sep 17 00:00:00 2001 From: Kirill Kamakin Date: Sun, 13 Nov 2022 15:38:45 +0100 Subject: [PATCH] Fix absent recipe refresh on authorization --- .../mealient/data/recipes/RecipeRepo.kt | 2 ++ .../data/recipes/impl/RecipeRepoImpl.kt | 16 ++++++++++++- .../ui/recipes/RecipesListFragment.kt | 9 -------- .../ui/recipes/RecipesListViewModel.kt | 19 ++++++--------- .../ui/recipes/RecipesListViewModelTest.kt | 23 ++++++++----------- 5 files changed, 33 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/gq/kirmanak/mealient/data/recipes/RecipeRepo.kt b/app/src/main/java/gq/kirmanak/mealient/data/recipes/RecipeRepo.kt index afb786c..b7c1266 100644 --- a/app/src/main/java/gq/kirmanak/mealient/data/recipes/RecipeRepo.kt +++ b/app/src/main/java/gq/kirmanak/mealient/data/recipes/RecipeRepo.kt @@ -15,4 +15,6 @@ interface RecipeRepo { suspend fun loadRecipeInfo(recipeId: String): FullRecipeEntity? fun updateNameQuery(name: String?) + + suspend fun refreshRecipes() } \ No newline at end of file diff --git a/app/src/main/java/gq/kirmanak/mealient/data/recipes/impl/RecipeRepoImpl.kt b/app/src/main/java/gq/kirmanak/mealient/data/recipes/impl/RecipeRepoImpl.kt index 97f9b74..542cd18 100644 --- a/app/src/main/java/gq/kirmanak/mealient/data/recipes/impl/RecipeRepoImpl.kt +++ b/app/src/main/java/gq/kirmanak/mealient/data/recipes/impl/RecipeRepoImpl.kt @@ -25,7 +25,11 @@ class RecipeRepoImpl @Inject constructor( override fun createPager(): Pager { 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 } } \ No newline at end of file diff --git a/app/src/main/java/gq/kirmanak/mealient/ui/recipes/RecipesListFragment.kt b/app/src/main/java/gq/kirmanak/mealient/ui/recipes/RecipesListFragment.kt index 64c67a7..019d099 100644 --- a/app/src/main/java/gq/kirmanak/mealient/ui/recipes/RecipesListFragment.kt +++ b/app/src/main/java/gq/kirmanak/mealient/ui/recipes/RecipesListFragment.kt @@ -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) { diff --git a/app/src/main/java/gq/kirmanak/mealient/ui/recipes/RecipesListViewModel.kt b/app/src/main/java/gq/kirmanak/mealient/ui/recipes/RecipesListViewModel.kt index 1a75853..2536f8a 100644 --- a/app/src/main/java/gq/kirmanak/mealient/ui/recipes/RecipesListViewModel.kt +++ b/app/src/main/java/gq/kirmanak/mealient/ui/recipes/RecipesListViewModel.kt @@ -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(null) - val isAuthorized: LiveData = _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> { logger.v { "refreshRecipeInfo called with: recipeSlug = $recipeSlug" } return liveData { diff --git a/app/src/test/java/gq/kirmanak/mealient/ui/recipes/RecipesListViewModelTest.kt b/app/src/test/java/gq/kirmanak/mealient/ui/recipes/RecipesListViewModelTest.kt index ee9ddcd..3d9ccbb 100644 --- a/app/src/test/java/gq/kirmanak/mealient/ui/recipes/RecipesListViewModelTest.kt +++ b/app/src/test/java/gq/kirmanak/mealient/ui/recipes/RecipesListViewModelTest.kt @@ -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