[Firebase] Implement handling server messages.

This commit is contained in:
Kuba Szczodrzyński 2020-01-19 21:31:37 +01:00
parent 512baaa43f
commit 6cd2c23aac
5 changed files with 92 additions and 4 deletions

View File

@ -49,6 +49,7 @@ import pl.szczodrzynski.edziennik.databinding.ActivitySzkolnyBinding
import pl.szczodrzynski.edziennik.sync.AppManagerDetectedEvent
import pl.szczodrzynski.edziennik.sync.SyncWorker
import pl.szczodrzynski.edziennik.sync.UpdateWorker
import pl.szczodrzynski.edziennik.ui.dialogs.ServerMessageDialog
import pl.szczodrzynski.edziennik.ui.dialogs.changelog.ChangelogDialog
import pl.szczodrzynski.edziennik.ui.dialogs.settings.ProfileRemoveDialog
import pl.szczodrzynski.edziennik.ui.dialogs.sync.SyncViewListDialog
@ -695,6 +696,22 @@ class MainActivity : AppCompatActivity(), CoroutineScope {
}
d(TAG, "}")
if (extras?.containsKey("action") == true) {
val handled = when (extras.getString("action")) {
"serverMessage" -> {
ServerMessageDialog(
this,
extras.getString("serverMessageTitle") ?: getString(R.string.app_name),
extras.getString("serverMessageText") ?: ""
)
true
}
else -> false
}
if (handled)
return
}
if (extras?.containsKey("reloadProfileId") == true) {
val reloadProfileId = extras.getInt("reloadProfileId", -1)
extras.remove("reloadProfileId")

View File

@ -9,9 +9,10 @@ import androidx.core.app.NotificationCompat
import androidx.core.util.forEach
import androidx.core.util.set
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.data.db.entity.Notification.Companion.TYPE_SERVER_MESSAGE
import pl.szczodrzynski.edziennik.data.db.entity.Notification as AppNotification
class PostNotifications(val app: App, nList: MutableList<AppNotification>) {
class PostNotifications(val app: App, nList: List<AppNotification>) {
companion object {
private const val TAG = "PostNotifications"
}
@ -124,7 +125,7 @@ class PostNotifications(val app: App, nList: MutableList<AppNotification>) {
NotificationCompat.Builder(app, app.notifications.dataKey)
.setContentTitle(it.profileName ?: app.getString(R.string.app_name))
.setContentText(it.text)
.setSubText(it.title)
.setSubText(if (it.type == TYPE_SERVER_MESSAGE) null else it.title)
.setTicker("${it.profileName}: ${it.title}")
.setSmallIcon(R.drawable.ic_notification)
.setStyle(NotificationCompat.BigTextStyle()

View File

@ -29,14 +29,33 @@ class SzkolnyAppFirebase(val app: App, val profiles: List<Profile>, val message:
message.data.getLong("eventId") ?: return@run,
message.data.getString("message") ?: return@run
)
"serverMessage",
"unpairedBrowser" -> serverMessage(
message.data.getString("title") ?: "",
message.data.getString("message") ?: ""
)
}
}
}
private fun serverMessage(title: String, message: String) {
val notification = Notification(
id = System.currentTimeMillis(),
title = title,
text = message,
type = Notification.TYPE_SERVER_MESSAGE,
profileId = null,
profileName = title
).addExtra("action", "serverMessage").addExtra("serverMessageTitle", title).addExtra("serverMessageText", message)
app.db.notificationDao().add(notification)
PostNotifications(app, listOf(notification))
}
private fun sharedEvent(teamCode: String, jsonStr: String, message: String) {
val json = JsonParser().parse(jsonStr).asJsonObject
val teams = app.db.teamDao().allNow
val eventTypes = app.db.eventTypeDao().allNow
// not used, as the server provides a sharing message
//val eventTypes = app.db.eventTypeDao().allNow
val events = mutableListOf<Event>()
val metadataList = mutableListOf<Metadata>()

View File

@ -0,0 +1,51 @@
/*
* Copyright (c) Kuba Szczodrzyński 2020-1-19.
*/
package pl.szczodrzynski.edziennik.ui.dialogs
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import pl.szczodrzynski.edziennik.App
import pl.szczodrzynski.edziennik.R
import kotlin.coroutines.CoroutineContext
class ServerMessageDialog(
val activity: AppCompatActivity,
val title: String,
val message: String,
val onShowListener: ((tag: String) -> Unit)? = null,
val onDismissListener: ((tag: String) -> Unit)? = null
) : CoroutineScope {
companion object {
private const val TAG = "ServerMessageDialog"
}
private lateinit var app: App
private lateinit var dialog: AlertDialog
private val job = Job()
override val coroutineContext: CoroutineContext
get() = job + Dispatchers.Main
init { run {
if (activity.isFinishing)
return@run
onShowListener?.invoke(TAG)
app = activity.applicationContext as App
dialog = MaterialAlertDialogBuilder(activity)
.setTitle(title)
.setMessage(message)
.setPositiveButton(R.string.close) { dialog, _ ->
dialog.dismiss()
}
.setOnDismissListener {
onDismissListener?.invoke(TAG)
}
.show()
}}
}

View File

@ -53,7 +53,7 @@ class NotificationsAdapter(
//Log.d(TAG, "Got date "+intent.getLongExtra("timetableDate", 0));
if (notification.profileId != -1 && notification.profileId != app.profile.id && context is Activity) {
if (notification.profileId != null && notification.profileId != -1 && notification.profileId != app.profile.id && context is Activity) {
Toast.makeText(app, app.getString(R.string.toast_changing_profile), Toast.LENGTH_LONG).show()
}
app.sendBroadcast(intent)