Skip to content

Commit 9033386

Browse files
committed
refactor: move code watch code to repository
1 parent e711bd3 commit 9033386

File tree

2 files changed

+75
-68
lines changed

2 files changed

+75
-68
lines changed

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package to.bitkit.repositories
22

3+
import androidx.lifecycle.viewModelScope
4+
import com.synonym.bitkitcore.BtOrderState2
35
import com.synonym.bitkitcore.CreateCjitOptions
46
import com.synonym.bitkitcore.CreateOrderOptions
57
import com.synonym.bitkitcore.IBtEstimateFeeResponse2
@@ -26,13 +28,15 @@ import kotlinx.coroutines.isActive
2628
import kotlinx.coroutines.launch
2729
import kotlinx.coroutines.withContext
2830
import to.bitkit.data.CacheStore
31+
import to.bitkit.data.SettingsStore
2932
import to.bitkit.di.BgDispatcher
3033
import to.bitkit.env.Env
3134
import to.bitkit.ext.nowTimestamp
3235
import to.bitkit.services.CoreService
3336
import to.bitkit.services.LightningService
3437
import to.bitkit.utils.Logger
3538
import to.bitkit.utils.ServiceError
39+
import to.bitkit.viewmodels.TransferViewModel
3640
import java.math.BigDecimal
3741
import javax.inject.Inject
3842
import javax.inject.Named
@@ -47,6 +51,7 @@ class BlocktankRepo @Inject constructor(
4751
private val lightningService: LightningService,
4852
private val currencyRepo: CurrencyRepo,
4953
private val cacheStore: CacheStore,
54+
private val settingsStore: SettingsStore,
5055
@Named("enablePolling") private val enablePolling: Boolean,
5156
) {
5257
private val repoScope = CoroutineScope(bgDispatcher + SupervisorJob())
@@ -287,6 +292,75 @@ class BlocktankRepo @Inject constructor(
287292
}
288293
}
289294

295+
suspend fun watchOrder(orderId: String, frequencyMs: Long = 2_500) = withContext(bgDispatcher) {
296+
var isSettled = false
297+
var error: Throwable? = null
298+
299+
Logger.debug("Started to watch order '$orderId'", context = TAG)
300+
301+
while (!isSettled && error == null) {
302+
try {
303+
Logger.debug("Refreshing order '$orderId'")
304+
val order = getOrder(orderId, refresh = true).getOrNull()
305+
if (order == null) {
306+
error = Exception("Order not found '$orderId'")
307+
Logger.error("Order not found '$orderId'", context = TAG)
308+
break
309+
}
310+
311+
val step = updateOrder(order)
312+
settingsStore.update { it.copy(lightningSetupStep = step) }
313+
Logger.debug("LN setup step: $step")
314+
315+
if (order.state2 == BtOrderState2.EXPIRED) {
316+
error = Exception("Order expired '$orderId'")
317+
Logger.error("Order expired '$orderId'", context = TAG)
318+
break
319+
}
320+
if (step > 2) {
321+
Logger.debug(
322+
"Order settled, stopping watch order '$orderId'",
323+
context = TAG
324+
)
325+
isSettled = true
326+
break
327+
}
328+
} catch (e: Throwable) {
329+
Logger.error("Failed to watch order '$orderId'", e, context = TAG)
330+
error = e
331+
break
332+
}
333+
delay(frequencyMs)
334+
}
335+
Logger.debug("Stopped watching order '$orderId'", context = TAG)
336+
337+
}
338+
339+
private suspend fun updateOrder(order: IBtOrder): Int {
340+
var currentStep = 0
341+
if (order.channel != null) {
342+
return 3
343+
}
344+
345+
when (order.state2) {
346+
BtOrderState2.CREATED -> {
347+
currentStep = 0
348+
}
349+
350+
BtOrderState2.PAID -> {
351+
currentStep = 1
352+
openChannel(order.id)
353+
}
354+
355+
BtOrderState2.EXECUTED -> {
356+
currentStep = 2
357+
}
358+
359+
else -> Unit
360+
}
361+
return currentStep
362+
}
363+
290364
private suspend fun defaultCreateOrderOptions(clientBalanceSat: ULong): CreateOrderOptions {
291365
val nodeId = lightningService.nodeId ?: throw ServiceError.NodeNotStarted
292366
val timestamp = nowTimestamp().toString()

app/src/main/java/to/bitkit/viewmodels/TransferViewModel.kt

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -193,56 +193,14 @@ class TransferViewModel @Inject constructor(
193193
.onSuccess { txId ->
194194
cacheStore.addPaidOrder(orderId = order.id, txId = txId)
195195
settingsStore.update { it.copy(lightningSetupStep = 0) }
196-
watchOrder(order.id)
196+
blocktankRepo.watchOrder(order.id)
197197
}
198198
.onFailure { error ->
199199
ToastEventBus.send(error)
200200
}
201201
}
202202
}
203203

