feat: migrate LightningAddressStore to DataStore

This commit is contained in:
2026-06-02 01:57:29 +02:00
parent 54e06506ae
commit fabf18760d
5 changed files with 101 additions and 21 deletions
@@ -55,13 +55,4 @@ class BalancePrefsStore(context: Context) {
else remove("fiat_currency")
}
}
// ── Lightning address ─────────────────────────────────────────────────────
val lightningAddress: String?
get() = prefs.getString("lightning_address", null)
fun setLightningAddress(address: String) {
prefs.edit { putString("lightning_address", address) }
}
}
@@ -0,0 +1,72 @@
package com.bitcointxoko.gudariwallet.data
import android.content.Context
import android.util.Log
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.emptyPreferences
import androidx.datastore.preferences.core.longPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import com.bitcointxoko.gudariwallet.util.WalletConstants.LNADDRESS_TTL_MS
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import java.io.IOException
private val Context.lightningAddressDataStore: DataStore<Preferences>
by preferencesDataStore(name = "lightning_address")
class LightningAddressStore(private val context: Context) {
companion object {
private val KEY_ADDRESS = stringPreferencesKey("lightning_address")
private val KEY_FETCHED_AT = longPreferencesKey("lightning_address_fetched_at")
}
// In-memory snapshot — safe for ClipboardViewModel's synchronous lambda
@Volatile private var _currentAddress: String? = null
val currentAddress: String? get() = _currentAddress
val lightningAddressFlow: Flow<String?> = context.lightningAddressDataStore.data
.catch { e ->
if (e is IOException) emit(emptyPreferences())
else throw e
}
.map { prefs ->
prefs[KEY_ADDRESS].also { address ->
_currentAddress = address
Log.d("LightningAddressStore", "LNADDR [CACHE ] read from cache: " +
if (address != null) "\"$address\"" else "(empty — nothing cached yet)") }
}
suspend fun isCacheStale(): Boolean {
return context.lightningAddressDataStore.data
.catch { e ->
if (e is IOException) emit(emptyPreferences())
else throw e
}
.map { prefs ->
val address = prefs[KEY_ADDRESS]
val fetchedAt = prefs[KEY_FETCHED_AT] ?: 0L
val age = System.currentTimeMillis() - fetchedAt
val isStale = address == null || age > LNADDRESS_TTL_MS
Log.d("LightningAddressStore", "LNADDR [TTL ] cached=${address != null} " +
"age=${age / 1000}s ttl=${LNADDRESS_TTL_MS / 1000}s " +
if (isStale) "→ cache stale, will fetch from API"
else "→ cache fresh, skipping API call")
isStale
}
.first()
}
suspend fun setLightningAddress(address: String) {
context.lightningAddressDataStore.edit { prefs ->
prefs[KEY_ADDRESS] = address
prefs[KEY_FETCHED_AT] = System.currentTimeMillis() // ← add
}
}
}