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