forked from upstream-mirrors/quik
Store lastMessage instead of it's fields in conversation
This commit is contained in:
@@ -36,33 +36,21 @@ class CursorToConversationImpl @Inject constructor(
|
||||
val URI: Uri = Uri.parse("content://mms-sms/conversations?simple=true")
|
||||
val PROJECTION = arrayOf(
|
||||
Threads._ID,
|
||||
Threads.DATE,
|
||||
Threads.RECIPIENT_IDS,
|
||||
Threads.MESSAGE_COUNT,
|
||||
Threads.READ,
|
||||
Threads.SNIPPET
|
||||
Threads.RECIPIENT_IDS
|
||||
)
|
||||
|
||||
const val ID = 0
|
||||
const val DATE = 1
|
||||
const val RECIPIENT_IDS = 2
|
||||
const val MESSAGE_COUNT = 3
|
||||
const val READ = 4
|
||||
const val SNIPPET = 5
|
||||
const val RECIPIENT_IDS = 1
|
||||
}
|
||||
|
||||
override fun map(from: Cursor): Conversation {
|
||||
return Conversation().apply {
|
||||
id = from.getLong(ID)
|
||||
date = from.getLong(DATE)
|
||||
recipients.addAll(from.getString(RECIPIENT_IDS)
|
||||
.split(" ")
|
||||
.filter { it.isNotBlank() }
|
||||
.map { recipientId -> recipientId.toLong() }
|
||||
.map { recipientId -> Recipient().apply { id = recipientId } })
|
||||
count = from.getInt(MESSAGE_COUNT)
|
||||
read = from.getInt(READ) == 1
|
||||
snippet = from.getString(SNIPPET) ?: ""
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ import io.realm.RealmMigration
|
||||
class QkRealmMigration : RealmMigration {
|
||||
|
||||
companion object {
|
||||
const val SCHEMA_VERSION: Long = 7
|
||||
const val SCHEMA_VERSION: Long = 8
|
||||
}
|
||||
|
||||
override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) {
|
||||
@@ -92,9 +92,19 @@ class QkRealmMigration : RealmMigration {
|
||||
version++
|
||||
}
|
||||
|
||||
if (version < newVersion) {
|
||||
throw IllegalStateException("Migration missing from v$oldVersion to v$newVersion")
|
||||
if (version == 7L) {
|
||||
realm.schema.get("Conversation")
|
||||
?.addRealmObjectField("lastMessage", realm.schema.get("Message"))
|
||||
?.removeField("count")
|
||||
?.removeField("date")
|
||||
?.removeField("snippet")
|
||||
?.removeField("read")
|
||||
?.removeField("me")
|
||||
|
||||
version++
|
||||
}
|
||||
|
||||
check(version >= newVersion) { "Migration missing from v$oldVersion to v$newVersion" }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -54,11 +54,11 @@ class ConversationRepositoryImpl @Inject constructor(
|
||||
return Realm.getDefaultInstance()
|
||||
.where(Conversation::class.java)
|
||||
.notEqualTo("id", 0L)
|
||||
.greaterThan("count", 0)
|
||||
.isNotNull("lastMessage")
|
||||
.equalTo("archived", archived)
|
||||
.equalTo("blocked", false)
|
||||
.isNotEmpty("recipients")
|
||||
.sort("pinned", Sort.DESCENDING, "date", Sort.DESCENDING)
|
||||
.sort("pinned", Sort.DESCENDING, "lastMessage.date", Sort.DESCENDING)
|
||||
.findAllAsync()
|
||||
}
|
||||
|
||||
@@ -67,28 +67,37 @@ class ConversationRepositoryImpl @Inject constructor(
|
||||
realm.refresh()
|
||||
realm.copyFromRealm(realm.where(Conversation::class.java)
|
||||
.notEqualTo("id", 0L)
|
||||
.greaterThan("count", 0)
|
||||
.isNotNull("lastMessage")
|
||||
.equalTo("archived", false)
|
||||
.equalTo("blocked", false)
|
||||
.isNotEmpty("recipients")
|
||||
.sort("pinned", Sort.DESCENDING, "date", Sort.DESCENDING)
|
||||
.sort("pinned", Sort.DESCENDING, "lastMessage.date", Sort.DESCENDING)
|
||||
.findAll())
|
||||
}
|
||||
}
|
||||
|
||||
override fun getTopConversations(): List<Conversation> {
|
||||
val realm = Realm.getDefaultInstance()
|
||||
return realm.copyFromRealm(
|
||||
realm.where(Conversation::class.java)
|
||||
.notEqualTo("id", 0L)
|
||||
.greaterThan("count", 0)
|
||||
.greaterThan("date", System.currentTimeMillis() - TimeUnit.DAYS.toMillis(7))
|
||||
.equalTo("archived", false)
|
||||
.equalTo("blocked", false)
|
||||
.isNotEmpty("recipients")
|
||||
.sort("pinned", Sort.DESCENDING, "count", Sort.DESCENDING)
|
||||
.findAll()
|
||||
)
|
||||
return Realm.getDefaultInstance().use { realm ->
|
||||
realm.copyFromRealm(realm.where(Conversation::class.java)
|
||||
.notEqualTo("id", 0L)
|
||||
.isNotNull("lastMessage")
|
||||
.beginGroup()
|
||||
.equalTo("pinned", true)
|
||||
.or()
|
||||
.greaterThan("lastMessage.date", System.currentTimeMillis() - TimeUnit.DAYS.toMillis(7))
|
||||
.endGroup()
|
||||
.equalTo("archived", false)
|
||||
.equalTo("blocked", false)
|
||||
.isNotEmpty("recipients")
|
||||
.findAll())
|
||||
.sortedWith(compareByDescending<Conversation> { conversation -> conversation.pinned }
|
||||
.thenByDescending { conversation ->
|
||||
realm.where(Message::class.java)
|
||||
.equalTo("threadId", conversation.id)
|
||||
.greaterThan("date", System.currentTimeMillis() - TimeUnit.DAYS.toMillis(7))
|
||||
.count()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
override fun setConversationName(id: Long, name: String) {
|
||||
@@ -234,20 +243,14 @@ class ConversationRepositoryImpl @Inject constructor(
|
||||
.equalTo("id", threadId)
|
||||
.findFirst() ?: return
|
||||
|
||||
val messages = realm
|
||||
val message = realm
|
||||
.where(Message::class.java)
|
||||
.equalTo("threadId", threadId)
|
||||
.sort("date", Sort.DESCENDING)
|
||||
.findAll()
|
||||
|
||||
val message = messages.firstOrNull()
|
||||
.findFirst()
|
||||
|
||||
realm.executeTransaction {
|
||||
conversation.count = messages.size
|
||||
conversation.date = message?.date ?: 0
|
||||
conversation.snippet = message?.getSummary() ?: ""
|
||||
conversation.read = message?.read ?: true
|
||||
conversation.me = message?.isMe() ?: false
|
||||
conversation.lastMessage = message
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,7 +113,7 @@ class MessageRepositoryImpl @Inject constructor(
|
||||
realm.where(Conversation::class.java)
|
||||
.equalTo("archived", false)
|
||||
.equalTo("blocked", false)
|
||||
.equalTo("read", false)
|
||||
.equalTo("lastMessage.read", false)
|
||||
.count()
|
||||
}
|
||||
}
|
||||
@@ -251,13 +251,15 @@ class MessageRepositoryImpl @Inject constructor(
|
||||
|
||||
override fun markUnread(vararg threadIds: Long) {
|
||||
Realm.getDefaultInstance()?.use { realm ->
|
||||
val conversation = realm.where(Conversation::class.java)
|
||||
val conversations = realm.where(Conversation::class.java)
|
||||
.anyOf("id", threadIds)
|
||||
.equalTo("read", true)
|
||||
.equalTo("lastMessage.read", true)
|
||||
.findAll()
|
||||
|
||||
realm.executeTransaction {
|
||||
conversation.forEach { it.read = false }
|
||||
conversations.forEach { conversation ->
|
||||
conversation.lastMessage?.read = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,9 +147,7 @@ class SyncRepositoryImpl @Inject constructor(
|
||||
.findAll()
|
||||
.forEach { message ->
|
||||
val conversation = conversations.find { conversation -> conversation.id == message.threadId }
|
||||
conversation?.date = message.date
|
||||
conversation?.snippet = message.getSummary()
|
||||
conversation?.me = message.isMe()
|
||||
conversation?.lastMessage = message
|
||||
}
|
||||
|
||||
realm.insertOrUpdate(conversations)
|
||||
|
||||
@@ -19,13 +19,11 @@
|
||||
package com.moez.QKSMS.interactor
|
||||
|
||||
import com.moez.QKSMS.manager.NotificationManager
|
||||
import com.moez.QKSMS.repository.ConversationRepository
|
||||
import com.moez.QKSMS.repository.MessageRepository
|
||||
import io.reactivex.Flowable
|
||||
import javax.inject.Inject
|
||||
|
||||
class MarkRead @Inject constructor(
|
||||
private val conversationRepo: ConversationRepository,
|
||||
private val messageRepo: MessageRepository,
|
||||
private val notificationManager: NotificationManager,
|
||||
private val updateBadge: UpdateBadge
|
||||
@@ -34,7 +32,6 @@ class MarkRead @Inject constructor(
|
||||
override fun buildObservable(params: List<Long>): Flowable<*> {
|
||||
return Flowable.just(params.toLongArray())
|
||||
.doOnNext { threadIds -> messageRepo.markRead(*threadIds) }
|
||||
.doOnNext { threadIds -> conversationRepo.updateConversations(*threadIds) } // Update the conversation
|
||||
.doOnNext { threadIds -> threadIds.forEach(notificationManager::update) }
|
||||
.flatMap { updateBadge.buildObservable(Unit) } // Update the badge
|
||||
}
|
||||
|
||||
@@ -29,11 +29,7 @@ open class Conversation(
|
||||
@Index var blocked: Boolean = false,
|
||||
@Index var pinned: Boolean = false,
|
||||
var recipients: RealmList<Recipient> = RealmList(),
|
||||
var count: Int = 0,
|
||||
var date: Long = 0,
|
||||
var snippet: String = "",
|
||||
var read: Boolean = true,
|
||||
var me: Boolean = false,
|
||||
var lastMessage: Message? = null,
|
||||
var draft: String = "",
|
||||
|
||||
var blockingClient: Int? = null,
|
||||
@@ -42,6 +38,11 @@ open class Conversation(
|
||||
var name: String = "" // For group chats, the user is allowed to set a custom title for the conversation
|
||||
) : RealmObject() {
|
||||
|
||||
val date: Long get() = lastMessage?.date ?: 0
|
||||
val snippet: String? get() = lastMessage?.getSummary()
|
||||
val unread: Boolean get() = lastMessage?.read == false
|
||||
val me: Boolean get() = lastMessage?.isMe() == true
|
||||
|
||||
fun getTitle(): String {
|
||||
return name.takeIf { it.isNotBlank() } ?: recipients.joinToString { recipient -> recipient.getDisplayName() }
|
||||
}
|
||||
|
||||
+1
-1
@@ -91,7 +91,7 @@ class BlockedMessagesAdapter @Inject constructor(
|
||||
|
||||
override fun getItemViewType(position: Int): Int {
|
||||
val conversation = getItem(position)
|
||||
return if (conversation?.read == true) 1 else 0
|
||||
return if (conversation?.unread == false) 1 else 0
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -106,6 +106,6 @@ class ConversationsAdapter @Inject constructor(
|
||||
}
|
||||
|
||||
override fun getItemViewType(position: Int): Int {
|
||||
return if (getItem(position)?.read == true) 0 else 1
|
||||
return if (getItem(position)?.unread == false) 0 else 1
|
||||
}
|
||||
}
|
||||
@@ -365,7 +365,7 @@ class MainViewModel @Inject constructor(
|
||||
?.recipients?.first()
|
||||
?.takeIf { recipient -> recipient.contact == null } != null
|
||||
val pin = conversations.sumBy { if (it.pinned) -1 else 1 } >= 0
|
||||
val read = conversations.sumBy { if (it.read) -1 else 1 } >= 0
|
||||
val read = conversations.sumBy { if (!it.unread) -1 else 1 } >= 0
|
||||
val selected = selection.size
|
||||
|
||||
when (state.page) {
|
||||
|
||||
@@ -54,7 +54,8 @@ class WidgetAdapter(intent: Intent) : RemoteViewsService.RemoteViewsFactory {
|
||||
@Inject lateinit var dateFormatter: DateFormatter
|
||||
@Inject lateinit var prefs: Preferences
|
||||
|
||||
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 var conversations: List<Conversation> = listOf()
|
||||
private val appWidgetManager by lazy { AppWidgetManager.getInstance(context) }
|
||||
@@ -68,9 +69,12 @@ class WidgetAdapter(intent: Intent) : RemoteViewsService.RemoteViewsFactory {
|
||||
night && !black -> R.color.backgroundDark
|
||||
else -> R.color.white
|
||||
})
|
||||
private val textPrimary get() = context.getColorCompat(if (night) R.color.textPrimaryDark else R.color.textPrimary)
|
||||
private val textSecondary get() = context.getColorCompat(if (night) R.color.textSecondaryDark else R.color.textSecondary)
|
||||
private val textTertiary get() = context.getColorCompat(if (night) R.color.textTertiaryDark else R.color.textTertiary)
|
||||
private val textPrimary
|
||||
get() = context.getColorCompat(if (night) R.color.textPrimaryDark else R.color.textPrimary)
|
||||
private val textSecondary
|
||||
get() = context.getColorCompat(if (night) R.color.textSecondaryDark else R.color.textSecondary)
|
||||
private val textTertiary
|
||||
get() = context.getColorCompat(if (night) R.color.textTertiaryDark else R.color.textTertiary)
|
||||
|
||||
override fun onCreate() {
|
||||
appComponent.inject(this)
|
||||
@@ -116,7 +120,6 @@ class WidgetAdapter(intent: Intent) : RemoteViewsService.RemoteViewsFactory {
|
||||
recipient.contact ?: Contact().apply { numbers.add(PhoneNumber().apply { address = recipient.address }) }
|
||||
}.firstOrNull()
|
||||
|
||||
|
||||
// Use the icon if there's no name, otherwise show an initial
|
||||
if (contact?.name.orEmpty().isNotEmpty()) {
|
||||
remoteViews.setTextViewText(R.id.initial, contact?.name?.substring(0, 1))
|
||||
@@ -141,15 +144,16 @@ class WidgetAdapter(intent: Intent) : RemoteViewsService.RemoteViewsFactory {
|
||||
|
||||
// Name
|
||||
remoteViews.setTextColor(R.id.name, textPrimary)
|
||||
remoteViews.setTextViewText(R.id.name, boldText(conversation.getTitle(), !conversation.read))
|
||||
remoteViews.setTextViewText(R.id.name, boldText(conversation.getTitle(), conversation.unread))
|
||||
|
||||
// Date
|
||||
remoteViews.setTextColor(R.id.date, if (conversation.read) textTertiary else textPrimary)
|
||||
remoteViews.setTextViewText(R.id.date, boldText(dateFormatter.getConversationTimestamp(conversation.date), !conversation.read))
|
||||
val timestamp = dateFormatter.getConversationTimestamp(conversation.date)
|
||||
remoteViews.setTextColor(R.id.date, if (conversation.unread) textPrimary else textTertiary)
|
||||
remoteViews.setTextViewText(R.id.date, boldText(timestamp, conversation.unread))
|
||||
|
||||
// Snippet
|
||||
remoteViews.setTextColor(R.id.snippet, if (conversation.read) textTertiary else textPrimary)
|
||||
remoteViews.setTextViewText(R.id.snippet, boldText(conversation.snippet, !conversation.read))
|
||||
remoteViews.setTextColor(R.id.snippet, if (conversation.unread) textPrimary else textTertiary)
|
||||
remoteViews.setTextViewText(R.id.snippet, boldText(conversation.snippet, conversation.unread))
|
||||
|
||||
// Launch conversation on click
|
||||
val clickIntent = Intent().putExtra("threadId", conversation.id)
|
||||
@@ -168,7 +172,7 @@ class WidgetAdapter(intent: Intent) : RemoteViewsService.RemoteViewsFactory {
|
||||
return view
|
||||
}
|
||||
|
||||
private fun boldText(text: String, shouldBold: Boolean): CharSequence = when {
|
||||
private fun boldText(text: String?, shouldBold: Boolean): CharSequence? = when {
|
||||
shouldBold -> SpannableStringBuilder()
|
||||
.bold { append(text) }
|
||||
else -> text
|
||||
|
||||
Reference in New Issue
Block a user