[#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.ClipboardManager
import android.content.Context import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
@ -83,6 +85,10 @@ class MainActivity :
override fun onCreateOptionsMenu(menu: Menu): Boolean { override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present. // Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu) menuInflater.inflate(R.menu.main, menu)
if (ZcashNetwork.Mainnet == ZcashNetwork.fromResources(applicationContext)) {
menu.findItem(R.id.action_faucet).isVisible = false
}
return true return true
} }
@ -91,6 +97,11 @@ class MainActivity :
val navController = findNavController(R.id.nav_host_fragment) val navController = findNavController(R.id.nav_host_fragment)
navController.navigate(R.id.nav_home) navController.navigate(R.id.nav_home)
true true
} else if (item.itemId == R.id.action_faucet) {
runCatching {
startActivity(newBrowserIntent("https://faucet.zecpages.com/"))
}
true
} else { } else {
super.onOptionsItemSelected(item) super.onOptionsItemSelected(item)
} }
@ -157,3 +168,12 @@ class MainActivity :
hideKeyboard() 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() { private fun displayAddress() {
viewLifecycleOwner.lifecycleScope.launchWhenStarted { viewLifecycleOwner.lifecycleScope.launchWhenStarted {
val uaddress = synchronizer.getCurrentAddress() binding.unifiedAddress.apply {
val sapling = synchronizer.getLegacySaplingAddress() val uaddress = synchronizer.getCurrentAddress()
val transparent = synchronizer.getLegacyTransparentAddress() text = uaddress
binding.textInfo.text = """ setOnClickListener { copyToClipboard(uaddress) }
Unified Address: }
$uaddress binding.saplingAddress.apply {
val sapling = synchronizer.getLegacySaplingAddress()
Legacy Sapling: text = sapling
$sapling setOnClickListener { copyToClipboard(sapling) }
}
Legacy transparent: binding.transparentAddress.apply {
$transparent val transparent = synchronizer.getLegacyTransparentAddress()
""".trimIndent() 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.WalletBalance
import cash.z.ecc.android.sdk.model.ZcashNetwork import cash.z.ecc.android.sdk.model.ZcashNetwork
import cash.z.ecc.android.sdk.model.defaultForNetwork 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. * Displays the available balance && total balance associated with the seed defined by the default config.
* comments. * comments.
*/ */
@Suppress("TooManyFunctions")
class GetBalanceFragment : BaseDemoFragment<FragmentGetBalanceBinding>() { class GetBalanceFragment : BaseDemoFragment<FragmentGetBalanceBinding>() {
private lateinit var synchronizer: Synchronizer private lateinit var synchronizer: Synchronizer
@ -64,25 +64,40 @@ class GetBalanceFragment : BaseDemoFragment<FragmentGetBalanceBinding>() {
synchronizer.status.collectWith(lifecycleScope, ::onStatus) synchronizer.status.collectWith(lifecycleScope, ::onStatus)
synchronizer.progress.collectWith(lifecycleScope, ::onProgress) synchronizer.progress.collectWith(lifecycleScope, ::onProgress)
synchronizer.processorInfo.collectWith(lifecycleScope, ::onProcessorInfoUpdated) 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 onOrchardBalance(
private fun onBalance(balance: WalletBalance) { orchardBalance: WalletBalance?
binding.textBalance.text = """ ) {
Available balance: ${balance.available.convertZatoshiToZecString(12)} binding.orchardBalance.apply {
Total balance: ${balance.total.convertZatoshiToZecString(12)} text = orchardBalance.humanString()
""".trimIndent() }
}
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) { private fun onStatus(status: Synchronizer.Status) {
binding.textStatus.text = "Status: $status" binding.textStatus.text = "Status: $status"
val balance: WalletBalance? = synchronizer.saplingBalances.value onOrchardBalance(synchronizer.orchardBalances.value)
if (null == balance) { onSaplingBalance(synchronizer.saplingBalances.value)
binding.textBalance.text = "Calculating balance..." onTransparentBalance(synchronizer.transparentBalances.value)
} else {
onBalance(balance)
}
} }
@Suppress("MagicNumber") @Suppress("MagicNumber")
@ -96,3 +111,13 @@ class GetBalanceFragment : BaseDemoFragment<FragmentGetBalanceBinding>() {
if (info.isScanning) binding.textStatus.text = "Scanning blocks...${info.scanProgress}%" 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"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<TextView <androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/text_info" android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:padding="16dp" android:orientation="vertical">
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"/>
</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"?> <?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:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent"
android:orientation="vertical">
<TextView <androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/text_status" android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Initializing wallet..." android:orientation="vertical">
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView <TextView
android:id="@+id/text_balance" android:id="@+id/text_status"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp" android:text="Initializing wallet..."
android:text="Balance" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toEndOf="@+id/text_status" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHorizontal_bias="0.492" app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintStart_toStartOf="@+id/text_status"
app:layout_constraintTop_toBottomOf="@+id/text_status"
app:layout_constraintVertical_bias="0.0" />
</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:orderInCategory="100"
android:title="@string/action_settings" android:title="@string/action_settings"
app:showAsAction="never" /> app:showAsAction="never" />
<item
android:id="@+id/action_faucet"
android:orderInCategory="100"
android:title="@string/action_faucet"
app:showAsAction="never" />
</menu> </menu>

View File

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