mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-20 12:48:53 +01:00
Conversation as field in Message
This commit is contained in:
@@ -24,6 +24,7 @@ import android.net.Uri
|
|||||||
import android.provider.Telephony.*
|
import android.provider.Telephony.*
|
||||||
import com.google.android.mms.pdu_alt.PduHeaders
|
import com.google.android.mms.pdu_alt.PduHeaders
|
||||||
import manager.KeyManager
|
import manager.KeyManager
|
||||||
|
import model.Conversation
|
||||||
import model.Message
|
import model.Message
|
||||||
import util.extensions.map
|
import util.extensions.map
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
@@ -77,12 +78,13 @@ class CursorToMessageImpl @Inject constructor(
|
|||||||
|
|
||||||
id = keys.newId()
|
id = keys.newId()
|
||||||
contentId = cursor.getLong(columnsMap.msgId)
|
contentId = cursor.getLong(columnsMap.msgId)
|
||||||
threadId = cursor.getLong(columnsMap.threadId)
|
|
||||||
date = cursor.getLong(columnsMap.date)
|
date = cursor.getLong(columnsMap.date)
|
||||||
dateSent = cursor.getLong(columnsMap.dateSent)
|
dateSent = cursor.getLong(columnsMap.dateSent)
|
||||||
read = cursor.getInt(columnsMap.read) != 0
|
read = cursor.getInt(columnsMap.read) != 0
|
||||||
locked = cursor.getInt(columnsMap.locked) != 0
|
locked = cursor.getInt(columnsMap.locked) != 0
|
||||||
|
|
||||||
|
conversation = Conversation().apply { id = cursor.getLong(columnsMap.threadId) }
|
||||||
|
|
||||||
when (type) {
|
when (type) {
|
||||||
"sms" -> {
|
"sms" -> {
|
||||||
address = cursor.getString(columnsMap.smsAddress) ?: ""
|
address = cursor.getString(columnsMap.smsAddress) ?: ""
|
||||||
|
|||||||
@@ -34,7 +34,6 @@ import io.reactivex.Flowable
|
|||||||
import io.reactivex.Maybe
|
import io.reactivex.Maybe
|
||||||
import io.reactivex.Observable
|
import io.reactivex.Observable
|
||||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||||
import io.reactivex.rxkotlin.Flowables
|
|
||||||
import io.reactivex.schedulers.Schedulers
|
import io.reactivex.schedulers.Schedulers
|
||||||
import io.realm.Realm
|
import io.realm.Realm
|
||||||
import io.realm.RealmResults
|
import io.realm.RealmResults
|
||||||
@@ -64,54 +63,24 @@ class MessageRepositoryImpl @Inject constructor(
|
|||||||
private val cursorToRecipient: CursorToRecipient,
|
private val cursorToRecipient: CursorToRecipient,
|
||||||
private val prefs: Preferences) : MessageRepository {
|
private val prefs: Preferences) : MessageRepository {
|
||||||
|
|
||||||
override fun getConversations(archived: Boolean): Flowable<List<Pair<Conversation, Message>>> {
|
override fun getConversations(archived: Boolean): RealmResults<Message> {
|
||||||
val realm = Realm.getDefaultInstance()
|
return Realm.getDefaultInstance()
|
||||||
|
.where(Message::class.java)
|
||||||
val conversationFlowable = realm.where(Conversation::class.java)
|
|
||||||
.equalTo("archived", archived)
|
|
||||||
.equalTo("blocked", false)
|
|
||||||
.findAllAsync()
|
|
||||||
.asFlowable()
|
|
||||||
.filter { it.isLoaded }
|
|
||||||
.map { realm.copyFromRealm(it) }
|
|
||||||
|
|
||||||
val messageFlowable = realm.where(Message::class.java)
|
|
||||||
.sort("date", Sort.DESCENDING)
|
|
||||||
.distinctValues("threadId")
|
.distinctValues("threadId")
|
||||||
|
.equalTo("conversation.archived", archived)
|
||||||
|
.equalTo("conversation.blocked", false)
|
||||||
|
.sort("date", Sort.DESCENDING)
|
||||||
.findAllAsync()
|
.findAllAsync()
|
||||||
.asFlowable()
|
|
||||||
.filter { it.isLoaded }
|
|
||||||
.map { realm.copyFromRealm(it) }
|
|
||||||
|
|
||||||
return Flowables.combineLatest(conversationFlowable, messageFlowable, { conversations, messages ->
|
|
||||||
val conversationMap = conversations.associateBy { conversation -> conversation.id }
|
|
||||||
|
|
||||||
messages.mapNotNull { message ->
|
|
||||||
val conversation = conversationMap[message.threadId]
|
|
||||||
if (conversation == null) null else Pair(conversation, message)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getConversationsForWidget(): List<Pair<Conversation, Message>> {
|
override fun getConversationsForWidget(): List<Message> {
|
||||||
val realm = Realm.getDefaultInstance()
|
val realm = Realm.getDefaultInstance()
|
||||||
|
return realm.copyFromRealm(realm.where(Message::class.java)
|
||||||
val conversations = realm.copyFromRealm(realm.where(Conversation::class.java)
|
|
||||||
.equalTo("archived", false)
|
|
||||||
.equalTo("blocked", false)
|
|
||||||
.findAll())
|
|
||||||
|
|
||||||
val messages = realm.copyFromRealm(realm.where(Message::class.java)
|
|
||||||
.sort("date", Sort.DESCENDING)
|
|
||||||
.distinctValues("threadId")
|
.distinctValues("threadId")
|
||||||
.findAll())
|
.equalTo("conversation.archived", false)
|
||||||
|
.equalTo("conversation.blocked", false)
|
||||||
val conversationMap = conversations.associateBy { conversation -> conversation.id }
|
.sort("date", Sort.DESCENDING)
|
||||||
|
.findAllAsync())
|
||||||
return messages.mapNotNull { message ->
|
|
||||||
val conversation = conversationMap[message.threadId]
|
|
||||||
if (conversation == null) null else Pair(conversation, message)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getBlockedConversations(): Flowable<List<Conversation>> {
|
override fun getBlockedConversations(): Flowable<List<Conversation>> {
|
||||||
@@ -383,7 +352,6 @@ class MessageRepositoryImpl @Inject constructor(
|
|||||||
|
|
||||||
// Insert the message to Realm
|
// Insert the message to Realm
|
||||||
val message = Message().apply {
|
val message = Message().apply {
|
||||||
this.threadId = threadId
|
|
||||||
this.address = address
|
this.address = address
|
||||||
this.body = body
|
this.body = body
|
||||||
this.date = System.currentTimeMillis()
|
this.date = System.currentTimeMillis()
|
||||||
@@ -393,10 +361,12 @@ class MessageRepositoryImpl @Inject constructor(
|
|||||||
type = "sms"
|
type = "sms"
|
||||||
read = true
|
read = true
|
||||||
seen = true
|
seen = true
|
||||||
|
|
||||||
|
conversation = getOrCreateConversation(threadId)
|
||||||
}
|
}
|
||||||
val realm = Realm.getDefaultInstance()
|
val realm = Realm.getDefaultInstance()
|
||||||
var managedMessage: Message? = null
|
var managedMessage: Message? = null
|
||||||
realm.executeTransaction { managedMessage = realm.copyToRealm(message) }
|
realm.executeTransaction { managedMessage = realm.copyToRealmOrUpdate(message) }
|
||||||
|
|
||||||
// Insert the message to the native content provider
|
// Insert the message to the native content provider
|
||||||
val values = ContentValues().apply {
|
val values = ContentValues().apply {
|
||||||
@@ -427,14 +397,14 @@ class MessageRepositoryImpl @Inject constructor(
|
|||||||
this.date = System.currentTimeMillis()
|
this.date = System.currentTimeMillis()
|
||||||
|
|
||||||
id = messageIds.newId()
|
id = messageIds.newId()
|
||||||
threadId = getOrCreateConversation(address).blockingGet().id
|
|
||||||
boxId = Telephony.Sms.MESSAGE_TYPE_INBOX
|
boxId = Telephony.Sms.MESSAGE_TYPE_INBOX
|
||||||
type = "sms"
|
type = "sms"
|
||||||
|
|
||||||
|
conversation = getOrCreateConversation(address).blockingGet()
|
||||||
}
|
}
|
||||||
val realm = Realm.getDefaultInstance()
|
val realm = Realm.getDefaultInstance()
|
||||||
var managedMessage: Message? = null
|
var managedMessage: Message? = null
|
||||||
realm.executeTransaction { managedMessage = realm.copyToRealm(message) }
|
realm.executeTransaction { managedMessage = realm.copyToRealmOrUpdate(message) }
|
||||||
|
|
||||||
// Insert the message to the native content provider
|
// Insert the message to the native content provider
|
||||||
val values = ContentValues().apply {
|
val values = ContentValues().apply {
|
||||||
|
|||||||
@@ -97,6 +97,17 @@ class SyncRepositoryImpl @Inject constructor(
|
|||||||
val lastSync = realm.where(Message::class.java)?.max("date")?.toLong() ?: 0
|
val lastSync = realm.where(Message::class.java)?.max("date")?.toLong() ?: 0
|
||||||
realm.insert(SyncLog())
|
realm.insert(SyncLog())
|
||||||
|
|
||||||
|
|
||||||
|
// Sync messages
|
||||||
|
val messageCursor = cursorToMessage.getMessagesCursor()
|
||||||
|
val messageColumns = CursorToMessage.MessageColumns(messageCursor)
|
||||||
|
val messages = messageCursor.mapWhile(
|
||||||
|
{ cursor -> cursorToMessage.map(Pair(cursor, messageColumns)) },
|
||||||
|
{ message -> message.date > lastSync })
|
||||||
|
realm.insertOrUpdate(messages)
|
||||||
|
messageCursor.close()
|
||||||
|
|
||||||
|
|
||||||
// Sync conversations
|
// Sync conversations
|
||||||
val conversationCursor = cursorToConversation.getConversationsCursor()
|
val conversationCursor = cursorToConversation.getConversationsCursor()
|
||||||
val conversations = conversationCursor
|
val conversations = conversationCursor
|
||||||
@@ -112,16 +123,6 @@ class SyncRepositoryImpl @Inject constructor(
|
|||||||
conversationCursor.close()
|
conversationCursor.close()
|
||||||
|
|
||||||
|
|
||||||
// Sync messages
|
|
||||||
val messageCursor = cursorToMessage.getMessagesCursor()
|
|
||||||
val messageColumns = CursorToMessage.MessageColumns(messageCursor)
|
|
||||||
val messages = messageCursor.mapWhile(
|
|
||||||
{ cursor -> cursorToMessage.map(Pair(cursor, messageColumns)) },
|
|
||||||
{ message -> message.date > lastSync })
|
|
||||||
realm.insertOrUpdate(messages)
|
|
||||||
messageCursor.close()
|
|
||||||
|
|
||||||
|
|
||||||
// Sync recipients
|
// Sync recipients
|
||||||
val recipientCursor = cursorToRecipient.getRecipientCursor()
|
val recipientCursor = cursorToRecipient.getRecipientCursor()
|
||||||
val recipients = recipientCursor.map { cursor -> cursorToRecipient.map(cursor) }
|
val recipients = recipientCursor.map { cursor -> cursorToRecipient.map(cursor) }
|
||||||
@@ -167,11 +168,11 @@ class SyncRepositoryImpl @Inject constructor(
|
|||||||
// Map the cursor to a message
|
// Map the cursor to a message
|
||||||
return cursor.asFlowable()
|
return cursor.asFlowable()
|
||||||
.map { cursorToMessage.map(Pair(it, columnsMap)) }
|
.map { cursorToMessage.map(Pair(it, columnsMap)) }
|
||||||
.map { message ->
|
.doOnNext { message -> existingId?.let { message.id = it } }
|
||||||
existingId?.let { message.id = it }
|
.doOnNext { message ->
|
||||||
message
|
val threadId = message.conversation?.id
|
||||||
|
message.conversation = threadId?.let { messageRepo.getOrCreateConversation(threadId) }
|
||||||
}
|
}
|
||||||
.doOnNext { message -> messageRepo.getOrCreateConversation(message.threadId) }
|
|
||||||
.doOnNext { message -> message.insertOrUpdate() }
|
.doOnNext { message -> message.insertOrUpdate() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ class ReceiveMms @Inject constructor(
|
|||||||
override fun buildObservable(params: Uri): Flowable<*> {
|
override fun buildObservable(params: Uri): Flowable<*> {
|
||||||
return Flowable.just(params)
|
return Flowable.just(params)
|
||||||
.flatMap { uri -> syncManager.syncMessage(uri) } // Sync the message
|
.flatMap { uri -> syncManager.syncMessage(uri) } // Sync the message
|
||||||
.mapNotNull { message -> messageRepo.getOrCreateConversation(message.threadId) } // Map message to conversation
|
.mapNotNull { message -> message.conversation } // Map message to conversation
|
||||||
.filter { conversation -> !conversation.blocked } // Don't notify for blocked conversations
|
.filter { conversation -> !conversation.blocked } // Don't notify for blocked conversations
|
||||||
.doOnNext { conversation -> if (conversation.archived) messageRepo.markUnarchived(conversation.id) } // Unarchive conversation if necessary
|
.doOnNext { conversation -> if (conversation.archived) messageRepo.markUnarchived(conversation.id) } // Unarchive conversation if necessary
|
||||||
.doOnNext { conversation -> notificationManager.update(conversation.id) } // Update the notification
|
.doOnNext { conversation -> notificationManager.update(conversation.id) } // Update the notification
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ class ReceiveSms @Inject constructor(
|
|||||||
|
|
||||||
messageRepo.insertReceivedSms(address, body, time) // Add the message to the db
|
messageRepo.insertReceivedSms(address, body, time) // Add the message to the db
|
||||||
}
|
}
|
||||||
.mapNotNull { message -> messageRepo.getOrCreateConversation(message.threadId) } // Map message to conversation
|
.mapNotNull { message -> message.conversation } // Map message to conversation
|
||||||
.filter { conversation -> !conversation.blocked } // Don't notify for blocked conversations
|
.filter { conversation -> !conversation.blocked } // Don't notify for blocked conversations
|
||||||
.doOnNext { conversation -> if (conversation.archived) messageRepo.markUnarchived(conversation.id) } // Unarchive conversation if necessary
|
.doOnNext { conversation -> if (conversation.archived) messageRepo.markUnarchived(conversation.id) } // Unarchive conversation if necessary
|
||||||
.doOnNext { conversation -> notificationManager.update(conversation.id) } // Update the notification
|
.doOnNext { conversation -> notificationManager.update(conversation.id) } // Update the notification
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
package interactor
|
package interactor
|
||||||
|
|
||||||
import io.reactivex.Flowable
|
import io.reactivex.Flowable
|
||||||
|
import model.Conversation
|
||||||
import model.Message
|
import model.Message
|
||||||
import repository.MessageRepository
|
import repository.MessageRepository
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
@@ -32,7 +33,7 @@ class RetrySending @Inject constructor(private val messageRepo: MessageRepositor
|
|||||||
val message = Message().apply {
|
val message = Message().apply {
|
||||||
id = params.id
|
id = params.id
|
||||||
type = params.type
|
type = params.type
|
||||||
threadId = params.threadId
|
conversation = params.conversation?.id?.let { threadId -> Conversation().apply { id = threadId } }
|
||||||
address = params.address
|
address = params.address
|
||||||
body = params.body
|
body = params.body
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,12 +32,26 @@ open class Message : RealmObject() {
|
|||||||
|
|
||||||
@PrimaryKey var id: Long = 0
|
@PrimaryKey var id: Long = 0
|
||||||
|
|
||||||
|
var conversation: Conversation? = null
|
||||||
|
set(value) {
|
||||||
|
field = value
|
||||||
|
threadId = value?.id ?: 0
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is a hack so that we can index by the threadId, and return distinct threadIds when
|
||||||
|
* querying for all conversations for the MainActivity
|
||||||
|
*/
|
||||||
|
@Index
|
||||||
|
@Deprecated("This is only here for Realm to access - it should be ignored in our codebase")
|
||||||
|
var threadId: Long = 0
|
||||||
|
get() = conversation?.id ?: 0
|
||||||
|
|
||||||
// The MMS-SMS content provider returns messages where duplicate ids can exist. This is because
|
// The MMS-SMS content provider returns messages where duplicate ids can exist. This is because
|
||||||
// SMS and MMS are stored in separate tables. We can't use these ids as our realm message id
|
// SMS and MMS are stored in separate tables. We can't use these ids as our realm message id
|
||||||
// since it's our primary key for the single message object, so we'll store the original id in
|
// since it's our primary key for the single message object, so we'll store the original id in
|
||||||
// case we need to access the original message item in the content provider
|
// case we need to access the original message item in the content provider
|
||||||
var contentId: Long = 0
|
var contentId: Long = 0
|
||||||
@Index var threadId: Long = 0
|
|
||||||
var address: String = ""
|
var address: String = ""
|
||||||
var boxId: Int = 0
|
var boxId: Int = 0
|
||||||
var type: String = ""
|
var type: String = ""
|
||||||
|
|||||||
@@ -28,9 +28,9 @@ import model.MmsPart
|
|||||||
|
|
||||||
interface MessageRepository {
|
interface MessageRepository {
|
||||||
|
|
||||||
fun getConversations(archived: Boolean = false): Flowable<List<Pair<Conversation, Message>>>
|
fun getConversations(archived: Boolean = false): RealmResults<Message>
|
||||||
|
|
||||||
fun getConversationsForWidget(): List<Pair<Conversation, Message>>
|
fun getConversationsForWidget(): List<Message>
|
||||||
|
|
||||||
fun getBlockedConversations(): Flowable<List<Conversation>>
|
fun getBlockedConversations(): Flowable<List<Conversation>>
|
||||||
|
|
||||||
|
|||||||
@@ -138,8 +138,8 @@ class NotificationManagerImpl @Inject constructor(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val threadId = message.threadId
|
val conversation = message.conversation ?: return
|
||||||
val conversation = messageRepo.getConversation(threadId) ?: return
|
val threadId = conversation.id
|
||||||
|
|
||||||
val contentIntent = Intent(context, ComposeActivity::class.java).putExtra("threadId", threadId)
|
val contentIntent = Intent(context, ComposeActivity::class.java).putExtra("threadId", threadId)
|
||||||
val taskStackBuilder = TaskStackBuilder.create(context)
|
val taskStackBuilder = TaskStackBuilder.create(context)
|
||||||
|
|||||||
@@ -18,13 +18,13 @@
|
|||||||
*/
|
*/
|
||||||
package common.util.filter
|
package common.util.filter
|
||||||
|
|
||||||
import model.Conversation
|
import model.Message
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class ConversationFilter @Inject constructor(private val recipientFilter: RecipientFilter) : Filter<Conversation>() {
|
class ConversationFilter @Inject constructor(private val recipientFilter: RecipientFilter) : Filter<Message>() {
|
||||||
|
|
||||||
override fun filter(item: Conversation, query: CharSequence): Boolean {
|
override fun filter(item: Message, query: CharSequence): Boolean {
|
||||||
return item.recipients.any { recipient -> recipientFilter.filter(recipient, query) }
|
return item.conversation?.recipients?.any { recipient -> recipientFilter.filter(recipient, query) } ?: false
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -25,20 +25,19 @@ import android.view.ViewGroup
|
|||||||
import com.jakewharton.rxbinding2.view.clicks
|
import com.jakewharton.rxbinding2.view.clicks
|
||||||
import com.jakewharton.rxbinding2.view.longClicks
|
import com.jakewharton.rxbinding2.view.longClicks
|
||||||
import com.moez.QKSMS.R
|
import com.moez.QKSMS.R
|
||||||
|
import common.Navigator
|
||||||
|
import common.base.QkViewHolder
|
||||||
import common.util.Colors
|
import common.util.Colors
|
||||||
import common.util.DateFormatter
|
import common.util.DateFormatter
|
||||||
import io.reactivex.disposables.CompositeDisposable
|
import io.reactivex.disposables.CompositeDisposable
|
||||||
import io.reactivex.rxkotlin.plusAssign
|
import io.reactivex.rxkotlin.plusAssign
|
||||||
import io.reactivex.subjects.PublishSubject
|
import io.reactivex.subjects.PublishSubject
|
||||||
import io.reactivex.subjects.Subject
|
import io.reactivex.subjects.Subject
|
||||||
|
import io.realm.RealmRecyclerViewAdapter
|
||||||
import kotlinx.android.synthetic.main.conversation_list_item.view.*
|
import kotlinx.android.synthetic.main.conversation_list_item.view.*
|
||||||
import model.Contact
|
import model.Contact
|
||||||
import model.Conversation
|
|
||||||
import model.Message
|
import model.Message
|
||||||
import model.PhoneNumber
|
import model.PhoneNumber
|
||||||
import common.Navigator
|
|
||||||
import common.base.FlowableAdapter
|
|
||||||
import common.base.QkViewHolder
|
|
||||||
import repository.MessageRepository
|
import repository.MessageRepository
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@@ -48,7 +47,7 @@ class ConversationsAdapter @Inject constructor(
|
|||||||
val messageRepo: MessageRepository,
|
val messageRepo: MessageRepository,
|
||||||
val dateFormatter: DateFormatter,
|
val dateFormatter: DateFormatter,
|
||||||
val colors: Colors
|
val colors: Colors
|
||||||
) : FlowableAdapter<Pair<Conversation, Message>>() {
|
) : RealmRecyclerViewAdapter<Message, QkViewHolder>(null, true) {
|
||||||
|
|
||||||
val clicks: Subject<Long> = PublishSubject.create()
|
val clicks: Subject<Long> = PublishSubject.create()
|
||||||
val longClicks: Subject<Long> = PublishSubject.create()
|
val longClicks: Subject<Long> = PublishSubject.create()
|
||||||
@@ -78,8 +77,8 @@ class ConversationsAdapter @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onBindViewHolder(viewHolder: QkViewHolder, position: Int) {
|
override fun onBindViewHolder(viewHolder: QkViewHolder, position: Int) {
|
||||||
val conversation = getItem(position).first
|
val message = getItem(position)!!
|
||||||
val message = getItem(position).second
|
val conversation = message.conversation!!
|
||||||
val view = viewHolder.itemView
|
val view = viewHolder.itemView
|
||||||
|
|
||||||
view.clicks().subscribe { clicks.onNext(conversation.id) }
|
view.clicks().subscribe { clicks.onNext(conversation.id) }
|
||||||
@@ -94,16 +93,6 @@ class ConversationsAdapter @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun getItemViewType(position: Int): Int {
|
override fun getItemViewType(position: Int): Int {
|
||||||
return if (getItem(position).second.read) 0 else 1
|
return if (getItem(position)!!.read) 0 else 1
|
||||||
}
|
|
||||||
|
|
||||||
override fun areItemsTheSame(old: Pair<Conversation, Message>, new: Pair<Conversation, Message>): Boolean {
|
|
||||||
return old.first.id == new.first.id
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun areContentsTheSame(old: Pair<Conversation, Message>, new: Pair<Conversation, Message>): Boolean {
|
|
||||||
return old.second.id == new.second.id &&
|
|
||||||
old.second.read == new.second.read &&
|
|
||||||
old.first.recipients == new.first.recipients
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -23,13 +23,13 @@ import android.content.Intent
|
|||||||
import com.moez.QKSMS.R
|
import com.moez.QKSMS.R
|
||||||
import com.uber.autodispose.android.lifecycle.scope
|
import com.uber.autodispose.android.lifecycle.scope
|
||||||
import com.uber.autodispose.kotlin.autoDisposable
|
import com.uber.autodispose.kotlin.autoDisposable
|
||||||
import injection.appComponent
|
import common.base.QkViewModel
|
||||||
import common.util.extensions.makeToast
|
import common.util.extensions.makeToast
|
||||||
|
import injection.appComponent
|
||||||
import interactor.SaveImage
|
import interactor.SaveImage
|
||||||
import io.reactivex.Flowable
|
import io.reactivex.Flowable
|
||||||
import io.reactivex.rxkotlin.plusAssign
|
import io.reactivex.rxkotlin.plusAssign
|
||||||
import io.reactivex.rxkotlin.withLatestFrom
|
import io.reactivex.rxkotlin.withLatestFrom
|
||||||
import common.base.QkViewModel
|
|
||||||
import repository.MessageRepository
|
import repository.MessageRepository
|
||||||
import util.extensions.mapNotNull
|
import util.extensions.mapNotNull
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
@@ -54,7 +54,7 @@ class GalleryViewModel(intent: Intent) : QkViewModel<GalleryView, GalleryState>(
|
|||||||
|
|
||||||
disposables += partIdFlowable
|
disposables += partIdFlowable
|
||||||
.mapNotNull { partId -> messageRepo.getMessageForPart(partId) }
|
.mapNotNull { partId -> messageRepo.getMessageForPart(partId) }
|
||||||
.mapNotNull { message -> messageRepo.getConversation(message.threadId) }
|
.mapNotNull { message -> message.conversation }
|
||||||
.subscribe { conversation -> newState { it.copy(title = conversation.getTitle()) } }
|
.subscribe { conversation -> newState { it.copy(title = conversation.getTitle()) } }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -199,7 +199,7 @@ class MainActivity : QkThemedActivity<MainViewModel>(), MainView {
|
|||||||
is Inbox -> {
|
is Inbox -> {
|
||||||
if (!inbox.isSelected) toolbarSearch.text = null
|
if (!inbox.isSelected) toolbarSearch.text = null
|
||||||
if (recyclerView.adapter !== conversationsAdapter) recyclerView.adapter = conversationsAdapter
|
if (recyclerView.adapter !== conversationsAdapter) recyclerView.adapter = conversationsAdapter
|
||||||
conversationsAdapter.flowable = state.page.data
|
conversationsAdapter.updateData(state.page.data)
|
||||||
itemTouchHelper.attachToRecyclerView(recyclerView)
|
itemTouchHelper.attachToRecyclerView(recyclerView)
|
||||||
menuItemAdapter.data = state.page.menu
|
menuItemAdapter.data = state.page.menu
|
||||||
empty.setText(R.string.inbox_empty_text)
|
empty.setText(R.string.inbox_empty_text)
|
||||||
@@ -210,7 +210,7 @@ class MainActivity : QkThemedActivity<MainViewModel>(), MainView {
|
|||||||
is Archived -> {
|
is Archived -> {
|
||||||
if (!archived.isSelected) toolbarSearch.setText(R.string.title_archived)
|
if (!archived.isSelected) toolbarSearch.setText(R.string.title_archived)
|
||||||
if (recyclerView.adapter !== conversationsAdapter) recyclerView.adapter = conversationsAdapter
|
if (recyclerView.adapter !== conversationsAdapter) recyclerView.adapter = conversationsAdapter
|
||||||
conversationsAdapter.flowable = state.page.data
|
conversationsAdapter.updateData(state.page.data)
|
||||||
itemTouchHelper.attachToRecyclerView(null)
|
itemTouchHelper.attachToRecyclerView(null)
|
||||||
menuItemAdapter.data = state.page.menu
|
menuItemAdapter.data = state.page.menu
|
||||||
empty.setText(R.string.archived_empty_text)
|
empty.setText(R.string.archived_empty_text)
|
||||||
|
|||||||
@@ -18,10 +18,9 @@
|
|||||||
*/
|
*/
|
||||||
package feature.main
|
package feature.main
|
||||||
|
|
||||||
import io.reactivex.Flowable
|
|
||||||
import model.Conversation
|
|
||||||
import model.Message
|
|
||||||
import common.MenuItem
|
import common.MenuItem
|
||||||
|
import io.realm.RealmResults
|
||||||
|
import model.Message
|
||||||
|
|
||||||
data class MainState(
|
data class MainState(
|
||||||
val page: MainPage = Inbox(),
|
val page: MainPage = Inbox(),
|
||||||
@@ -33,13 +32,13 @@ sealed class MainPage
|
|||||||
|
|
||||||
data class Inbox(
|
data class Inbox(
|
||||||
val showClearButton: Boolean = false,
|
val showClearButton: Boolean = false,
|
||||||
val data: Flowable<List<Pair<Conversation, Message>>>? = null,
|
val data: RealmResults<Message>? = null,
|
||||||
val empty: Boolean = false,
|
val empty: Boolean = false,
|
||||||
val menu: List<MenuItem> = ArrayList(),
|
val menu: List<MenuItem> = ArrayList(),
|
||||||
val showArchivedSnackbar: Boolean = false) : MainPage()
|
val showArchivedSnackbar: Boolean = false) : MainPage()
|
||||||
|
|
||||||
data class Archived(
|
data class Archived(
|
||||||
val data: Flowable<List<Pair<Conversation, Message>>>?,
|
val data: RealmResults<Message>? = null,
|
||||||
val empty: Boolean = false,
|
val empty: Boolean = false,
|
||||||
val menu: List<MenuItem> = ArrayList()) : MainPage()
|
val menu: List<MenuItem> = ArrayList()) : MainPage()
|
||||||
|
|
||||||
|
|||||||
@@ -26,8 +26,11 @@ import android.support.v4.content.ContextCompat
|
|||||||
import com.moez.QKSMS.R
|
import com.moez.QKSMS.R
|
||||||
import com.uber.autodispose.android.lifecycle.scope
|
import com.uber.autodispose.android.lifecycle.scope
|
||||||
import com.uber.autodispose.kotlin.autoDisposable
|
import com.uber.autodispose.kotlin.autoDisposable
|
||||||
import injection.appComponent
|
import common.MenuItem
|
||||||
|
import common.Navigator
|
||||||
|
import common.base.QkViewModel
|
||||||
import common.util.filter.ConversationFilter
|
import common.util.filter.ConversationFilter
|
||||||
|
import injection.appComponent
|
||||||
import interactor.DeleteConversation
|
import interactor.DeleteConversation
|
||||||
import interactor.MarkAllSeen
|
import interactor.MarkAllSeen
|
||||||
import interactor.MarkArchived
|
import interactor.MarkArchived
|
||||||
@@ -39,11 +42,10 @@ import io.reactivex.rxkotlin.plusAssign
|
|||||||
import io.reactivex.rxkotlin.withLatestFrom
|
import io.reactivex.rxkotlin.withLatestFrom
|
||||||
import io.realm.Realm
|
import io.realm.Realm
|
||||||
import model.SyncLog
|
import model.SyncLog
|
||||||
import common.MenuItem
|
|
||||||
import common.Navigator
|
|
||||||
import common.base.QkViewModel
|
|
||||||
import repository.MessageRepository
|
import repository.MessageRepository
|
||||||
import util.Preferences
|
import util.Preferences
|
||||||
|
import util.extensions.asObservable
|
||||||
|
import util.extensions.mapNotNull
|
||||||
import util.extensions.toFlowable
|
import util.extensions.toFlowable
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
@@ -63,23 +65,27 @@ class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
|||||||
@Inject lateinit var prefs: Preferences
|
@Inject lateinit var prefs: Preferences
|
||||||
|
|
||||||
private val conversations by lazy {
|
private val conversations by lazy {
|
||||||
messageRepo.getConversations()
|
messageRepo.getConversations().also {
|
||||||
.withLatestFrom(state.toFlowable(), { conversations, state ->
|
it.asFlowable()
|
||||||
(state.page as? Inbox)?.let { page ->
|
.withLatestFrom(state.toFlowable(), { conversations, state ->
|
||||||
newState { it.copy(page = page.copy(empty = conversations.isEmpty())) }
|
(state.page as? Inbox)?.let { page ->
|
||||||
}
|
newState { it.copy(page = page.copy(empty = conversations.isEmpty())) }
|
||||||
conversations
|
}
|
||||||
})
|
conversations
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private val archivedConversations by lazy {
|
private val archivedConversations by lazy {
|
||||||
messageRepo.getConversations(true)
|
messageRepo.getConversations(true).also {
|
||||||
.withLatestFrom(state.toFlowable(), { conversations, state ->
|
it.asFlowable()
|
||||||
(state.page as? Archived)?.let { page ->
|
.withLatestFrom(state.toFlowable(), { conversations, state ->
|
||||||
newState { it.copy(page = page.copy(empty = conversations.isEmpty())) }
|
(state.page as? Archived)?.let { page ->
|
||||||
}
|
newState { it.copy(page = page.copy(empty = conversations.isEmpty())) }
|
||||||
conversations
|
}
|
||||||
})
|
conversations
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private val menuArchive by lazy { MenuItem(context.getString(R.string.menu_archive), 0) }
|
private val menuArchive by lazy { MenuItem(context.getString(R.string.menu_archive), 0) }
|
||||||
@@ -137,11 +143,11 @@ class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
|||||||
if (state.page is Inbox) {
|
if (state.page is Inbox) {
|
||||||
val conversations = when {
|
val conversations = when {
|
||||||
query.isEmpty() -> conversations
|
query.isEmpty() -> conversations
|
||||||
else -> conversations.map { list -> list.filter { conversationFilter.filter(it.first, query) } }
|
else -> conversations.filter { conversationFilter.filter(it, query) }
|
||||||
}
|
}
|
||||||
|
|
||||||
val page = state.page.copy(showClearButton = query.isNotEmpty(), data = conversations)
|
//val page = state.page.copy(showClearButton = query.isNotEmpty(), data = conversations)
|
||||||
newState { it.copy(page = page) }
|
//newState { it.copy(page = page) }
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
@@ -230,9 +236,8 @@ class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
|||||||
.subscribe { threadId -> deleteConversation.execute(threadId) }
|
.subscribe { threadId -> deleteConversation.execute(threadId) }
|
||||||
|
|
||||||
val swipedConversation = view.swipeConversationIntent
|
val swipedConversation = view.swipeConversationIntent
|
||||||
.withLatestFrom(conversations.toObservable(), { position, conversations -> conversations[position] })
|
.withLatestFrom(conversations.asObservable(), { position, conversations -> conversations[position] })
|
||||||
.map { pair -> pair.first }
|
.mapNotNull { message -> message?.conversation?.id }
|
||||||
.map { conversation -> conversation.id }
|
|
||||||
|
|
||||||
swipedConversation
|
swipedConversation
|
||||||
.withLatestFrom(state, { threadId, state ->
|
.withLatestFrom(state, { threadId, state ->
|
||||||
|
|||||||
@@ -38,7 +38,6 @@ import common.util.extensions.dpToPx
|
|||||||
import feature.main.MainActivity
|
import feature.main.MainActivity
|
||||||
import injection.appComponent
|
import injection.appComponent
|
||||||
import model.Contact
|
import model.Contact
|
||||||
import model.Conversation
|
|
||||||
import model.Message
|
import model.Message
|
||||||
import model.PhoneNumber
|
import model.PhoneNumber
|
||||||
import repository.MessageRepository
|
import repository.MessageRepository
|
||||||
@@ -57,7 +56,7 @@ class WidgetAdapter(intent: Intent) : RemoteViewsService.RemoteViewsFactory {
|
|||||||
|
|
||||||
private val appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID)
|
private val appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID)
|
||||||
private val smallWidget = intent.getBooleanExtra("small_widget", false)
|
private val smallWidget = intent.getBooleanExtra("small_widget", false)
|
||||||
private var conversations: List<Pair<Conversation, Message>> = listOf()
|
private var conversations: List<Message> = listOf()
|
||||||
private val appWidgetManager by lazy { AppWidgetManager.getInstance(context) }
|
private val appWidgetManager by lazy { AppWidgetManager.getInstance(context) }
|
||||||
|
|
||||||
// Cache colors, load lazily. This entire class is recreated when the widget is recreated,
|
// Cache colors, load lazily. This entire class is recreated when the widget is recreated,
|
||||||
@@ -98,8 +97,8 @@ class WidgetAdapter(intent: Intent) : RemoteViewsService.RemoteViewsFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun getConversationView(position: Int): RemoteViews {
|
private fun getConversationView(position: Int): RemoteViews {
|
||||||
val conversation = conversations[position].first
|
val message = conversations[position]
|
||||||
val message = conversations[position].second
|
val conversation = message.conversation!!
|
||||||
|
|
||||||
val remoteViews = RemoteViews(context.packageName, R.layout.widget_list_item)
|
val remoteViews = RemoteViews(context.packageName, R.layout.widget_list_item)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user