package com.peachpasswords.license import android.content.Context import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import javax.inject.Inject import javax.inject.Singleton data class PendingProNotice( val feature: String, val triggeredAt: Long, ) @Singleton class ProNoticeStore @Inject constructor( @ApplicationContext context: Context, ) { private val prefs = context.getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE) private val _notice = MutableStateFlow(load()) val notice: StateFlow = _notice.asStateFlow() fun record(feature: ProFeature, nowMs: Long = System.currentTimeMillis()) { val next = PendingProNotice(feature.key, nowMs) prefs.edit() .putString(KEY_FEATURE, next.feature) .putLong(KEY_TRIGGERED_AT, next.triggeredAt) .apply() _notice.value = next } fun dismiss() { prefs.edit().remove(KEY_FEATURE).remove(KEY_TRIGGERED_AT).apply() _notice.value = null } private fun load(): PendingProNotice? { val feature = prefs.getString(KEY_FEATURE, null)?.takeIf { it.isNotBlank() } ?: return null val triggeredAt = prefs.getLong(KEY_TRIGGERED_AT, 0L).takeIf { it > 0L } ?: return null return PendingProNotice(feature, triggeredAt) } private companion object { const val PREFS_FILE = "peach_pro_notice" const val KEY_FEATURE = "feature" const val KEY_TRIGGERED_AT = "triggered_at" } } fun proNoticeText(notice: PendingProNotice): String = if (notice.feature == "sync") "Sync requires Peach Pro" else "A Pro feature requires Peach Pro"