Skip to content

Commit

Permalink
feat: Change unique request code generator
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielGreenEngineer committed Jun 13, 2024
1 parent 72b769f commit 3b01b21
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,10 @@ object NotificationHelper {
intent.putExtra(NOTIFICATION_ID, data[NOTIFICATION_ID])
intent.putExtra(CURRENT_IMAGE_INDEX, currentIndex)

val requestCode = RequestCodeGenerator.generateRequestCode(action, currentIndex)
return PendingIntent.getService(
/* context = */ context,
/* requestCode = */ RequestCodeGenerator.getNextRequestCode(),
/* requestCode = */ requestCode,
/* intent = */ intent,
/* flags = */ PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.personalizatio.notification

import java.util.concurrent.atomic.AtomicInteger
import kotlin.math.abs

object RequestCodeGenerator {
private val counter = AtomicInteger(0)

fun getNextRequestCode(): Int {
return counter.getAndIncrement()
/**
* Create a unique requestCode based on the hashcode of the combination of action and currentIndex
* */
fun generateRequestCode(action: String, currentIndex: Int): Int {
val uniqueKey = "${action}_${currentIndex}".hashCode()
return if (uniqueKey != Int.MIN_VALUE) abs(uniqueKey) else 0
}
}
}

0 comments on commit 3b01b21

Please sign in to comment.