From 5bf181b6d1d788cab132cbe4fc6fc24d42dd5916 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Sun, 26 Jan 2020 22:03:20 +0100 Subject: [PATCH] [Feedback] Implement notifications. --- .../szczodrzynski/edziennik/MainActivity.kt | 18 ++++---- .../data/firebase/SzkolnyAppFirebase.kt | 30 +++++++++---- .../ui/modules/feedback/FeedbackFragment.kt | 42 +++++++++++++------ 3 files changed, 62 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt b/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt index c48799ed..d5857a7e 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt @@ -696,6 +696,9 @@ class MainActivity : AppCompatActivity(), CoroutineScope { } d(TAG, "}") + var intentProfileId = -1 + var intentTargetId = -1 + if (extras?.containsKey("action") == true) { val handled = when (extras.getString("action")) { "serverMessage" -> { @@ -706,6 +709,10 @@ class MainActivity : AppCompatActivity(), CoroutineScope { ) true } + "feedbackMessage" -> { + intentTargetId = TARGET_FEEDBACK + false + } else -> false } if (handled) @@ -721,17 +728,14 @@ class MainActivity : AppCompatActivity(), CoroutineScope { } } - var intentProfileId = -1 - var intentTargetId = -1 - - if (extras?.containsKey("profileId") == true) { + if (extras?.getInt("profileId", -1) != -1) { intentProfileId = extras.getInt("profileId", -1) - extras.remove("profileId") + extras?.remove("profileId") } - if (extras?.containsKey("fragmentId") == true) { + if (extras?.getInt("fragmentId", -1) != -1) { intentTargetId = extras.getInt("fragmentId", -1) - extras.remove("fragmentId") + extras?.remove("fragmentId") } /*if (intentTargetId == -1 && navController.currentDestination?.id == R.id.loadingFragment) { 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 78a1f688..85860d84 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 @@ -48,14 +48,7 @@ class SzkolnyAppFirebase(val app: App, val profiles: List, 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, 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 diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/feedback/FeedbackFragment.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/feedback/FeedbackFragment.kt index 5f4fe29e..81531cdb 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/feedback/FeedbackFragment.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/feedback/FeedbackFragment.kt @@ -6,6 +6,7 @@ import android.graphics.BitmapFactory import android.graphics.Canvas import android.graphics.Color import android.os.Bundle +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -36,6 +37,9 @@ import java.util.* import kotlin.coroutines.CoroutineContext class FeedbackFragment : Fragment(), CoroutineScope { + companion object { + private const val TAG = "FeedbackFragment" + } private lateinit var app: App private lateinit var activity: MainActivity @@ -60,6 +64,8 @@ class FeedbackFragment : Fragment(), CoroutineScope { context!!.theme.applyStyle(Themes.appTheme, true) // activity, context and profile is valid b = FragmentFeedbackBinding.inflate(inflater) + // prevent doubled received messages on enter + EventBus.getDefault().removeStickyEvent(FeedbackMessageEvent::class.java) return b.root } @@ -97,7 +103,7 @@ class FeedbackFragment : Fragment(), CoroutineScope { val messages = withContext(Dispatchers.Default) { app.db.feedbackMessageDao().allWithCountNow } val popupMenu = PopupMenu(activity, b.targetDeviceDropDown) messages.forEachIndexed { index, m -> - popupMenu.menu.add(0, index, index, "${m.senderName} ${m.deviceName} (${m.count})") + popupMenu.menu.add(0, index, index, "${m.senderName} (${m.deviceId}) - ${m.deviceName}") } popupMenu.setOnMenuItemClickListener { item -> b.targetDeviceDropDown.setText(item.title) @@ -166,9 +172,16 @@ class FeedbackFragment : Fragment(), CoroutineScope { } if (isDev) { - messages.firstOrNull()?.let { + messages.firstOrNull { it.received }?.let { currentDeviceId = it.deviceId - b.targetDeviceDropDown.setText("${it.senderName} ${it.deviceName}") + b.targetDeviceDropDown.setText("${it.senderName} (${it.deviceId}) - ${it.deviceName}") + } + // handle notification intent + arguments?.getString("feedbackMessageDeviceId")?.let { deviceId -> + currentDeviceId = deviceId + messages.firstOrNull { it.received && it.deviceId == deviceId }?.let { + b.targetDeviceDropDown.setText("${it.senderName} (${it.deviceId}) - ${it.deviceName}") + } } b.chatLayout.visibility = View.VISIBLE b.inputLayout.visibility = View.GONE @@ -190,21 +203,24 @@ class FeedbackFragment : Fragment(), CoroutineScope { } inner class User(val id: Int, val userName: String, val image: String?) : IChatUser { - override fun getIcon(): Bitmap? { + private val bitmap by lazy { if (image == null) - return BitmapFactory.decodeResource(activity.resources, R.drawable.profile_) - return Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888).also { bmp -> - launch { - Coil.load(activity, image) { - target { - val canvas = Canvas(bmp) - it.setBounds(0, 0, bmp.width, bmp.height) - it.draw(canvas) + BitmapFactory.decodeResource(activity.resources, R.drawable.profile_) + else + Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888).also { bmp -> + launch { + Log.d(TAG, "Created image for $userName") + Coil.load(activity, image) { + target { + val canvas = Canvas(bmp) + it.setBounds(0, 0, bmp.width, bmp.height) + it.draw(canvas) + } } } } - } } + override fun getIcon() = bitmap override fun getId() = id.toString() override fun getName() = userName