Skip to content

Commit

Permalink
feat: adjust to new notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriykulikov committed Sep 18, 2024
1 parent 70177f4 commit 33d9a43
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ class NotificationListener : NotificationListenerService() {
activeNotifications
.filter { it.packageName == "com.paypal.android.p2pmobile" }
.mapNotNull { notification ->
val title =
notification.notification?.extras?.getCharSequence("android.title")?.toString()
val text =
notification.notification?.extras?.getCharSequence("android.text")?.toString()
when {
text != null ->
PaypalNotification(
time = notification.postTime,
title = title,
text = text,
device = signedInUser ?: "unknown",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ data class PaypalNotification(
val time: Long,
val text: String,
val device: String? = null,
val title: String? = null,
)

/** Notifications can only be added, never removed. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ fun PaypalNotification.sumCents(): Int {
text.substringAfter(" on a ").substringBefore(" purchase at ")
text.startsWith("You sent") ->
text.substringAfter("You sent ").substringBefore(" to ")
text.startsWith("You authorized a payment of ") ->
text
.substringAfter("You authorized a payment of ")
.substringBefore(". Tap for more details.")
text.startsWith("You placed an order for ") ->
text
.substringAfter("You placed an order for ")
.substringBefore(". Tap for more details.")
text.startsWith("Your ") ->
text
.substringAfter("Your ")
.substringBefore("payment was completed. Tap for more details.")
else -> return 0
}.trim()
val currency = sumText.substringAfter(" ").substringAfterLast(" ")
Expand All @@ -42,5 +54,11 @@ fun PaypalNotification.sumCents(): Int {
}

fun PaypalNotification.merchant(): String {
return text.substringAfter(" to ").substringAfter("purchase at ")
val title = title
return when {
title != null && title.startsWith("Your purchase at ") -> title.substringAfter("Your purchase at ")
// this should not have happened
text.endsWith("Tap for more details.") -> ""
else -> text.substringAfter(" to ").substringAfter("purchase at ")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,16 @@ class NotificationTest {
fun parse4() {
PaypalNotification(0, "You sent 30.00 € EUR to Some One", "Pixel 4a").sumCents() shouldBe 3000
}

@Test
fun parse5() {
val longNotification = PaypalNotification(
0,
"You placed an order for €2.960,00 EUR. Tap for more details.",
"Pixel 4a",
"Your purchase at Coral Baral GmbH"
)
longNotification.merchant() shouldBe "Coral Baral GmbH"
longNotification.sumCents() shouldBe 296000
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ class NotificationsTest {
.grouped
.shouldHaveSize(2444)
}

@Test
fun `parsing all kinds of notifications`() = runBlocking<Unit> {
firebase.notifications(prod = true).values.toList()
.asSequence()
.filter { notification -> "You received" !in notification.text }
.filter { notification -> "Partial refund" !in notification.text }
.filter { notification -> !notification.text.startsWith("Are you trying") }
.filter { it.sumCents() == 0 }
.forEach { println(it) }
}
}

private val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.GERMANY)
Expand Down

0 comments on commit 33d9a43

Please sign in to comment.