Properly hide unread counts if not supported by homeserver

Showing a "1" for every unread chat doesn't make too much sense.

Change-Id: I8e49f2bf20477f9b58039005c411041269ca6652
This commit is contained in:
SpiritCroc 2021-09-23 18:10:47 +02:00
parent e1546cec06
commit 586f565c23
6 changed files with 29 additions and 10 deletions

View File

@ -50,7 +50,7 @@ data class RoomSummary(
val hasUnreadMessages: Boolean = false,
val hasUnreadContentMessages: Boolean = false,
val hasUnreadOriginalContentMessages: Boolean = false,
val unreadCount: Int? = 0,
val unreadCount: Int? = null,
val markedUnread: Boolean = false,
val aggregatedUnreadCount: Int = 0,
val aggregatedNotificationCount: Int = 0,

View File

@ -49,7 +49,7 @@ internal object RealmSessionStoreMigration : RealmMigration {
// SC-specific DB changes on top of Element
// 1: added markedUnread field
const val SESSION_STORE_SCHEMA_SC_VERSION = 3L
const val SESSION_STORE_SCHEMA_SC_VERSION = 4L
const val SESSION_STORE_SCHEMA_SC_VERSION_OFFSET = (1L shl 12)
const val SESSION_STORE_SCHEMA_VERSION = 17L +
@ -83,6 +83,7 @@ internal object RealmSessionStoreMigration : RealmMigration {
if (oldScVersion <= 0) migrateToSc1(realm)
if (oldScVersion <= 1) migrateToSc2(realm)
if (oldScVersion <= 2) migrateToSc3(realm)
if (oldScVersion <= 3) migrateToSc4(realm)
}
// SC Version 1L added markedUnread
@ -107,6 +108,13 @@ internal object RealmSessionStoreMigration : RealmMigration {
?.addField(RoomSummaryEntityFields.AGGREGATED_NOTIFICATION_COUNT, Int::class.java)
}
// SC Version 4L exposes non-reported unread counters to upper layers
private fun migrateToSc4(realm: DynamicRealm) {
Timber.d("Step SC 3 -> 4")
realm.schema.get("RoomSummaryEntity")
?.setNullable(RoomSummaryEntityFields.UNREAD_COUNT, true)
}
private fun migrateTo1(realm: DynamicRealm) {

View File

@ -108,16 +108,27 @@ internal open class RoomSummaryEntity(
if (value != field) field = value
}
var unreadCount: Int = 0
var unreadCount: Int? = null
set(value) {
if (value != field) field = value
}
/* -> safeUnreadCount
get() {
if (field == 0 && hasUnreadOriginalContentMessages) {
return 1
}
return field
}
*/
fun safeUnreadCount(): Int {
val safeUnreadCount = unreadCount
return if (safeUnreadCount == null || safeUnreadCount <= 0) {
if (hasUnreadOriginalContentMessages) 1 else 0
} else {
safeUnreadCount
}
}
var aggregatedUnreadCount: Int = 0
set(value) {

View File

@ -99,9 +99,9 @@ internal class RoomSummaryUpdater @Inject constructor(
}
roomSummaryEntity.highlightCount = unreadNotifications?.highlightCount ?: 0
roomSummaryEntity.notificationCount = unreadNotifications?.notificationCount ?: 0
roomSummaryEntity.unreadCount = unreadCount ?: 0
roomSummaryEntity.unreadCount = unreadCount
roomSummaryEntity.aggregatedNotificationCount = roomSummaryEntity.notificationCount
roomSummaryEntity.aggregatedUnreadCount = roomSummaryEntity.unreadCount
roomSummaryEntity.aggregatedUnreadCount = roomSummaryEntity.safeUnreadCount()
if (membership != null) {
roomSummaryEntity.membership = membership
@ -399,9 +399,9 @@ internal class RoomSummaryUpdater @Inject constructor(
.findAll().forEach {
highlightCount += it.highlightCount
notificationCount += it.notificationCount
unreadCount += it.unreadCount
unreadCount += it.safeUnreadCount()
aggregateNotificationCount += min(it.notificationCount, 1)
aggregateUnreadCount += min(it.unreadCount, 1)
aggregateUnreadCount += min(it.safeUnreadCount(), 1)
markedUnreadCount += if (it.markedUnread) 1 else 0
}

View File

@ -57,7 +57,7 @@ abstract class RoomSummaryItem : VectorEpoxyModel<RoomSummaryItem.Holder>() {
@EpoxyAttribute var unreadNotificationCount: Int = 0
@EpoxyAttribute var hasUnreadMessage: Boolean = false
@EpoxyAttribute var markedUnread: Boolean = false
@EpoxyAttribute var unreadCount: Int = 0
@EpoxyAttribute var unreadCount: Int? = null
@EpoxyAttribute var hasDraft: Boolean = false
@EpoxyAttribute var showHighlighted: Boolean = false
@EpoxyAttribute var hasFailedSending: Boolean = false
@ -75,7 +75,7 @@ abstract class RoomSummaryItem : VectorEpoxyModel<RoomSummaryItem.Holder>() {
holder.titleView.text = matrixItem.getBestName()
holder.lastEventTimeView.text = lastEventTime
holder.lastEventView.text = lastFormattedEvent
holder.unreadCounterBadgeView.render(UnreadCounterBadgeView.State(unreadNotificationCount, showHighlighted, unreadCount, markedUnread))
holder.unreadCounterBadgeView.render(UnreadCounterBadgeView.State(unreadNotificationCount, showHighlighted, unreadCount ?: 0, markedUnread))
holder.unreadIndentIndicator.isVisible = hasUnreadMessage
holder.draftView.isVisible = hasDraft
avatarRenderer.render(matrixItem, holder.avatarImageView)

View File

@ -137,7 +137,7 @@ class RoomSummaryItemFactory @Inject constructor(private val displayableEventFor
.unreadNotificationCount(unreadCount)
.hasUnreadMessage(roomSummary.scIsUnread(scSdkPreferences))
.markedUnread(roomSummary.markedUnread)
.unreadCount(roomSummary.unreadCount ?: 0)
.unreadCount(roomSummary.unreadCount)
.hasDraft(roomSummary.userDrafts.isNotEmpty())
.itemLongClickListener { _ ->
onLongClick?.invoke(roomSummary) ?: false