diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt b/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt index 0ea51fd4..c48799ed 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt @@ -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") diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/task/PostNotifications.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/task/PostNotifications.kt index 91aa0df0..b581a993 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/task/PostNotifications.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/task/PostNotifications.kt @@ -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) { +class PostNotifications(val app: App, nList: List) { companion object { private const val TAG = "PostNotifications" } @@ -124,7 +125,7 @@ class PostNotifications(val app: App, nList: MutableList) { 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() diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyAppFirebase.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyAppFirebase.kt index 904be226..4f079293 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyAppFirebase.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyAppFirebase.kt @@ -29,14 +29,33 @@ class SzkolnyAppFirebase(val app: App, val profiles: List, 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() val metadataList = mutableListOf() diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/ServerMessageDialog.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/ServerMessageDialog.kt new file mode 100644 index 00000000..27d11dad --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/ServerMessageDialog.kt @@ -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() + }} +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/notifications/NotificationsAdapter.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/notifications/NotificationsAdapter.kt index 5519c1ef..473b9855 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/notifications/NotificationsAdapter.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/notifications/NotificationsAdapter.kt @@ -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)