|
| 1 | +package to.bitkit.ui.components |
| 2 | + |
| 3 | +import androidx.compose.foundation.background |
| 4 | +import androidx.compose.foundation.layout.Arrangement |
| 5 | +import androidx.compose.foundation.layout.Box |
| 6 | +import androidx.compose.foundation.layout.Row |
| 7 | +import androidx.compose.foundation.layout.padding |
| 8 | +import androidx.compose.foundation.layout.size |
| 9 | +import androidx.compose.foundation.shape.CircleShape |
| 10 | +import androidx.compose.material3.Icon |
| 11 | +import androidx.compose.runtime.Composable |
| 12 | +import androidx.compose.ui.Alignment |
| 13 | +import androidx.compose.ui.Modifier |
| 14 | +import androidx.compose.ui.graphics.Color |
| 15 | +import androidx.compose.ui.graphics.painter.Painter |
| 16 | +import androidx.compose.ui.res.painterResource |
| 17 | +import androidx.compose.ui.tooling.preview.Preview |
| 18 | +import androidx.compose.ui.unit.Dp |
| 19 | +import androidx.compose.ui.unit.dp |
| 20 | +import to.bitkit.R |
| 21 | +import to.bitkit.ui.theme.AppThemeSurface |
| 22 | +import to.bitkit.ui.theme.Colors |
| 23 | +import uniffi.bitkitcore.Activity |
| 24 | +import uniffi.bitkitcore.LightningActivity |
| 25 | +import uniffi.bitkitcore.OnchainActivity |
| 26 | +import uniffi.bitkitcore.PaymentState |
| 27 | +import uniffi.bitkitcore.PaymentType |
| 28 | + |
| 29 | +@Composable |
| 30 | +fun ActivityIcon( |
| 31 | + activity: Activity, |
| 32 | + size: Dp = 32.dp, |
| 33 | + modifier: Modifier = Modifier, |
| 34 | +) { |
| 35 | + val isLightning = activity is Activity.Lightning |
| 36 | + val status: PaymentState? = when (activity) { |
| 37 | + is Activity.Lightning -> activity.v1.status |
| 38 | + is Activity.Onchain -> null |
| 39 | + } |
| 40 | + val txType: PaymentType = when (activity) { |
| 41 | + is Activity.Lightning -> activity.v1.txType |
| 42 | + is Activity.Onchain -> activity.v1.txType |
| 43 | + } |
| 44 | + val arrowIcon = painterResource(if (txType == PaymentType.SENT) R.drawable.ic_sent else R.drawable.ic_received) |
| 45 | + |
| 46 | + if (isLightning) { |
| 47 | + when (status) { |
| 48 | + PaymentState.FAILED -> { |
| 49 | + CircularIcon( |
| 50 | + icon = painterResource(R.drawable.ic_x), |
| 51 | + iconColor = Colors.Purple, |
| 52 | + backgroundColor = Colors.Purple16, |
| 53 | + size = size, |
| 54 | + modifier = modifier, |
| 55 | + ) |
| 56 | + } |
| 57 | + |
| 58 | + PaymentState.PENDING -> { |
| 59 | + CircularIcon( |
| 60 | + icon = painterResource(R.drawable.ic_hourglass_simple), |
| 61 | + iconColor = Colors.Purple, |
| 62 | + backgroundColor = Colors.Purple16, |
| 63 | + size = size, |
| 64 | + modifier = modifier, |
| 65 | + ) |
| 66 | + } |
| 67 | + |
| 68 | + else -> { |
| 69 | + CircularIcon( |
| 70 | + icon = arrowIcon, |
| 71 | + iconColor = Colors.Purple, |
| 72 | + backgroundColor = Colors.Purple16, |
| 73 | + size = size, |
| 74 | + modifier = modifier, |
| 75 | + ) |
| 76 | + } |
| 77 | + } |
| 78 | + } else { |
| 79 | + val isTransfer = (activity as? Activity.Onchain)?.v1?.isTransfer == true |
| 80 | + val onChainIcon = if (isTransfer) painterResource(R.drawable.ic_transfer) else arrowIcon |
| 81 | + |
| 82 | + CircularIcon( |
| 83 | + icon = onChainIcon, |
| 84 | + iconColor = Colors.Brand, |
| 85 | + backgroundColor = Colors.Brand16, |
| 86 | + size = size, |
| 87 | + modifier = modifier, |
| 88 | + ) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +@Composable |
| 93 | +fun CircularIcon( |
| 94 | + icon: Painter, |
| 95 | + iconColor: Color, |
| 96 | + backgroundColor: Color, |
| 97 | + size: Dp = 32.dp, |
| 98 | + modifier: Modifier = Modifier, |
| 99 | +) { |
| 100 | + Box( |
| 101 | + contentAlignment = Alignment.Companion.Center, |
| 102 | + modifier = modifier |
| 103 | + .size(size) |
| 104 | + .background(backgroundColor, CircleShape) |
| 105 | + ) { |
| 106 | + Icon( |
| 107 | + painter = icon, |
| 108 | + contentDescription = null, |
| 109 | + tint = iconColor, |
| 110 | + modifier = Modifier.size(size * 0.5f), |
| 111 | + ) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +@Preview(showBackground = true) |
| 116 | +@Composable |
| 117 | +private fun Preview() { |
| 118 | + AppThemeSurface { |
| 119 | + Row( |
| 120 | + horizontalArrangement = Arrangement.spacedBy(16.dp), |
| 121 | + modifier = Modifier.padding(16.dp), |
| 122 | + ) { |
| 123 | + // Lightning Sent Succeeded |
| 124 | + ActivityIcon( |
| 125 | + activity = Activity.Lightning( |
| 126 | + v1 = LightningActivity( |
| 127 | + id = "test-lightning-1", |
| 128 | + txType = PaymentType.SENT, |
| 129 | + status = PaymentState.SUCCEEDED, |
| 130 | + value = 50000uL, |
| 131 | + fee = 1uL, |
| 132 | + invoice = "lnbc...", |
| 133 | + message = "", |
| 134 | + timestamp = (System.currentTimeMillis() / 1000).toULong(), |
| 135 | + preimage = null, |
| 136 | + createdAt = null, |
| 137 | + updatedAt = null, |
| 138 | + ) |
| 139 | + ) |
| 140 | + ) |
| 141 | + |
| 142 | + // Lightning Received Failed |
| 143 | + ActivityIcon( |
| 144 | + activity = Activity.Lightning( |
| 145 | + v1 = LightningActivity( |
| 146 | + id = "test-lightning-2", |
| 147 | + txType = PaymentType.RECEIVED, |
| 148 | + status = PaymentState.FAILED, |
| 149 | + value = 50000uL, |
| 150 | + fee = 1uL, |
| 151 | + invoice = "lnbc...", |
| 152 | + message = "", |
| 153 | + timestamp = (System.currentTimeMillis() / 1000).toULong(), |
| 154 | + preimage = null, |
| 155 | + createdAt = null, |
| 156 | + updatedAt = null, |
| 157 | + ) |
| 158 | + ) |
| 159 | + ) |
| 160 | + |
| 161 | + // Lightning Pending |
| 162 | + ActivityIcon( |
| 163 | + activity = Activity.Lightning( |
| 164 | + v1 = LightningActivity( |
| 165 | + id = "test-lightning-3", |
| 166 | + txType = PaymentType.SENT, |
| 167 | + status = PaymentState.PENDING, |
| 168 | + value = 50000uL, |
| 169 | + fee = 1uL, |
| 170 | + invoice = "lnbc...", |
| 171 | + message = "", |
| 172 | + timestamp = (System.currentTimeMillis() / 1000).toULong(), |
| 173 | + preimage = null, |
| 174 | + createdAt = null, |
| 175 | + updatedAt = null, |
| 176 | + ) |
| 177 | + ) |
| 178 | + ) |
| 179 | + |
| 180 | + // Onchain Received |
| 181 | + ActivityIcon( |
| 182 | + activity = Activity.Onchain( |
| 183 | + v1 = OnchainActivity( |
| 184 | + id = "test-onchain-1", |
| 185 | + txType = PaymentType.RECEIVED, |
| 186 | + txId = "abc123", |
| 187 | + value = 100000uL, |
| 188 | + fee = 500uL, |
| 189 | + feeRate = 8uL, |
| 190 | + address = "bc1...", |
| 191 | + confirmed = true, |
| 192 | + timestamp = (System.currentTimeMillis() / 1000).toULong(), |
| 193 | + isBoosted = false, |
| 194 | + isTransfer = false, |
| 195 | + doesExist = true, |
| 196 | + confirmTimestamp = (System.currentTimeMillis() / 1000).toULong(), |
| 197 | + channelId = null, |
| 198 | + transferTxId = null, |
| 199 | + createdAt = null, |
| 200 | + updatedAt = null, |
| 201 | + ) |
| 202 | + ) |
| 203 | + ) |
| 204 | + |
| 205 | + // Onchain Transfer |
| 206 | + ActivityIcon( |
| 207 | + activity = Activity.Onchain( |
| 208 | + v1 = OnchainActivity( |
| 209 | + id = "test-onchain-2", |
| 210 | + txType = PaymentType.SENT, |
| 211 | + txId = "abc123", |
| 212 | + value = 100000uL, |
| 213 | + fee = 500uL, |
| 214 | + feeRate = 8uL, |
| 215 | + address = "bc1...", |
| 216 | + confirmed = true, |
| 217 | + timestamp = (System.currentTimeMillis() / 1000).toULong(), |
| 218 | + isBoosted = false, |
| 219 | + isTransfer = true, |
| 220 | + doesExist = true, |
| 221 | + confirmTimestamp = (System.currentTimeMillis() / 1000).toULong(), |
| 222 | + channelId = null, |
| 223 | + transferTxId = "transferTxId", |
| 224 | + createdAt = null, |
| 225 | + updatedAt = null, |
| 226 | + ) |
| 227 | + ) |
| 228 | + ) |
| 229 | + } |
| 230 | + } |
| 231 | +} |
0 commit comments