For #21275 - Sort items by how many times they were actually shown
parent
4596d4f905
commit
c1f0e5a611
@ -0,0 +1,44 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.home
|
||||
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
import mozilla.components.lib.state.Action
|
||||
import mozilla.components.lib.state.Middleware
|
||||
import mozilla.components.lib.state.MiddlewareContext
|
||||
import mozilla.components.service.pocket.PocketStoriesService
|
||||
|
||||
/**
|
||||
* [HomeFragmentStore] middleware reacting in response to Pocket related [Action]s.
|
||||
*/
|
||||
class PocketUpdatesMiddleware(
|
||||
private val coroutineScope: CoroutineScope,
|
||||
private val pocketStoriesService: PocketStoriesService
|
||||
) : Middleware<HomeFragmentState, HomeFragmentAction> {
|
||||
override fun invoke(
|
||||
context: MiddlewareContext<HomeFragmentState, HomeFragmentAction>,
|
||||
next: (HomeFragmentAction) -> Unit,
|
||||
action: HomeFragmentAction
|
||||
) {
|
||||
next(action)
|
||||
|
||||
// Post process actions
|
||||
when (action) {
|
||||
is HomeFragmentAction.PocketStoriesShown -> {
|
||||
coroutineScope.launch {
|
||||
pocketStoriesService.updateStoriesTimesShown(
|
||||
action.storiesShown.map {
|
||||
it.copy(timesShown = it.timesShown.inc())
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
// no-op
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.home
|
||||
|
||||
import io.mockk.coVerify
|
||||
import io.mockk.mockk
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.TestCoroutineScope
|
||||
import mozilla.components.service.pocket.PocketRecommendedStory
|
||||
import mozilla.components.service.pocket.PocketStoriesService
|
||||
import mozilla.components.support.test.ext.joinBlocking
|
||||
import org.junit.Test
|
||||
|
||||
class PocketUpdatesMiddlewareTest {
|
||||
@ExperimentalCoroutinesApi
|
||||
@Test
|
||||
fun `WHEN PocketStoriesShown is dispatched THEN update PocketStoriesService`() {
|
||||
val story1 = PocketRecommendedStory("title", "url1", "imageUrl", "publisher", "category", 0, timesShown = 0)
|
||||
val story2 = story1.copy("title2", "url2")
|
||||
val story3 = story1.copy("title3", "url3")
|
||||
val coroutineScope = TestCoroutineScope()
|
||||
val pocketService: PocketStoriesService = mockk(relaxed = true)
|
||||
val pocketMiddleware = PocketUpdatesMiddleware(coroutineScope, pocketService)
|
||||
val homeStore = HomeFragmentStore(
|
||||
HomeFragmentState(
|
||||
pocketStories = listOf(story1, story2, story3)
|
||||
),
|
||||
listOf(pocketMiddleware)
|
||||
)
|
||||
|
||||
homeStore.dispatch(HomeFragmentAction.PocketStoriesShown(listOf(story2))).joinBlocking()
|
||||
|
||||
coVerify { pocketService.updateStoriesTimesShown(listOf(story2.copy(timesShown = 1))) }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue