From b2c7282b468f9c5aa5745c3622ad8fadbd4909fe Mon Sep 17 00:00:00 2001 From: Cris Barreiro Date: Wed, 6 Nov 2024 13:21:22 +0100 Subject: [PATCH] Fix dismiss streak --- .../brokensite/impl/BrokenSitePomptDataStore.kt | 2 +- .../impl/BrokenSitePromptInMemoryStore.kt | 4 +++- .../brokensite/impl/RealBrokenSitePrompt.kt | 13 ++++++++----- .../brokensite/impl/RealBrokenSitePromptTest.kt | 8 ++++---- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/broken-site/broken-site-impl/src/main/java/com/duckduckgo/brokensite/impl/BrokenSitePomptDataStore.kt b/broken-site/broken-site-impl/src/main/java/com/duckduckgo/brokensite/impl/BrokenSitePomptDataStore.kt index 53c5ac5c9dc8..0c252a49be87 100644 --- a/broken-site/broken-site-impl/src/main/java/com/duckduckgo/brokensite/impl/BrokenSitePomptDataStore.kt +++ b/broken-site/broken-site-impl/src/main/java/com/duckduckgo/brokensite/impl/BrokenSitePomptDataStore.kt @@ -96,7 +96,7 @@ class SharedPreferencesDuckPlayerDataStore @Inject constructor( private val dismissStreak: StateFlow = store.data .map { prefs -> - prefs[DISMISS_STREAK] ?: 7 + prefs[DISMISS_STREAK] ?: 0 } .distinctUntilChanged() .stateIn(appCoroutineScope, SharingStarted.Eagerly, 0) diff --git a/broken-site/broken-site-impl/src/main/java/com/duckduckgo/brokensite/impl/BrokenSitePromptInMemoryStore.kt b/broken-site/broken-site-impl/src/main/java/com/duckduckgo/brokensite/impl/BrokenSitePromptInMemoryStore.kt index ad3b4bd5ceb1..ab677998dd14 100644 --- a/broken-site/broken-site-impl/src/main/java/com/duckduckgo/brokensite/impl/BrokenSitePromptInMemoryStore.kt +++ b/broken-site/broken-site-impl/src/main/java/com/duckduckgo/brokensite/impl/BrokenSitePromptInMemoryStore.kt @@ -22,6 +22,7 @@ import com.squareup.anvil.annotations.ContributesBinding import dagger.SingleInstanceIn import java.time.LocalDateTime import javax.inject.Inject +import timber.log.Timber interface BrokenSitePromptInMemoryStore { fun resetRefreshCount() @@ -61,7 +62,8 @@ class RealBrokenSitePromptInMemoryStore @Inject constructor() : BrokenSitePrompt return refreshes?.let { val time = it.time.filter { time -> time.isAfter(t1) && time.isBefore(t2) } refreshes = it.copy(time = time) - it.time.size + Timber.d("Cris. $refreshes") + time.size } ?: 0 } } diff --git a/broken-site/broken-site-impl/src/main/java/com/duckduckgo/brokensite/impl/RealBrokenSitePrompt.kt b/broken-site/broken-site-impl/src/main/java/com/duckduckgo/brokensite/impl/RealBrokenSitePrompt.kt index d2e80c397558..a6e2e824ccd1 100644 --- a/broken-site/broken-site-impl/src/main/java/com/duckduckgo/brokensite/impl/RealBrokenSitePrompt.kt +++ b/broken-site/broken-site-impl/src/main/java/com/duckduckgo/brokensite/impl/RealBrokenSitePrompt.kt @@ -44,18 +44,21 @@ class RealBrokenSitePrompt @Inject constructor( override suspend fun userDismissedPrompt() { if (!_featureEnabled) return + Timber.d("Cris. Dismiss streak: ${brokenSiteReportRepository.getDismissStreak()}") if (brokenSiteReportRepository.getDismissStreak() >= brokenSiteReportRepository.getMaxDismissStreak() - 1) { + brokenSiteReportRepository.resetDismissStreak() val nextShownDate = brokenSiteReportRepository.getNextShownDate() val newNextShownDate = currentTimeProvider.localDateTimeNow().plusDays(brokenSiteReportRepository.getDismissStreakResetDays().toLong()) if (nextShownDate == null || newNextShownDate.isAfter(nextShownDate)) { brokenSiteReportRepository.setNextShownDate(newNextShownDate) - Timber.d("New next shown date: $newNextShownDate") + Timber.d("Cris. Dismiss. New next shown date: $newNextShownDate") } else { - Timber.d("Next shown date not updated to $newNextShownDate, keeping existing value: $nextShownDate") + Timber.d("Cris. Dismiss. Next shown date not updated to $newNextShownDate, keeping existing value: $nextShownDate") } + } else { + brokenSiteReportRepository.incrementDismissStreak() } - brokenSiteReportRepository.incrementDismissStreak() } override suspend fun userAcceptedPrompt() { @@ -101,9 +104,9 @@ class RealBrokenSitePrompt @Inject constructor( val newNextShownDate = currentTimeProvider.localDateTimeNow().plusDays(brokenSiteReportRepository.getCoolDownDays()) if (nextShownDate == null || newNextShownDate.isAfter(nextShownDate)) { brokenSiteReportRepository.setNextShownDate(newNextShownDate) - Timber.d("New next shown date: $newNextShownDate") + Timber.d("Cris. Shown. New next shown date: $newNextShownDate") } else { - Timber.d("Next shown date not updated to $newNextShownDate, keeping existing value: $nextShownDate") + Timber.d("Cris. Shown. Next shown date not updated to $newNextShownDate, keeping existing value: $nextShownDate") } } } diff --git a/broken-site/broken-site-impl/src/test/java/com/duckduckgo/brokensite/impl/RealBrokenSitePromptTest.kt b/broken-site/broken-site-impl/src/test/java/com/duckduckgo/brokensite/impl/RealBrokenSitePromptTest.kt index 372893020d1c..5f7383641c8e 100644 --- a/broken-site/broken-site-impl/src/test/java/com/duckduckgo/brokensite/impl/RealBrokenSitePromptTest.kt +++ b/broken-site/broken-site-impl/src/test/java/com/duckduckgo/brokensite/impl/RealBrokenSitePromptTest.kt @@ -53,7 +53,7 @@ class RealBrokenSitePromptTest { } @Test - fun whenUserDismissedPromptMaxDismissStreakTimesAndNextShownDateEarlierThanDismissStreakDaysThenIncrementDismissStreakAndUpdateNextShownDate() = + fun whenUserDismissedPromptMaxDismissStreakTimesAndNextShownDateEarlierThanDismissStreakDaysThenResetDismissStreakAndUpdateNextShownDate() = runTest { whenever(mockBrokenSiteReportRepository.getNextShownDate()).thenReturn(LocalDateTime.now().plusDays(5)) whenever(mockBrokenSiteReportRepository.getDismissStreak()).thenReturn(2) @@ -65,11 +65,11 @@ class RealBrokenSitePromptTest { val argumentCaptor = argumentCaptor() verify(mockBrokenSiteReportRepository).setNextShownDate(argumentCaptor.capture()) assertEquals(LocalDateTime.now().plusDays(30).toLocalDate(), argumentCaptor.firstValue.toLocalDate()) - verify(mockBrokenSiteReportRepository).incrementDismissStreak() + verify(mockBrokenSiteReportRepository).resetDismissStreak() } @Test - fun whenUserDismissedPromptMaxDismissStreakTimesAndNextShownDateLaterThanCooldownDaysThenIncrementDismissStreakAndDoNotUpdateNextShownDate() = + fun whenUserDismissedPromptMaxDismissStreakTimesAndNextShownDateLaterThanCooldownDaysThenResetDismissStreakAndDoNotUpdateNextShownDate() = runTest { whenever(mockBrokenSiteReportRepository.getNextShownDate()).thenReturn(LocalDateTime.now().plusDays(11)) whenever(mockBrokenSiteReportRepository.getDismissStreak()).thenReturn(2) @@ -78,7 +78,7 @@ class RealBrokenSitePromptTest { testee.userDismissedPrompt() verify(mockBrokenSiteReportRepository, never()).setNextShownDate(any()) - verify(mockBrokenSiteReportRepository).incrementDismissStreak() + verify(mockBrokenSiteReportRepository).resetDismissStreak() } @Test