From b6eae2907c6b0aed922616d8da49c05cc8730a57 Mon Sep 17 00:00:00 2001 From: Alex Vanyo Date: Tue, 5 Apr 2022 15:32:32 -0700 Subject: [PATCH] Reset in-progress topic selection upon saving Previously, the in-progress topic selection wasn't reset upon saving. This meant that clearing out the selected topics (from the topics tab) would bring back the topic selection that was last used. Bug: 228050946 Change-Id: I3d52baf6396b41e2116f38c89a79b419c90c63b5 --- .../feature/foryou/ForYouViewModel.kt | 5 ++ .../feature/foryou/ForYouViewModelTest.kt | 63 +++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/feature-foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModel.kt b/feature-foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModel.kt index 27f328240..f800eee29 100644 --- a/feature-foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModel.kt +++ b/feature-foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModel.kt @@ -169,6 +169,11 @@ class ForYouViewModel @Inject constructor( viewModelScope.launch { topicsRepository.setFollowedTopicIds(inProgressTopicSelection) + + // Clear out the in-progress selection after saving it + withMutableSnapshot { + inProgressTopicSelection = emptySet() + } } } } diff --git a/feature-foryou/src/test/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModelTest.kt b/feature-foryou/src/test/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModelTest.kt index 1ed4ce9b4..09bd402c0 100644 --- a/feature-foryou/src/test/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModelTest.kt +++ b/feature-foryou/src/test/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModelTest.kt @@ -328,6 +328,69 @@ class ForYouViewModelTest { } } + @Test + fun topicSelectionIsResetAfterSavingTopicsAndRemovingThem() = runTest { + viewModel.uiState + .test { + awaitItem() + topicsRepository.sendTopics(sampleTopics) + topicsRepository.setFollowedTopicIds(emptySet()) + newsRepository.sendNewsResources(sampleNewsResources) + + awaitItem() + viewModel.updateTopicSelection(1, isChecked = true) + + awaitItem() + viewModel.saveFollowedTopics() + + awaitItem() + topicsRepository.setFollowedTopicIds(emptySet()) + + assertEquals( + ForYouFeedUiState.PopulatedFeed.FeedWithTopicSelection( + 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() + ), + awaitItem() + ) + cancel() + } + } + @Test fun newsResourceSelectionUpdatesAfterLoadingFollowedTopics() = runTest { viewModel.uiState