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]}) [{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 countChanged(self: Model) {.signal.}
proc getCount*(self: Model): int {.slot.} = proc getCount*(self: Model): int {.slot.} =
@ -183,16 +178,6 @@ QtObject:
self.countChanged() 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 = proc getItemAtIndex*(self: Model, index: int): Item =
if(index < 0 or index >= self.items.len): if(index < 0 or index >= self.items.len):
return return
@ -334,11 +319,13 @@ QtObject:
if(index == -1): if(index == -1):
return return
if(self.items[index].BaseItem.position == position):
return
self.items[index].BaseItem.position = position self.items[index].BaseItem.position = position
self.beginResetModel() let modelIndex = self.createIndex(index, 0, nil)
self.items.sort(sortChats) self.dataChanged(modelIndex, modelIndex, @[ModelRole.Position.int])
self.endResetModel()
proc clearItems*(self: Model) = proc clearItems*(self: Model) =
self.beginResetModel() 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: # Since we cannot return QVariant from the proc which has arguments, so cannot have proc like this:
# prepareCommunitySectionModuleForCommunityId(self: View, communityId: string): QVariant {.slot.} # prepareCommunitySectionModuleForCommunityId(self: View, communityId: string): QVariant {.slot.}
# we're using combinaiton of # we're using combination of
# prepareCommunitySectionModuleForCommunityId/getCommunitySectionModule procs # prepareCommunitySectionModuleForCommunityId/getCommunitySectionModule procs
proc prepareCommunitySectionModuleForCommunityId*(self: View, communityId: string) {.slot.} = proc prepareCommunitySectionModuleForCommunityId*(self: View, communityId: string) {.slot.} =
self.tmpCommunityId = communityId self.tmpCommunityId = communityId

View File

@ -295,14 +295,14 @@ QtObject:
if(community.chats.len > prev_community.chats.len): if(community.chats.len > prev_community.chats.len):
for chat in community.chats: for chat in community.chats:
if findIndexById(chat.id, prev_community.chats) == -1: if findIndexById(chat.id, prev_community.chats) == -1:
let chatFullId = community.id & chat.id self.chatService.updateOrAddChat(chat) # we have to update chats stored in the chat service.
var createdChat = findChatById(chatFullId, updatedChats) let data = CommunityChatArgs(chat: chat)
createdChat.updateMissingFields(chat)
self.chatService.updateOrAddChat(createdChat) # we have to update chats stored in the chat service.
let data = CommunityChatArgs(chat: createdChat)
self.events.emit(SIGNAL_COMMUNITY_CHANNEL_CREATED, data) 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 # channel was removed
elif(community.chats.len < prev_community.chats.len): elif(community.chats.len < prev_community.chats.len):
for prv_chat in prev_community.chats: for prv_chat in prev_community.chats:
@ -798,20 +798,20 @@ QtObject:
if response.result.isNil or response.result.kind == JNull: if response.result.isNil or response.result.kind == JNull:
error "response is invalid", procName="reorderCommunityChat" error "response is invalid", procName="reorderCommunityChat"
return
if response.result != nil and response.result.kind != JNull:
let updatedCommunity = response.result["communities"][0].toCommunityDto() let updatedCommunity = response.result["communities"][0].toCommunityDto()
for chat in updatedCommunity.chats: for chat in updatedCommunity.chats:
let prev_chat_idx = findIndexById(chat.id, self.joinedCommunities[communityId].chats) let prev_chat_idx = findIndexById(chat.id, self.joinedCommunities[communityId].chats)
if prev_chat_idx > -1: if prev_chat_idx > -1:
let fullChatId = communityId & chat.id
let prev_chat = self.joinedCommunities[communityId].chats[prev_chat_idx] let prev_chat = self.joinedCommunities[communityId].chats[prev_chat_idx]
if(chat.position != prev_chat.position and chat.categoryId == categoryId): 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 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 self.joinedCommunities[communityId].chats[prev_chat_idx].position = chat.position
chatDetails.updateMissingFields(self.joinedCommunities[communityId].chats[prev_chat_idx]) chatDetails.updateMissingFields(self.joinedCommunities[communityId].chats[prev_chat_idx])
self.chatService.updateOrAddChat(chatDetails) # we have to update chats stored in the chat service. 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)) self.events.emit(SIGNAL_COMMUNITY_CHANNEL_REORDERED, CommunityChatOrderArgs(communityId: updatedCommunity.id, chatId: chat.id, categoryId: chat.categoryId, position: chat.position))
except Exception as e: except Exception as e:
error "Error reordering community channel", msg = e.msg, communityId, chatId, position, procName="reorderCommunityChat" error "Error reordering community channel", msg = e.msg, communityId, chatId, position, procName="reorderCommunityChat"