From dc972f252894a147e72ab5176129ae6c5658ffb9 Mon Sep 17 00:00:00 2001 From: Eljo Date: Mon, 14 Dec 2020 17:10:50 +0100 Subject: [PATCH] refactor code --- .../demos/getbalance/GetBalanceFragment.kt | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/samples/demo-app/app/src/main/java/cash/z/ecc/android/sdk/demoapp/demos/getbalance/GetBalanceFragment.kt b/samples/demo-app/app/src/main/java/cash/z/ecc/android/sdk/demoapp/demos/getbalance/GetBalanceFragment.kt index a7874b04..6edda8e2 100644 --- a/samples/demo-app/app/src/main/java/cash/z/ecc/android/sdk/demoapp/demos/getbalance/GetBalanceFragment.kt +++ b/samples/demo-app/app/src/main/java/cash/z/ecc/android/sdk/demoapp/demos/getbalance/GetBalanceFragment.kt @@ -15,9 +15,13 @@ import cash.z.ecc.android.sdk.ext.collectWith import cash.z.ecc.android.sdk.ext.convertZatoshiToZecString import cash.z.ecc.android.sdk.tool.DerivationTool +/** + * Displays the available balance && total balance associated with the seed defined by the default config. + * comments. + */ class GetBalanceFragment : BaseDemoFragment() { - private lateinit var synchronize: Synchronizer + private lateinit var synchronizer: Synchronizer override fun inflateBinding(layoutInflater: LayoutInflater): FragmentGetBalanceBinding = FragmentGetBalanceBinding.inflate(layoutInflater) @@ -44,7 +48,7 @@ class GetBalanceFragment : BaseDemoFragment() { it.importWallet(viewingKey, config.birthdayHeight) it.server(config.host, config.port) }.let { initializer -> - synchronize = Synchronizer(initializer) + synchronizer = Synchronizer(initializer) } } } @@ -52,36 +56,39 @@ class GetBalanceFragment : BaseDemoFragment() { override fun onResume() { super.onResume() // the lifecycleScope is used to dispose of the synchronize when the fragment dies - synchronize.start(lifecycleScope) + synchronizer.start(lifecycleScope) monitorChanges() } private fun monitorChanges() { - synchronize.status.collectWith(lifecycleScope, ::onStatus) - synchronize.balances.collectWith(lifecycleScope, ::onBalance) + synchronizer.status.collectWith(lifecycleScope, ::onStatus) + synchronizer.balances.collectWith(lifecycleScope, ::onBalance) } - private var isSyncing = true - private fun onBalance(balance: CompactBlockProcessor.WalletBalance) { - this.balance = balance - if (!isSyncing) { binding.textBalance.text = """ Available balance: ${balance.availableZatoshi.convertZatoshiToZecString(12)} Total balance: ${balance.totalZatoshi.convertZatoshiToZecString(12)} """.trimIndent() - } + } - private var balance = CompactBlockProcessor.WalletBalance() private fun onStatus(status: Synchronizer.Status) { binding.textBalance.text = "Status: $status" - isSyncing = status != Synchronizer.Status.SYNCED - if (status == Synchronizer.Status.SCANNING) { + if (CompactBlockProcessor.WalletBalance().none()) { binding.textBalance.text = "Calculating balance..." } else { - if (!isSyncing) onBalance(balance) + onBalance(synchronizer.latestBalance) } } + + /** + * Extension function which checks if the balance has been updated or its -1 + */ + private fun CompactBlockProcessor.WalletBalance.none(): Boolean{ + if(synchronizer.latestBalance.totalZatoshi == -1L + && synchronizer.latestBalance.availableZatoshi == -1L) return true + return false + } }