Skip to content

Commit 7fe233f

Browse files
committed
refactor: replace LightningService direct access in ActivityViewModel.kt
1 parent 0035c44 commit 7fe233f

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import org.lightningdevkit.ldknode.BalanceDetails
1111
import org.lightningdevkit.ldknode.Bolt11Invoice
1212
import org.lightningdevkit.ldknode.ChannelDetails
1313
import org.lightningdevkit.ldknode.NodeStatus
14+
import org.lightningdevkit.ldknode.PaymentDetails
1415
import org.lightningdevkit.ldknode.PaymentId
1516
import org.lightningdevkit.ldknode.UserChannelId
1617
import org.lightningdevkit.ldknode.generateEntropyMnemonic
@@ -215,6 +216,16 @@ class LightningRepo @Inject constructor(
215216
}
216217
}
217218

219+
suspend fun getPayments(): Result< List<PaymentDetails>> = withContext(bgDispatcher) {
220+
try {
221+
val payments = lightningService.payments ?: return@withContext Result.failure(Exception("It wan't possible get the payments"))
222+
Result.success(payments)
223+
} catch (e: Throwable) {
224+
Logger.error("getPayments error", e)
225+
Result.failure(e)
226+
}
227+
}
228+
218229
suspend fun openChannel(
219230
peer: LnPeer,
220231
channelAmountSats: ULong,

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import kotlinx.coroutines.flow.asStateFlow
99
import kotlinx.coroutines.flow.combine
1010
import kotlinx.coroutines.flow.debounce
1111
import kotlinx.coroutines.launch
12+
import to.bitkit.repositories.LightningRepo
1213
import to.bitkit.services.CoreService
1314
import to.bitkit.services.LdkNodeEventBus
14-
import to.bitkit.services.LightningService
1515
import to.bitkit.utils.Logger
1616
import uniffi.bitkitcore.Activity
1717
import uniffi.bitkitcore.ActivityFilter
@@ -20,7 +20,7 @@ import javax.inject.Inject
2020
@HiltViewModel
2121
class ActivityListViewModel @Inject constructor(
2222
private val coreService: CoreService,
23-
private val lightningService: LightningService,
23+
private val lightningRepo: LightningRepo,
2424
private val ldkNodeEventBus: LdkNodeEventBus,
2525
) : ViewModel() {
2626
private val _filteredActivities = MutableStateFlow<List<Activity>?>(null)
@@ -166,12 +166,10 @@ class ActivityListViewModel @Inject constructor(
166166

167167
fun syncLdkNodePayments() {
168168
viewModelScope.launch {
169-
try {
170-
lightningService.payments?.let {
171-
coreService.activity.syncLdkNodePayments(it)
172-
syncState()
173-
}
174-
} catch (e: Exception) {
169+
lightningRepo.getPayments().onSuccess {
170+
coreService.activity.syncLdkNodePayments(it)
171+
syncState()
172+
}.onFailure { e ->
175173
Logger.error("Failed to sync ldk-node payments", e)
176174
}
177175
}

0 commit comments

Comments
 (0)