ProNoticeStore.kt

1.8 KB · raw

1package com.peachpasswords.license23import android.content.Context4import dagger.hilt.android.qualifiers.ApplicationContext5import kotlinx.coroutines.flow.MutableStateFlow6import kotlinx.coroutines.flow.StateFlow7import kotlinx.coroutines.flow.asStateFlow8import javax.inject.Inject9import javax.inject.Singleton1011data class PendingProNotice(12    val feature: String,13    val triggeredAt: Long,14)1516@Singleton17class ProNoticeStore @Inject constructor(18    @ApplicationContext context: Context,19) {20    private val prefs = context.getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE)21    private val _notice = MutableStateFlow(load())22    val notice: StateFlow<PendingProNotice?> = _notice.asStateFlow()2324    fun record(feature: ProFeature, nowMs: Long = System.currentTimeMillis()) {25        val next = PendingProNotice(feature.key, nowMs)26        prefs.edit()27            .putString(KEY_FEATURE, next.feature)28            .putLong(KEY_TRIGGERED_AT, next.triggeredAt)29            .apply()30        _notice.value = next31    }3233    fun dismiss() {34        prefs.edit().remove(KEY_FEATURE).remove(KEY_TRIGGERED_AT).apply()35        _notice.value = null36    }3738    private fun load(): PendingProNotice? {39        val feature = prefs.getString(KEY_FEATURE, null)?.takeIf { it.isNotBlank() } ?: return null40        val triggeredAt = prefs.getLong(KEY_TRIGGERED_AT, 0L).takeIf { it > 0L } ?: return null41        return PendingProNotice(feature, triggeredAt)42    }4344    private companion object {45        const val PREFS_FILE = "peach_pro_notice"46        const val KEY_FEATURE = "feature"47        const val KEY_TRIGGERED_AT = "triggered_at"48    }49}5051fun proNoticeText(notice: PendingProNotice): String =52    if (notice.feature == "sync") "Sync requires Peach Pro"53    else "A Pro feature requires Peach Pro"