From 8d3f1a88a411a7108e45cf689b411b7be4a793b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Cie=C5=9Blak?= Date: Fri, 5 Aug 2022 11:36:56 +0200 Subject: [PATCH] fix(Communities/ChatsList): chats order updates handled correctly, subitems reordering fixed fixes #6292 --- src/app/modules/main/chat_section/item.nim | 8 ----- src/app/modules/main/chat_section/model.nim | 6 ++++ src/app/modules/main/chat_section/module.nim | 6 ++-- .../modules/main/chat_section/sub_model.nim | 32 ++++++------------- src/app_service/service/community/service.nim | 2 +- 5 files changed, 20 insertions(+), 34 deletions(-) diff --git a/src/app/modules/main/chat_section/item.nim b/src/app/modules/main/chat_section/item.nim index dcc7e13c26..232eacd5c0 100644 --- a/src/app/modules/main/chat_section/item.nim +++ b/src/app/modules/main/chat_section/item.nim @@ -73,13 +73,5 @@ proc appendSubItem*(self: Item, item: SubItem) = self.subItems.appendItem(item) self.BaseItem.muted = self.subItems.isAllMuted() -proc prependSubItems*(self: Item, items: seq[SubItem]) = - self.subItems.prependItems(items) - self.BaseItem.muted = self.subItems.isAllMuted() - -proc prependSubItem*(self: Item, item: SubItem) = - self.subItems.prependItem(item) - self.BaseItem.muted = self.subItems.isAllMuted() - proc setActiveSubItem*(self: Item, subItemId: string) = self.subItems.setActiveItem(subItemId) diff --git a/src/app/modules/main/chat_section/model.nim b/src/app/modules/main/chat_section/model.nim index 14b00c3951..c24cda069d 100644 --- a/src/app/modules/main/chat_section/model.nim +++ b/src/app/modules/main/chat_section/model.nim @@ -326,9 +326,15 @@ QtObject: result.hasNotifications = result.hasNotifications or self.items[i].BaseItem.hasUnreadMessages result.notificationsCount = result.notificationsCount + self.items[i].BaseItem.notificationsCount + proc reorderSubModel(self: Model, chatId: string, position: int) = + for it in self.items: + if(it.subItems.getCount() > 0): + it.subItems.reorder(chatId, position) + proc reorder*(self: Model, chatOrCategoryId: string, position: int) = let index = self.getItemIdxById(chatOrCategoryId) if(index == -1): + self.reorderSubModel(chatOrCategoryId, position) return if(self.items[index].BaseItem.position == position): diff --git a/src/app/modules/main/chat_section/module.nim b/src/app/modules/main/chat_section/module.nim index a5d0afe431..a6495bdc70 100644 --- a/src/app/modules/main/chat_section/module.nim +++ b/src/app/modules/main/chat_section/module.nim @@ -201,7 +201,7 @@ proc buildChatSectionUI( description="", ChatType.Unknown.int, amIChatAdmin=false, hasNotificationPerCategory, notificationsCountPerCategory, muted=false, blocked=false, active=false, cat.position, cat.id) - categoryItem.prependSubItems(categoryChannels) + categoryItem.appendSubItems(categoryChannels) self.view.chatsModel().appendItem(categoryItem) self.setActiveItemSubItem(selectedItemId, selectedSubItemId) @@ -478,7 +478,7 @@ method onCommunityCategoryCreated*(self: Module, cat: Category, chats: seq[ChatD self.view.chatsModel().removeItemById(chatDto.id) categoryChannels.add(channelItem) - categoryItem.prependSubItems(categoryChannels) + categoryItem.appendSubItems(categoryChannels) self.view.chatsModel().appendItem(categoryItem) method onCommunityCategoryDeleted*(self: Module, cat: Category) = @@ -534,7 +534,7 @@ method onCommunityCategoryEdited*(self: Module, cat: Category, chats: seq[ChatDt chatDto.color, chatDto.emoji, chatDto.description, chatDto.chatType.int, amIChatAdmin=true, hasNotification, notificationsCount, chatDto.muted, blocked=false, active=false, chatDto.position) - categoryItem.prependSubItem(channelItem) + categoryItem.appendSubItem(channelItem) else: let channelItem = initItem(chatDto.id, chatDto.name, chatDto.icon, chatDto.color, chatDto.emoji, chatDto.description, chatDto.chatType.int, amIChatAdmin, diff --git a/src/app/modules/main/chat_section/sub_model.nim b/src/app/modules/main/chat_section/sub_model.nim index 792d1ffdd1..7b1a57a995 100644 --- a/src/app/modules/main/chat_section/sub_model.nim +++ b/src/app/modules/main/chat_section/sub_model.nim @@ -143,28 +143,6 @@ QtObject: self.countChanged() - proc prependItems*(self: SubModel, items: seq[SubItem]) = - let parentModelIndex = newQModelIndex() - defer: parentModelIndex.delete - - let first = 0 - let last = items.len - 1 - self.beginInsertRows(parentModelIndex, first, last) - self.items = items & self.items - self.endInsertRows() - - self.countChanged() - - proc prependItem*(self: SubModel, item: SubItem) = - let parentModelIndex = newQModelIndex() - defer: parentModelIndex.delete - - self.beginInsertRows(parentModelIndex, 0, 0) - self.items = item & self.items - self.endInsertRows() - - self.countChanged() - proc getItemById*(self: SubModel, id: string): SubItem = for it in self.items: if(it.id == id): @@ -247,6 +225,16 @@ QtObject: self.endRemoveRows() self.countChanged() + proc reorder*(self: SubModel, id: string, position: int) = + let index = self.getItemIdxById(id) + if index == -1: + return + + self.items[index].BaseItem.position = position + + let modelIndex = self.createIndex(index, 0, nil) + self.dataChanged(modelIndex, modelIndex, @[ModelRole.Position.int]) + proc updateNotificationsForItemById*(self: SubModel, id: string, hasUnreadMessages: bool, notificationsCount: int): bool = for i in 0 ..< self.items.len: diff --git a/src/app_service/service/community/service.nim b/src/app_service/service/community/service.nim index 85e12f46e3..d7bc808c6b 100644 --- a/src/app_service/service/community/service.nim +++ b/src/app_service/service/community/service.nim @@ -320,7 +320,7 @@ QtObject: # Handle position changes if(chat.id == prev_chat.id and chat.position != prev_chat.position): self.events.emit(SIGNAL_COMMUNITY_CHANNEL_REORDERED, CommunityChatOrderArgs(communityId: community.id, - chatId: community.id&chat.id, categoryId: chat.categoryId, position: chat.position)) + chatId: chat.id, categoryId: chat.categoryId, position: chat.position)) # Handle name/description changes if(chat.id == prev_chat.id and (chat.name != prev_chat.name or chat.description != prev_chat.description)):