2020-06-10 00:08:19 -07:00
|
|
|
package cash.z.ecc.android.sdk.demoapp.demos.getblockrange
|
2019-10-21 03:17:07 -07:00
|
|
|
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
2020-06-10 00:08:19 -07:00
|
|
|
import cash.z.ecc.android.sdk.demoapp.App
|
|
|
|
import cash.z.ecc.android.sdk.demoapp.BaseDemoFragment
|
|
|
|
import cash.z.ecc.android.sdk.demoapp.databinding.FragmentGetBlockRangeBinding
|
|
|
|
import cash.z.ecc.android.sdk.service.LightWalletGrpcService
|
|
|
|
import cash.z.ecc.android.sdk.service.LightWalletService
|
2019-10-21 03:17:07 -07:00
|
|
|
|
2020-02-27 00:25:07 -08:00
|
|
|
/**
|
|
|
|
* Retrieves a range of compact block from the lightwalletd service and displays basic information
|
|
|
|
* about them. This demonstrates the basic ability to connect to the server, request a range of
|
|
|
|
* compact block and parse the response. This could be augmented to display metadata about certain
|
|
|
|
* block ranges for instance, to find the block with the most shielded transactions in a range.
|
|
|
|
*/
|
2019-10-21 03:17:07 -07:00
|
|
|
class GetBlockRangeFragment : BaseDemoFragment<FragmentGetBlockRangeBinding>() {
|
|
|
|
|
|
|
|
private val host = App.instance.defaultConfig.host
|
2020-02-25 23:43:27 -08:00
|
|
|
private val port = App.instance.defaultConfig.port
|
2019-10-21 03:17:07 -07:00
|
|
|
|
|
|
|
private lateinit var lightwalletService: LightWalletService
|
|
|
|
|
|
|
|
override fun inflateBinding(layoutInflater: LayoutInflater): FragmentGetBlockRangeBinding =
|
|
|
|
FragmentGetBlockRangeBinding.inflate(layoutInflater)
|
|
|
|
|
2020-08-13 18:08:01 -07:00
|
|
|
fun resetInBackground() {
|
2020-02-25 23:43:27 -08:00
|
|
|
lightwalletService = LightWalletGrpcService(App.instance, host, port)
|
2019-10-21 03:17:07 -07:00
|
|
|
}
|
|
|
|
|
2020-08-13 18:08:01 -07:00
|
|
|
fun onResetComplete() {
|
2019-10-21 03:17:07 -07:00
|
|
|
binding.buttonApply.setOnClickListener(::onApply)
|
|
|
|
onApply(binding.textInfo)
|
|
|
|
}
|
|
|
|
|
2020-08-13 18:08:01 -07:00
|
|
|
fun onClear() {
|
2019-10-21 03:17:07 -07:00
|
|
|
lightwalletService.shutdown()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun onApply(_unused: View) {
|
|
|
|
val start = binding.textStartHeight.text.toString().toInt()
|
|
|
|
val end = binding.textEndHeight.text.toString().toInt()
|
|
|
|
if (start <= end) {
|
|
|
|
setBlockRange(start..end)
|
|
|
|
} else {
|
|
|
|
setError("Invalid range")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:47:50 -08:00
|
|
|
// TODO: iterate on this demo to show all the blocks in a recyclerview showing block heights and vtx count
|
2019-10-21 03:17:07 -07:00
|
|
|
private fun setBlockRange(blockRange: IntRange) {
|
|
|
|
val blocks =
|
|
|
|
lightwalletService.getBlockRange(blockRange)
|
|
|
|
val block = blocks.firstOrNull()
|
|
|
|
binding.textInfo.text = """
|
|
|
|
block height: ${block?.height}
|
|
|
|
block vtxCount: ${block?.vtxCount}
|
|
|
|
block time: ${block?.time}
|
|
|
|
""".trimIndent()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun setError(message: String) {
|
|
|
|
binding.textInfo.text = "Error: $message"
|
|
|
|
}
|
2020-06-09 20:28:21 -07:00
|
|
|
}
|