-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLightningChannel.kt
More file actions
195 lines (183 loc) · 6.64 KB
/
LightningChannel.kt
File metadata and controls
195 lines (183 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
@file:Suppress("MatchingDeclarationName")
package to.bitkit.ui.components
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowDownward
import androidx.compose.material.icons.filled.ArrowUpward
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Alignment.Companion.CenterVertically
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import to.bitkit.R
import to.bitkit.ui.theme.AppThemeSurface
import to.bitkit.ui.theme.Colors
enum class ChannelStatusUi { PENDING, OPEN, CLOSED }
@Composable
fun LightningChannel(
capacity: Long,
localBalance: Long,
remoteBalance: Long,
modifier: Modifier = Modifier,
status: ChannelStatusUi = ChannelStatusUi.PENDING,
showLabels: Boolean = false,
) {
val spendingColor = if (status == ChannelStatusUi.CLOSED) Colors.Gray5 else Colors.Purple50
val spendingAvailableColor = if (status == ChannelStatusUi.CLOSED) Colors.Gray3 else Colors.Purple
val receivingColor = if (status == ChannelStatusUi.CLOSED) Colors.Gray5 else Colors.White64
val receivingAvailableColor = if (status == ChannelStatusUi.CLOSED) Colors.Gray3 else Colors.White
val spendingAvailableFraction = if (capacity > 0) localBalance.toFloat() / capacity else 0f
val receivingAvailableFraction = if (capacity > 0) remoteBalance.toFloat() / capacity else 0f
Column(
modifier = modifier
.then(if (status == ChannelStatusUi.PENDING) Modifier.alpha(0.5f) else Modifier)
) {
if (showLabels) {
Row(
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier.fillMaxWidth(),
) {
Caption13Up(text = stringResource(R.string.lightning__spending_label), color = Colors.White64)
Caption13Up(text = stringResource(R.string.lightning__receiving_label), color = Colors.White64)
}
VerticalSpacer(8.dp)
}
Row(
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier.fillMaxWidth(),
) {
Row(verticalAlignment = CenterVertically) {
Icon(
imageVector = Icons.Default.ArrowUpward,
contentDescription = null,
tint = spendingAvailableColor,
modifier = Modifier.size(14.dp)
)
MoneyCaptionB(
sats = localBalance,
color = spendingAvailableColor,
)
}
Row(verticalAlignment = CenterVertically) {
Icon(
imageVector = Icons.Default.ArrowDownward,
contentDescription = null,
tint = receivingAvailableColor,
modifier = Modifier.size(14.dp)
)
MoneyCaptionB(
sats = remoteBalance,
color = receivingAvailableColor,
)
}
}
VerticalSpacer(8.dp)
Row(
verticalAlignment = CenterVertically,
modifier = Modifier
.fillMaxWidth()
.height(16.dp),
) {
Box(
modifier = Modifier
.weight(1f)
.fillMaxHeight()
.background(spendingColor, RoundedCornerShape(topStart = 8.dp, bottomStart = 8.dp))
) {
Box(
modifier = Modifier
.fillMaxHeight()
.fillMaxWidth(spendingAvailableFraction)
.align(Alignment.CenterEnd)
.background(spendingAvailableColor, RoundedCornerShape(topStart = 8.dp, bottomStart = 8.dp))
)
}
HorizontalSpacer(4.dp)
Box(
modifier = Modifier
.weight(1f)
.fillMaxHeight()
.background(receivingColor, RoundedCornerShape(topEnd = 8.dp, bottomEnd = 8.dp))
) {
Box(
modifier = Modifier
.fillMaxHeight()
.fillMaxWidth(receivingAvailableFraction)
.background(receivingAvailableColor, RoundedCornerShape(topEnd = 8.dp, bottomEnd = 8.dp))
)
}
}
}
}
// region preview
@Preview(showBackground = true)
@Composable
private fun PreviewChannelOpen() {
AppThemeSurface {
LightningChannel(
capacity = 500_000,
localBalance = 50_000,
remoteBalance = 450_000,
status = ChannelStatusUi.OPEN,
showLabels = true,
modifier = Modifier.padding(16.dp)
)
}
}
@Preview(showBackground = true)
@Composable
private fun PreviewChannelOpenWithoutLabels() {
AppThemeSurface {
LightningChannel(
capacity = 1_000_000,
localBalance = 500_000,
remoteBalance = 500_000,
status = ChannelStatusUi.OPEN,
showLabels = false,
modifier = Modifier.padding(16.dp)
)
}
}
@Preview(showBackground = true)
@Composable
private fun PreviewChannelPending() {
AppThemeSurface {
LightningChannel(
capacity = 1_000_000,
localBalance = 400_000,
remoteBalance = 600_000,
status = ChannelStatusUi.PENDING,
showLabels = true,
modifier = Modifier.padding(16.dp)
)
}
}
@Preview(showBackground = true)
@Composable
private fun PreviewChannelClosed() {
AppThemeSurface {
LightningChannel(
capacity = 500_000,
localBalance = 400_000,
remoteBalance = 100_000,
status = ChannelStatusUi.CLOSED,
showLabels = true,
modifier = Modifier.padding(16.dp)
)
}
}
// endregion