forked from mozilla-mobile/fenix
-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For mozilla-mobile#21275 - Sort items by how many times they were act…
…ually shown
- Loading branch information
1 parent
4596d4f
commit c1f0e5a
Showing
15 changed files
with
231 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
app/src/main/java/org/mozilla/fenix/home/PocketUpdatesMiddleware.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
app/src/test/java/org/mozilla/fenix/home/PocketUpdatesMiddlewareTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))) } | ||
} | ||
} |
Oops, something went wrong.