@@ -14,23 +14,25 @@ import kotlinx.coroutines.flow.asSharedFlow
1414import kotlinx.coroutines.launch
1515import to.bitkit.async.ServiceQueue
1616import to.bitkit.di.BgDispatcher
17+ import to.bitkit.ext.calculateRemoteBalance
1718import to.bitkit.models.NewTransactionSheetDetails
1819import to.bitkit.models.NewTransactionSheetDirection
1920import to.bitkit.models.NewTransactionSheetType
21+ import to.bitkit.repositories.ActivityRepo
2022import to.bitkit.repositories.BlocktankRepo
2123import to.bitkit.repositories.LightningRepo
22- import to.bitkit.services.CoreService
2324import to.bitkit.services.LightningService
2425import to.bitkit.utils.Logger
2526import javax.inject.Inject
27+ import kotlin.time.Duration.Companion.milliseconds
2628
2729@HiltViewModel
2830class 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