[#756] Improve funding testnet demo

This commit is contained in:
Carter Jernigan 2022-11-02 12:39:48 -04:00 committed by Carter Jernigan
parent f976b79a46
commit 5fb5773a7d
7 changed files with 192 additions and 66 deletions

View File

@ -2,6 +2,8 @@ package cash.z.ecc.android.sdk.demoapp
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
@ -83,6 +85,10 @@ class MainActivity :
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu)
if (ZcashNetwork.Mainnet == ZcashNetwork.fromResources(applicationContext)) {
menu.findItem(R.id.action_faucet).isVisible = false
}
return true
}
@ -91,6 +97,11 @@ class MainActivity :
val navController = findNavController(R.id.nav_host_fragment)
navController.navigate(R.id.nav_home)
true
} else if (item.itemId == R.id.action_faucet) {
runCatching {
startActivity(newBrowserIntent("https://faucet.zecpages.com/"))
}
true
} else {
super.onOptionsItemSelected(item)
}
@ -157,3 +168,12 @@ class MainActivity :
hideKeyboard()
}
}
private fun newBrowserIntent(url: String): Intent {
val uri = Uri.parse(url)
val intent = Intent(Intent.ACTION_VIEW, uri).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
return intent
}

View File

@ -60,19 +60,21 @@ class GetAddressFragment : BaseDemoFragment<FragmentGetAddressBinding>() {
private fun displayAddress() {
viewLifecycleOwner.lifecycleScope.launchWhenStarted {
val uaddress = synchronizer.getCurrentAddress()
val sapling = synchronizer.getLegacySaplingAddress()
val transparent = synchronizer.getLegacyTransparentAddress()
binding.textInfo.text = """
Unified Address:
$uaddress
Legacy Sapling:
$sapling
Legacy transparent:
$transparent
""".trimIndent()
binding.unifiedAddress.apply {
val uaddress = synchronizer.getCurrentAddress()
text = uaddress
setOnClickListener { copyToClipboard(uaddress) }
}
binding.saplingAddress.apply {
val sapling = synchronizer.getLegacySaplingAddress()
text = sapling
setOnClickListener { copyToClipboard(sapling) }
}
binding.transparentAddress.apply {
val transparent = synchronizer.getLegacyTransparentAddress()
text = transparent
setOnClickListener { copyToClipboard(transparent) }
}
}
}

View File

@ -17,12 +17,12 @@ import cash.z.ecc.android.sdk.model.LightWalletEndpoint
import cash.z.ecc.android.sdk.model.WalletBalance
import cash.z.ecc.android.sdk.model.ZcashNetwork
import cash.z.ecc.android.sdk.model.defaultForNetwork
import kotlinx.coroutines.flow.filterNotNull
/**
* Displays the available balance && total balance associated with the seed defined by the default config.
* comments.
*/
@Suppress("TooManyFunctions")
class GetBalanceFragment : BaseDemoFragment<FragmentGetBalanceBinding>() {
private lateinit var synchronizer: Synchronizer
@ -64,25 +64,40 @@ class GetBalanceFragment : BaseDemoFragment<FragmentGetBalanceBinding>() {
synchronizer.status.collectWith(lifecycleScope, ::onStatus)
synchronizer.progress.collectWith(lifecycleScope, ::onProgress)
synchronizer.processorInfo.collectWith(lifecycleScope, ::onProcessorInfoUpdated)
synchronizer.saplingBalances.filterNotNull().collectWith(lifecycleScope, ::onBalance)
synchronizer.orchardBalances.collectWith(lifecycleScope, ::onOrchardBalance)
synchronizer.saplingBalances.collectWith(lifecycleScope, ::onSaplingBalance)
synchronizer.transparentBalances.collectWith(lifecycleScope, ::onTransparentBalance)
}
@Suppress("MagicNumber")
private fun onBalance(balance: WalletBalance) {
binding.textBalance.text = """
Available balance: ${balance.available.convertZatoshiToZecString(12)}
Total balance: ${balance.total.convertZatoshiToZecString(12)}
""".trimIndent()
private fun onOrchardBalance(
orchardBalance: WalletBalance?
) {
binding.orchardBalance.apply {
text = orchardBalance.humanString()
}
}
private fun onSaplingBalance(
saplingBalance: WalletBalance?
) {
binding.saplingBalance.apply {
text = saplingBalance.humanString()
}
}
private fun onTransparentBalance(
transparentBalance: WalletBalance?
) {
binding.transparentBalance.apply {
text = transparentBalance.humanString()
}
}
private fun onStatus(status: Synchronizer.Status) {
binding.textStatus.text = "Status: $status"
val balance: WalletBalance? = synchronizer.saplingBalances.value
if (null == balance) {
binding.textBalance.text = "Calculating balance..."
} else {
onBalance(balance)
}
onOrchardBalance(synchronizer.orchardBalances.value)
onSaplingBalance(synchronizer.saplingBalances.value)
onTransparentBalance(synchronizer.transparentBalances.value)
}
@Suppress("MagicNumber")
@ -96,3 +111,13 @@ class GetBalanceFragment : BaseDemoFragment<FragmentGetBalanceBinding>() {
if (info.isScanning) binding.textStatus.text = "Scanning blocks...${info.scanProgress}%"
}
}
@Suppress("MagicNumber")
private fun WalletBalance?.humanString() = if (null == this) {
"Calculating balance"
} else {
"""
Available balance: ${available.convertZatoshiToZecString(12)}
Total balance: ${total.convertZatoshiToZecString(12)}
""".trimIndent()
}

View File

@ -1,21 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_info"
android:layout_width="wrap_content"
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="18sp"
android:text="loading address..."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.2"/>
android:orientation="vertical">
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/unified_address"
android:textSize="18sp" />
<TextView
android:id="@+id/unified_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp"
android:text=""
android:textSize="18sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/sapling_address"
android:textSize="18sp" />
<TextView
android:id="@+id/sapling_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp"
android:text="@string/sapling_address"
android:textSize="18sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/transparent_address"
android:textSize="18sp" />
<TextView
android:id="@+id/transparent_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp"
android:textSize="18sp" />
</androidx.appcompat.widget.LinearLayoutCompat>
</ScrollView>

View File

@ -1,31 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/text_status"
android:layout_width="wrap_content"
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Initializing wallet..."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:orientation="vertical">
<TextView
android:id="@+id/text_balance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Balance"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/text_status"
app:layout_constraintHorizontal_bias="0.492"
app:layout_constraintStart_toStartOf="@+id/text_status"
app:layout_constraintTop_toBottomOf="@+id/text_status"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/text_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Initializing wallet..."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Orchard balance" />
<TextView
android:id="@+id/orchard_balance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Sapling balance" />
<TextView
android:id="@+id/sapling_balance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Transparent balance" />
<TextView
android:id="@+id/transparent_balance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp" />
</androidx.appcompat.widget.LinearLayoutCompat>
</ScrollView>

View File

@ -7,4 +7,9 @@
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
<item
android:id="@+id/action_faucet"
android:orderInCategory="100"
android:title="@string/action_faucet"
app:showAsAction="never" />
</menu>

View File

@ -5,6 +5,7 @@
<string name="nav_header_subtitle">v1.1.0</string>
<string name="nav_header_desc">Navigation header</string>
<string name="action_settings">Change Seed Phrase</string>
<string name="action_faucet">Testnet Faucet</string>
<!-- Drawer Menu -->
<string name="menu_home">Home</string>
@ -25,4 +26,7 @@
<string name="load">Load</string>
<string name="apply">Apply</string>
<string name="loading">⌛ Loading</string>
<string name="unified_address">Unified address</string>
<string name="sapling_address">Sapling address</string>
<string name="transparent_address">Transparent address</string>
</resources>