refactor(DnD): improve chat reorder by passing destination directly

This commit is contained in:
Jonathan Rainville 2023-05-18 11:08:01 -04:00
parent a69b8de3c3
commit 4dd383ffb5
6 changed files with 21 additions and 34 deletions

View File

@ -612,7 +612,7 @@ proc inviteUsersToCommunity*(self: Controller, pubKeys: string, inviteMessage: s
proc reorderCommunityCategories*(self: Controller, categoryId: string, position: int) = proc reorderCommunityCategories*(self: Controller, categoryId: string, position: int) =
self.communityService.reorderCommunityCategories(self.sectionId, categoryId, position) 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) self.communityService.reorderCommunityChat(self.sectionId, categoryId, chatId, position)
proc getRenderedText*(self: Controller, parsedTextArray: seq[ParsedText], communityChats: seq[ChatDto]): string = proc getRenderedText*(self: Controller, parsedTextArray: seq[ParsedText], communityChats: seq[ChatDto]): string =

View File

@ -328,7 +328,7 @@ method prepareEditCategoryModel*(self: AccessInterface, categoryId: string) {.ba
method reorderCommunityCategories*(self: AccessInterface, categoryId: string, position: int) {.base.} = method reorderCommunityCategories*(self: AccessInterface, categoryId: string, position: int) {.base.} =
raise newException(ValueError, "No implementation available") 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") raise newException(ValueError, "No implementation available")
method downloadMessages*(self: AccessInterface, chatId: string, filePath: string) {.base.} = method downloadMessages*(self: AccessInterface, chatId: string, filePath: string) {.base.} =

View File

@ -174,15 +174,6 @@ QtObject:
proc getItemIdxById*(self: Model, id: string): int = proc getItemIdxById*(self: Model, id: string): int =
return getItemIdxById(self.items, id) 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 = proc cmpChatsAndCats*(x, y: Item): int =
# Sort proc to compare chats and categories # Sort proc to compare chats and categories
# Compares first by categoryPosition, then by position # Compares first by categoryPosition, then by position

View File

@ -1123,28 +1123,15 @@ method prepareEditCategoryModel*(self: Module, categoryId: string) =
) )
self.view.editCategoryChannelsModel().appendItem(chatItem, ignoreCategory = true) self.view.editCategoryChannelsModel().appendItem(chatItem, ignoreCategory = true)
method reorderCommunityCategories*(self: Module, categoryId: string, categoryPositon: int) = method reorderCommunityCategories*(self: Module, categoryId: string, categoryPosition: int) =
var finalPosition = categoryPositon var finalPosition = categoryPosition
if finalPosition < 0: if finalPosition < 0:
finalPosition = 0 finalPosition = 0
self.controller.reorderCommunityCategories(categoryId, finalPosition) self.controller.reorderCommunityCategories(categoryId, finalPosition)
method reorderCommunityChat*(self: Module, categoryId: string, chatId: string, toPosition: int): string = method reorderCommunityChat*(self: Module, categoryId: string, chatId: string, toPosition: int) =
# Calculate actual position, since the position coming from the UI is assuming a single list where categories are items self.controller.reorderCommunityChat(categoryId, chatId, toPosition + 1)
# 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 setLoadingHistoryMessagesInProgress*(self: Module, isLoading: bool) = method setLoadingHistoryMessagesInProgress*(self: Module, isLoading: bool) =
self.view.setLoadingHistoryMessagesInProgress(isLoading) self.view.setLoadingHistoryMessagesInProgress(isLoading)

View File

@ -344,10 +344,10 @@ QtObject:
proc prepareEditCategoryModel*(self: View, categoryId: string) {.slot.} = proc prepareEditCategoryModel*(self: View, categoryId: string) {.slot.} =
self.delegate.prepareEditCategoryModel(categoryId) self.delegate.prepareEditCategoryModel(categoryId)
proc reorderCommunityCategories*(self: View, categoryId: string, categoryPositon: int) {.slot} = proc reorderCommunityCategories*(self: View, categoryId: string, categoryPosition: int) {.slot} =
self.delegate.reorderCommunityCategories(categoryId, categoryPositon) 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) self.delegate.reorderCommunityChat(categoryId, chatId, position)
proc loadingHistoryMessagesInProgressChanged*(self: View) {.signal.} proc loadingHistoryMessagesInProgressChanged*(self: View) {.signal.}

View File

@ -51,6 +51,7 @@ Item {
readonly property int visualIndex: index readonly property int visualIndex: index
readonly property string chatId: model.itemId readonly property string chatId: model.itemId
readonly property string categoryId: model.categoryId 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 int categoryPosition: model.categoryPosition // needed for the DnD
readonly property bool isCategory: model.isCategory readonly property bool isCategory: model.isCategory
readonly property Item item: isCategory ? draggableItem.actions[0] : draggableItem.actions[1] readonly property Item item: isCategory ? draggableItem.actions[0] : draggableItem.actions[1]
@ -70,10 +71,18 @@ Item {
const to = chatListDelegate.visualIndex; const to = chatListDelegate.visualIndex;
if (to === from) if (to === from)
return; return;
if (!drop.source.isCategory) { if (drop.source.isCategory) {
root.chatItemReordered(statusChatListItems.itemAtIndex(from).categoryId, statusChatListItems.itemAtIndex(from).chatId, to); root.categoryReordered(
statusChatListItems.itemAtIndex(from).categoryId,
statusChatListItems.itemAtIndex(to).categoryPosition
);
} else { } 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,
);
} }
} }