@@ -3,8 +3,13 @@ package to.bitkit.data
33import android.content.Context
44import androidx.datastore.core.DataStore
55import androidx.datastore.dataStore
6+ import androidx.datastore.preferences.core.Preferences
7+ import androidx.datastore.preferences.core.booleanPreferencesKey
8+ import androidx.datastore.preferences.core.edit
9+ import androidx.datastore.preferences.preferencesDataStore
610import dagger.hilt.android.qualifiers.ApplicationContext
711import kotlinx.coroutines.flow.Flow
12+ import kotlinx.coroutines.flow.map
813import kotlinx.serialization.Serializable
914import to.bitkit.data.serializers.SettingsSerializer
1015import to.bitkit.env.Env
@@ -24,13 +29,17 @@ private val Context.settingsDataStore: DataStore<SettingsData> by dataStore(
2429 serializer = SettingsSerializer ,
2530)
2631
32+ private val Context .localSettingsDataStore: DataStore <Preferences > by preferencesDataStore(" local_settings" )
33+
2734@Singleton
2835class SettingsStore @Inject constructor(
2936 @ApplicationContext private val context : Context ,
3037) {
3138 private val store = context.settingsDataStore
39+ private val localStore = context.localSettingsDataStore
3240
3341 val data: Flow <SettingsData > = store.data
42+ val isPaykitEnabled: Flow <Boolean > = localStore.data.map { it[PAYKIT_ENABLED_KEY ] ? : false }
3443
3544 @Volatile
3645 var restoredMonitoredTypesFromBackup: Boolean = false
@@ -53,6 +62,10 @@ class SettingsStore @Inject constructor(
5362 store.updateData(transform)
5463 }
5564
65+ suspend fun setIsPaykitEnabled (value : Boolean ) {
66+ localStore.edit { it[PAYKIT_ENABLED_KEY ] = value }
67+ }
68+
5669 suspend fun addLastUsedTag (newTag : String ) {
5770 store.updateData { currentSettings ->
5871 val combinedTags = (listOf (newTag) + currentSettings.lastUsedTags).distinct()
@@ -76,13 +89,15 @@ class SettingsStore @Inject constructor(
7689
7790 suspend fun reset () {
7891 store.updateData { SettingsData () }
92+ localStore.edit { it.clear() }
7993 restoredMonitoredTypesFromBackup = false
8094 Logger .info(" Deleted all user settings data." )
8195 }
8296
8397 companion object {
8498 private const val TAG = " SettingsStore"
8599 private const val MAX_LAST_USED_TAGS = 10
100+ private val PAYKIT_ENABLED_KEY = booleanPreferencesKey(" paykit_enabled" )
86101 }
87102}
88103
@@ -103,6 +118,7 @@ data class SettingsData(
103118 val hasConfirmedPublicPaykitEndpoints : Boolean = false ,
104119 val sharesPublicPaykitEndpoints : Boolean = false ,
105120 val sharesPrivatePaykitEndpoints : Boolean = false ,
121+ val publicPaykitCleanupPending : Boolean = false ,
106122 val publicPaykitLightningEnabled : Boolean = true ,
107123 val publicPaykitOnchainEnabled : Boolean = true ,
108124 val publicPaykitBolt11 : String = " " ,
@@ -148,3 +164,25 @@ fun SettingsData.resetPin() = this.copy(
148164 isPinForPaymentsEnabled = false ,
149165 isBiometricEnabled = false ,
150166)
167+
168+ fun SettingsData.hasPublicPaykitPublicationState (): Boolean =
169+ hasConfirmedPublicPaykitEndpoints ||
170+ sharesPublicPaykitEndpoints ||
171+ publicPaykitCleanupPending ||
172+ publicPaykitBolt11.isNotBlank() ||
173+ publicPaykitBolt11PaymentHash.isNotBlank() ||
174+ publicPaykitBolt11ExpiresAtMillis > 0L
175+
176+ fun SettingsData.hasPaykitState (): Boolean =
177+ hasPublicPaykitPublicationState() ||
178+ sharesPrivatePaykitEndpoints
179+
180+ fun SettingsData.paykitDisabled (markPublicCleanupPending : Boolean = false) = copy(
181+ hasConfirmedPublicPaykitEndpoints = false ,
182+ sharesPublicPaykitEndpoints = false ,
183+ sharesPrivatePaykitEndpoints = false ,
184+ publicPaykitCleanupPending = publicPaykitCleanupPending || markPublicCleanupPending,
185+ publicPaykitBolt11 = " " ,
186+ publicPaykitBolt11PaymentHash = " " ,
187+ publicPaykitBolt11ExpiresAtMillis = 0 ,
188+ )
0 commit comments