improving the unread messages counter badge

This commit is contained in:
tibbi 2020-07-27 22:37:11 +02:00
parent f511f35df0
commit 2e0d6a877d
6 changed files with 18 additions and 22 deletions

View File

@ -61,7 +61,7 @@ dependencies {
implementation 'org.greenrobot:eventbus:3.2.0'
implementation 'com.klinkerapps:android-smsmms:5.2.6'
implementation 'com.github.tibbi:IndicatorFastScroll:08f512858a'
implementation "me.leolin:ShortcutBadger:1.1.22@aar"
implementation "me.leolin:ShortcutBadger:1.1.22"
kapt "androidx.room:room-compiler:2.2.5"
implementation "androidx.room:room-runtime:2.2.5"

View File

@ -452,23 +452,6 @@ fun Context.getNameAndPhotoFromPhoneNumber(number: String): NamePhoto? {
return NamePhoto(number, null)
}
fun Context.updateUnreadCountBadge(conversations : ArrayList<Conversation>){
var count = 0
conversations.forEach {
if(!it.read) {
count++
}
}
if(count == 0) {
ShortcutBadger.removeCount(this)
} else {
ShortcutBadger.applyCount(this,count)
}
}
fun Context.insertNewSMS(address: String, subject: String, body: String, date: Long, read: Int, threadId: Long, type: Int, subscriptionId: Int): Int {
val uri = Sms.CONTENT_URI
val contentValues = ContentValues().apply {
@ -540,6 +523,15 @@ fun Context.markThreadMessagesUnread(threadId: Int) {
}
}
fun Context.updateUnreadCountBadge(conversations: List<Conversation>) {
val unreadCount = conversations.count { !it.read }
if (unreadCount == 0) {
ShortcutBadger.removeCount(this)
} else {
ShortcutBadger.applyCount(this, unreadCount)
}
}
@SuppressLint("NewApi")
fun Context.getThreadId(address: String): Long {
return if (isMarshmallowPlus()) {

View File

@ -14,6 +14,9 @@ interface ConversationsDao {
@Query("SELECT * FROM conversations")
fun getAll(): List<Conversation>
@Query("SELECT * FROM conversations WHERE read = 0")
fun getUnreadConversations(): List<Conversation>
@Query("UPDATE conversations SET read = 1 WHERE thread_id = :threadId")
fun markRead(threadId: Long)

View File

@ -7,6 +7,7 @@ import com.simplemobiletools.commons.extensions.notificationManager
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.smsmessenger.extensions.conversationsDB
import com.simplemobiletools.smsmessenger.extensions.markThreadMessagesRead
import com.simplemobiletools.smsmessenger.extensions.updateUnreadCountBadge
import com.simplemobiletools.smsmessenger.helpers.MARK_AS_READ
import com.simplemobiletools.smsmessenger.helpers.THREAD_ID
@ -19,6 +20,7 @@ class MarkAsReadReceiver : BroadcastReceiver() {
ensureBackgroundThread {
context.markThreadMessagesRead(threadId)
context.conversationsDB.markRead(threadId.toLong())
context.updateUnreadCountBadge(context.conversationsDB.getUnreadConversations())
}
}
}

View File

@ -6,10 +6,7 @@ import com.bumptech.glide.Glide
import com.simplemobiletools.commons.extensions.isNumberBlocked
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.smsmessenger.R
import com.simplemobiletools.smsmessenger.extensions.conversationsDB
import com.simplemobiletools.smsmessenger.extensions.getConversations
import com.simplemobiletools.smsmessenger.extensions.getLatestMMS
import com.simplemobiletools.smsmessenger.extensions.showReceivedMessageNotification
import com.simplemobiletools.smsmessenger.extensions.*
// more info at https://github.com/klinker41/android-smsmms
class MmsReceiver : com.klinker.android.send_message.MmsReceivedReceiver() {
@ -36,6 +33,7 @@ class MmsReceiver : com.klinker.android.send_message.MmsReceivedReceiver() {
context.showReceivedMessageNotification(address, mms.body, mms.thread, glideBitmap)
val conversation = context.getConversations(mms.thread.toLong()).firstOrNull() ?: return@ensureBackgroundThread
context.conversationsDB.insertOrUpdate(conversation)
context.updateUnreadCountBadge(context.conversationsDB.getUnreadConversations())
}
}

View File

@ -37,6 +37,7 @@ class SmsReceiver : BroadcastReceiver() {
ensureBackgroundThread {
val conversation = context.getConversations(threadId).firstOrNull() ?: return@ensureBackgroundThread
context.conversationsDB.insertOrUpdate(conversation)
context.updateUnreadCountBadge(context.conversationsDB.getUnreadConversations())
}
}
}