Skip to content

Commit

Permalink
Added button to paste address from clipboard + some checks
Browse files Browse the repository at this point in the history
  • Loading branch information
tohrxyz committed Jan 14, 2023
1 parent dd21c90 commit 882a598
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package xyz.tomashrib.zephyruswallet.ui.wallet

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.util.Log
import android.view.MenuItem
import android.widget.Toast
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
Expand Down Expand Up @@ -33,7 +38,7 @@ import xyz.tomashrib.zephyruswallet.ui.theme.ZephyrusColors
import xyz.tomashrib.zephyruswallet.ui.theme.sourceSans

@Composable
internal fun SendScreen(navController: NavController){
internal fun SendScreen(navController: NavController, context: Context){

val (showDialog, setShowDialog) = remember { mutableStateOf(false) }

Expand Down Expand Up @@ -75,6 +80,24 @@ internal fun SendScreen(navController: NavController){
height = Dimension.fillToConstraints
}
){

//paste address button
Text(
text = stringResource(R.string.paste_address),
fontSize = 15.sp,
fontFamily = sourceSans,
color = ZephyrusColors.lightPurplePrimary,
modifier = Modifier
.align(Alignment.End)
//upon click, the address from clipboard is inserted into recipientAddress input field
.clickable {
if(pasteFromClipboard(context) != "Wrong format"){
recipientAddress.value = pasteFromClipboard(context)
} else {
Toast.makeText(context, "Wrong address format", Toast.LENGTH_SHORT).show()
}
}
)
TransactionAddressInput(recipientAddress)
TransactionAmountInput(amount)

Expand All @@ -90,9 +113,10 @@ internal fun SendScreen(navController: NavController){
.align(Alignment.Start)

//when clicked, it puts wallet balance value into amount input field
.clickable { amount.value = Wallet
.getBalance()
.toString()
.clickable {
amount.value = Wallet
.getBalance()
.toString()
}
)

Expand Down Expand Up @@ -169,11 +193,10 @@ private fun TransactionAddressInput(recipientAddress: MutableState<String>){
Column(
horizontalAlignment = Alignment.CenterHorizontally,
){
// var inputText by remember { mutableStateOf("") }

OutlinedTextField(
modifier = Modifier
.padding(vertical = 10.dp)
.padding(bottom = 10.dp)
.fillMaxWidth(0.9f),
value = recipientAddress.value,
onValueChange = { recipientAddress.value = it },
Expand Down Expand Up @@ -320,8 +343,40 @@ private fun broadcastTransaction(recipientAddress: String, amount: ULong, feeRat
}
}

@Preview(device = Devices.PIXEL_4, showBackground = true)
@Composable
internal fun PreviewSendScreen() {
SendScreen(rememberNavController())
// returns string from clipboard
private fun pasteFromClipboard(context: Context): String{

//lengths of different bitcoin address formats
val legacyAddressLenght = 32
val segwitAddressLenght = 42
val taprootAddressLenght = 62

//initialize clipboardManager
val clipboardManager = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager

//get input from clipboard
val item = clipboardManager.primaryClip!!.getItemAt(0)

//check if null
if(item == null || item.text == null){
return "Wrong format"
//could return "Null input"
}

//converts clipboard input to String
val pasteData = item.text.toString()

//check if empty
if (pasteData.isEmpty()){
return "Wrong format"
//could return "Empty string"
}

//check if valid bitcoin address format length
if(pasteData.length != legacyAddressLenght && pasteData.length != segwitAddressLenght && pasteData.length != taprootAddressLenght) {
return "Wrong format"
}

//default return
return pasteData
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fun WalletNavigation() {

composable(
route = Screen.SendScreen.route,
) { SendScreen(navController) }
) { SendScreen(navController, LocalContext.current) }
//
// composable(
// route = Screen.TransactionsScreen.route,
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
<string name="recover_wallet">Recover Wallet</string>
<string name="wallet_syncing">Wallet is Syncing...</string>
<string name="clear_all">Clear All</string>
<string name="paste_address">Paste Address</string>
</resources>

0 comments on commit 882a598

Please sign in to comment.