Load parts during message sync

This commit is contained in:
Moez Bhatti
2018-01-14 20:03:39 -05:00
parent 4cc98e1cc1
commit 3ca5bfe6d7
6 changed files with 15 additions and 68 deletions
@@ -21,17 +21,21 @@ package data.mapper
import android.content.Context
import android.database.Cursor
import android.net.Uri
import android.provider.Telephony
import android.provider.Telephony.*
import com.google.android.mms.pdu_alt.PduHeaders
import common.util.Keys
import common.util.extensions.map
import data.model.Message
import data.model.MmsPart
import timber.log.Timber
import java.util.*
import javax.inject.Inject
class CursorToMessage @Inject constructor(
private val context: Context,
private val keys: Keys
private val keys: Keys,
private val cursorToPart: CursorToPart
) : Mapper<Pair<Cursor, CursorToMessage.MessageColumns>, Message> {
override fun map(from: Pair<Cursor, MessageColumns>): Message {
@@ -78,7 +82,9 @@ class CursorToMessage @Inject constructor(
mmsStatus = cursor.getInt(columnsMap.mmsStatus)
subject = cursor.getString(columnsMap.mmsSubject) ?: ""
textContentType = ""
attachmentType = Message.AttachmentType.NOT_LOADED
attachmentType = Message.AttachmentType.NOT_LOADED
parts.addAll(getMmsParts(contentId))
}
}
}
@@ -104,6 +110,12 @@ class CursorToMessage @Inject constructor(
return ""
}
private fun getMmsParts(contentId: Long): List<MmsPart> {
return context.contentResolver.query(CursorToPart.CONTENT_URI, null,
"${Telephony.Mms.Part.MSG_ID} = ?", arrayOf(contentId.toString()), null)
.map { cursorToPart.map(it) }
}
companion object {
val URI = Uri.parse("content://mms-sms/complete-conversations")
val PROJECTION = arrayOf(
@@ -142,20 +142,6 @@ class MessageRepository @Inject constructor(
.findAllSorted("date")
}
fun loadParts(id: Long) {
Realm.getDefaultInstance()?.use { realm ->
realm.where(Message::class.java).equalTo("id", id).findFirst()?.let { message ->
realm.executeTransaction {
val parts = realm.copyToRealmOrUpdate(context.contentResolver.query(CursorToPart.CONTENT_URI, null,
"${Telephony.Mms.Part.MSG_ID} = ?", arrayOf(message.contentId.toString()), null)
.map { cursorToPart.map(it) })
message.parts = RealmList<MmsPart>().apply { addAll(parts) }
}
}
}
}
/**
* Retrieves the list of messages which should be shown in the notification
* for a given conversation
@@ -1,32 +0,0 @@
/*
* 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 data.repository.MessageRepository
import io.reactivex.Flowable
import javax.inject.Inject
class LoadMmsParts @Inject constructor(private val messageRepo: MessageRepository): Interactor<Long, Long>() {
override fun buildObservable(params: Long): Flowable<Long> {
return Flowable.just(params)
.doOnNext { messageId -> messageRepo.loadParts(messageId) }
}
}
@@ -36,7 +36,6 @@ class ReceiveMms @Inject constructor(
override fun buildObservable(params: Uri): Flowable<Conversation> {
return Flowable.just(params)
.flatMap { uri -> syncManager.syncMessage(uri) } // Sync the message
.doOnNext { message -> messageRepo.loadParts(message.id) } // Sync the message parts
.mapNotNull { message -> messageRepo.getOrCreateConversation(message.threadId) } // Map message to conversation
.doOnNext { conversation -> if (conversation.archived) messageRepo.markUnarchived(conversation.id) } // Unarchive conversation if necessary
.doOnNext { conversation -> notificationManager.update(conversation.id) } // Update the notification
@@ -33,11 +33,6 @@ class SyncMessage @Inject constructor(
override fun buildObservable(params: Uri): Flowable<Message> {
return Flowable.just(params)
.flatMap { uri -> syncManager.syncMessage(uri) }
.doOnNext { message ->
if (message.isMms()) {
messageRepo.loadParts(message.id)
}
}
}
}
@@ -34,7 +34,6 @@ import common.util.extensions.setVisible
import data.model.Contact
import data.model.Message
import data.model.PhoneNumber
import interactor.LoadMmsParts
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.subjects.PublishSubject
@@ -48,8 +47,7 @@ import javax.inject.Inject
class MessagesAdapter @Inject constructor(
private val context: Context,
private val colors: Colors,
private val dateFormatter: DateFormatter,
private val loadMmsParts: LoadMmsParts)
private val dateFormatter: DateFormatter)
: RealmRecyclerViewAdapter<Message, QkViewHolder>(null, true) {
companion object {
@@ -102,11 +100,6 @@ class MessagesAdapter @Inject constructor(
return QkViewHolder(view)
}
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
disposables += loadMmsParts
}
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
super.onDetachedFromRecyclerView(recyclerView)
disposables.clear()
@@ -138,12 +131,6 @@ class MessagesAdapter @Inject constructor(
private fun bindMmsPreview(view: View, position: Int) {
val message = getItem(position)!!
// If it's an MMS and the parts haven't been loaded, kick off a load
if (message.isMms() && message.parts.isEmpty()) {
loadMmsParts.execute(message.id)
}
view.mmsPreview.parts = message.parts
}