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.Initializer
|
|
|
|
import cash.z.ecc.android.sdk.Synchronizer
|
|
|
|
import cash.z.ecc.android.sdk.block.CompactBlockProcessor
|
|
|
|
import cash.z.ecc.android.sdk.demoapp.App
|
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
|
2020-12-13 06:08:42 -08:00
|
|
|
import cash.z.ecc.android.sdk.ext.collectWith
|
|
|
|
import cash.z.ecc.android.sdk.ext.convertZatoshiToZecString
|
|
|
|
import cash.z.ecc.android.sdk.tool.DerivationTool
|
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)
|
|
|
|
|
2020-12-13 06:08:42 -08:00
|
|
|
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
|
|
|
super.onActivityCreated(savedInstanceState)
|
|
|
|
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()
|
|
|
|
|
2020-12-13 06:14:05 -08:00
|
|
|
// converting seed into viewingKey
|
|
|
|
val viewingKey = DerivationTool.deriveViewingKeys(seed).first()
|
2020-12-13 06:08:42 -08:00
|
|
|
|
2020-12-13 06:14:05 -08:00
|
|
|
// using the ViewingKey to initialize
|
2020-12-13 06:08:42 -08:00
|
|
|
App.instance.defaultConfig.let { config ->
|
|
|
|
Initializer(App.instance) {
|
|
|
|
it.importWallet(viewingKey, config.birthdayHeight)
|
|
|
|
it.server(config.host, config.port)
|
|
|
|
}.let { initializer ->
|
2020-12-14 08:10:50 -08:00
|
|
|
synchronizer = Synchronizer(initializer)
|
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)
|
|
|
|
synchronizer.balances.collectWith(lifecycleScope, ::onBalance)
|
2020-12-13 06:08:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun onBalance(balance: CompactBlockProcessor.WalletBalance) {
|
|
|
|
binding.textBalance.text = """
|
|
|
|
Available balance: ${balance.availableZatoshi.convertZatoshiToZecString(12)}
|
|
|
|
Total balance: ${balance.totalZatoshi.convertZatoshiToZecString(12)}
|
|
|
|
""".trimIndent()
|
2020-12-14 08:10:50 -08:00
|
|
|
|
2020-12-13 06:08:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun onStatus(status: Synchronizer.Status) {
|
|
|
|
binding.textBalance.text = "Status: $status"
|
2020-12-14 08:10:50 -08:00
|
|
|
if (CompactBlockProcessor.WalletBalance().none()) {
|
2020-12-13 06:08:42 -08:00
|
|
|
binding.textBalance.text = "Calculating balance..."
|
|
|
|
} else {
|
2020-12-14 08:10:50 -08:00
|
|
|
onBalance(synchronizer.latestBalance)
|
2020-12-13 06:08:42 -08:00
|
|
|
}
|
|
|
|
}
|
2020-12-14 08:10:50 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
}
|
2020-12-11 13:20:56 -08:00
|
|
|
}
|