Update badge whenever message read status is modified

This commit is contained in:
Moez Bhatti
2018-03-09 15:46:07 -08:00
parent 6f8dcac958
commit b15568cef9
8 changed files with 72 additions and 13 deletions
+1
View File
@@ -97,6 +97,7 @@ dependencies {
implementation "io.reactivex.rxjava2:rxandroid:2.0.1"
implementation "io.reactivex.rxjava2:rxjava:2.1.4"
implementation "io.reactivex.rxjava2:rxkotlin:2.1.0"
implementation "me.leolin:ShortcutBadger:1.1.21"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation project(":android-smsmms")
kapt "com.github.bumptech.glide:compiler:4.3.0"
@@ -187,6 +187,13 @@ class MessageRepository @Inject constructor(
.findFirst()
}
fun getUnreadMessageCount(): Long {
return Realm.getDefaultInstance()
.where(Message::class.java)
.equalTo("read", false)
.count()
}
fun getPart(id: Long): MmsPart? {
return Realm.getDefaultInstance()
.where(MmsPart::class.java)
@@ -22,11 +22,15 @@ import data.repository.MessageRepository
import io.reactivex.Flowable
import javax.inject.Inject
class DeleteConversation @Inject constructor(private val messageRepo: MessageRepository): Interactor<Long>() {
class DeleteConversation @Inject constructor(
private val messageRepo: MessageRepository,
private val updateBadge: UpdateBadge
): Interactor<Long>() {
override fun buildObservable(params: Long): Flowable<Unit> {
override fun buildObservable(params: Long): Flowable<*> {
return Flowable.just(Unit)
.doOnNext { messageRepo.deleteConversation(params) }
.flatMap { updateBadge.buildObservable(Unit) } // Update the badge
}
}
@@ -22,11 +22,15 @@ import data.repository.MessageRepository
import io.reactivex.Flowable
import javax.inject.Inject
class DeleteMessage @Inject constructor(private val messageRepository: MessageRepository): Interactor<Long>() {
class DeleteMessage @Inject constructor(
private val messageRepository: MessageRepository,
private val updateBadge: UpdateBadge
) : Interactor<Long>() {
override fun buildObservable(params: Long): Flowable<Long> {
override fun buildObservable(params: Long): Flowable<*> {
return Flowable.just(params)
.doOnNext { messageId -> messageRepository.deleteMessage(messageId) }
.flatMap { updateBadge.buildObservable(Unit) } // Update the badge
}
}
+4 -2
View File
@@ -25,13 +25,15 @@ import javax.inject.Inject
class MarkRead @Inject constructor(
private val messageRepo: MessageRepository,
private val notificationManager: NotificationManager
private val notificationManager: NotificationManager,
private val updateBadge: UpdateBadge
) : Interactor<Long>() {
override fun buildObservable(params: Long): Flowable<Unit> {
override fun buildObservable(params: Long): Flowable<*> {
return Flowable.just(Unit)
.doOnNext { messageRepo.markRead(params) }
.doOnNext { notificationManager.update(params) }
.flatMap { updateBadge.buildObservable(Unit) } // Update the badge
}
}
+4 -3
View File
@@ -22,7 +22,6 @@ import android.net.Uri
import common.util.NotificationManager
import common.util.SyncManager
import common.util.extensions.mapNotNull
import data.model.Conversation
import data.repository.MessageRepository
import io.reactivex.Flowable
import javax.inject.Inject
@@ -30,16 +29,18 @@ import javax.inject.Inject
class ReceiveMms @Inject constructor(
private val syncManager: SyncManager,
private val messageRepo: MessageRepository,
private val notificationManager: NotificationManager
private val notificationManager: NotificationManager,
private val updateBadge: UpdateBadge
) : Interactor<Uri>() {
override fun buildObservable(params: Uri): Flowable<Conversation> {
override fun buildObservable(params: Uri): Flowable<*> {
return Flowable.just(params)
.flatMap { uri -> syncManager.syncMessage(uri) } // Sync the message
.mapNotNull { message -> messageRepo.getOrCreateConversation(message.threadId) } // Map message to conversation
.filter { conversation -> !conversation.blocked } // Don't notify for blocked conversations
.doOnNext { conversation -> if (conversation.archived) messageRepo.markUnarchived(conversation.id) } // Unarchive conversation if necessary
.doOnNext { conversation -> notificationManager.update(conversation.id) } // Update the notification
.flatMap { updateBadge.buildObservable(Unit) } // Update the badge
}
}
+5 -4
View File
@@ -21,17 +21,17 @@ package interactor
import android.telephony.SmsMessage
import common.util.NotificationManager
import common.util.extensions.mapNotNull
import data.model.Conversation
import data.repository.MessageRepository
import io.reactivex.Flowable
import javax.inject.Inject
class ReceiveSms @Inject constructor(
private val messageRepo: MessageRepository,
private val notificationManager: NotificationManager)
: Interactor<Array<SmsMessage>>() {
private val notificationManager: NotificationManager,
private val updateBadge: UpdateBadge
) : Interactor<Array<SmsMessage>>() {
override fun buildObservable(params: Array<SmsMessage>): Flowable<Conversation> {
override fun buildObservable(params: Array<SmsMessage>): Flowable<*> {
return Flowable.just(params)
.filter { it.isNotEmpty() }
.map { messages ->
@@ -47,6 +47,7 @@ class ReceiveSms @Inject constructor(
.filter { conversation -> !conversation.blocked } // Don't notify for blocked conversations
.doOnNext { conversation -> if (conversation.archived) messageRepo.markUnarchived(conversation.id) } // Unarchive conversation if necessary
.doOnNext { conversation -> notificationManager.update(conversation.id) } // Update the notification
.flatMap { updateBadge.buildObservable(Unit) } // Update the badge
}
}
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package interactor
import android.content.Context
import data.repository.MessageRepository
import io.reactivex.Flowable
import me.leolin.shortcutbadger.ShortcutBadger
import javax.inject.Inject
class UpdateBadge @Inject constructor(
private val context: Context,
private val messageRepo: MessageRepository
) : Interactor<Unit>() {
override fun buildObservable(params: Unit): Flowable<*> {
return Flowable.just(params)
.map { messageRepo.getUnreadMessageCount() }
.map { count -> count.toInt() }
.doOnNext { count -> ShortcutBadger.applyCount(context, count) }
}
}