mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-17 11:38:51 +01:00
Manage notifications for each conversation independently
This commit is contained in:
@@ -45,12 +45,12 @@ class NotificationManager @Inject constructor(
|
||||
private val context: Context,
|
||||
private val prefs: Preferences,
|
||||
private val colors: Colors,
|
||||
private val messageRepo: MessageRepository,
|
||||
private val markUnarchived: MarkUnarchived
|
||||
) {
|
||||
|
||||
companion object {
|
||||
val DEFAULT_CHANNEL_ID = "channel_1"
|
||||
|
||||
val VIBRATE_PATTERN = longArrayOf(0, 200, 0, 200)
|
||||
}
|
||||
|
||||
@@ -75,56 +75,64 @@ class NotificationManager @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
fun update(messageRepo: MessageRepository) {
|
||||
if (!prefs.notifications.get()) return
|
||||
/**
|
||||
* Updates the notification for a particular conversation
|
||||
*/
|
||||
fun update(threadId: Long) {
|
||||
// If notifications are disabled, don't do anything
|
||||
if (!prefs.notifications.get()) {
|
||||
return
|
||||
}
|
||||
|
||||
messageRepo.getUnreadUnseenMessages()
|
||||
.groupBy { message -> message.threadId }
|
||||
.forEach { group ->
|
||||
val threadId = group.key
|
||||
val messages = group.value
|
||||
val conversation = messageRepo.getConversation(threadId) ?: return
|
||||
val messages = messageRepo.getUnreadUnseenMessages(threadId)
|
||||
|
||||
if (conversation.archived) {
|
||||
markUnarchived.execute(threadId)
|
||||
}
|
||||
// If there are no messages to be displayed, make sure that the notification is dismissed
|
||||
if (messages.isEmpty()) {
|
||||
notificationManager.cancel(threadId.toInt())
|
||||
return
|
||||
}
|
||||
|
||||
val style = NotificationCompat.MessagingStyle("Me")
|
||||
messages.forEach { message ->
|
||||
val name = if (message.isMe()) null else conversation.getTitle()
|
||||
style.addMessage(message.body, message.date, name)
|
||||
}
|
||||
val conversation = messageRepo.getConversation(threadId) ?: return
|
||||
|
||||
val contentIntent = Intent(context, ComposeActivity::class.java).putExtra("threadId", threadId)
|
||||
val taskStackBuilder = TaskStackBuilder.create(context)
|
||||
taskStackBuilder.addParentStack(ComposeActivity::class.java)
|
||||
taskStackBuilder.addNextIntent(contentIntent)
|
||||
val contentPI = taskStackBuilder.getPendingIntent(threadId.toInt() + 10000, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
if (conversation.archived) {
|
||||
markUnarchived.execute(threadId)
|
||||
}
|
||||
|
||||
val seenIntent = Intent(context, MarkSeenReceiver::class.java).putExtra("threadId", threadId)
|
||||
val seenPI = PendingIntent.getBroadcast(context, threadId.toInt() + 20000, seenIntent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
val style = NotificationCompat.MessagingStyle("Me")
|
||||
messages.forEach { message ->
|
||||
val name = if (message.isMe()) null else conversation.getTitle()
|
||||
style.addMessage(message.body, message.date, name)
|
||||
}
|
||||
|
||||
val readIntent = Intent(context, MarkReadReceiver::class.java).putExtra("threadId", threadId)
|
||||
val readPI = PendingIntent.getBroadcast(context, threadId.toInt() + 30000, readIntent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
val readAction = NotificationCompat.Action(R.drawable.ic_done_black_24dp, context.getString(R.string.notification_read), readPI)
|
||||
val contentIntent = Intent(context, ComposeActivity::class.java).putExtra("threadId", threadId)
|
||||
val taskStackBuilder = TaskStackBuilder.create(context)
|
||||
taskStackBuilder.addParentStack(ComposeActivity::class.java)
|
||||
taskStackBuilder.addNextIntent(contentIntent)
|
||||
val contentPI = taskStackBuilder.getPendingIntent(threadId.toInt() + 10000, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
|
||||
val notification = NotificationCompat.Builder(context, DEFAULT_CHANNEL_ID)
|
||||
.setColor(colors.theme.blockingFirst())
|
||||
.setSmallIcon(R.mipmap.ic_launcher)
|
||||
.setNumber(messages.size)
|
||||
.setAutoCancel(true)
|
||||
.setContentIntent(contentPI)
|
||||
.setDeleteIntent(seenPI)
|
||||
.addAction(readAction)
|
||||
.setStyle(style)
|
||||
.setVibrate(if (prefs.vibration.get()) VIBRATE_PATTERN else longArrayOf(0))
|
||||
val seenIntent = Intent(context, MarkSeenReceiver::class.java).putExtra("threadId", threadId)
|
||||
val seenPI = PendingIntent.getBroadcast(context, threadId.toInt() + 20000, seenIntent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
|
||||
if (Build.VERSION.SDK_INT >= 24) {
|
||||
notification.addAction(getReplyAction(conversation.recipients[0]?.address.orEmpty(), conversation.id))
|
||||
}
|
||||
val readIntent = Intent(context, MarkReadReceiver::class.java).putExtra("threadId", threadId)
|
||||
val readPI = PendingIntent.getBroadcast(context, threadId.toInt() + 30000, readIntent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
val readAction = NotificationCompat.Action(R.drawable.ic_done_black_24dp, context.getString(R.string.notification_read), readPI)
|
||||
|
||||
notificationManager.notify(threadId.toInt(), notification.build())
|
||||
}
|
||||
val notification = NotificationCompat.Builder(context, DEFAULT_CHANNEL_ID)
|
||||
.setColor(colors.theme.blockingFirst())
|
||||
.setSmallIcon(R.mipmap.ic_launcher)
|
||||
.setNumber(messages.size)
|
||||
.setAutoCancel(true)
|
||||
.setContentIntent(contentPI)
|
||||
.setDeleteIntent(seenPI)
|
||||
.addAction(readAction)
|
||||
.setStyle(style)
|
||||
.setVibrate(if (prefs.vibration.get()) VIBRATE_PATTERN else longArrayOf(0))
|
||||
|
||||
if (Build.VERSION.SDK_INT >= 24) {
|
||||
notification.addAction(getReplyAction(conversation.recipients[0]?.address.orEmpty(), conversation.id))
|
||||
}
|
||||
|
||||
notificationManager.notify(threadId.toInt(), notification.build())
|
||||
}
|
||||
|
||||
private fun getReplyAction(address: String, threadId: Long): NotificationCompat.Action {
|
||||
|
||||
@@ -139,11 +139,16 @@ class MessageRepository @Inject constructor(
|
||||
.findAllSorted("date")
|
||||
}
|
||||
|
||||
fun getUnreadUnseenMessages(): RealmResults<Message> {
|
||||
/**
|
||||
* Retrieves the list of messages which should be shown in the notification
|
||||
* for a given conversation
|
||||
*/
|
||||
fun getUnreadUnseenMessages(threadId: Long): RealmResults<Message> {
|
||||
return Realm.getDefaultInstance()
|
||||
.where(Message::class.java)
|
||||
.equalTo("seen", false)
|
||||
.equalTo("read", false)
|
||||
.equalTo("threadId", threadId)
|
||||
.findAllSorted("date")
|
||||
}
|
||||
|
||||
@@ -319,20 +324,15 @@ class MessageRepository @Inject constructor(
|
||||
.subscribe { addMessageFromUri(uri) }
|
||||
}
|
||||
|
||||
fun addMessageFromUri(uri: Uri) {
|
||||
fun addMessageFromUri(uri: Uri): Flowable<Message> {
|
||||
val cursor = context.contentResolver.query(uri, null, null, null, "date DESC")
|
||||
val columnsMap = CursorToMessage.MessageColumns(cursor)
|
||||
|
||||
if (cursor.moveToFirst()) {
|
||||
val message = cursorToMessage.map(Pair(cursor, columnsMap))
|
||||
|
||||
// If the conversation isn't in Realm, add it
|
||||
getConversation(message.threadId) ?: getConversationFromCp(message.threadId)
|
||||
|
||||
message.insertOrUpdate()
|
||||
}
|
||||
|
||||
cursor.close()
|
||||
// Map the cursor to a message
|
||||
return cursor.asFlowable()
|
||||
.map { cursorToMessage.map(Pair(it, columnsMap)) }
|
||||
.doOnNext { message -> getConversation(message.threadId) ?: getConversationFromCp(message.threadId) }
|
||||
.doOnNext { message -> message.insertOrUpdate() }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,15 +18,20 @@
|
||||
*/
|
||||
package com.moez.QKSMS.domain.interactor
|
||||
|
||||
import com.moez.QKSMS.common.util.NotificationManager
|
||||
import com.moez.QKSMS.data.repository.MessageRepository
|
||||
import io.reactivex.Flowable
|
||||
import javax.inject.Inject
|
||||
|
||||
class MarkRead @Inject constructor(private val messageRepo: MessageRepository) : Interactor<Long, Unit>() {
|
||||
class MarkRead @Inject constructor(
|
||||
private val messageRepo: MessageRepository,
|
||||
private val notificationManager: NotificationManager
|
||||
) : Interactor<Long, Unit>() {
|
||||
|
||||
override fun buildObservable(params: Long): Flowable<Unit> {
|
||||
return Flowable.just(Unit)
|
||||
.doOnNext { messageRepo.markRead(params) }
|
||||
.doOnNext { notificationManager.update(params) }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -45,8 +45,9 @@ class ReceiveMessage @Inject constructor(
|
||||
val contentResolver = context.contentResolver
|
||||
return Flowable.just(values)
|
||||
.map { contentResolver.insert(Telephony.Sms.Inbox.CONTENT_URI, values) }
|
||||
.doOnNext { uri -> messageRepo.addMessageFromUri(uri) }
|
||||
.doOnNext { notificationManager.update(messageRepo) }
|
||||
.flatMap { uri -> messageRepo.addMessageFromUri(uri) }
|
||||
.map { message -> message.threadId }
|
||||
.doOnNext { threadId -> notificationManager.update(threadId) }
|
||||
.map { Unit }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user