Don't show notification for active conversation, improve messaging performance

Previously, the app would mark the conversation read every time a message was received. Now, when we're receiving a message, we can see which conversation is open (if any), and mark it read during the inital write. Now we don't need to query and do another transaction for marking read
This commit is contained in:
Moez Bhatti
2018-09-05 17:51:53 -04:00
parent 8c01ef6612
commit 957a1ad1d4
7 changed files with 71 additions and 18 deletions
@@ -0,0 +1,19 @@
package com.moez.QKSMS.manager
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class ActiveConversationManagerImpl @Inject constructor() : ActiveConversationManager {
private var threadId: Long? = null
override fun setActiveConversation(threadId: Long?) {
this.threadId = threadId
}
override fun getActiveConversation(): Long? {
return threadId
}
}
@@ -36,6 +36,7 @@ import com.klinker.android.send_message.StripAccents
import com.klinker.android.send_message.Transaction
import com.moez.QKSMS.compat.TelephonyCompat
import com.moez.QKSMS.extensions.anyOf
import com.moez.QKSMS.manager.ActiveConversationManager
import com.moez.QKSMS.manager.KeyManager
import com.moez.QKSMS.model.Attachment
import com.moez.QKSMS.model.Conversation
@@ -57,6 +58,7 @@ import javax.inject.Singleton
@Singleton
class MessageRepositoryImpl @Inject constructor(
private val activeConversationManager: ActiveConversationManager,
private val context: Context,
private val messageIds: KeyManager,
private val imageRepository: ImageRepository,
@@ -351,6 +353,7 @@ class MessageRepositoryImpl @Inject constructor(
threadId = TelephonyCompat.getOrCreateThreadId(context, address)
boxId = Telephony.Sms.MESSAGE_TYPE_INBOX
type = "sms"
read = activeConversationManager.getActiveConversation() == threadId
}
val realm = Realm.getDefaultInstance()
var managedMessage: Message? = null
@@ -20,16 +20,17 @@ package com.moez.QKSMS.interactor
import android.net.Uri
import com.moez.QKSMS.extensions.mapNotNull
import com.moez.QKSMS.manager.ActiveConversationManager
import com.moez.QKSMS.manager.ExternalBlockingManager
import com.moez.QKSMS.manager.NotificationManager
import com.moez.QKSMS.repository.ConversationRepository
import com.moez.QKSMS.repository.MessageRepository
import com.moez.QKSMS.repository.SyncRepository
import io.reactivex.Flowable
import java.util.concurrent.TimeUnit
import javax.inject.Inject
class ReceiveMms @Inject constructor(
private val activeConversationManager: ActiveConversationManager,
private val conversationRepo: ConversationRepository,
private val externalBlockingManager: ExternalBlockingManager,
private val syncManager: SyncRepository,
@@ -40,7 +41,14 @@ class ReceiveMms @Inject constructor(
override fun buildObservable(params: Uri): Flowable<*> {
return Flowable.just(params)
.mapNotNull { uri -> syncManager.syncMessage(uri) } // Sync the message
.mapNotNull(syncManager::syncMessage) // Sync the message
.doOnNext { message ->
// TODO: Ideally this is done when we're saving the MMS to ContentResolver
// This change can be made once we move the MMS storing code to the Data module
if (activeConversationManager.getActiveConversation() == message.threadId) {
messageRepo.markRead(message.threadId)
}
}
.filter { message ->
// Because we use the smsmms library for receiving and storing MMS, we'll need
// to check if it should be blocked after we've pulled it into realm. If it
@@ -55,8 +63,7 @@ class ReceiveMms @Inject constructor(
.filter { conversation -> !conversation.blocked } // Don't notify for blocked conversations
.doOnNext { conversation -> if (conversation.archived) conversationRepo.markUnarchived(conversation.id) } // Unarchive conversation if necessary
.map { conversation -> conversation.id } // Map to the id because [delay] will put us on the wrong thread
.delay(1, TimeUnit.SECONDS) // Wait one second before trying to notify, in case the foreground app marks it as read first
.doOnNext { threadId -> notificationManager.update(threadId) } // Update the notification
.doOnNext(notificationManager::update) // Update the notification
.flatMap { updateBadge.buildObservable(Unit) } // Update the badge
}
@@ -26,7 +26,6 @@ import com.moez.QKSMS.manager.ShortcutManager
import com.moez.QKSMS.repository.ConversationRepository
import com.moez.QKSMS.repository.MessageRepository
import io.reactivex.Flowable
import java.util.concurrent.TimeUnit
import javax.inject.Inject
class ReceiveSms @Inject constructor(
@@ -63,7 +62,6 @@ class ReceiveSms @Inject constructor(
.filter { conversation -> !conversation.blocked } // Don't notify for blocked conversations
.doOnNext { conversation -> if (conversation.archived) conversationRepo.markUnarchived(conversation.id) } // Unarchive conversation if necessary
.map { conversation -> conversation.id } // Map to the id because [delay] will put us on the wrong thread
.delay(1, TimeUnit.SECONDS) // Wait one second before trying to notify, in case the foreground app marks it as read first
.doOnNext { threadId -> notificationManager.update(threadId) } // Update the notification
.doOnNext { shortcutManager.updateShortcuts() } // Update shortcuts
.flatMap { updateBadge.buildObservable(Unit) } // Update the badge
@@ -0,0 +1,14 @@
package com.moez.QKSMS.manager
/**
* Keeps track of the conversation that the user is currently viewing. This is useful when we
* receive a message, because it allows us to immediately mark the message read and not display
* a notification
*/
interface ActiveConversationManager {
fun setActiveConversation(threadId: Long?)
fun getActiveConversation(): Long?
}
@@ -45,6 +45,7 @@ import com.moez.QKSMS.interactor.DeleteMessages
import com.moez.QKSMS.interactor.MarkRead
import com.moez.QKSMS.interactor.RetrySending
import com.moez.QKSMS.interactor.SendMessage
import com.moez.QKSMS.manager.ActiveConversationManager
import com.moez.QKSMS.manager.PermissionManager
import com.moez.QKSMS.model.Attachment
import com.moez.QKSMS.model.Contact
@@ -69,7 +70,6 @@ import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
import io.realm.RealmList
import java.util.*
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import javax.inject.Named
@@ -80,6 +80,7 @@ class ComposeViewModel @Inject constructor(
@Named("text") private val sharedText: String,
@Named("attachments") private val sharedAttachments: List<Attachment>,
private val context: Context,
private val activeConversationManager: ActiveConversationManager,
private val addScheduledMessage: AddScheduledMessage,
private val billingManager: BillingManager,
private val cancelMessage: CancelDelayedMessage,
@@ -162,9 +163,10 @@ class ComposeViewModel @Inject constructor(
.takeUntil(state.filter { state -> !state.editingMode })
.subscribe(selectedContacts::onNext)
// When the conversation changes, update the threadId and the messages for the adapter
// When the conversation changes, mark read, and update the threadId and the messages for the adapter
disposables += conversation
.distinctUntilChanged { conversation -> conversation.id }
.doOnNext { conversation -> markRead.execute(listOf(conversation.id)) }
.observeOn(AndroidSchedulers.mainThread())
.map { conversation ->
val messages = messageRepo.getMessages(conversation.id)
@@ -172,7 +174,7 @@ class ComposeViewModel @Inject constructor(
messages
}
.switchMap { messages -> messages.asObservable() }
.subscribe { messages.onNext(it) }
.subscribe(messages::onNext)
disposables += conversation
.map { conversation -> conversation.getTitle() }
@@ -426,6 +428,20 @@ class ComposeViewModel @Inject constructor(
.autoDisposable(view.scope())
.subscribe { message -> cancelMessage.execute(message.id) }
// Set the current conversation
Observables
.combineLatest(
view.activityVisibleIntent.distinctUntilChanged(),
conversation.mapNotNull { conversation -> conversation.takeIf { it.isValid }?.id }.distinctUntilChanged())
{ visible, threadId ->
when (visible) {
true -> activeConversationManager.setActiveConversation(threadId)
false -> activeConversationManager.setActiveConversation(null)
}
}
.autoDisposable(view.scope())
.subscribe()
// Save draft when the activity goes into the background
view.activityVisibleIntent
.filter { visible -> !visible }
@@ -438,15 +454,6 @@ class ComposeViewModel @Inject constructor(
.autoDisposable(view.scope())
.subscribe()
// Mark the conversation read, if in foreground
Observables.combineLatest(messages, view.activityVisibleIntent) { _, visible -> visible }
.filter { visible -> visible }
.withLatestFrom(conversation) { _, conversation -> conversation }
.mapNotNull { conversation -> conversation.takeIf { it.isValid }?.id }
.debounce(200, TimeUnit.MILLISECONDS)
.autoDisposable(view.scope())
.subscribe { threadId -> markRead.execute(listOf(threadId)) }
// Open the attachment options
view.attachIntent
.autoDisposable(view.scope())
@@ -31,6 +31,8 @@ import com.moez.QKSMS.feature.conversationinfo.injection.ConversationInfoCompone
import com.moez.QKSMS.feature.themepicker.injection.ThemePickerComponent
import com.moez.QKSMS.listener.ContactAddedListener
import com.moez.QKSMS.listener.ContactAddedListenerImpl
import com.moez.QKSMS.manager.ActiveConversationManager
import com.moez.QKSMS.manager.ActiveConversationManagerImpl
import com.moez.QKSMS.manager.AlarmManager
import com.moez.QKSMS.manager.AlarmManagerImpl
import com.moez.QKSMS.manager.AnalyticsManager
@@ -111,6 +113,9 @@ class AppModule(private var application: Application) {
// Manager
@Provides
fun provideActiveConversationManager(manager: ActiveConversationManagerImpl): ActiveConversationManager = manager
@Provides
fun provideAlarmManager(manager: AlarmManagerImpl): AlarmManager = manager