[Feedback] Implement notifications.

This commit is contained in:
Kuba Szczodrzyński
2020-01-26 22:03:20 +01:00
parent 21b2e5d194
commit 5bf181b6d1
3 changed files with 62 additions and 28 deletions

View File

@ -48,14 +48,7 @@ class SzkolnyAppFirebase(val app: App, val profiles: List<Profile>, val message:
"appUpdate" -> launch { UpdateWorker.runNow(app, app.gson.fromJson(message.data.getString("update"), Update::class.java)) }
"feedbackMessage" -> launch {
val message = app.gson.fromJson(message.data.getString("message"), FeedbackMessage::class.java)
if (message.deviceId == app.deviceId) {
message.deviceId = null
message.deviceName = null
}
withContext(Dispatchers.Default) {
app.db.feedbackMessageDao().add(message)
}
EventBus.getDefault().postSticky(FeedbackMessageEvent(message))
feedbackMessage(message)
}
}
}
@ -74,6 +67,27 @@ class SzkolnyAppFirebase(val app: App, val profiles: List<Profile>, val message:
PostNotifications(app, listOf(notification))
}
private suspend fun feedbackMessage(message: FeedbackMessage) {
if (message.deviceId == app.deviceId) {
message.deviceId = null
message.deviceName = null
}
withContext(Dispatchers.Default) {
app.db.feedbackMessageDao().add(message)
val notification = Notification(
id = System.currentTimeMillis(),
title = "Wiadomość od ${message.senderName}",
text = message.text,
type = Notification.TYPE_FEEDBACK_MESSAGE,
profileId = null,
profileName = "Wiadomość od ${message.senderName}"
).addExtra("action", "feedbackMessage").addExtra("feedbackMessageDeviceId", message.deviceId)
app.db.notificationDao().add(notification)
PostNotifications(app, listOf(notification))
}
EventBus.getDefault().postSticky(FeedbackMessageEvent(message))
}
private fun sharedEvent(teamCode: String, jsonStr: String, message: String) {
val json = JsonParser().parse(jsonStr).asJsonObject
val teams = app.db.teamDao().allNow