|
| 1 | +package to.bitkit.ui.screens.trezor |
| 2 | + |
| 3 | +import androidx.compose.animation.AnimatedVisibility |
| 4 | +import androidx.compose.foundation.layout.Column |
| 5 | +import androidx.compose.foundation.layout.Row |
| 6 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 7 | +import androidx.compose.foundation.layout.size |
| 8 | +import androidx.compose.material3.Icon |
| 9 | +import androidx.compose.material3.OutlinedTextField |
| 10 | +import androidx.compose.material3.OutlinedTextFieldDefaults |
| 11 | +import androidx.compose.runtime.Composable |
| 12 | +import androidx.compose.ui.Alignment |
| 13 | +import androidx.compose.ui.Modifier |
| 14 | +import androidx.compose.ui.res.painterResource |
| 15 | +import androidx.compose.ui.tooling.preview.Preview |
| 16 | +import androidx.compose.ui.unit.dp |
| 17 | +import com.synonym.bitkitcore.HistoryTransaction |
| 18 | +import com.synonym.bitkitcore.TransactionHistoryResult |
| 19 | +import com.synonym.bitkitcore.TxDirection |
| 20 | +import to.bitkit.R |
| 21 | +import to.bitkit.ui.components.ButtonSize |
| 22 | +import to.bitkit.ui.components.Caption |
| 23 | +import to.bitkit.ui.components.Caption13Up |
| 24 | +import to.bitkit.ui.components.HorizontalSpacer |
| 25 | +import to.bitkit.ui.components.PrimaryButton |
| 26 | +import to.bitkit.ui.components.VerticalSpacer |
| 27 | +import to.bitkit.ui.shared.modifiers.clickableAlpha |
| 28 | +import to.bitkit.ui.theme.AppThemeSurface |
| 29 | +import to.bitkit.ui.theme.Colors |
| 30 | +import to.bitkit.ui.utils.copyToClipboard |
| 31 | +import java.text.SimpleDateFormat |
| 32 | +import java.util.Date |
| 33 | +import java.util.Locale |
| 34 | + |
| 35 | +@Composable |
| 36 | +internal fun TransactionHistorySection( |
| 37 | + uiState: TrezorUiState, |
| 38 | + onInputChange: (String) -> Unit, |
| 39 | + onLookup: () -> Unit, |
| 40 | +) { |
| 41 | + Column { |
| 42 | + Caption13Up( |
| 43 | + text = "Transaction History", |
| 44 | + color = Colors.White64, |
| 45 | + ) |
| 46 | + VerticalSpacer(8.dp) |
| 47 | + |
| 48 | + OutlinedTextField( |
| 49 | + value = uiState.txHistoryInput, |
| 50 | + onValueChange = onInputChange, |
| 51 | + label = { Caption("xpub / vpub / zpub", color = Colors.White50) }, |
| 52 | + colors = OutlinedTextFieldDefaults.colors( |
| 53 | + focusedTextColor = Colors.White, |
| 54 | + unfocusedTextColor = Colors.White, |
| 55 | + focusedBorderColor = Colors.Brand, |
| 56 | + unfocusedBorderColor = Colors.White32, |
| 57 | + cursorColor = Colors.Brand, |
| 58 | + ), |
| 59 | + maxLines = 3, |
| 60 | + modifier = Modifier.fillMaxWidth(), |
| 61 | + ) |
| 62 | + |
| 63 | + VerticalSpacer(16.dp) |
| 64 | + |
| 65 | + PrimaryButton( |
| 66 | + text = if (uiState.isLoadingTxHistory) "Loading..." else "Lookup History", |
| 67 | + onClick = onLookup, |
| 68 | + enabled = !uiState.isLoadingTxHistory && uiState.txHistoryInput.isNotBlank(), |
| 69 | + size = ButtonSize.Small, |
| 70 | + modifier = Modifier.fillMaxWidth(), |
| 71 | + ) |
| 72 | + |
| 73 | + AnimatedVisibility(visible = uiState.txHistoryResult != null) { |
| 74 | + uiState.txHistoryResult?.let { TransactionHistoryResultView(it) } |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +@Composable |
| 80 | +private fun TransactionHistoryResultView(result: TransactionHistoryResult) { |
| 81 | + Column { |
| 82 | + VerticalSpacer(16.dp) |
| 83 | + |
| 84 | + Caption13Up(text = "Summary", color = Colors.White64) |
| 85 | + VerticalSpacer(4.dp) |
| 86 | + ResultCard { |
| 87 | + InfoRow("Account Type", result.accountType.name) |
| 88 | + InfoRow("Transactions", "${result.txCount}") |
| 89 | + InfoRow("Block Height", "${result.blockHeight}") |
| 90 | + } |
| 91 | + |
| 92 | + VerticalSpacer(12.dp) |
| 93 | + |
| 94 | + Caption13Up(text = "Balance", color = Colors.White64) |
| 95 | + VerticalSpacer(4.dp) |
| 96 | + ResultCard { |
| 97 | + InfoRow("Confirmed", "${result.balance.confirmed} sats") |
| 98 | + InfoRow("Trusted Pending", "${result.balance.trustedPending} sats") |
| 99 | + InfoRow("Untrusted Pending", "${result.balance.untrustedPending} sats") |
| 100 | + InfoRow("Spendable", "${result.balance.spendable} sats") |
| 101 | + InfoRow("Total", "${result.balance.total} sats") |
| 102 | + } |
| 103 | + |
| 104 | + if (result.transactions.isNotEmpty()) { |
| 105 | + VerticalSpacer(12.dp) |
| 106 | + Caption13Up( |
| 107 | + text = "Transactions (${result.transactions.size})", |
| 108 | + color = Colors.White64, |
| 109 | + ) |
| 110 | + VerticalSpacer(4.dp) |
| 111 | + result.transactions.forEach { tx -> |
| 112 | + TransactionRow(tx) |
| 113 | + VerticalSpacer(4.dp) |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +@Composable |
| 120 | +private fun TransactionRow(tx: HistoryTransaction) { |
| 121 | + val onCopyTxid = copyToClipboard(text = tx.txid, label = "TXID") |
| 122 | + val directionLabel = when (tx.direction) { |
| 123 | + TxDirection.SENT -> "Sent" |
| 124 | + TxDirection.RECEIVED -> "Received" |
| 125 | + TxDirection.SELF_TRANSFER -> "Self Transfer" |
| 126 | + } |
| 127 | + ResultCard { |
| 128 | + Row( |
| 129 | + verticalAlignment = Alignment.CenterVertically, |
| 130 | + modifier = Modifier.fillMaxWidth(), |
| 131 | + ) { |
| 132 | + Caption( |
| 133 | + text = "${tx.txid.take(8)}...${tx.txid.takeLast(8)}", |
| 134 | + color = Colors.Brand, |
| 135 | + modifier = Modifier.weight(1f), |
| 136 | + ) |
| 137 | + HorizontalSpacer(8.dp) |
| 138 | + Icon( |
| 139 | + painter = painterResource(R.drawable.ic_copy), |
| 140 | + contentDescription = "Copy txid", |
| 141 | + tint = Colors.Brand, |
| 142 | + modifier = Modifier |
| 143 | + .size(16.dp) |
| 144 | + .clickableAlpha(onClick = onCopyTxid), |
| 145 | + ) |
| 146 | + } |
| 147 | + InfoRow("Direction", directionLabel) |
| 148 | + InfoRow("Net", "${tx.net} sats") |
| 149 | + tx.fee?.let { InfoRow("Fee", "$it sats") } |
| 150 | + InfoRow("Confirmations", "${tx.confirmations}") |
| 151 | + tx.blockHeight?.let { InfoRow("Block", "$it") } |
| 152 | + tx.timestamp?.let { InfoRow("Time", formatTimestamp(it)) } |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +private fun formatTimestamp(epochSeconds: ULong, locale: Locale = Locale.getDefault()): String = runCatching { |
| 157 | + val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm", locale) |
| 158 | + sdf.format(Date(epochSeconds.toLong() * 1000)) |
| 159 | +}.getOrDefault("$epochSeconds") |
| 160 | + |
| 161 | +@Preview |
| 162 | +@Composable |
| 163 | +private fun PreviewTransactionHistoryEmpty() { |
| 164 | + AppThemeSurface { |
| 165 | + TransactionHistorySection( |
| 166 | + uiState = TrezorUiState(), |
| 167 | + onInputChange = {}, |
| 168 | + onLookup = {}, |
| 169 | + ) |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +@Preview |
| 174 | +@Composable |
| 175 | +private fun PreviewTransactionHistoryLoading() { |
| 176 | + AppThemeSurface { |
| 177 | + TransactionHistorySection( |
| 178 | + uiState = TrezorUiState(txHistoryInput = "vpub5Y...", isLoadingTxHistory = true), |
| 179 | + onInputChange = {}, |
| 180 | + onLookup = {}, |
| 181 | + ) |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +@Preview |
| 186 | +@Composable |
| 187 | +private fun PreviewTransactionHistoryWithResult() { |
| 188 | + AppThemeSurface { |
| 189 | + TransactionHistorySection( |
| 190 | + uiState = TrezorPreviewData.uiStateWithTxHistory, |
| 191 | + onInputChange = {}, |
| 192 | + onLookup = {}, |
| 193 | + ) |
| 194 | + } |
| 195 | +} |
0 commit comments