2020-12-11 13:20:56 -08:00
|
|
|
package cash.z.ecc.android.sdk.demoapp.demos.getbalance
|
|
|
|
|
2020-12-13 06:08:42 -08:00
|
|
|
import android.os.Bundle
|
2020-12-11 13:20:56 -08:00
|
|
|
import android.view.LayoutInflater
|
2020-12-13 06:08:42 -08:00
|
|
|
import androidx.lifecycle.lifecycleScope
|
|
|
|
import cash.z.ecc.android.bip39.Mnemonics
|
|
|
|
import cash.z.ecc.android.bip39.toSeed
|
|
|
|
import cash.z.ecc.android.sdk.Synchronizer
|
|
|
|
import cash.z.ecc.android.sdk.block.CompactBlockProcessor
|
2020-12-11 13:20:56 -08:00
|
|
|
import cash.z.ecc.android.sdk.demoapp.BaseDemoFragment
|
|
|
|
import cash.z.ecc.android.sdk.demoapp.databinding.FragmentGetBalanceBinding
|
2021-09-04 04:05:41 -07:00
|
|
|
import cash.z.ecc.android.sdk.demoapp.ext.requireApplicationContext
|
|
|
|
import cash.z.ecc.android.sdk.demoapp.util.fromResources
|
2020-12-13 06:08:42 -08:00
|
|
|
import cash.z.ecc.android.sdk.ext.collectWith
|
|
|
|
import cash.z.ecc.android.sdk.ext.convertZatoshiToZecString
|
2022-08-02 06:29:09 -07:00
|
|
|
import cash.z.ecc.android.sdk.model.LightWalletEndpoint
|
2022-07-07 05:52:07 -07:00
|
|
|
import cash.z.ecc.android.sdk.model.WalletBalance
|
2022-08-02 06:29:09 -07:00
|
|
|
import cash.z.ecc.android.sdk.model.ZcashNetwork
|
|
|
|
import cash.z.ecc.android.sdk.model.defaultForNetwork
|
2022-06-21 16:34:42 -07:00
|
|
|
import kotlinx.coroutines.flow.filterNotNull
|
2020-12-11 13:20:56 -08:00
|
|
|
|
2020-12-14 08:10:50 -08:00
|
|
|
/**
|
|
|
|
* Displays the available balance && total balance associated with the seed defined by the default config.
|
|
|
|
* comments.
|
|
|
|
*/
|
2020-12-11 13:20:56 -08:00
|
|
|
class GetBalanceFragment : BaseDemoFragment<FragmentGetBalanceBinding>() {
|
|
|
|
|
2020-12-14 08:10:50 -08:00
|
|
|
private lateinit var synchronizer: Synchronizer
|
2020-12-13 06:08:42 -08:00
|
|
|
|
2020-12-11 13:20:56 -08:00
|
|
|
override fun inflateBinding(layoutInflater: LayoutInflater): FragmentGetBalanceBinding =
|
|
|
|
FragmentGetBalanceBinding.inflate(layoutInflater)
|
|
|
|
|
2021-09-04 04:05:41 -07:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
2020-12-13 06:08:42 -08:00
|
|
|
setup()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun setup() {
|
2020-12-13 06:14:05 -08:00
|
|
|
// defaults to the value of `DemoConfig.seedWords` but can also be set by the user
|
|
|
|
val seedPhrase = sharedViewModel.seedPhrase.value
|
2020-12-13 06:08:42 -08:00
|
|
|
|
2020-12-13 06:14:05 -08:00
|
|
|
// Use a BIP-39 library to convert a seed phrase into a byte array. Most wallets already
|
|
|
|
// have the seed stored
|
2020-12-13 06:08:42 -08:00
|
|
|
val seed = Mnemonics.MnemonicCode(seedPhrase).toSeed()
|
|
|
|
|
2022-10-06 10:48:21 -07:00
|
|
|
val network = ZcashNetwork.fromResources(requireApplicationContext())
|
|
|
|
synchronizer = Synchronizer.newBlocking(
|
|
|
|
requireApplicationContext(),
|
|
|
|
network,
|
|
|
|
lightWalletEndpoint = LightWalletEndpoint.defaultForNetwork(network),
|
|
|
|
seed = seed,
|
2022-10-26 18:37:40 -07:00
|
|
|
birthday = sharedViewModel.birthdayHeight.value
|
2022-10-06 10:48:21 -07:00
|
|
|
)
|
2020-12-13 06:08:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
2020-12-13 06:14:05 -08:00
|
|
|
// the lifecycleScope is used to dispose of the synchronize when the fragment dies
|
2020-12-14 08:10:50 -08:00
|
|
|
synchronizer.start(lifecycleScope)
|
2020-12-13 06:08:42 -08:00
|
|
|
monitorChanges()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun monitorChanges() {
|
2020-12-14 08:10:50 -08:00
|
|
|
synchronizer.status.collectWith(lifecycleScope, ::onStatus)
|
2020-12-19 12:38:25 -08:00
|
|
|
synchronizer.progress.collectWith(lifecycleScope, ::onProgress)
|
|
|
|
synchronizer.processorInfo.collectWith(lifecycleScope, ::onProcessorInfoUpdated)
|
2022-06-21 16:34:42 -07:00
|
|
|
synchronizer.saplingBalances.filterNotNull().collectWith(lifecycleScope, ::onBalance)
|
2020-12-13 06:08:42 -08:00
|
|
|
}
|
|
|
|
|
2022-08-23 06:49:00 -07:00
|
|
|
@Suppress("MagicNumber")
|
2021-09-04 04:05:41 -07:00
|
|
|
private fun onBalance(balance: WalletBalance) {
|
2021-09-25 08:11:33 -07:00
|
|
|
binding.textBalance.text = """
|
2022-06-21 16:34:42 -07:00
|
|
|
Available balance: ${balance.available.convertZatoshiToZecString(12)}
|
|
|
|
Total balance: ${balance.total.convertZatoshiToZecString(12)}
|
2021-09-25 08:11:33 -07:00
|
|
|
""".trimIndent()
|
2020-12-13 06:08:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun onStatus(status: Synchronizer.Status) {
|
2020-12-19 12:38:25 -08:00
|
|
|
binding.textStatus.text = "Status: $status"
|
2022-07-12 05:40:09 -07:00
|
|
|
val balance: WalletBalance? = synchronizer.saplingBalances.value
|
2022-06-21 16:34:42 -07:00
|
|
|
if (null == balance) {
|
2020-12-13 06:08:42 -08:00
|
|
|
binding.textBalance.text = "Calculating balance..."
|
|
|
|
} else {
|
2022-06-21 16:34:42 -07:00
|
|
|
onBalance(balance)
|
2020-12-13 06:08:42 -08:00
|
|
|
}
|
|
|
|
}
|
2020-12-14 08:10:50 -08:00
|
|
|
|
2022-08-23 06:49:00 -07:00
|
|
|
@Suppress("MagicNumber")
|
2020-12-19 12:38:25 -08:00
|
|
|
private fun onProgress(i: Int) {
|
|
|
|
if (i < 100) {
|
|
|
|
binding.textStatus.text = "Downloading blocks...$i%"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun onProcessorInfoUpdated(info: CompactBlockProcessor.ProcessorInfo) {
|
|
|
|
if (info.isScanning) binding.textStatus.text = "Scanning blocks...${info.scanProgress}%"
|
|
|
|
}
|
2020-12-11 13:20:56 -08:00
|
|
|
}
|