204-
private fun watchOrder(orderId: String, frequencyMs: Long = 2_500) {
205-
var isSettled = false
206-
var error: Throwable? = null
207-
208-
viewModelScope.launch {
209-
Logger.debug("Started to watch order '$orderId'", context = TAG)
210-
211-
while (!isSettled && error == null) {
212-
try {
213-
Logger.debug("Refreshing order '$orderId'")
214-
val order = blocktankRepo.getOrder(orderId, refresh = true).getOrNull()
215-
if (order == null) {
216-
error = Exception("Order not found '$orderId'")
217-
Logger.error("Order not found '$orderId'", context = TAG)
218-
break
219-
}
220-
221-
val step = updateOrder(order)
222-
settingsStore.update { it.copy(lightningSetupStep = step) }
223-
Logger.debug("LN setup step: $step")
224-
225-
if (order.state2 == BtOrderState2.EXPIRED) {
226-
error = Exception("Order expired '$orderId'")
227-
Logger.error("Order expired '$orderId'", context = TAG)
228-
break
229-
}
230-
if (step > 2) {
231-
Logger.debug("Order settled, stopping watch order '$orderId'", context = TAG)
232-
isSettled = true
233-
break
234-
}
235-
} catch (e: Throwable) {
236-
Logger.error("Failed to watch order '$orderId'", e, context = TAG)
237-
error = e
238-
break
239-
}
240-
delay(frequencyMs)
241-
}
242-
Logger.debug("Stopped watching order '$orderId'", context = TAG)
243-
}
244-
}
245-
246204
private suspend fun getMinAmountToPurchase(): Long {
247205
val fee = lightningRepo.calculateTotalFee(_spendingUiState.value.satsAmount.toULong()).getOrNull() ?: 0u
248206
return max((fee + maxLspFee).toLong(), Env.TransactionDefaults.dustLimit.toLong())
@@ -298,31 +256,6 @@ class TransferViewModel @Inject constructor(
298256
}
299257
}
300258

301-
private suspend fun updateOrder(order: IBtOrder): Int {
302-
var currentStep = 0
303-
if (order.channel != null) {
304-
return 3
305-
}
306-
307-
when (order.state2) {
308-
BtOrderState2.CREATED -> {
309-
currentStep = 0
310-
}
311-
312-
BtOrderState2.PAID -> {
313-
currentStep = 1
314-
blocktankRepo.openChannel(order.id)
315-
}
316-
317-
BtOrderState2.EXECUTED -> {
318-
currentStep = 2
319-
}
320-
321-
else -> Unit
322-
}
323-
return currentStep
324-
}
325-
326259
fun onUseDefaultLspBalanceClick() {
327260
val defaultOrder = _spendingUiState.value.defaultOrder
328261
_spendingUiState.update { it.copy(order = defaultOrder, defaultOrder = null, isAdvanced = false) }

0 commit comments

Comments
 (0)