Commit 836e33cf authored by Mugurell's avatar Mugurell Committed by Christian Sadilek
Browse files

For #22070 - Prevent crash for when a selected topic does not exist anymore

The list of selected topics overwrites old data whenever user selects or
deselects another so the old selections will not leak for long.

(cherry picked from commit acdde511)
parent 36e59ba7
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -32,8 +32,8 @@ fun HomeFragmentState.getFilteredStories(

    val oldestSortedCategories = pocketStoriesCategoriesSelections
        .sortedByDescending { it.selectionTimestamp }
        .map { selectedCategory ->
            pocketStoriesCategories.first {
        .mapNotNull { selectedCategory ->
            pocketStoriesCategories.find {
                it.name == selectedCategory.name
            }
        }
+16 −0
Original line number Diff line number Diff line
@@ -258,6 +258,22 @@ class HomeFragmentStateTest {
        assertSame(firstCategory.stories[0], result[4])
        assertSame(firstCategory.stories[2], result[5])
    }

    @Test
    fun `GIVEN old selections of categories which do not exist anymore WHEN getFilteredStories is called THEN ignore not found selections`() {
        val homeState = HomeFragmentState(
            pocketStoriesCategories = listOf(otherStoriesCategory, anotherStoriesCategory, defaultStoriesCategory),
            pocketStoriesCategoriesSelections = listOf(
                PocketRecommendedStoriesSelectedCategory("unexistent"),
                PocketRecommendedStoriesSelectedCategory(anotherStoriesCategory.name)
            )
        )

        val result = homeState.getFilteredStories(6)

        assertEquals(3, result.size)
        assertNull(result.firstOrNull { it.category != anotherStoriesCategory.name })
    }
}

private fun getFakePocketStories(