diff --git a/apps/ios/Shared/Model/ChatModel.swift b/apps/ios/Shared/Model/ChatModel.swift index 0be8436320..9171bf5073 100644 --- a/apps/ios/Shared/Model/ChatModel.swift +++ b/apps/ios/Shared/Model/ChatModel.swift @@ -123,11 +123,9 @@ class ChatTagsModel: ObservableObject { } } } - if case let .presetTag(tag) = tm.activeFilter, (newPresetTags[tag] ?? 0) == 0 { - activeFilter = nil - } presetTags = newPresetTags unreadTags = newUnreadTags + clearActiveChatFilterIfNeeded() } func updateChatFavorite(favorite: Bool, wasFavorite: Bool) { @@ -136,9 +134,7 @@ class ChatTagsModel: ObservableObject { presetTags[.favorites] = (count ?? 0) + 1 } else if !favorite && wasFavorite, let count { presetTags[.favorites] = max(0, count - 1) - if case .presetTag(.favorites) = activeFilter, (presetTags[.favorites] ?? 0) == 0 { - activeFilter = nil - } + clearActiveChatFilterIfNeeded() } } @@ -162,6 +158,7 @@ class ChatTagsModel: ObservableObject { } } } + clearActiveChatFilterIfNeeded() } func markChatTagRead(_ chat: Chat) -> Void { @@ -192,7 +189,17 @@ class ChatTagsModel: ObservableObject { func changeGroupReportsTag(_ by: Int = 0) { if by == 0 { return } - presetTags[.groupReports] = (presetTags[.groupReports] ?? 0) + by + presetTags[.groupReports] = max(0, (presetTags[.groupReports] ?? 0) + by) + clearActiveChatFilterIfNeeded() + } + + func clearActiveChatFilterIfNeeded() { + let clear = switch activeFilter { + case let .presetTag(tag): (presetTags[tag] ?? 0) == 0 + case let .userTag(tag): !userTags.contains(tag) + case .unread, nil: false + } + if clear { activeFilter = nil } } } diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt index 656937641c..6424397531 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt @@ -247,15 +247,12 @@ object ChatModel { } } - if (activeChatTagFilter.value is ActiveFilter.PresetTag && - (newPresetTags[(activeChatTagFilter.value as ActiveFilter.PresetTag).tag] ?: 0) == 0) { - activeChatTagFilter.value = null - } - presetTags.clear() presetTags.putAll(newPresetTags) unreadTags.clear() unreadTags.putAll(newUnreadTags) + + clearActiveChatFilterIfNeeded() } fun updateChatFavorite(favorite: Boolean, wasFavorite: Boolean) { @@ -265,9 +262,7 @@ object ChatModel { presetTags[PresetTagKind.FAVORITES] = (count ?: 0) + 1 } else if (!favorite && wasFavorite && count != null) { presetTags[PresetTagKind.FAVORITES] = maxOf(0, count - 1) - if (activeChatTagFilter.value == ActiveFilter.PresetTag(PresetTagKind.FAVORITES) && (presetTags[PresetTagKind.FAVORITES] ?: 0) == 0) { - activeChatTagFilter.value = null - } + clearActiveChatFilterIfNeeded() } } @@ -288,6 +283,7 @@ object ChatModel { } } } + clearActiveChatFilterIfNeeded() } fun moveChatTagUnread(chat: Chat, oldTags: List?, newTags: List) { @@ -875,10 +871,20 @@ object ChatModel { private fun changeGroupReportsTagNoContentTag(by: Int = 0) { if (by == 0 || contentTag != null) return - presetTags[PresetTagKind.GROUP_REPORTS] = (presetTags[PresetTagKind.GROUP_REPORTS] ?: 0) + by + presetTags[PresetTagKind.GROUP_REPORTS] = kotlin.math.max(0, (presetTags[PresetTagKind.GROUP_REPORTS] ?: 0) + by) + clearActiveChatFilterIfNeeded() } } + fun clearActiveChatFilterIfNeeded() { + val clear = when(val f = activeChatTagFilter.value) { + is ActiveFilter.PresetTag -> (presetTags[f.tag] ?: 0) == 0 + is ActiveFilter.UserTag -> userTags.value.none { it.chatTagId == f.tag.chatTagId } + is ActiveFilter.Unread, null -> false + } + if (clear) activeChatTagFilter.value = null + } + fun updateCurrentUser(rhId: Long?, newProfile: Profile, preferences: FullChatPreferences? = null) { val current = currentUser.value ?: return val updated = current.copy( diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/TagListView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/TagListView.kt index 1b563e6d02..6c88162f8a 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/TagListView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/TagListView.kt @@ -31,6 +31,7 @@ import chat.simplex.common.model.* import chat.simplex.common.model.ChatController.apiDeleteChatTag import chat.simplex.common.model.ChatController.apiSetChatTags import chat.simplex.common.model.ChatController.appPrefs +import chat.simplex.common.model.ChatModel.clearActiveChatFilterIfNeeded import chat.simplex.common.model.ChatModel.withChats import chat.simplex.common.model.ChatModel.withReportsChatsIfOpen import chat.simplex.common.platform.* @@ -447,9 +448,7 @@ private fun deleteTag(rhId: Long?, tag: ChatTag, saving: MutableState) val tagId = tag.chatTagId if (apiDeleteChatTag(rhId, tagId)) { chatModel.userTags.value = chatModel.userTags.value.filter { it.chatTagId != tagId } - if (chatModel.activeChatTagFilter.value == ActiveFilter.UserTag(tag)) { - chatModel.activeChatTagFilter.value = null - } + clearActiveChatFilterIfNeeded() chatModel.chats.value.forEach { c -> when (val cInfo = c.chatInfo) { is ChatInfo.Direct -> {