Skip to content

Commit

Permalink
Migrate from StateFlow to Flow
Browse files Browse the repository at this point in the history
  • Loading branch information
CrisBarreiro committed Nov 7, 2024
1 parent e415dbb commit cde1268
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,9 @@ import java.time.format.DateTimeFormatter
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn

interface BrokenSitePomptDataStore {
suspend fun setMaxDismissStreak(maxDismissStreak: Int)
Expand All @@ -53,9 +50,9 @@ interface BrokenSitePomptDataStore {
suspend fun setCoolDownDays(days: Int)
suspend fun getCoolDownDays(): Int
suspend fun setDismissStreak(streak: Int)
fun getDismissStreak(): Int
suspend fun getDismissStreak(): Int
suspend fun setNextShownDate(nextShownDate: LocalDate?)
fun getNextShownDate(): LocalDate?
suspend fun getNextShownDate(): LocalDate?
}

@ContributesBinding(AppScope::class)
Expand Down Expand Up @@ -93,19 +90,17 @@ class SharedPreferencesDuckPlayerDataStore @Inject constructor(
}
.distinctUntilChanged()

private val dismissStreak: StateFlow<Int> = store.data
private val dismissStreak: Flow<Int> = store.data
.map { prefs ->
prefs[DISMISS_STREAK] ?: 7
}
.distinctUntilChanged()
.stateIn(appCoroutineScope, SharingStarted.Eagerly, 0)

private val nextShownDate: StateFlow<String?> = store.data
private val nextShownDate: Flow<String?> = store.data
.map { prefs ->
prefs[NEXT_SHOWN_DATE]
}
.distinctUntilChanged()
.stateIn(appCoroutineScope, SharingStarted.Eagerly, null)

override suspend fun setMaxDismissStreak(maxDismissStreak: Int) {
store.edit { prefs -> prefs[MAX_DISMISS_STREAK] = maxDismissStreak }
Expand Down Expand Up @@ -140,11 +135,11 @@ class SharedPreferencesDuckPlayerDataStore @Inject constructor(
}
}

override fun getDismissStreak(): Int {
return dismissStreak.value
override suspend fun getDismissStreak(): Int {
return dismissStreak.first()
}

override fun getNextShownDate(): LocalDate? {
return nextShownDate.value?.let { LocalDate.parse(it, formatter) }
override suspend fun getNextShownDate(): LocalDate? {
return nextShownDate.first()?.let { LocalDate.parse(it, formatter) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ interface BrokenSiteReportRepository {
suspend fun setBrokenSitePromptRCSettings(maxDismissStreak: Int, dismissStreakResetDays: Int, coolDownDays: Int)

suspend fun setNextShownDate(nextShownDate: LocalDate?)
fun getNextShownDate(): LocalDate?
suspend fun getNextShownDate(): LocalDate?

suspend fun incrementDismissStreak()
fun getDismissStreak(): Int
suspend fun getDismissStreak(): Int
suspend fun resetDismissStreak()
}

Expand Down Expand Up @@ -133,11 +133,11 @@ class RealBrokenSiteReportRepository(
brokenSitePromptDataStore.setNextShownDate(nextShownDate)
}

override fun getDismissStreak(): Int {
override suspend fun getDismissStreak(): Int {
return brokenSitePromptDataStore.getDismissStreak()
}

override fun getNextShownDate(): LocalDate? {
override suspend fun getNextShownDate(): LocalDate? {
return brokenSitePromptDataStore.getNextShownDate()
}

Expand Down

0 comments on commit cde1268

Please sign in to comment.