feat: migrate LightningAddressStore to DataStore
This commit is contained in:
@@ -13,11 +13,14 @@ import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import androidx.fragment.app.FragmentActivity
|
||||
import com.bitcointxoko.gudariwallet.data.BalancePrefsStore
|
||||
import com.bitcointxoko.gudariwallet.data.LightningAddressStore
|
||||
import com.bitcointxoko.gudariwallet.ui.balance.BalanceViewModel
|
||||
import com.bitcointxoko.gudariwallet.ui.clipboard.ClipboardViewModel
|
||||
import com.bitcointxoko.gudariwallet.ui.fiat.FiatViewModel
|
||||
import com.bitcointxoko.gudariwallet.ui.receive.ReceiveViewModel
|
||||
import com.bitcointxoko.gudariwallet.ui.send.SendViewModel
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
|
||||
private const val TAG = "WalletViewModel"
|
||||
|
||||
@@ -26,12 +29,12 @@ class WalletViewModel(
|
||||
val aliasRepo : NodeAliasRepository,
|
||||
val paymentCache: PaymentCacheRepository,
|
||||
private val balancePrefs: BalancePrefsStore,
|
||||
private val lnAddressStore : LightningAddressStore,
|
||||
val balanceVm : BalanceViewModel,
|
||||
val fiatVm : FiatViewModel,
|
||||
val receiveVm : ReceiveViewModel,
|
||||
val sendVm : SendViewModel,
|
||||
val clipboardVm : ClipboardViewModel,
|
||||
val lightningAddress: MutableStateFlow<String?>,
|
||||
) : ViewModel() {
|
||||
val balanceState: StateFlow<BalanceState> get() = balanceVm.balanceState
|
||||
val balanceHidden: StateFlow<Boolean> get() = fiatVm.balanceHidden
|
||||
@@ -63,6 +66,12 @@ class WalletViewModel(
|
||||
val clipboardOfferState: StateFlow<ClipboardOfferState> get() = clipboardVm.clipboardOfferState
|
||||
fun checkClipboard(text: String?) = clipboardVm.checkClipboard(text)
|
||||
fun dismissClipboardOffer() = clipboardVm.dismissClipboardOffer()
|
||||
val lightningAddress: StateFlow<String?> = lnAddressStore.lightningAddressFlow
|
||||
.stateIn(
|
||||
scope = viewModelScope,
|
||||
started = SharingStarted.Eagerly,
|
||||
initialValue = null
|
||||
)
|
||||
|
||||
init {
|
||||
observePaymentEvents()
|
||||
@@ -80,22 +89,26 @@ class WalletViewModel(
|
||||
}
|
||||
|
||||
private fun fetchLightningAddress() {
|
||||
// If we already have a persisted address, show it immediately and
|
||||
// still refresh in the background (address could have changed).
|
||||
// lightningAddressFlow already emits the cached DataStore value immediately
|
||||
// via SharingStarted.Eagerly — this coroutine only does the network refresh.
|
||||
viewModelScope.launch {
|
||||
if (!lnAddressStore.isCacheStale()) {
|
||||
Log.d(TAG, "LNADDR [TTL ] cache is fresh — skipping network fetch")
|
||||
return@launch
|
||||
}
|
||||
runCatching { repo.getLightningAddress() }
|
||||
.onSuccess { address ->
|
||||
if (address != null) {
|
||||
Log.d(TAG, "LNADDR [FETCH OK ] $address")
|
||||
lightningAddress.value = address
|
||||
balancePrefs.setLightningAddress(address)
|
||||
Log.d(TAG, "LNADDR [FETCH OK ] fetched from API: \"$address\" — updating cache")
|
||||
lnAddressStore.setLightningAddress(address)
|
||||
// StateFlow updates automatically via the DataStore flow
|
||||
} else {
|
||||
Log.d(TAG, "LNADDR [FETCH OK ] no pay link / username found")
|
||||
Log.d(TAG, "LNADDR [FETCH OK ] fetched from API: no pay link / username configured")
|
||||
}
|
||||
}
|
||||
.onFailure { e ->
|
||||
Log.w(TAG, "LNADDR [FETCH ERR] ${e.message} — keeping cached value")
|
||||
// Keep whatever was loaded from prefs; don't clear it on network error
|
||||
Log.w(TAG, "LNADDR [FETCH ERR] API call failed (${e.message}) — falling back to cached value")
|
||||
// Nothing to do — DataStore flow already holds the last good value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user