@@ -37,6 +37,7 @@ import to.bitkit.utils.Logger
3737import to.bitkit.utils.withPerformanceLogging
3838import kotlin.time.Duration.Companion.minutes
3939
40+ @Suppress(" LongParameterList" )
4041@HiltWorker
4142class WakeNodeWorker @AssistedInject constructor(
4243 @Assisted private val appContext : Context ,
@@ -120,75 +121,16 @@ class WakeNodeWorker @AssistedInject constructor(
120121 val showDetails = settingsStore.data.first().showNotificationDetails
121122 val openBitkitMessage = " Open Bitkit to see details"
122123 when (event) {
123- is Event .PaymentReceived -> {
124- bestAttemptContent?.title = " Payment Received"
125- val sats = event.amountMsat / 1000u
126- // Save for UI to pick up
127- NewTransactionSheetDetails .save(
128- appContext,
129- NewTransactionSheetDetails (
130- type = NewTransactionSheetType .LIGHTNING ,
131- direction = NewTransactionSheetDirection .RECEIVED ,
132- paymentHashOrTxId = event.paymentHash,
133- sats = sats.toLong(),
134- )
135- )
136- val content = if (showDetails) " $BITCOIN_SYMBOL $sats " else openBitkitMessage
137- bestAttemptContent?.body = content
138- if (self.notificationType == incomingHtlc) {
139- self.deliver()
140- }
141- }
124+ is Event .PaymentReceived -> onPaymentReceived(event, showDetails, openBitkitMessage)
142125
143126 is Event .ChannelPending -> {
144127 self.bestAttemptContent?.title = " Channel Opened"
145128 self.bestAttemptContent?.body = " Pending"
146129 // Don't deliver, give a chance for channelReady event to update the content if it's a turbo channel
147130 }
148131
149- is Event .ChannelReady -> {
150- if (self.notificationType == cjitPaymentArrived) {
151- self.bestAttemptContent?.title = " Payment received"
152- self.bestAttemptContent?.body = " Via new channel"
153-
154- lightningRepo.getChannels()?.find { it.channelId == event.channelId }?.let { channel ->
155- val sats = channel.amountOnClose
156- val content = if (showDetails) " $BITCOIN_SYMBOL $sats " else openBitkitMessage
157- self.bestAttemptContent?.title = content
158- val cjitEntry = channel.let { blocktankRepo.getCjitEntry(it) }
159- if (cjitEntry != null ) {
160- // Save for UI to pick up
161- NewTransactionSheetDetails .save(
162- appContext,
163- NewTransactionSheetDetails (
164- type = NewTransactionSheetType .LIGHTNING ,
165- direction = NewTransactionSheetDirection .RECEIVED ,
166- sats = sats.toLong(),
167- )
168- )
169- activityRepo.insertActivityFromCjit(cjitEntry = cjitEntry, channel = channel)
170- }
171- }
172- } else if (self.notificationType == orderPaymentConfirmed) {
173- self.bestAttemptContent?.title = " Channel opened"
174- self.bestAttemptContent?.body = " Ready to send"
175- }
176- self.deliver()
177- }
178-
179- is Event .ChannelClosed -> {
180- self.bestAttemptContent?.title = " Channel closed"
181- self.bestAttemptContent?.body = " Reason: ${event.reason} "
182-
183- if (self.notificationType == mutualClose) {
184- self.bestAttemptContent?.body = " Balance moved from spending to savings"
185- } else if (self.notificationType == orderPaymentConfirmed) {
186- self.bestAttemptContent?.title = " Channel failed to open in the background"
187- self.bestAttemptContent?.body = " Please try again"
188- }
189-
190- self.deliver()
191- }
132+ is Event .ChannelReady -> onChannelReady(event, showDetails, openBitkitMessage)
133+ is Event .ChannelClosed -> onChannelClosed(event)
192134
193135 is Event .PaymentSuccessful -> Unit
194136 is Event .PaymentClaimable -> Unit
@@ -205,6 +147,78 @@ class WakeNodeWorker @AssistedInject constructor(
205147 }
206148 }
207149
150+ private suspend fun onChannelClosed (event : Event .ChannelClosed ) {
151+ self.bestAttemptContent?.title = " Channel closed"
152+ self.bestAttemptContent?.body = " Reason: ${event.reason} "
153+
154+ if (self.notificationType == mutualClose) {
155+ self.bestAttemptContent?.body = " Balance moved from spending to savings"
156+ } else if (self.notificationType == orderPaymentConfirmed) {
157+ self.bestAttemptContent?.title = " Channel failed to open in the background"
158+ self.bestAttemptContent?.body = " Please try again"
159+ }
160+
161+ self.deliver()
162+ }
163+
164+ private suspend fun onPaymentReceived (
165+ event : Event .PaymentReceived ,
166+ showDetails : Boolean ,
167+ openBitkitMessage : String ,
168+ ) {
169+ bestAttemptContent?.title = " Payment Received"
170+ val sats = event.amountMsat / 1000u
171+ // Save for UI to pick up
172+ NewTransactionSheetDetails .save(
173+ appContext,
174+ NewTransactionSheetDetails (
175+ type = NewTransactionSheetType .LIGHTNING ,
176+ direction = NewTransactionSheetDirection .RECEIVED ,
177+ paymentHashOrTxId = event.paymentHash,
178+ sats = sats.toLong(),
179+ )
180+ )
181+ val content = if (showDetails) " $BITCOIN_SYMBOL $sats " else openBitkitMessage
182+ bestAttemptContent?.body = content
183+ if (self.notificationType == incomingHtlc) {
184+ self.deliver()
185+ }
186+ }
187+
188+ private suspend fun onChannelReady (
189+ event : Event .ChannelReady ,
190+ showDetails : Boolean ,
191+ openBitkitMessage : String ,
192+ ) {
193+ if (self.notificationType == cjitPaymentArrived) {
194+ self.bestAttemptContent?.title = " Payment received"
195+ self.bestAttemptContent?.body = " Via new channel"
196+
197+ lightningRepo.getChannels()?.find { it.channelId == event.channelId }?.let { channel ->
198+ val sats = channel.amountOnClose
199+ val content = if (showDetails) " $BITCOIN_SYMBOL $sats " else openBitkitMessage
200+ self.bestAttemptContent?.title = content
201+ val cjitEntry = channel.let { blocktankRepo.getCjitEntry(it) }
202+ if (cjitEntry != null ) {
203+ // Save for UI to pick up
204+ NewTransactionSheetDetails .save(
205+ appContext,
206+ NewTransactionSheetDetails (
207+ type = NewTransactionSheetType .LIGHTNING ,
208+ direction = NewTransactionSheetDirection .RECEIVED ,
209+ sats = sats.toLong(),
210+ )
211+ )
212+ activityRepo.insertActivityFromCjit(cjitEntry = cjitEntry, channel = channel)
213+ }
214+ }
215+ } else if (self.notificationType == orderPaymentConfirmed) {
216+ self.bestAttemptContent?.title = " Channel opened"
217+ self.bestAttemptContent?.body = " Ready to send"
218+ }
219+ self.deliver()
220+ }
221+
208222 private suspend fun deliver () {
209223 lightningRepo.stop()
210224
0 commit comments