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
|
|
|
|
|
|
|
class GetBalanceFragment : BaseDemoFragment<FragmentGetBalanceBinding>() {
|
|
|
|
|
2020-12-13 06:14:05 -08:00
|
|
|
private lateinit var synchronize: 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-13 06:14:05 -08:00
|
|
|
synchronize = 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
|
|
|
|
synchronize.start(lifecycleScope)
|
2020-12-13 06:08:42 -08:00
|
|
|
monitorChanges()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun monitorChanges() {
|
2020-12-13 06:14:05 -08:00
|
|
|
synchronize.status.collectWith(lifecycleScope, ::onStatus)
|
|
|
|
synchronize.balances.collectWith(lifecycleScope, ::onBalance)
|
2020-12-13 06:08:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
binding.textBalance.text = "Calculating balance..."
|
|
|
|
} else {
|
|
|
|
if (!isSyncing) onBalance(balance)
|
|
|
|
}
|
|
|
|
}
|
2020-12-11 13:20:56 -08:00
|
|
|
}
|