Skip to content

Commit b96d7ec

Browse files
authored
Merge pull request #75 from synonymdev/feat/cache-balance
Cache balance and load it on startup
2 parents 07c5830 + 51f85ed commit b96d7ec

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

app/src/main/java/to/bitkit/data/AppStorage.kt

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ package to.bitkit.data
22

33
import android.content.Context
44
import android.content.SharedPreferences
5+
import androidx.core.content.edit
56
import dagger.hilt.android.qualifiers.ApplicationContext
7+
import kotlinx.serialization.encodeToString
8+
import to.bitkit.di.json
9+
import to.bitkit.models.BalanceState
10+
import to.bitkit.utils.Logger
611
import javax.inject.Inject
712
import kotlin.reflect.KProperty
813

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

27+
fun cacheBalance(balanceState: BalanceState) {
28+
try {
29+
val jsonData = json.encodeToString(balanceState)
30+
sharedPreferences.edit { putString(Key.BALANCE.name, jsonData) }
31+
32+
} catch (e: Throwable) {
33+
Logger.debug("Failed to cache balance: $e")
34+
}
35+
}
36+
37+
fun loadBalance(): BalanceState? {
38+
val jsonData = sharedPreferences.getString(Key.BALANCE.name, null) ?: return null
39+
40+
return try {
41+
json.decodeFromString(jsonData)
42+
} catch (e: Throwable) {
43+
Logger.debug("Failed to load cached balance: $e")
44+
null
45+
}
46+
}
47+
2248
enum class Key {
2349
ONCHAIN_ADDRESS,
2450
BOLT11,
2551
BIP21,
52+
BALANCE,
2653
}
2754

2855
fun clear() {
29-
sharedPreferences.edit().clear().apply()
56+
sharedPreferences.edit { clear() }
3057
}
3158
}
3259

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

3865
operator fun setValue(thisRef: AppStorage, property: KProperty<*>, value: String) {
39-
thisRef.sharedPreferences.edit().putString(key.name, value).apply()
66+
thisRef.sharedPreferences.edit { putString(key.name, value) }
4067
}
4168
}

app/src/main/java/to/bitkit/models/BalanceState.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package to.bitkit.models
22

3+
import kotlinx.serialization.Serializable
4+
5+
@Serializable
36
data class BalanceState(
47
val totalOnchainSats: ULong = 0uL,
58
val totalLightningSats: ULong = 0uL,

app/src/main/java/to/bitkit/ui/ContentView.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import androidx.hilt.navigation.compose.hiltViewModel
1818
import androidx.lifecycle.Lifecycle
1919
import androidx.lifecycle.LifecycleEventObserver
2020
import androidx.lifecycle.compose.LocalLifecycleOwner
21+
import androidx.lifecycle.compose.collectAsStateWithLifecycle
2122
import androidx.navigation.NavController
2223
import androidx.navigation.NavGraphBuilder
2324
import androidx.navigation.NavHostController
@@ -185,7 +186,7 @@ fun ContentView(
185186
walletViewModel.isRestoringWallet = false
186187
}
187188
} else {
188-
val balance by walletViewModel.balanceState.collectAsState()
189+
val balance by walletViewModel.balanceState.collectAsStateWithLifecycle()
189190
val currencies by currencyViewModel.uiState.collectAsState()
190191

191192
CompositionLocalProvider(

app/src/main/java/to/bitkit/viewmodels/WalletViewModel.kt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class WalletViewModel @Inject constructor(
6161
private val _uiState = MutableStateFlow(MainUiState())
6262
val uiState = _uiState.asStateFlow()
6363

64-
private val _balanceState = MutableStateFlow(BalanceState())
64+
private val _balanceState = MutableStateFlow(appStorage.loadBalance() ?: BalanceState())
6565
val balanceState = _balanceState.asStateFlow()
6666

6767
private var _nodeLifecycleState: NodeLifecycleState = NodeLifecycleState.Stopped
@@ -195,13 +195,15 @@ class WalletViewModel @Inject constructor(
195195
lightningService.balances?.let { balance ->
196196
_uiState.update { it.copy(balanceDetails = balance) }
197197
val totalSats = balance.totalLightningBalanceSats + balance.totalOnchainBalanceSats
198-
_balanceState.update {
199-
it.copy(
200-
totalOnchainSats = balance.totalOnchainBalanceSats,
201-
totalLightningSats = balance.totalLightningBalanceSats,
202-
totalSats = totalSats,
203-
)
204-
}
198+
199+
val newBalance = BalanceState(
200+
totalOnchainSats = balance.totalOnchainBalanceSats,
201+
totalLightningSats = balance.totalLightningBalanceSats,
202+
totalSats = totalSats,
203+
)
204+
_balanceState.update { newBalance }
205+
appStorage.cacheBalance(newBalance)
206+
205207
if (totalSats > 0u) {
206208
viewModelScope.launch {
207209
settingsStore.setShowEmptyState(false)

0 commit comments

Comments
 (0)