Skip to content

Commit e256a08

Browse files
committed
fix: apply formatting to WakeNodeWorker.onCjitChannelReady
1 parent af747ed commit e256a08

2 files changed

Lines changed: 15 additions & 51 deletions

File tree

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

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import dagger.assisted.AssistedInject
1010
import kotlinx.coroutines.CancellationException
1111
import kotlinx.coroutines.CompletableDeferred
1212
import kotlinx.coroutines.delay
13-
import kotlinx.coroutines.flow.first
1413
import kotlinx.coroutines.withTimeout
1514
import kotlinx.serialization.json.JsonObject
1615
import kotlinx.serialization.json.JsonPrimitive
@@ -21,12 +20,10 @@ import to.bitkit.App
2120
import to.bitkit.R
2221
import to.bitkit.androidServices.LightningNodeService
2322
import to.bitkit.data.CacheStore
24-
import to.bitkit.data.SettingsStore
2523
import to.bitkit.di.json
2624
import to.bitkit.domain.commands.ReceivedNotificationContent
2725
import to.bitkit.ext.amountOnClose
2826
import to.bitkit.ext.toUserMessage
29-
import to.bitkit.models.BITCOIN_SYMBOL
3027
import to.bitkit.models.BlocktankNotificationType
3128
import to.bitkit.models.BlocktankNotificationType.cjitPaymentArrived
3229
import to.bitkit.models.BlocktankNotificationType.incomingHtlc
@@ -37,7 +34,6 @@ import to.bitkit.models.NewTransactionSheetDetails
3734
import to.bitkit.models.NewTransactionSheetDirection
3835
import to.bitkit.models.NewTransactionSheetType
3936
import to.bitkit.models.NotificationDetails
40-
import to.bitkit.models.formatToModernDisplay
4137
import to.bitkit.models.msatCeilOf
4238
import to.bitkit.repositories.ActivityRepo
4339
import to.bitkit.repositories.BlocktankRepo
@@ -56,7 +52,6 @@ class WakeNodeWorker @AssistedInject constructor(
5652
private val lightningRepo: LightningRepo,
5753
private val blocktankRepo: BlocktankRepo,
5854
private val activityRepo: ActivityRepo,
59-
private val settingsStore: SettingsStore,
6055
private val cacheStore: CacheStore,
6156
private val receivedNotificationContent: ReceivedNotificationContent,
6257
) : CoroutineWorker(appContext, workerParams) {
@@ -141,8 +136,6 @@ class WakeNodeWorker @AssistedInject constructor(
141136
* @param event The LDK event to check.
142137
*/
143138
private suspend fun handleLdkEvent(event: Event) {
144-
val showDetails = settingsStore.data.first().showNotificationDetails
145-
val hiddenBody = appContext.getString(R.string.notification__received__body_hidden)
146139
when (event) {
147140
is Event.PaymentReceived -> onPaymentReceived(event)
148141

@@ -154,7 +147,7 @@ class WakeNodeWorker @AssistedInject constructor(
154147
// Don't deliver, give a chance for channelReady event to update the content if it's a turbo channel
155148
}
156149

157-
is Event.ChannelReady -> onChannelReady(event, showDetails, hiddenBody)
150+
is Event.ChannelReady -> onChannelReady(event)
158151
is Event.ChannelClosed -> onChannelClosed(event)
159152

160153
is Event.PaymentFailed -> {
@@ -218,13 +211,9 @@ class WakeNodeWorker @AssistedInject constructor(
218211
}
219212
}
220213

221-
private suspend fun onChannelReady(
222-
event: Event.ChannelReady,
223-
showDetails: Boolean,
224-
hiddenBody: String,
225-
) {
214+
private suspend fun onChannelReady(event: Event.ChannelReady) {
226215
when (notificationType) {
227-
cjitPaymentArrived -> onCjitChannelReady(event, showDetails, hiddenBody)
216+
cjitPaymentArrived -> onCjitChannelReady(event)
228217

229218
orderPaymentConfirmed -> bestAttemptContent = NotificationDetails(
230219
title = appContext.getString(R.string.notification__channel_opened_title),
@@ -236,11 +225,7 @@ class WakeNodeWorker @AssistedInject constructor(
236225
deliver()
237226
}
238227

239-
private suspend fun onCjitChannelReady(
240-
event: Event.ChannelReady,
241-
showDetails: Boolean,
242-
hiddenBody: String,
243-
) {
228+
private suspend fun onCjitChannelReady(event: Event.ChannelReady) {
244229
val channel = lightningRepo.getChannels()?.find { it.channelId == event.channelId }
245230
val cjitEntry = channel?.let { blocktankRepo.getCjitEntry(it) }
246231

@@ -277,11 +262,7 @@ class WakeNodeWorker @AssistedInject constructor(
277262
return
278263
}
279264

280-
val content = if (showDetails) "$BITCOIN_SYMBOL ${sats.formatToModernDisplay()}" else hiddenBody
281-
bestAttemptContent = NotificationDetails(
282-
title = content,
283-
body = appContext.getString(R.string.notification__received__body_channel),
284-
)
265+
bestAttemptContent = receivedNotificationContent.build(sats.toLong())
285266
}
286267

287268
private fun isHandledInProcess(): Boolean =

app/src/test/java/to/bitkit/fcm/WakeNodeWorkerTest.kt

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import androidx.work.ListenableWorker
1010
import androidx.work.WorkerParameters
1111
import androidx.work.workDataOf
1212
import com.synonym.bitkitcore.IcJitEntry
13-
import kotlinx.coroutines.flow.flowOf
1413
import org.junit.After
1514
import org.junit.Before
1615
import org.junit.Test
@@ -33,16 +32,12 @@ import to.bitkit.CurrentActivity
3332
import to.bitkit.R
3433
import to.bitkit.androidServices.LightningNodeService
3534
import to.bitkit.data.CacheStore
36-
import to.bitkit.data.SettingsData
37-
import to.bitkit.data.SettingsStore
3835
import to.bitkit.domain.commands.ReceivedNotificationContent
3936
import to.bitkit.ext.createChannelDetails
4037
import to.bitkit.ext.mock
4138
import to.bitkit.ext.notificationManager
42-
import to.bitkit.models.BITCOIN_SYMBOL
4339
import to.bitkit.models.BlocktankNotificationType
4440
import to.bitkit.models.NotificationDetails
45-
import to.bitkit.models.formatToModernDisplay
4641
import to.bitkit.repositories.ActivityRepo
4742
import to.bitkit.repositories.BlocktankRepo
4843
import to.bitkit.repositories.LightningRepo
@@ -51,7 +46,6 @@ import to.bitkit.test.BaseUnitTest
5146
import kotlin.test.assertEquals
5247
import kotlin.test.assertNotNull
5348
import kotlin.test.assertNull
54-
import kotlin.test.assertTrue
5549

5650
@Config(sdk = [34])
5751
@RunWith(RobolectricTestRunner::class)
@@ -61,19 +55,17 @@ class WakeNodeWorkerTest : BaseUnitTest() {
6155
private val lightningRepo = mock<LightningRepo>()
6256
private val blocktankRepo = mock<BlocktankRepo>()
6357
private val activityRepo = mock<ActivityRepo>()
64-
private val settingsStore = mock<SettingsStore>()
6558
private val cacheStore = mock<CacheStore>()
6659
private val receivedNotificationContent = mock<ReceivedNotificationContent>()
6760

6861
private val channelId = "channel-1"
69-
private val viaNewChannel by lazy { context.getString(R.string.notification__received__body_channel) }
62+
private val receivedTitle by lazy { context.getString(R.string.notification__received__title) }
7063

7164
@Before
7265
fun setUp() {
7366
whenever(workerParams.inputData).thenReturn(
7467
workDataOf("type" to BlocktankNotificationType.cjitPaymentArrived.name),
7568
)
76-
whenever(settingsStore.data).thenReturn(flowOf(SettingsData(showNotificationDetails = true)))
7769

7870
val app = context as Application
7971
Shadows.shadowOf(app).grantPermissions(Manifest.permission.POST_NOTIFICATIONS)
@@ -90,22 +82,19 @@ class WakeNodeWorkerTest : BaseUnitTest() {
9082
}
9183

9284
@Test
93-
fun `cjit channel ready formats amount with thousands separators`() = test {
85+
fun `cjit channel ready delivers rich notification content with fiat`() = test {
86+
val body = $$"Received ₿ 48 064 ($30.79)"
87+
whenever(receivedNotificationContent.build(48_064L)).thenReturn(NotificationDetails(receivedTitle, body))
9488
val channel = cjitChannel(sats = 48_064)
9589
stubChannel(channel, cjitEntry = IcJitEntry.mock())
9690
stubStartFiring(channelReadyEvent())
9791

9892
val result = worker().doWork()
9993

10094
assertEquals(ListenableWorker.Result.success(), result)
101-
val notification = findNotification(viaNewChannel)
95+
val notification = findNotificationByTitle(receivedTitle)
10296
assertNotNull(notification, "CJIT notification should be delivered when app is killed")
103-
assertEquals(
104-
"$BITCOIN_SYMBOL ${48_064L.formatToModernDisplay()}",
105-
notification?.extras?.getString(Notification.EXTRA_TITLE),
106-
)
107-
// sanity: a thousands separator is actually present
108-
assertTrue(48_064L.formatToModernDisplay().contains(' '), "amount should be grouped")
97+
assertEquals(body, notification?.extras?.getString(Notification.EXTRA_TEXT))
10998
verify(activityRepo).insertActivityFromCjit(any(), any())
11099
}
111100

@@ -118,7 +107,7 @@ class WakeNodeWorkerTest : BaseUnitTest() {
118107
val result = worker().doWork()
119108

120109
assertEquals(ListenableWorker.Result.success(), result)
121-
assertNull(findNotification(viaNewChannel), "A non-CJIT channel must not show a 'via new channel' notification")
110+
assertNull(findNotificationByTitle(receivedTitle), "A non-CJIT channel must not show a payment notification")
122111
verify(activityRepo, never()).insertActivityFromCjit(any(), any())
123112
verify(cacheStore, never()).setBackgroundReceive(any())
124113
}
@@ -133,7 +122,7 @@ class WakeNodeWorkerTest : BaseUnitTest() {
133122
val result = worker().doWork()
134123

135124
assertEquals(ListenableWorker.Result.success(), result)
136-
assertNull(findNotification(viaNewChannel), "Notification is deduped when the foreground service handles it")
125+
assertNull(findNotificationByTitle(receivedTitle), "Deduped when the foreground service handles it")
137126
// Activity is still recorded so the receive is not lost
138127
verify(activityRepo).insertActivityFromCjit(any(), any())
139128
}
@@ -148,7 +137,7 @@ class WakeNodeWorkerTest : BaseUnitTest() {
148137
val result = worker().doWork()
149138

150139
assertEquals(ListenableWorker.Result.success(), result)
151-
assertNull(findNotification(viaNewChannel), "A duplicate CJIT channel ready must not notify again")
140+
assertNull(findNotificationByTitle(receivedTitle), "A duplicate CJIT channel ready must not notify again")
152141
verify(cacheStore, never()).setBackgroundReceive(any())
153142
}
154143

@@ -162,7 +151,7 @@ class WakeNodeWorkerTest : BaseUnitTest() {
162151
val result = worker().doWork()
163152

164153
assertEquals(ListenableWorker.Result.success(), result)
165-
assertNull(findNotification(viaNewChannel), "Notification is deduped when the in-app UI handles it")
154+
assertNull(findNotificationByTitle(receivedTitle), "Notification is deduped when the in-app UI handles it")
166155
verify(activityRepo).insertActivityFromCjit(any(), any())
167156
}
168157

@@ -211,7 +200,6 @@ class WakeNodeWorkerTest : BaseUnitTest() {
211200
lightningRepo = lightningRepo,
212201
blocktankRepo = blocktankRepo,
213202
activityRepo = activityRepo,
214-
settingsStore = settingsStore,
215203
cacheStore = cacheStore,
216204
receivedNotificationContent = receivedNotificationContent,
217205
)
@@ -250,11 +238,6 @@ class WakeNodeWorkerTest : BaseUnitTest() {
250238
}
251239
}
252240

253-
private fun findNotification(body: String): Notification? {
254-
val shadows = Shadows.shadowOf(context.notificationManager)
255-
return shadows.allNotifications.find { it.extras.getString(Notification.EXTRA_TEXT) == body }
256-
}
257-
258241
private fun findNotificationByTitle(title: String): Notification? {
259242
val shadows = Shadows.shadowOf(context.notificationManager)
260243
return shadows.allNotifications.find { it.extras.getString(Notification.EXTRA_TITLE) == title }

0 commit comments

Comments
 (0)