forked from upstream-mirrors/quik
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 context: Context,
|
||||||
private val prefs: Preferences,
|
private val prefs: Preferences,
|
||||||
private val colors: Colors,
|
private val colors: Colors,
|
||||||
|
private val messageRepo: MessageRepository,
|
||||||
private val markUnarchived: MarkUnarchived
|
private val markUnarchived: MarkUnarchived
|
||||||
) {
|
) {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val DEFAULT_CHANNEL_ID = "channel_1"
|
val DEFAULT_CHANNEL_ID = "channel_1"
|
||||||
|
|
||||||
val VIBRATE_PATTERN = longArrayOf(0, 200, 0, 200)
|
val VIBRATE_PATTERN = longArrayOf(0, 200, 0, 200)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,14 +75,23 @@ 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
|
||||||
|
}
|
||||||
|
|
||||||
|
val messages = messageRepo.getUnreadUnseenMessages(threadId)
|
||||||
|
|
||||||
|
// If there are no messages to be displayed, make sure that the notification is dismissed
|
||||||
|
if (messages.isEmpty()) {
|
||||||
|
notificationManager.cancel(threadId.toInt())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
messageRepo.getUnreadUnseenMessages()
|
|
||||||
.groupBy { message -> message.threadId }
|
|
||||||
.forEach { group ->
|
|
||||||
val threadId = group.key
|
|
||||||
val messages = group.value
|
|
||||||
val conversation = messageRepo.getConversation(threadId) ?: return
|
val conversation = messageRepo.getConversation(threadId) ?: return
|
||||||
|
|
||||||
if (conversation.archived) {
|
if (conversation.archived) {
|
||||||
@@ -125,7 +134,6 @@ class NotificationManager @Inject constructor(
|
|||||||
|
|
||||||
notificationManager.notify(threadId.toInt(), notification.build())
|
notificationManager.notify(threadId.toInt(), notification.build())
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private fun getReplyAction(address: String, threadId: Long): NotificationCompat.Action {
|
private fun getReplyAction(address: String, threadId: Long): NotificationCompat.Action {
|
||||||
val replyIntent = Intent(context, RemoteMessagingReceiver::class.java)
|
val replyIntent = Intent(context, RemoteMessagingReceiver::class.java)
|
||||||
|
|||||||
@@ -139,11 +139,16 @@ class MessageRepository @Inject constructor(
|
|||||||
.findAllSorted("date")
|
.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()
|
return Realm.getDefaultInstance()
|
||||||
.where(Message::class.java)
|
.where(Message::class.java)
|
||||||
.equalTo("seen", false)
|
.equalTo("seen", false)
|
||||||
.equalTo("read", false)
|
.equalTo("read", false)
|
||||||
|
.equalTo("threadId", threadId)
|
||||||
.findAllSorted("date")
|
.findAllSorted("date")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -319,20 +324,15 @@ class MessageRepository @Inject constructor(
|
|||||||
.subscribe { addMessageFromUri(uri) }
|
.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 cursor = context.contentResolver.query(uri, null, null, null, "date DESC")
|
||||||
val columnsMap = CursorToMessage.MessageColumns(cursor)
|
val columnsMap = CursorToMessage.MessageColumns(cursor)
|
||||||
|
|
||||||
if (cursor.moveToFirst()) {
|
// Map the cursor to a message
|
||||||
val message = cursorToMessage.map(Pair(cursor, columnsMap))
|
return cursor.asFlowable()
|
||||||
|
.map { cursorToMessage.map(Pair(it, columnsMap)) }
|
||||||
// If the conversation isn't in Realm, add it
|
.doOnNext { message -> getConversation(message.threadId) ?: getConversationFromCp(message.threadId) }
|
||||||
getConversation(message.threadId) ?: getConversationFromCp(message.threadId)
|
.doOnNext { message -> message.insertOrUpdate() }
|
||||||
|
|
||||||
message.insertOrUpdate()
|
|
||||||
}
|
|
||||||
|
|
||||||
cursor.close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -18,15 +18,20 @@
|
|||||||
*/
|
*/
|
||||||
package com.moez.QKSMS.domain.interactor
|
package com.moez.QKSMS.domain.interactor
|
||||||
|
|
||||||
|
import com.moez.QKSMS.common.util.NotificationManager
|
||||||
import com.moez.QKSMS.data.repository.MessageRepository
|
import com.moez.QKSMS.data.repository.MessageRepository
|
||||||
import io.reactivex.Flowable
|
import io.reactivex.Flowable
|
||||||
import javax.inject.Inject
|
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> {
|
override fun buildObservable(params: Long): Flowable<Unit> {
|
||||||
return Flowable.just(Unit)
|
return Flowable.just(Unit)
|
||||||
.doOnNext { messageRepo.markRead(params) }
|
.doOnNext { messageRepo.markRead(params) }
|
||||||
|
.doOnNext { notificationManager.update(params) }
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -45,8 +45,9 @@ class ReceiveMessage @Inject constructor(
|
|||||||
val contentResolver = context.contentResolver
|
val contentResolver = context.contentResolver
|
||||||
return Flowable.just(values)
|
return Flowable.just(values)
|
||||||
.map { contentResolver.insert(Telephony.Sms.Inbox.CONTENT_URI, values) }
|
.map { contentResolver.insert(Telephony.Sms.Inbox.CONTENT_URI, values) }
|
||||||
.doOnNext { uri -> messageRepo.addMessageFromUri(uri) }
|
.flatMap { uri -> messageRepo.addMessageFromUri(uri) }
|
||||||
.doOnNext { notificationManager.update(messageRepo) }
|
.map { message -> message.threadId }
|
||||||
|
.doOnNext { threadId -> notificationManager.update(threadId) }
|
||||||
.map { Unit }
|
.map { Unit }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user