Skip to content

Commit 6ee9d91

Browse files
committed
Update code to reduce duplication
1 parent 972363b commit 6ee9d91

File tree

5 files changed

+26
-32
lines changed

5 files changed

+26
-32
lines changed

app/src/main/java/to/bitkit/ext/ChannelDetails.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ fun List<ChannelDetails>?.totalNextOutboundHtlcLimitSats(): ULong {
3939
?: 0u
4040
}
4141

42+
/** Calculates the total remote balance (inbound capacity) from open channels. */
43+
fun List<ChannelDetails>.calculateRemoteBalance(): ULong {
44+
return this
45+
.filterOpen()
46+
.sumOf { it.inboundCapacityMsat / 1000u }
47+
}
48+
4249
fun createChannelDetails(): ChannelDetails {
4350
return ChannelDetails(
4451
channelId = "channelId",

app/src/main/java/to/bitkit/repositories/LightningRepo.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class LightningRepo @Inject constructor(
9494
* @param operation Lambda to execute when the node is running
9595
* @return Result of the operation, or failure if node isn't running or operation fails
9696
*/
97-
private suspend fun <T> executeWhenNodeRunning(
97+
suspend fun <T> executeWhenNodeRunning(
9898
operationName: String,
9999
waitTimeout: Duration = 1.minutes,
100100
operation: suspend () -> Result<T>,

app/src/main/java/to/bitkit/ui/settings/lightning/LightningConnectionsViewModel.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import org.lightningdevkit.ldknode.OutPoint
2121
import to.bitkit.R
2222
import to.bitkit.di.BgDispatcher
2323
import to.bitkit.ext.amountOnClose
24+
import to.bitkit.ext.calculateRemoteBalance
2425
import to.bitkit.ext.createChannelDetails
2526
import to.bitkit.ext.filterOpen
2627
import to.bitkit.ext.filterPending
@@ -82,7 +83,7 @@ class LightningConnectionsViewModel @Inject constructor(
8283
.map { it.mapToUiModel() },
8384
failedOrders = getFailedOrdersAsChannels(blocktankState.paidOrders).map { it.mapToUiModel() },
8485
localBalance = calculateLocalBalance(channels),
85-
remoteBalance = calculateRemoteBalance(channels),
86+
remoteBalance = channels.calculateRemoteBalance(),
8687
)
8788
}.collect { newState ->
8889
_uiState.update { newState }
@@ -263,12 +264,6 @@ class LightningConnectionsViewModel @Inject constructor(
263264
.sumOf { it.amountOnClose }
264265
}
265266

266-
private fun calculateRemoteBalance(channels: List<ChannelDetails>): ULong {
267-
return channels
268-
.filterOpen()
269-
.sumOf { it.inboundCapacityMsat / 1000u }
270-
}
271-
272267
fun zipLogsForSharing(onReady: (Uri) -> Unit) {
273268
viewModelScope.launch {
274269
logsRepo.zipLogsForSharing()

app/src/main/java/to/bitkit/ui/sheets/GiftLoading.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package to.bitkit.ui.sheets
33
import androidx.compose.foundation.Image
44
import androidx.compose.foundation.layout.Column
55
import androidx.compose.foundation.layout.Row
6-
import androidx.compose.foundation.layout.Spacer
76
import androidx.compose.foundation.layout.aspectRatio
87
import androidx.compose.foundation.layout.fillMaxWidth
98
import androidx.compose.foundation.layout.navigationBarsPadding
@@ -24,6 +23,7 @@ import to.bitkit.models.formatToModernDisplay
2423
import to.bitkit.ui.LocalCurrencies
2524
import to.bitkit.ui.components.BodyM
2625
import to.bitkit.ui.components.Display
26+
import to.bitkit.ui.components.FillHeight
2727
import to.bitkit.ui.components.MoneySSB
2828
import to.bitkit.ui.components.SheetSize
2929
import to.bitkit.ui.components.VerticalSpacer
@@ -127,7 +127,7 @@ private fun Content(
127127
color = Colors.White64,
128128
)
129129

130-
Spacer(modifier = Modifier.weight(1f))
130+
FillHeight()
131131

132132
Image(
133133
painter = painterResource(R.drawable.gift),

app/src/main/java/to/bitkit/ui/sheets/GiftViewModel.kt

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,25 @@ import kotlinx.coroutines.flow.asSharedFlow
1414
import kotlinx.coroutines.launch
1515
import to.bitkit.async.ServiceQueue
1616
import to.bitkit.di.BgDispatcher
17+
import to.bitkit.ext.calculateRemoteBalance
1718
import to.bitkit.models.NewTransactionSheetDetails
1819
import to.bitkit.models.NewTransactionSheetDirection
1920
import to.bitkit.models.NewTransactionSheetType
21+
import to.bitkit.repositories.ActivityRepo
2022
import to.bitkit.repositories.BlocktankRepo
2123
import to.bitkit.repositories.LightningRepo
22-
import to.bitkit.services.CoreService
2324
import to.bitkit.services.LightningService
2425
import to.bitkit.utils.Logger
2526
import javax.inject.Inject
27+
import kotlin.time.Duration.Companion.milliseconds
2628

2729
@HiltViewModel
2830
class GiftViewModel @Inject constructor(
2931
@BgDispatcher private val bgDispatcher: CoroutineDispatcher,
3032
private val lightningRepo: LightningRepo,
3133
private val lightningService: LightningService,
32-
private val coreService: CoreService,
3334
private val blocktankRepo: BlocktankRepo,
35+
private val activityRepo: ActivityRepo,
3436
) : ViewModel() {
3537

3638
private val _navigationEvent = MutableSharedFlow<GiftRoute>(extraBufferCapacity = 1)
@@ -62,10 +64,17 @@ class GiftViewModel @Inject constructor(
6264

6365
isClaiming = true
6466
runCatching {
65-
waitForPeers()
67+
lightningRepo.executeWhenNodeRunning(
68+
operationName = "waitForNodeRunning",
69+
waitTimeout = NODE_STARTUP_TIMEOUT_MS.milliseconds,
70+
) {
71+
Result.success(Unit)
72+
}.getOrThrow()
73+
74+
delay(PEER_CONNECTION_DELAY_MS)
6675

6776
val channels = lightningRepo.lightningState.value.channels
68-
val maxInboundCapacity = channels.sumOf { it.inboundCapacityMsat / 1000u }
77+
val maxInboundCapacity = channels.calculateRemoteBalance()
6978

7079
if (maxInboundCapacity >= amount) {
7180
claimWithLiquidity()
@@ -78,22 +87,6 @@ class GiftViewModel @Inject constructor(
7887
}
7988
}
8089

81-
private suspend fun waitForPeers() {
82-
val nodeLifecycleState = lightningRepo.lightningState.value.nodeLifecycleState
83-
if (!nodeLifecycleState.isRunning()) {
84-
val startTime = System.currentTimeMillis()
85-
while (!lightningRepo.lightningState.value.nodeLifecycleState.isRunning()) {
86-
if (System.currentTimeMillis() - startTime > NODE_STARTUP_TIMEOUT_MS) {
87-
Logger.warn("Timeout waiting for node to be running", context = TAG)
88-
error("Timeout waiting for node to be running")
89-
}
90-
delay(NODE_STATE_CHECK_INTERVAL_MS)
91-
}
92-
}
93-
94-
delay(PEER_CONNECTION_DELAY_MS)
95-
}
96-
9790
private suspend fun claimWithLiquidity() {
9891
runCatching {
9992
val invoice = lightningService.receive(
@@ -144,7 +137,7 @@ class GiftViewModel @Inject constructor(
144137
updatedAt = null,
145138
)
146139

147-
coreService.activity.insert(Activity.Lightning(lightningActivity))
140+
activityRepo.insertActivity(Activity.Lightning(lightningActivity)).getOrThrow()
148141

149142
isClaiming = false
150143
_successEvent.emit(
@@ -190,6 +183,5 @@ class GiftViewModel @Inject constructor(
190183
private const val TAG = "GiftViewModel"
191184
private const val NODE_STARTUP_TIMEOUT_MS = 30_000L
192185
private const val PEER_CONNECTION_DELAY_MS = 2_000L
193-
private const val NODE_STATE_CHECK_INTERVAL_MS = 500L
194186
}
195187
}

0 commit comments

Comments
 (0)