From 4dd383ffb53726c324b4f12e159f092e696ff949 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Thu, 18 May 2023 11:08:01 -0400 Subject: [PATCH] refactor(DnD): improve chat reorder by passing destination directly --- .../modules/main/chat_section/controller.nim | 2 +- .../main/chat_section/io_interface.nim | 2 +- src/app/modules/main/chat_section/model.nim | 9 -------- src/app/modules/main/chat_section/module.nim | 21 ++++--------------- src/app/modules/main/chat_section/view.nim | 6 +++--- .../src/StatusQ/Components/StatusChatList.qml | 15 ++++++++++--- 6 files changed, 21 insertions(+), 34 deletions(-) diff --git a/src/app/modules/main/chat_section/controller.nim b/src/app/modules/main/chat_section/controller.nim index fac2a4efe5..b54d3a927f 100644 --- a/src/app/modules/main/chat_section/controller.nim +++ b/src/app/modules/main/chat_section/controller.nim @@ -612,7 +612,7 @@ proc inviteUsersToCommunity*(self: Controller, pubKeys: string, inviteMessage: s proc reorderCommunityCategories*(self: Controller, categoryId: string, position: int) = self.communityService.reorderCommunityCategories(self.sectionId, categoryId, position) -proc reorderCommunityChat*(self: Controller, categoryId: string, chatId: string, position: int): string = +proc reorderCommunityChat*(self: Controller, categoryId: string, chatId: string, position: int) = self.communityService.reorderCommunityChat(self.sectionId, categoryId, chatId, position) proc getRenderedText*(self: Controller, parsedTextArray: seq[ParsedText], communityChats: seq[ChatDto]): string = diff --git a/src/app/modules/main/chat_section/io_interface.nim b/src/app/modules/main/chat_section/io_interface.nim index edb27d4445..e6735846d3 100644 --- a/src/app/modules/main/chat_section/io_interface.nim +++ b/src/app/modules/main/chat_section/io_interface.nim @@ -328,7 +328,7 @@ method prepareEditCategoryModel*(self: AccessInterface, categoryId: string) {.ba method reorderCommunityCategories*(self: AccessInterface, categoryId: string, position: int) {.base.} = raise newException(ValueError, "No implementation available") -method reorderCommunityChat*(self: AccessInterface, categoryId: string, chatId: string, position: int): string {.base.} = +method reorderCommunityChat*(self: AccessInterface, categoryId: string, chatId: string, position: int) {.base.} = raise newException(ValueError, "No implementation available") method downloadMessages*(self: AccessInterface, chatId: string, filePath: string) {.base.} = diff --git a/src/app/modules/main/chat_section/model.nim b/src/app/modules/main/chat_section/model.nim index 1ca4cd8d1b..f70a512b08 100644 --- a/src/app/modules/main/chat_section/model.nim +++ b/src/app/modules/main/chat_section/model.nim @@ -174,15 +174,6 @@ QtObject: proc getItemIdxById*(self: Model, id: string): int = return getItemIdxById(self.items, id) - proc getClosestCategoryAtIndex*(self: Model, index: int): tuple[categoryIem: Item, categoryIndex: int] = - if index > self.items.len: - return (Item(), -1) - # Count down from the index to 0 and find the first category - for i in countdown(index - 1, 0): - if self.items[i].isCategory: - return (self.items[i], i) - return (Item(), -1) - proc cmpChatsAndCats*(x, y: Item): int = # Sort proc to compare chats and categories # Compares first by categoryPosition, then by position diff --git a/src/app/modules/main/chat_section/module.nim b/src/app/modules/main/chat_section/module.nim index 0b1634926a..d2cbf9d312 100644 --- a/src/app/modules/main/chat_section/module.nim +++ b/src/app/modules/main/chat_section/module.nim @@ -1123,28 +1123,15 @@ method prepareEditCategoryModel*(self: Module, categoryId: string) = ) self.view.editCategoryChannelsModel().appendItem(chatItem, ignoreCategory = true) -method reorderCommunityCategories*(self: Module, categoryId: string, categoryPositon: int) = - var finalPosition = categoryPositon +method reorderCommunityCategories*(self: Module, categoryId: string, categoryPosition: int) = + var finalPosition = categoryPosition if finalPosition < 0: finalPosition = 0 self.controller.reorderCommunityCategories(categoryId, finalPosition) -method reorderCommunityChat*(self: Module, categoryId: string, chatId: string, toPosition: int): string = - # Calculate actual position, since the position coming from the UI is assuming a single list where categories are items - # eg: if we have 2 categories with 2 channels each, then it means 6 items (2 categories, 2 chats) - # if we move the 2nd channel of the 2nd category to the 1st position of the 2nd category, then the UI would say - # that we move the chat from position 5 to position 4 - # We need to translate that to position 1 of category 2 - let (category, categoryIndex) = self.view.chatsModel().getClosestCategoryAtIndex(toPosition + 1) - var categoryId = "" - var newPos = toPosition - if (categoryIndex > -1): - categoryId = category.id - newPos = toPosition - categoryIndex - 1 # position is 0 based - if newPos < 0: - newPos = 0 - self.controller.reorderCommunityChat(categoryId, chatId, newPos) +method reorderCommunityChat*(self: Module, categoryId: string, chatId: string, toPosition: int) = + self.controller.reorderCommunityChat(categoryId, chatId, toPosition + 1) method setLoadingHistoryMessagesInProgress*(self: Module, isLoading: bool) = self.view.setLoadingHistoryMessagesInProgress(isLoading) diff --git a/src/app/modules/main/chat_section/view.nim b/src/app/modules/main/chat_section/view.nim index 9a031f9abc..7bf7c9d86a 100644 --- a/src/app/modules/main/chat_section/view.nim +++ b/src/app/modules/main/chat_section/view.nim @@ -344,10 +344,10 @@ QtObject: proc prepareEditCategoryModel*(self: View, categoryId: string) {.slot.} = self.delegate.prepareEditCategoryModel(categoryId) - proc reorderCommunityCategories*(self: View, categoryId: string, categoryPositon: int) {.slot} = - self.delegate.reorderCommunityCategories(categoryId, categoryPositon) + proc reorderCommunityCategories*(self: View, categoryId: string, categoryPosition: int) {.slot} = + self.delegate.reorderCommunityCategories(categoryId, categoryPosition) - proc reorderCommunityChat*(self: View, categoryId: string, chatId: string, position: int): string {.slot} = + proc reorderCommunityChat*(self: View, categoryId: string, chatId: string, position: int) {.slot} = self.delegate.reorderCommunityChat(categoryId, chatId, position) proc loadingHistoryMessagesInProgressChanged*(self: View) {.signal.} diff --git a/ui/StatusQ/src/StatusQ/Components/StatusChatList.qml b/ui/StatusQ/src/StatusQ/Components/StatusChatList.qml index d97095f609..d2e21b7712 100644 --- a/ui/StatusQ/src/StatusQ/Components/StatusChatList.qml +++ b/ui/StatusQ/src/StatusQ/Components/StatusChatList.qml @@ -51,6 +51,7 @@ Item { readonly property int visualIndex: index readonly property string chatId: model.itemId readonly property string categoryId: model.categoryId + readonly property int position: model.position // needed for the DnD readonly property int categoryPosition: model.categoryPosition // needed for the DnD readonly property bool isCategory: model.isCategory readonly property Item item: isCategory ? draggableItem.actions[0] : draggableItem.actions[1] @@ -70,10 +71,18 @@ Item { const to = chatListDelegate.visualIndex; if (to === from) return; - if (!drop.source.isCategory) { - root.chatItemReordered(statusChatListItems.itemAtIndex(from).categoryId, statusChatListItems.itemAtIndex(from).chatId, to); + if (drop.source.isCategory) { + root.categoryReordered( + statusChatListItems.itemAtIndex(from).categoryId, + statusChatListItems.itemAtIndex(to).categoryPosition + ); + } else { - root.categoryReordered(statusChatListItems.itemAtIndex(from).categoryId, statusChatListItems.itemAtIndex(to).categoryPosition); + root.chatItemReordered( + statusChatListItems.itemAtIndex(to).categoryId, + statusChatListItems.itemAtIndex(from).chatId, + statusChatListItems.itemAtIndex(to).position, + ); } }