[fenix] For https://github.com/mozilla-mobile/fenix/issues/21593 - Refactor the coroutine from PocketStoriesShown to outside the middleware

In so this code will no longer have access to the MiddlewareContext which only
makes sense in the thread of the Middleware itself.
pull/600/head
Mugurell 3 years ago committed by mergify[bot]
parent 6261e570c3
commit 7e65b07930

@ -13,6 +13,7 @@ import mozilla.components.lib.state.Action
import mozilla.components.lib.state.Middleware
import mozilla.components.lib.state.MiddlewareContext
import mozilla.components.lib.state.Store
import mozilla.components.service.pocket.PocketRecommendedStory
import mozilla.components.service.pocket.PocketStoriesService
import org.mozilla.fenix.datastore.SelectedPocketStoriesCategories
import org.mozilla.fenix.datastore.SelectedPocketStoriesCategories.SelectedPocketStoriesCategory
@ -60,13 +61,13 @@ class PocketUpdatesMiddleware(
// Post process actions
when (action) {
is HomeFragmentAction.PocketStoriesShown -> {
coroutineScope.launch {
pocketStoriesService.updateStoriesTimesShown(
action.storiesShown.map {
it.copy(timesShown = it.timesShown.inc())
}
)
}
persistStories(
coroutineScope = coroutineScope,
pocketStoriesService = pocketStoriesService,
updatedStories = action.storiesShown.map {
it.copy(timesShown = it.timesShown.inc())
}
)
}
is HomeFragmentAction.SelectPocketStoriesCategory,
is HomeFragmentAction.DeselectPocketStoriesCategory -> {
@ -83,6 +84,26 @@ class PocketUpdatesMiddleware(
}
}
/**
* Persist [updatedStories] for making their details available in between app restarts.
*
* @param coroutineScope [CoroutineScope] used for reading the locally persisted data.
* @param pocketStoriesService [PocketStoriesService] used for updating details about the Pocket recommended stories.
* @param updatedStories the list of stories to persist.
*/
@VisibleForTesting
internal fun persistStories(
coroutineScope: CoroutineScope,
pocketStoriesService: PocketStoriesService,
updatedStories: List<PocketRecommendedStory>
) {
coroutineScope.launch {
pocketStoriesService.updateStoriesTimesShown(
updatedStories
)
}
}
/**
* Persist [currentCategoriesSelections] for making this details available in between app restarts.
*

@ -44,6 +44,20 @@ class PocketUpdatesMiddlewareTest {
coVerify { pocketService.updateStoriesTimesShown(listOf(story2.copy(timesShown = 1))) }
}
@Test
fun `WHEN persistStories is called THEN update PocketStoriesService`() {
val stories: List<PocketRecommendedStory> = mockk()
val pocketService: PocketStoriesService = mockk(relaxed = true)
persistStories(
coroutineScope = TestCoroutineScope(),
pocketStoriesService = pocketService,
updatedStories = stories
)
coVerify { pocketService.updateStoriesTimesShown(stories) }
}
@Test
fun `WHEN PocketStoriesCategoriesChange is dispatched THEN intercept and dispatch PocketStoriesCategoriesSelectionsChange`() {
val persistedSelectedCategory: SelectedPocketStoriesCategory = mockk {

Loading…
Cancel
Save