Skip to content

Commit b9ff988

Browse files
committed
refactor: replace LightningService direct access with repository in WakeNodeWorker.kt
1 parent d0f514d commit b9ff988

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

app/src/main/java/to/bitkit/fcm/WakeNodeWorker.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import to.bitkit.models.blocktank.BlocktankNotificationType.incomingHtlc
2424
import to.bitkit.models.blocktank.BlocktankNotificationType.mutualClose
2525
import to.bitkit.models.blocktank.BlocktankNotificationType.orderPaymentConfirmed
2626
import to.bitkit.models.blocktank.BlocktankNotificationType.wakeToTimeout
27+
import to.bitkit.repositories.LightningRepository
2728
import to.bitkit.services.CoreService
28-
import to.bitkit.services.LightningService
2929
import to.bitkit.ui.pushNotification
3030
import to.bitkit.utils.Logger
3131
import to.bitkit.utils.withPerformanceLogging
@@ -36,7 +36,7 @@ class WakeNodeWorker @AssistedInject constructor(
3636
@Assisted private val appContext: Context,
3737
@Assisted private val workerParams: WorkerParameters,
3838
private val coreService: CoreService,
39-
private val lightningService: LightningService,
39+
private val lightningRepo: LightningRepository,
4040
) : CoroutineWorker(appContext, workerParams) {
4141
private val self = this
4242

@@ -63,10 +63,8 @@ class WakeNodeWorker @AssistedInject constructor(
6363

6464
try {
6565
withPerformanceLogging {
66-
// TODO: Only start node if it's not running or implement & use StateLocker
67-
lightningService.setup(walletIndex = 0)
68-
lightningService.start(timeout) { event -> handleLdkEvent(event) }
69-
lightningService.connectToTrustedPeers()
66+
lightningRepo.start(walletIndex = 0, timeout= timeout) { event -> handleLdkEvent(event) }
67+
lightningRepo.connectToTrustedPeers()
7068

7169
// Once node is started, handle the manual channel opening if needed
7270
if (self.notificationType == orderPaymentConfirmed) {
@@ -136,7 +134,7 @@ class WakeNodeWorker @AssistedInject constructor(
136134
self.bestAttemptContent?.title = "Payment received"
137135
self.bestAttemptContent?.body = "Via new channel"
138136

139-
lightningService.channels?.find { it.channelId == event.channelId }?.let { channel ->
137+
lightningRepo.getChannels()?.find { it.channelId == event.channelId }?.let { channel ->
140138
val sats = channel.outboundCapacityMsat / 1000u
141139
self.bestAttemptContent?.title = "Received ⚡ $sats sats"
142140
// Save for UI to pick up
@@ -185,7 +183,7 @@ class WakeNodeWorker @AssistedInject constructor(
185183
}
186184

187185
private suspend fun deliver() {
188-
lightningService.stop()
186+
lightningRepo.stop()
189187

190188
bestAttemptContent?.run {
191189
pushNotification(title, body, context = appContext)

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import to.bitkit.utils.Logger
2424
import to.bitkit.utils.ServiceError
2525
import javax.inject.Inject
2626
import javax.inject.Singleton
27+
import kotlin.time.Duration
2728

2829
@Singleton
2930
class LightningRepository @Inject constructor(
@@ -45,7 +46,7 @@ class LightningRepository @Inject constructor(
4546
}
4647
}
4748

48-
suspend fun start(walletIndex: Int, eventHandler: NodeEventHandler? = null): Result<Unit> =
49+
suspend fun start(walletIndex: Int, timeout: Duration? = null, eventHandler: NodeEventHandler? = null): Result<Unit> =
4950
withContext(bgDispatcher) {
5051
if (nodeLifecycleState.value.isRunningOrStarting()) {
5152
return@withContext Result.success(Unit)
@@ -66,7 +67,7 @@ class LightningRepository @Inject constructor(
6667
}
6768

6869
// Start the node service
69-
lightningService.start { event ->
70+
lightningService.start(timeout) { event ->
7071
eventHandler?.invoke(event)
7172
ldkNodeEventBus.emit(event)
7273
}

0 commit comments

Comments
 (0)