From cc13146f8127b847ad92ee5c76e980342428b79b Mon Sep 17 00:00:00 2001 From: SpiritCroc Date: Wed, 22 Sep 2021 17:37:17 +0200 Subject: [PATCH] Update unread counter handling Change-Id: I2f5738b822b4161d18de28074385c5664f3d1f98 --- .../android/sdk/api/session/room/model/RoomSummary.kt | 9 --------- .../session/room/summary/RoomSummaryDataSource.kt | 9 +++++++-- .../vector/app/core/resources/UserPreferencesProvider.kt | 4 ---- .../im/vector/app/features/home/room/ScSdkPreferences.kt | 4 ---- .../app/features/home/room/list/RoomSummaryItem.kt | 4 ++-- .../features/home/room/list/RoomSummaryItemFactory.kt | 1 + .../features/home/room/list/UnreadCounterBadgeView.kt | 5 ++++- .../im/vector/app/features/settings/VectorPreferences.kt | 2 +- .../vector/app/features/spaces/SpaceSummaryController.kt | 4 ++-- vector/src/main/res/values/strings_sc.xml | 4 ++-- vector/src/main/res/xml/vector_settings_preferences.xml | 4 ++-- 11 files changed, 21 insertions(+), 29 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomSummary.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomSummary.kt index c4a521c657..e33179a739 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomSummary.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomSummary.kt @@ -131,14 +131,6 @@ data class RoomSummary( } } - fun scUnreadCount(preferenceProvider: RoomSummaryPreferenceProvider?): Int { - return if (preferenceProvider?.shouldShowUnimportantCounterBadge() == true) { - safeUnreadCount - } else { - 0 - } - } - fun scNotificationCountWithManualUnread(): Int { return when { notificationCount > 0 -> { @@ -164,7 +156,6 @@ data class RoomSummary( // SC addition interface RoomSummaryPreferenceProvider { fun getUnreadKind(isDirect: Boolean): Int - fun shouldShowUnimportantCounterBadge(): Boolean fun getUnreadRoomSummaryField(isDirect: Boolean): String { return when(getUnreadKind(isDirect)) { UNREAD_KIND_ORIGINAL_CONTENT -> RoomSummaryEntityFields.HAS_UNREAD_ORIGINAL_CONTENT_MESSAGES diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/summary/RoomSummaryDataSource.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/summary/RoomSummaryDataSource.kt index 0651856e0e..9609fd47dd 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/summary/RoomSummaryDataSource.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/summary/RoomSummaryDataSource.kt @@ -52,6 +52,7 @@ import org.matrix.android.sdk.internal.di.SessionDatabase import org.matrix.android.sdk.internal.query.process import org.matrix.android.sdk.internal.util.fetchCopyMap import javax.inject.Inject +import kotlin.math.max internal class RoomSummaryDataSource @Inject constructor(@SessionDatabase private val monarchy: Monarchy, private val roomSummaryMapper: RoomSummaryMapper) { @@ -230,8 +231,12 @@ internal class RoomSummaryDataSource @Inject constructor(@SessionDatabase privat val roomSummariesQuery = roomSummariesQuery(realm, queryParams) val notifCount = roomSummariesQuery.sum(RoomSummaryEntityFields.NOTIFICATION_COUNT).toInt() val highlightCount = roomSummariesQuery.sum(RoomSummaryEntityFields.HIGHLIGHT_COUNT).toInt() - // TODO-SC-merge: respect setting for selecting right field here (HAS_UNREAD_CONTENT_MESSAGES, HAS_UNREAD_MESSAGES, HAS_UNREAD_ORIGINAL_CONTENT_MESSAGES) - val unreadCount = if (preferenceProvider.shouldShowUnimportantCounterBadge()) roomSummariesQuery(realm, queryParams).equalTo(preferenceProvider.getUnreadRoomSummaryField(false), true).count().toInt() else 0 + val unreadCount = max( + // Preferred since MSC 2654 + roomSummariesQuery.sum(RoomSummaryEntityFields.UNREAD_COUNT).toInt(), + // TODO-SC-merge: properly use dm/non-dm flag? (note that this will be likely overwritten either way by above field from MSC 2654) + roomSummariesQuery(realm, queryParams).equalTo(preferenceProvider.getUnreadRoomSummaryField(false), true).count().toInt() + ) val markedUnreadCount = roomSummariesQuery(realm, queryParams).equalTo(RoomSummaryEntityFields.MARKED_UNREAD, true).count().toInt() notificationCount = RoomAggregateNotificationCount( notifCount, diff --git a/vector/src/main/java/im/vector/app/core/resources/UserPreferencesProvider.kt b/vector/src/main/java/im/vector/app/core/resources/UserPreferencesProvider.kt index 17bee3c797..9ab3b9bf45 100644 --- a/vector/src/main/java/im/vector/app/core/resources/UserPreferencesProvider.kt +++ b/vector/src/main/java/im/vector/app/core/resources/UserPreferencesProvider.kt @@ -41,10 +41,6 @@ class UserPreferencesProvider @Inject constructor(private val vectorPreferences: vectorPreferences.neverShowLongClickOnRoomHelpAgain() } - fun shouldShowUnimportantCounterBadge(): Boolean { - return vectorPreferences.shouldShowUnimportantCounterBadge() - } - fun shouldShowJoinLeaves(): Boolean { return vectorPreferences.showJoinLeaveMessages() } diff --git a/vector/src/main/java/im/vector/app/features/home/room/ScSdkPreferences.kt b/vector/src/main/java/im/vector/app/features/home/room/ScSdkPreferences.kt index d20f0b8cad..21341b8e37 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/ScSdkPreferences.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/ScSdkPreferences.kt @@ -12,8 +12,4 @@ class ScSdkPreferences @Inject constructor(private val vectorPreferences: Vector override fun getUnreadKind(isDirect: Boolean): Int { return vectorPreferences?.roomUnreadKind(isDirect) ?: RoomSummary.UNREAD_KIND_FULL } - - override fun shouldShowUnimportantCounterBadge(): Boolean { - return vectorPreferences?.shouldShowUnimportantCounterBadge() ?: false - } } diff --git a/vector/src/main/java/im/vector/app/features/home/room/list/RoomSummaryItem.kt b/vector/src/main/java/im/vector/app/features/home/room/list/RoomSummaryItem.kt index 2bb8f23209..c986c83913 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/list/RoomSummaryItem.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/list/RoomSummaryItem.kt @@ -57,6 +57,7 @@ abstract class RoomSummaryItem : VectorEpoxyModel() { @EpoxyAttribute var unreadNotificationCount: Int = 0 @EpoxyAttribute var hasUnreadMessage: Boolean = false @EpoxyAttribute var markedUnread: Boolean = false + @EpoxyAttribute var unreadCount: Int = 0 @EpoxyAttribute var hasDraft: Boolean = false @EpoxyAttribute var showHighlighted: Boolean = false @EpoxyAttribute var hasFailedSending: Boolean = false @@ -74,8 +75,7 @@ abstract class RoomSummaryItem : VectorEpoxyModel() { holder.titleView.text = matrixItem.getBestName() holder.lastEventTimeView.text = lastEventTime holder.lastEventView.text = lastFormattedEvent - // SC-TODO: once we count unimportant unread messages, pass that as counter - for now, unreadIndentIndicator is enough, so pass 0 to the badge instead - holder.unreadCounterBadgeView.render(UnreadCounterBadgeView.State(unreadNotificationCount, showHighlighted, 0, markedUnread)) + holder.unreadCounterBadgeView.render(UnreadCounterBadgeView.State(unreadNotificationCount, showHighlighted, unreadCount, markedUnread)) holder.unreadIndentIndicator.isVisible = hasUnreadMessage holder.draftView.isVisible = hasDraft avatarRenderer.render(matrixItem, holder.avatarImageView) diff --git a/vector/src/main/java/im/vector/app/features/home/room/list/RoomSummaryItemFactory.kt b/vector/src/main/java/im/vector/app/features/home/room/list/RoomSummaryItemFactory.kt index 349e700052..7bd954fba4 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/list/RoomSummaryItemFactory.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/list/RoomSummaryItemFactory.kt @@ -137,6 +137,7 @@ class RoomSummaryItemFactory @Inject constructor(private val displayableEventFor .unreadNotificationCount(unreadCount) .hasUnreadMessage(roomSummary.scIsUnread(scSdkPreferences)) .markedUnread(roomSummary.markedUnread) + .unreadCount(roomSummary.unreadCount ?: 0) .hasDraft(roomSummary.userDrafts.isNotEmpty()) .itemLongClickListener { _ -> onLongClick?.invoke(roomSummary) ?: false diff --git a/vector/src/main/java/im/vector/app/features/home/room/list/UnreadCounterBadgeView.kt b/vector/src/main/java/im/vector/app/features/home/room/list/UnreadCounterBadgeView.kt index fb5f532011..3123c8041f 100755 --- a/vector/src/main/java/im/vector/app/features/home/room/list/UnreadCounterBadgeView.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/list/UnreadCounterBadgeView.kt @@ -20,6 +20,7 @@ import android.util.AttributeSet import android.view.View import com.google.android.material.textview.MaterialTextView import im.vector.app.R +import im.vector.app.features.settings.VectorPreferences class UnreadCounterBadgeView : MaterialTextView { @@ -29,8 +30,10 @@ class UnreadCounterBadgeView : MaterialTextView { constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) + var vectorPreferences: VectorPreferences = VectorPreferences(context) + fun render(state: State) { - if (state.count == 0 && state.unread == 0 && !state.markedUnread) { + if (state.count == 0 && !state.markedUnread && (state.unread == 0 || !vectorPreferences.shouldShowUnimportantCounterBadge())) { visibility = View.INVISIBLE } else { visibility = View.VISIBLE diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorPreferences.kt b/vector/src/main/java/im/vector/app/features/settings/VectorPreferences.kt index cf8ef251b1..adf8d10159 100755 --- a/vector/src/main/java/im/vector/app/features/settings/VectorPreferences.kt +++ b/vector/src/main/java/im/vector/app/features/settings/VectorPreferences.kt @@ -196,7 +196,7 @@ class VectorPreferences @Inject constructor(private val context: Context) { private const val SETTINGS_ROOM_UNREAD_KIND = "SETTINGS_ROOM_UNREAD_KIND" private const val SETTINGS_ROOM_UNREAD_KIND_DM = "SETTINGS_ROOM_UNREAD_KIND_DM" private const val SETTINGS_ROOM_UNREAD_KIND_GROUP = "SETTINGS_ROOM_UNREAD_KIND_GROUP" - private const val SETTINGS_UNIMPORTANT_COUNTER_BADGE = "SETTINGS_UNIMPORTANT_COUNTER_BADGE" + const val SETTINGS_UNIMPORTANT_COUNTER_BADGE = "SETTINGS_UNIMPORTANT_COUNTER_BADGE" private const val SETTINGS_SIMPLIFIED_MODE = "SETTINGS_SIMPLIFIED_MODE" private const val SETTINGS_LABS_ALLOW_MARK_UNREAD = "SETTINGS_LABS_ALLOW_MARK_UNREAD" const val SETTINGS_ALLOW_URL_PREVIEW_IN_ENCRYPTED_ROOM_KEY = "SETTINGS_ALLOW_URL_PREVIEW_IN_ENCRYPTED_ROOM_KEY" diff --git a/vector/src/main/java/im/vector/app/features/spaces/SpaceSummaryController.kt b/vector/src/main/java/im/vector/app/features/spaces/SpaceSummaryController.kt index b504d94559..30ce70e466 100644 --- a/vector/src/main/java/im/vector/app/features/spaces/SpaceSummaryController.kt +++ b/vector/src/main/java/im/vector/app/features/spaces/SpaceSummaryController.kt @@ -170,7 +170,7 @@ class SpaceSummaryController @Inject constructor( UnreadCounterBadgeView.State( groupSummary.notificationCount, groupSummary.highlightCount > 0, - groupSummary.scUnreadCount(host.scSdkPreferences), + groupSummary.safeUnreadCount, groupSummary.markedUnread ) ) @@ -219,7 +219,7 @@ class SpaceSummaryController @Inject constructor( UnreadCounterBadgeView.State( childSummary.notificationCount, childSummary.highlightCount > 0, - childSummary.scUnreadCount(host.scSdkPreferences), + childSummary.safeUnreadCount, childSummary.markedUnread ) ) diff --git a/vector/src/main/res/values/strings_sc.xml b/vector/src/main/res/values/strings_sc.xml index e4a019fbca..87a355f8f4 100644 --- a/vector/src/main/res/values/strings_sc.xml +++ b/vector/src/main/res/values/strings_sc.xml @@ -44,8 +44,8 @@ Top Bottom - Count unimportant chat events - Include chats without notifications in the category unread counter + Count muted messages + Show counts for muted messages in the chat overview Mark chats as unread Allow marking chats as unread. Other Matrix clients might not support this yet. diff --git a/vector/src/main/res/xml/vector_settings_preferences.xml b/vector/src/main/res/xml/vector_settings_preferences.xml index 419014a0d6..498aeb9ac8 100644 --- a/vector/src/main/res/xml/vector_settings_preferences.xml +++ b/vector/src/main/res/xml/vector_settings_preferences.xml @@ -99,8 +99,8 @@ + android:summary="@string/settings_unimportant_counter_badge_summary_v2" + android:title="@string/settings_unimportant_counter_badge_v2" />