[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

@ -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) {

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

View File

@ -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,11 +203,13 @@ 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 ->
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)
@ -205,6 +220,7 @@ class FeedbackFragment : Fragment(), CoroutineScope {
}
}
}
override fun getIcon() = bitmap
override fun getId() = id.toString()
override fun getName() = userName