Allow saving authors and topics only

Bug: 229012243

Change-Id: I4c8bc77002d39b8f97c925ea811d307e2cbe93db
pull/1837/head
Alex Vanyo 3 years ago committed by Don Turner
parent ca8fd5a4f3
commit 04d368bb58

@ -153,7 +153,7 @@ fun ForYouScreen(
) { ) {
Button( Button(
onClick = saveFollowedTopics, onClick = saveFollowedTopics,
enabled = uiState.canSaveSelectedTopics, enabled = uiState.canSaveInterests,
modifier = Modifier modifier = Modifier
.padding(horizontal = 40.dp) .padding(horizontal = 40.dp)
.width(364.dp) .width(364.dp)

@ -55,7 +55,7 @@ class ForYouViewModel @Inject constructor(
authorsRepository.getFollowedAuthorIdsStream(), authorsRepository.getFollowedAuthorIdsStream(),
topicsRepository.getFollowedTopicIdsStream(), topicsRepository.getFollowedTopicIdsStream(),
) { followedAuthors, followedTopics -> ) { followedAuthors, followedTopics ->
if (followedAuthors.isEmpty() || followedTopics.isEmpty()) { if (followedAuthors.isEmpty() && followedTopics.isEmpty()) {
FollowedInterestsState.None FollowedInterestsState.None
} else { } else {
FollowedInterestsState.FollowedInterests( FollowedInterestsState.FollowedInterests(
@ -204,20 +204,18 @@ class ForYouViewModel @Inject constructor(
} }
fun saveFollowedInterests() { fun saveFollowedInterests() {
if (inProgressTopicSelection.isNotEmpty()) { // Don't attempt to save anything if nothing is selected
viewModelScope.launch { if (inProgressTopicSelection.isEmpty() && inProgressAuthorSelection.isEmpty()) {
topicsRepository.setFollowedTopicIds(inProgressTopicSelection) return
withMutableSnapshot {
inProgressTopicSelection = emptySet()
}
}
} }
if (inProgressAuthorSelection.isNotEmpty()) {
viewModelScope.launch { viewModelScope.launch {
authorsRepository.setFollowedAuthorIds(inProgressAuthorSelection) topicsRepository.setFollowedTopicIds(inProgressTopicSelection)
withMutableSnapshot { authorsRepository.setFollowedAuthorIds(inProgressAuthorSelection)
inProgressAuthorSelection = emptySet() // Clear out the old selection, in case we return to onboarding
} withMutableSnapshot {
inProgressTopicSelection = emptySet()
inProgressAuthorSelection = emptySet()
} }
} }
} }
@ -275,7 +273,7 @@ sealed interface ForYouFeedUiState {
val authors: List<FollowableAuthor>, val authors: List<FollowableAuthor>,
override val feed: List<SaveableNewsResource> override val feed: List<SaveableNewsResource>
) : PopulatedFeed { ) : PopulatedFeed {
val canSaveSelectedTopics: Boolean = val canSaveInterests: Boolean =
topics.any { it.isFollowed } || authors.any { it.isFollowed } topics.any { it.isFollowed } || authors.any { it.isFollowed }
} }

@ -644,6 +644,82 @@ class ForYouViewModelTest {
} }
} }
@Test
fun topicSelectionUpdatesAfterSavingTopicsOnly() = runTest {
viewModel.uiState
.test {
awaitItem()
topicsRepository.sendTopics(sampleTopics)
topicsRepository.setFollowedTopicIds(emptySet())
authorsRepository.sendAuthors(sampleAuthors)
authorsRepository.setFollowedAuthorIds(emptySet())
newsRepository.sendNewsResources(sampleNewsResources)
awaitItem()
viewModel.updateTopicSelection(1, isChecked = true)
awaitItem()
viewModel.saveFollowedInterests()
awaitItem()
assertEquals(
ForYouFeedUiState.PopulatedFeed.FeedWithoutTopicSelection(
feed = listOf(
SaveableNewsResource(
newsResource = sampleNewsResources[1],
isSaved = false,
),
SaveableNewsResource(
newsResource = sampleNewsResources[2],
isSaved = false,
)
)
),
awaitItem()
)
assertEquals(setOf(1), topicsRepository.getCurrentFollowedTopics())
assertEquals(emptySet<Int>(), authorsRepository.getCurrentFollowedAuthors())
cancel()
}
}
@Test
fun topicSelectionUpdatesAfterSavingAuthorsOnly() = runTest {
viewModel.uiState
.test {
awaitItem()
topicsRepository.sendTopics(sampleTopics)
topicsRepository.setFollowedTopicIds(emptySet())
authorsRepository.sendAuthors(sampleAuthors)
authorsRepository.setFollowedAuthorIds(emptySet())
newsRepository.sendNewsResources(sampleNewsResources)
awaitItem()
viewModel.updateAuthorSelection(0, isChecked = true)
awaitItem()
viewModel.saveFollowedInterests()
awaitItem()
assertEquals(
ForYouFeedUiState.PopulatedFeed.FeedWithoutTopicSelection(
feed = listOf(
SaveableNewsResource(
newsResource = sampleNewsResources[0],
isSaved = false
),
)
),
awaitItem()
)
assertEquals(emptySet<Int>(), topicsRepository.getCurrentFollowedTopics())
assertEquals(setOf(0), authorsRepository.getCurrentFollowedAuthors())
cancel()
}
}
@Test @Test
fun topicSelectionUpdatesAfterSavingAuthorsAndTopics() = runTest { fun topicSelectionUpdatesAfterSavingAuthorsAndTopics() = runTest {
viewModel.uiState viewModel.uiState
@ -779,6 +855,100 @@ class ForYouViewModelTest {
} }
} }
@Test
fun authorSelectionIsResetAfterSavingAuthorsAndRemovingThem() = runTest {
viewModel.uiState
.test {
awaitItem()
topicsRepository.sendTopics(sampleTopics)
topicsRepository.setFollowedTopicIds(emptySet())
authorsRepository.sendAuthors(sampleAuthors)
authorsRepository.setFollowedAuthorIds(emptySet())
newsRepository.sendNewsResources(sampleNewsResources)
awaitItem()
viewModel.updateAuthorSelection(1, isChecked = true)
viewModel.saveFollowedInterests()
awaitItem()
authorsRepository.setFollowedAuthorIds(emptySet())
assertEquals(
ForYouFeedUiState.PopulatedFeed.FeedWithInterestsSelection(
topics = listOf(
FollowableTopic(
topic = Topic(
id = 0,
name = "Headlines",
shortDescription = "",
longDescription = "long description",
url = "URL",
imageUrl = "image URL",
),
isFollowed = false
),
FollowableTopic(
topic = Topic(
id = 1,
name = "UI",
shortDescription = "",
longDescription = "long description",
url = "URL",
imageUrl = "image URL",
),
isFollowed = false
),
FollowableTopic(
topic = Topic(
id = 2,
name = "Tools",
shortDescription = "",
longDescription = "long description",
url = "URL",
imageUrl = "image URL",
),
isFollowed = false
)
),
feed = emptyList(),
authors = listOf(
FollowableAuthor(
author = Author(
id = 0,
name = "Android Dev",
imageUrl = "",
twitter = "",
mediumPage = ""
),
isFollowed = false
),
FollowableAuthor(
author = Author(
id = 1,
name = "Android Dev 2",
imageUrl = "",
twitter = "",
mediumPage = ""
),
isFollowed = false
),
FollowableAuthor(
author = Author(
id = 2,
name = "Android Dev 3",
imageUrl = "",
twitter = "",
mediumPage = ""
),
isFollowed = false
)
)
),
awaitItem()
)
cancel()
}
}
@Test @Test
fun newsResourceSelectionUpdatesAfterLoadingFollowedTopics() = runTest { fun newsResourceSelectionUpdatesAfterLoadingFollowedTopics() = runTest {
viewModel.uiState viewModel.uiState

Loading…
Cancel
Save