Update channels position when receiving a new msg

This commit is contained in:
Richard Ramos 2020-05-29 14:24:48 -04:00 committed by Iuri Matias
parent de15f55f8a
commit 9d86082cfe
3 changed files with 32 additions and 7 deletions

View File

@ -57,6 +57,14 @@ QtObject:
self.activeChannel = selectedChannel.id
self.activeChannelChanged()
proc getActiveChannelIdx(self: ChatsView): QVariant {.slot.} =
return newQVariant(self.chats.chats.findById(self.activeChannel))
QtProperty[QVariant] activeChannelIndex:
read = getActiveChannelIdx
write = setActiveChannelByIndex
notify = activeChannelChanged
proc setActiveChannel*(self: ChatsView, channel: string) =
self.activeChannel = channel
self.activeChannelChanged()

View File

@ -77,10 +77,9 @@ QtObject:
}.toTable
proc addChatItemToList*(self: ChannelsList, channel: ChatItem): int =
self.beginInsertRows(newQModelIndex(), self.chats.len, self.chats.len)
self.chats.add(channel)
self.beginInsertRows(newQModelIndex(), 0, 0)
self.chats.insert(channel, 0)
self.endInsertRows()
result = self.chats.len - 1
proc removeChatItemFromList*(self: ChannelsList, channel: string): int =
@ -108,7 +107,12 @@ QtObject:
proc updateChat*(self: ChannelsList, channel: ChatItem) =
let idx = self.upsertChannel(channel)
self.chats[idx] = channel
let topLeft = self.createIndex(idx, 0, nil)
let bottomRight = self.createIndex(idx, 0, nil)
self.dataChanged(topLeft, bottomRight, @[ChannelsRoles.Timestamp.int, ChannelsRoles.LastMessage.int, ChannelsRoles.UnreadMessages.int])
let topLeft = self.createIndex(0, 0, nil)
let bottomRight = self.createIndex(self.chats.len - 1, 0, nil)
if(idx != 0): # Move last updated chat to the top of the list
self.chats.del(idx)
self.chats.insert(channel, 0)
else:
self.chats[0] = channel
self.dataChanged(topLeft, bottomRight, @[ChannelsRoles.Name.int, ChannelsRoles.LastMessage.int, ChannelsRoles.Timestamp.int, ChannelsRoles.LastMessage.int, ChannelsRoles.UnreadMessages.int, ChannelsRoles.Identicon.int, ChannelsRoles.LastMessage.int])

View File

@ -18,8 +18,21 @@ Item {
model: chatsModel.chats
delegate: Channel {}
onCountChanged: {
// If a chat is added or removed, we set the current index to the first value
if (count > 0) {
currentIndex = 0;
chatsModel.activeChannelIndex = 0;
}
}
}
Connections {
target: chatsModel.chats
onDataChanged: {
// If the current active channel receives messages and changes its position,
// refresh the currentIndex accordingly
if(chatsModel.activeChannelIndex != chatGroupsListView.currentIndex){
chatGroupsListView.currentIndex = chatsModel.activeChannelIndex
}
}
}