mirror of
https://github.com/SimpleMobileTools/Simple-SMS-Messenger.git
synced 2025-06-05 21:49:22 +02:00
improving the unread messages counter badge
This commit is contained in:
@@ -61,7 +61,7 @@ dependencies {
|
|||||||
implementation 'org.greenrobot:eventbus:3.2.0'
|
implementation 'org.greenrobot:eventbus:3.2.0'
|
||||||
implementation 'com.klinkerapps:android-smsmms:5.2.6'
|
implementation 'com.klinkerapps:android-smsmms:5.2.6'
|
||||||
implementation 'com.github.tibbi:IndicatorFastScroll:08f512858a'
|
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"
|
kapt "androidx.room:room-compiler:2.2.5"
|
||||||
implementation "androidx.room:room-runtime:2.2.5"
|
implementation "androidx.room:room-runtime:2.2.5"
|
||||||
|
@@ -452,23 +452,6 @@ fun Context.getNameAndPhotoFromPhoneNumber(number: String): NamePhoto? {
|
|||||||
return NamePhoto(number, null)
|
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 {
|
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 uri = Sms.CONTENT_URI
|
||||||
val contentValues = ContentValues().apply {
|
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")
|
@SuppressLint("NewApi")
|
||||||
fun Context.getThreadId(address: String): Long {
|
fun Context.getThreadId(address: String): Long {
|
||||||
return if (isMarshmallowPlus()) {
|
return if (isMarshmallowPlus()) {
|
||||||
|
@@ -14,6 +14,9 @@ interface ConversationsDao {
|
|||||||
@Query("SELECT * FROM conversations")
|
@Query("SELECT * FROM conversations")
|
||||||
fun getAll(): List<Conversation>
|
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")
|
@Query("UPDATE conversations SET read = 1 WHERE thread_id = :threadId")
|
||||||
fun markRead(threadId: Long)
|
fun markRead(threadId: Long)
|
||||||
|
|
||||||
|
@@ -7,6 +7,7 @@ import com.simplemobiletools.commons.extensions.notificationManager
|
|||||||
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
|
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
|
||||||
import com.simplemobiletools.smsmessenger.extensions.conversationsDB
|
import com.simplemobiletools.smsmessenger.extensions.conversationsDB
|
||||||
import com.simplemobiletools.smsmessenger.extensions.markThreadMessagesRead
|
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.MARK_AS_READ
|
||||||
import com.simplemobiletools.smsmessenger.helpers.THREAD_ID
|
import com.simplemobiletools.smsmessenger.helpers.THREAD_ID
|
||||||
|
|
||||||
@@ -19,6 +20,7 @@ class MarkAsReadReceiver : BroadcastReceiver() {
|
|||||||
ensureBackgroundThread {
|
ensureBackgroundThread {
|
||||||
context.markThreadMessagesRead(threadId)
|
context.markThreadMessagesRead(threadId)
|
||||||
context.conversationsDB.markRead(threadId.toLong())
|
context.conversationsDB.markRead(threadId.toLong())
|
||||||
|
context.updateUnreadCountBadge(context.conversationsDB.getUnreadConversations())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -6,10 +6,7 @@ import com.bumptech.glide.Glide
|
|||||||
import com.simplemobiletools.commons.extensions.isNumberBlocked
|
import com.simplemobiletools.commons.extensions.isNumberBlocked
|
||||||
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
|
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
|
||||||
import com.simplemobiletools.smsmessenger.R
|
import com.simplemobiletools.smsmessenger.R
|
||||||
import com.simplemobiletools.smsmessenger.extensions.conversationsDB
|
import com.simplemobiletools.smsmessenger.extensions.*
|
||||||
import com.simplemobiletools.smsmessenger.extensions.getConversations
|
|
||||||
import com.simplemobiletools.smsmessenger.extensions.getLatestMMS
|
|
||||||
import com.simplemobiletools.smsmessenger.extensions.showReceivedMessageNotification
|
|
||||||
|
|
||||||
// more info at https://github.com/klinker41/android-smsmms
|
// more info at https://github.com/klinker41/android-smsmms
|
||||||
class MmsReceiver : com.klinker.android.send_message.MmsReceivedReceiver() {
|
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)
|
context.showReceivedMessageNotification(address, mms.body, mms.thread, glideBitmap)
|
||||||
val conversation = context.getConversations(mms.thread.toLong()).firstOrNull() ?: return@ensureBackgroundThread
|
val conversation = context.getConversations(mms.thread.toLong()).firstOrNull() ?: return@ensureBackgroundThread
|
||||||
context.conversationsDB.insertOrUpdate(conversation)
|
context.conversationsDB.insertOrUpdate(conversation)
|
||||||
|
context.updateUnreadCountBadge(context.conversationsDB.getUnreadConversations())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -37,6 +37,7 @@ class SmsReceiver : BroadcastReceiver() {
|
|||||||
ensureBackgroundThread {
|
ensureBackgroundThread {
|
||||||
val conversation = context.getConversations(threadId).firstOrNull() ?: return@ensureBackgroundThread
|
val conversation = context.getConversations(threadId).firstOrNull() ?: return@ensureBackgroundThread
|
||||||
context.conversationsDB.insertOrUpdate(conversation)
|
context.conversationsDB.insertOrUpdate(conversation)
|
||||||
|
context.updateUnreadCountBadge(context.conversationsDB.getUnreadConversations())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user