forked from github/szkolny
[Feedback] Implement notifications.
This commit is contained in:
parent
21b2e5d194
commit
5bf181b6d1
@ -696,6 +696,9 @@ class MainActivity : AppCompatActivity(), CoroutineScope {
|
|||||||
}
|
}
|
||||||
d(TAG, "}")
|
d(TAG, "}")
|
||||||
|
|
||||||
|
var intentProfileId = -1
|
||||||
|
var intentTargetId = -1
|
||||||
|
|
||||||
if (extras?.containsKey("action") == true) {
|
if (extras?.containsKey("action") == true) {
|
||||||
val handled = when (extras.getString("action")) {
|
val handled = when (extras.getString("action")) {
|
||||||
"serverMessage" -> {
|
"serverMessage" -> {
|
||||||
@ -706,6 +709,10 @@ class MainActivity : AppCompatActivity(), CoroutineScope {
|
|||||||
)
|
)
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
"feedbackMessage" -> {
|
||||||
|
intentTargetId = TARGET_FEEDBACK
|
||||||
|
false
|
||||||
|
}
|
||||||
else -> false
|
else -> false
|
||||||
}
|
}
|
||||||
if (handled)
|
if (handled)
|
||||||
@ -721,17 +728,14 @@ class MainActivity : AppCompatActivity(), CoroutineScope {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var intentProfileId = -1
|
if (extras?.getInt("profileId", -1) != -1) {
|
||||||
var intentTargetId = -1
|
|
||||||
|
|
||||||
if (extras?.containsKey("profileId") == true) {
|
|
||||||
intentProfileId = extras.getInt("profileId", -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)
|
intentTargetId = extras.getInt("fragmentId", -1)
|
||||||
extras.remove("fragmentId")
|
extras?.remove("fragmentId")
|
||||||
}
|
}
|
||||||
|
|
||||||
/*if (intentTargetId == -1 && navController.currentDestination?.id == R.id.loadingFragment) {
|
/*if (intentTargetId == -1 && navController.currentDestination?.id == R.id.loadingFragment) {
|
||||||
|
@ -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)) }
|
"appUpdate" -> launch { UpdateWorker.runNow(app, app.gson.fromJson(message.data.getString("update"), Update::class.java)) }
|
||||||
"feedbackMessage" -> launch {
|
"feedbackMessage" -> launch {
|
||||||
val message = app.gson.fromJson(message.data.getString("message"), FeedbackMessage::class.java)
|
val message = app.gson.fromJson(message.data.getString("message"), FeedbackMessage::class.java)
|
||||||
if (message.deviceId == app.deviceId) {
|
feedbackMessage(message)
|
||||||
message.deviceId = null
|
|
||||||
message.deviceName = null
|
|
||||||
}
|
|
||||||
withContext(Dispatchers.Default) {
|
|
||||||
app.db.feedbackMessageDao().add(message)
|
|
||||||
}
|
|
||||||
EventBus.getDefault().postSticky(FeedbackMessageEvent(message))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -74,6 +67,27 @@ class SzkolnyAppFirebase(val app: App, val profiles: List<Profile>, val message:
|
|||||||
PostNotifications(app, listOf(notification))
|
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) {
|
private fun sharedEvent(teamCode: String, jsonStr: String, message: String) {
|
||||||
val json = JsonParser().parse(jsonStr).asJsonObject
|
val json = JsonParser().parse(jsonStr).asJsonObject
|
||||||
val teams = app.db.teamDao().allNow
|
val teams = app.db.teamDao().allNow
|
||||||
|
@ -6,6 +6,7 @@ import android.graphics.BitmapFactory
|
|||||||
import android.graphics.Canvas
|
import android.graphics.Canvas
|
||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.util.Log
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
@ -36,6 +37,9 @@ import java.util.*
|
|||||||
import kotlin.coroutines.CoroutineContext
|
import kotlin.coroutines.CoroutineContext
|
||||||
|
|
||||||
class FeedbackFragment : Fragment(), CoroutineScope {
|
class FeedbackFragment : Fragment(), CoroutineScope {
|
||||||
|
companion object {
|
||||||
|
private const val TAG = "FeedbackFragment"
|
||||||
|
}
|
||||||
|
|
||||||
private lateinit var app: App
|
private lateinit var app: App
|
||||||
private lateinit var activity: MainActivity
|
private lateinit var activity: MainActivity
|
||||||
@ -60,6 +64,8 @@ class FeedbackFragment : Fragment(), CoroutineScope {
|
|||||||
context!!.theme.applyStyle(Themes.appTheme, true)
|
context!!.theme.applyStyle(Themes.appTheme, true)
|
||||||
// activity, context and profile is valid
|
// activity, context and profile is valid
|
||||||
b = FragmentFeedbackBinding.inflate(inflater)
|
b = FragmentFeedbackBinding.inflate(inflater)
|
||||||
|
// prevent doubled received messages on enter
|
||||||
|
EventBus.getDefault().removeStickyEvent(FeedbackMessageEvent::class.java)
|
||||||
return b.root
|
return b.root
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,7 +103,7 @@ class FeedbackFragment : Fragment(), CoroutineScope {
|
|||||||
val messages = withContext(Dispatchers.Default) { app.db.feedbackMessageDao().allWithCountNow }
|
val messages = withContext(Dispatchers.Default) { app.db.feedbackMessageDao().allWithCountNow }
|
||||||
val popupMenu = PopupMenu(activity, b.targetDeviceDropDown)
|
val popupMenu = PopupMenu(activity, b.targetDeviceDropDown)
|
||||||
messages.forEachIndexed { index, m ->
|
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 ->
|
popupMenu.setOnMenuItemClickListener { item ->
|
||||||
b.targetDeviceDropDown.setText(item.title)
|
b.targetDeviceDropDown.setText(item.title)
|
||||||
@ -166,9 +172,16 @@ class FeedbackFragment : Fragment(), CoroutineScope {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isDev) {
|
if (isDev) {
|
||||||
messages.firstOrNull()?.let {
|
messages.firstOrNull { it.received }?.let {
|
||||||
currentDeviceId = it.deviceId
|
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.chatLayout.visibility = View.VISIBLE
|
||||||
b.inputLayout.visibility = View.GONE
|
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 {
|
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)
|
if (image == null)
|
||||||
return BitmapFactory.decodeResource(activity.resources, R.drawable.profile_)
|
BitmapFactory.decodeResource(activity.resources, R.drawable.profile_)
|
||||||
return Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888).also { bmp ->
|
else
|
||||||
launch {
|
Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888).also { bmp ->
|
||||||
Coil.load(activity, image) {
|
launch {
|
||||||
target {
|
Log.d(TAG, "Created image for $userName")
|
||||||
val canvas = Canvas(bmp)
|
Coil.load(activity, image) {
|
||||||
it.setBounds(0, 0, bmp.width, bmp.height)
|
target {
|
||||||
it.draw(canvas)
|
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 getId() = id.toString()
|
||||||
override fun getName() = userName
|
override fun getName() = userName
|
||||||
|
Loading…
x
Reference in New Issue
Block a user