Shifted a number of db writes when opening conversations to the IO thread so they don't block

This commit is contained in:
Morgan Pretty 2023-01-06 09:50:31 +11:00
parent c0bef51fe0
commit e7b6ddacbb
3 changed files with 22 additions and 4 deletions

View File

@ -343,7 +343,11 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe
super.onResume()
ApplicationContext.getInstance(this).messageNotifier.setVisibleThread(viewModel.threadId)
val recipient = viewModel.recipient ?: return
threadDb.markAllAsRead(viewModel.threadId, recipient.isOpenGroupRecipient)
lifecycleScope.launch(Dispatchers.IO) {
threadDb.markAllAsRead(viewModel.threadId, recipient.isOpenGroupRecipient)
}
contentResolver.registerContentObserver(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
true,

View File

@ -6,6 +6,8 @@ import androidx.lifecycle.viewModelScope
import com.goterl.lazysodium.utils.KeyPair
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update
@ -48,11 +50,19 @@ class ConversationViewModel(
}
fun saveDraft(text: String) {
repository.saveDraft(threadId, text)
GlobalScope.launch(Dispatchers.IO) {
repository.saveDraft(threadId, text)
}
}
fun getDraft(): String? {
return repository.getDraft(threadId)
val draft: String? = repository.getDraft(threadId)
viewModelScope.launch(Dispatchers.IO) {
repository.clearDrafts(threadId)
}
return draft
}
fun inviteContacts(contacts: List<Recipient>) {

View File

@ -35,6 +35,7 @@ interface ConversationRepository {
fun maybeGetRecipientForThreadId(threadId: Long): Recipient?
fun saveDraft(threadId: Long, text: String)
fun getDraft(threadId: Long): String?
fun clearDrafts(threadId: Long)
fun inviteContacts(threadId: Long, contacts: List<Recipient>)
fun setBlocked(recipient: Recipient, blocked: Boolean)
fun deleteLocally(recipient: Recipient, message: MessageRecord)
@ -98,10 +99,13 @@ class DefaultConversationRepository @Inject constructor(
override fun getDraft(threadId: Long): String? {
val drafts = draftDb.getDrafts(threadId)
draftDb.clearDrafts(threadId)
return drafts.find { it.type == DraftDatabase.Draft.TEXT }?.value
}
override fun clearDrafts(threadId: Long) {
draftDb.clearDrafts(threadId)
}
override fun inviteContacts(threadId: Long, contacts: List<Recipient>) {
val openGroup = lokiThreadDb.getOpenGroupChat(threadId) ?: return
for (contact in contacts) {