Skip to content

Commit

Permalink
Fix dismiss streak
Browse files Browse the repository at this point in the history
  • Loading branch information
CrisBarreiro committed Nov 7, 2024
1 parent 2157623 commit b0b1d34
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class SharedPreferencesDuckPlayerDataStore @Inject constructor(

private val dismissStreak: StateFlow<Int> = store.data
.map { prefs ->
prefs[DISMISS_STREAK] ?: 7
prefs[DISMISS_STREAK] ?: 0
}
.distinctUntilChanged()
.stateIn(appCoroutineScope, SharingStarted.Eagerly, 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -65,11 +65,11 @@ class RealBrokenSitePromptTest {
val argumentCaptor = argumentCaptor<LocalDateTime>()
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)
Expand All @@ -78,7 +78,7 @@ class RealBrokenSitePromptTest {
testee.userDismissedPrompt()

verify(mockBrokenSiteReportRepository, never()).setNextShownDate(any())
verify(mockBrokenSiteReportRepository).incrementDismissStreak()
verify(mockBrokenSiteReportRepository).resetDismissStreak()
}

@Test
Expand Down

0 comments on commit b0b1d34

Please sign in to comment.