Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions app/src/main/java/to/bitkit/data/AppStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ package to.bitkit.data

import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.serialization.encodeToString
import to.bitkit.di.json
import to.bitkit.models.BalanceState
import to.bitkit.utils.Logger
import javax.inject.Inject
import kotlin.reflect.KProperty

Expand All @@ -19,14 +24,36 @@ class AppStorage @Inject constructor(
var bolt11: String by SharedPrefDelegate(Key.BOLT11)
var bip21: String by SharedPrefDelegate(Key.BIP21)

fun cacheBalance(balanceState: BalanceState) {
try {
val jsonData = json.encodeToString(balanceState)
sharedPreferences.edit { putString(Key.BALANCE.name, jsonData) }

} catch (e: Throwable) {
Logger.debug("Failed to cache balance: $e")
}
}

fun loadBalance(): BalanceState? {
val jsonData = sharedPreferences.getString(Key.BALANCE.name, null) ?: return null

return try {
json.decodeFromString(jsonData)
} catch (e: Throwable) {
Logger.debug("Failed to load cached balance: $e")
null
}
}

enum class Key {
ONCHAIN_ADDRESS,
BOLT11,
BIP21,
BALANCE,
}

fun clear() {
sharedPreferences.edit().clear().apply()
sharedPreferences.edit { clear() }
}
}

Expand All @@ -36,6 +63,6 @@ private class SharedPrefDelegate(private val key: AppStorage.Key) {
}

operator fun setValue(thisRef: AppStorage, property: KProperty<*>, value: String) {
thisRef.sharedPreferences.edit().putString(key.name, value).apply()
thisRef.sharedPreferences.edit { putString(key.name, value) }
}
}
3 changes: 3 additions & 0 deletions app/src/main/java/to/bitkit/models/BalanceState.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package to.bitkit.models

import kotlinx.serialization.Serializable

@Serializable
data class BalanceState(
val totalOnchainSats: ULong = 0uL,
val totalLightningSats: ULong = 0uL,
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/to/bitkit/ui/ContentView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavHostController
Expand Down Expand Up @@ -185,7 +186,7 @@ fun ContentView(
walletViewModel.isRestoringWallet = false
}
} else {
val balance by walletViewModel.balanceState.collectAsState()
val balance by walletViewModel.balanceState.collectAsStateWithLifecycle()
val currencies by currencyViewModel.uiState.collectAsState()

CompositionLocalProvider(
Expand Down
18 changes: 10 additions & 8 deletions app/src/main/java/to/bitkit/viewmodels/WalletViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class WalletViewModel @Inject constructor(
private val _uiState = MutableStateFlow(MainUiState())
val uiState = _uiState.asStateFlow()

private val _balanceState = MutableStateFlow(BalanceState())
private val _balanceState = MutableStateFlow(appStorage.loadBalance() ?: BalanceState())
val balanceState = _balanceState.asStateFlow()

private var _nodeLifecycleState: NodeLifecycleState = NodeLifecycleState.Stopped
Expand Down Expand Up @@ -195,13 +195,15 @@ class WalletViewModel @Inject constructor(
lightningService.balances?.let { balance ->
_uiState.update { it.copy(balanceDetails = balance) }
val totalSats = balance.totalLightningBalanceSats + balance.totalOnchainBalanceSats
_balanceState.update {
it.copy(
totalOnchainSats = balance.totalOnchainBalanceSats,
totalLightningSats = balance.totalLightningBalanceSats,
totalSats = totalSats,
)
}

val newBalance = BalanceState(
totalOnchainSats = balance.totalOnchainBalanceSats,
totalLightningSats = balance.totalLightningBalanceSats,
totalSats = totalSats,
)
_balanceState.update { newBalance }
appStorage.cacheBalance(newBalance)

if (totalSats > 0u) {
viewModelScope.launch {
settingsStore.setShowEmptyState(false)
Expand Down