forked from joreilly/Confetti
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationReceiver.kt
More file actions
49 lines (43 loc) · 1.88 KB
/
NotificationReceiver.kt
File metadata and controls
49 lines (43 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package dev.johnoreilly.confetti.notifications
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import androidx.core.app.NotificationManagerCompat
import dev.johnoreilly.confetti.ConfettiRepository
import dev.johnoreilly.confetti.auth.Authentication
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
class NotificationReceiver: BroadcastReceiver(), KoinComponent {
private val repository: ConfettiRepository by inject()
private val appScope: CoroutineScope by inject()
private val authentication: Authentication by inject()
private val notificationManager: NotificationManagerCompat by inject()
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == "REMOVE_BOOKMARK") {
doAsync {
removeBookmark(intent)
val notificationId = intent.getIntExtra("notificationId", -1)
if (notificationId != -1) {
notificationManager.cancel(notificationId)
}
}
}
}
private suspend fun removeBookmark(intent: Intent?) {
val conference = intent?.getStringExtra("conferenceId") ?: return
val sessionId = intent.getStringExtra("sessionId") ?: return
val user = authentication.currentUser.value ?: return
repository.removeBookmark(conference, user.uid, user, sessionId)
}
private fun BroadcastReceiver.doAsync(
coroutineContext: CoroutineContext = EmptyCoroutineContext,
block: suspend CoroutineScope.() -> Unit
){
val pendingResult = goAsync()
appScope.launch(coroutineContext) { block() }.invokeOnCompletion { pendingResult.finish() }
}
}