fix(Communities/ChatsList): chat model reordering refactored, updates fixed

fixes: #6597, #6722
This commit is contained in:
Michał Cieślak 2022-08-03 12:32:15 +02:00 committed by Michał
parent 0ecda9e00a
commit c834bde150
3 changed files with 25 additions and 38 deletions

View File

@ -52,11 +52,6 @@ QtObject:
[{i}]:({$self.items[i]})
"""
proc sortChats(x, y: Item): int =
if x.position < y.position: -1
elif x.position == y.position: 0
else: 1
proc countChanged(self: Model) {.signal.}
proc getCount*(self: Model): int {.slot.} =
@ -183,16 +178,6 @@ QtObject:
self.countChanged()
proc prependItem*(self: Model, item: Item) =
let parentModelIndex = newQModelIndex()
defer: parentModelIndex.delete
self.beginInsertRows(parentModelIndex, 0, 0)
self.items = item & self.items
self.endInsertRows()
self.countChanged()
proc getItemAtIndex*(self: Model, index: int): Item =
if(index < 0 or index >= self.items.len):
return
@ -334,11 +319,13 @@ QtObject:
if(index == -1):
return
if(self.items[index].BaseItem.position == position):
return
self.items[index].BaseItem.position = position
self.beginResetModel()
self.items.sort(sortChats)
self.endResetModel()
let modelIndex = self.createIndex(index, 0, nil)
self.dataChanged(modelIndex, modelIndex, @[ModelRole.Position.int])
proc clearItems*(self: Model) =
self.beginResetModel()

View File

@ -163,7 +163,7 @@ QtObject:
# Since we cannot return QVariant from the proc which has arguments, so cannot have proc like this:
# prepareCommunitySectionModuleForCommunityId(self: View, communityId: string): QVariant {.slot.}
# we're using combinaiton of
# we're using combination of
# prepareCommunitySectionModuleForCommunityId/getCommunitySectionModule procs
proc prepareCommunitySectionModuleForCommunityId*(self: View, communityId: string) {.slot.} =
self.tmpCommunityId = communityId

View File

@ -295,14 +295,14 @@ QtObject:
if(community.chats.len > prev_community.chats.len):
for chat in community.chats:
if findIndexById(chat.id, prev_community.chats) == -1:
let chatFullId = community.id & chat.id
var createdChat = findChatById(chatFullId, updatedChats)
createdChat.updateMissingFields(chat)
self.chatService.updateOrAddChat(createdChat) # we have to update chats stored in the chat service.
let data = CommunityChatArgs(chat: createdChat)
self.chatService.updateOrAddChat(chat) # we have to update chats stored in the chat service.
let data = CommunityChatArgs(chat: chat)
self.events.emit(SIGNAL_COMMUNITY_CHANNEL_CREATED, data)
# if the chat was created by the current user then it's already in the model and should be reordered if necessary
self.events.emit(SIGNAL_COMMUNITY_CHANNEL_REORDERED, CommunityChatOrderArgs(communityId: community.id,
chatId: chat.id, categoryId: chat.categoryId, position: chat.position))
# channel was removed
elif(community.chats.len < prev_community.chats.len):
for prv_chat in prev_community.chats:
@ -798,20 +798,20 @@ QtObject:
if response.result.isNil or response.result.kind == JNull:
error "response is invalid", procName="reorderCommunityChat"
return
if response.result != nil and response.result.kind != JNull:
let updatedCommunity = response.result["communities"][0].toCommunityDto()
for chat in updatedCommunity.chats:
let prev_chat_idx = findIndexById(chat.id, self.joinedCommunities[communityId].chats)
if prev_chat_idx > -1:
let fullChatId = communityId & chat.id
let prev_chat = self.joinedCommunities[communityId].chats[prev_chat_idx]
if(chat.position != prev_chat.position and chat.categoryId == categoryId):
var chatDetails = self.chatService.getChatById(fullChatId) # we are free to do this cause channel must be created before we add it to a category
self.joinedCommunities[communityId].chats[prev_chat_idx].position = chat.position
chatDetails.updateMissingFields(self.joinedCommunities[communityId].chats[prev_chat_idx])
self.chatService.updateOrAddChat(chatDetails) # we have to update chats stored in the chat service.
self.events.emit(SIGNAL_COMMUNITY_CHANNEL_REORDERED, CommunityChatOrderArgs(communityId: updatedCommunity.id, chatId: fullChatId, categoryId: chat.categoryId, position: chat.position))
let updatedCommunity = response.result["communities"][0].toCommunityDto()
for chat in updatedCommunity.chats:
let prev_chat_idx = findIndexById(chat.id, self.joinedCommunities[communityId].chats)
if prev_chat_idx > -1:
let prev_chat = self.joinedCommunities[communityId].chats[prev_chat_idx]
if(chat.position != prev_chat.position and chat.categoryId == categoryId):
var chatDetails = self.chatService.getChatById(chat.id) # we are free to do this cause channel must be created before we add it to a category
self.joinedCommunities[communityId].chats[prev_chat_idx].position = chat.position
chatDetails.updateMissingFields(self.joinedCommunities[communityId].chats[prev_chat_idx])
self.chatService.updateOrAddChat(chatDetails) # we have to update chats stored in the chat service.
self.events.emit(SIGNAL_COMMUNITY_CHANNEL_REORDERED, CommunityChatOrderArgs(communityId: updatedCommunity.id, chatId: chat.id, categoryId: chat.categoryId, position: chat.position))
except Exception as e:
error "Error reordering community channel", msg = e.msg, communityId, chatId, position, procName="reorderCommunityChat"