diff --git a/src/app/chat/view.nim b/src/app/chat/view.nim index ceea4c2263..c41c351f6f 100644 --- a/src/app/chat/view.nim +++ b/src/app/chat/view.nim @@ -1,4 +1,4 @@ -import NimQml, Tables, json, sequtils, chronicles, times, re, sugar, strutils, os, strformat +import NimQml, Tables, json, sequtils, chronicles, times, re, sugar, strutils, os, strformat, algorithm import ../../status/status import ../../status/mailservers import ../../status/libstatus/chat as libstatus_chat @@ -24,6 +24,11 @@ import ../utils/image_utils logScope: topics = "chats-view" + +type + ChatViewRoles {.pure.} = enum + MessageList = UserRole + 1 + QtObject: type ChatsView* = ref object of QAbstractListModel @@ -31,7 +36,7 @@ QtObject: chats*: ChannelsList currentSuggestions*: SuggestionsList callResult: string - messageList*: Table[string, ChatMessageList] + messageList*: OrderedTable[string, ChatMessageList] reactions*: ReactionView stickers*: StickersView groups*: GroupsView @@ -67,7 +72,7 @@ QtObject: self.stickers.delete self.groups.delete self.transactions.delete - self.messageList = initTable[string, ChatMessageList]() + self.messageList = initOrderedTable[string, ChatMessageList]() self.channelOpenTime = initTable[string, int64]() self.QAbstractListModel.delete @@ -81,7 +86,7 @@ QtObject: result.activeCommunity = newCommunityItemView(status) result.observedCommunity = newCommunityItemView(status) result.currentSuggestions = newSuggestionsList() - result.messageList = initTable[string, ChatMessageList]() + result.messageList = initOrderedTable[string, ChatMessageList]() result.reactions = newReactionView(status, result.messageList.addr, result.activeChannel) result.stickers = newStickersView(status, result.activeChannel) result.groups = newGroupsView(status,result.activeChannel) @@ -312,8 +317,10 @@ QtObject: if self.status.chat.channels.hasKey(channel): chat = self.status.chat.channels[channel] if not self.messageList.hasKey(channel): + self.beginInsertRows(newQModelIndex(), self.messageList.len, self.messageList.len) self.messageList[channel] = newChatMessageList(channel, self.status, not chat.isNil and chat.chatType != ChatType.Profile) self.channelOpenTime[channel] = now().toTime.toUnix * 1000 + self.endInsertRows(); proc messagePushed*(self: ChatsView, messageIndex: int) {.signal.} proc newMessagePushed*(self: ChatsView) {.signal.} @@ -857,3 +864,25 @@ QtObject: except Exception as e: error "Error removing user from the community", msg = e.msg + + method rowCount*(self: ChatsView, index: QModelIndex = nil): int = + result = self.messageList.len + + method data(self: ChatsView, index: QModelIndex, role: int): QVariant = + if not index.isValid: + return + if index.row < 0 or index.row >= self.messageList.len: + return + return newQVariant(toSeq(self.messageList.values)[index.row]) + + method roleNames(self: ChatsView): Table[int, string] = + { + ChatViewRoles.MessageList.int:"messages" + }.toTable + + proc getMessageListIndex(self: ChatsView):int {.slot.} = + var idx = -1 + for msg in toSeq(self.messageList.values): + idx = idx + 1 + if(self.activeChannel.id == msg.id): return idx + return idx diff --git a/src/app/chat/views/message_list.nim b/src/app/chat/views/message_list.nim index 4c001efb04..efa5a79019 100644 --- a/src/app/chat/views/message_list.nim +++ b/src/app/chat/views/message_list.nim @@ -42,7 +42,7 @@ QtObject: ChatMessageList* = ref object of QAbstractListModel messages*: seq[Message] status: Status - id: string + id*: string messageIndex: Table[string, int] messageReactions*: Table[string, string] timedoutMessages: HashSet[string] @@ -281,3 +281,7 @@ QtObject: m.localName = c.localNickname self.dataChanged(topLeft, bottomRight, @[ChatMessageRoles.Username.int]) + + + proc getID*(self: ChatMessageList):string {.slot.} = + self.id \ No newline at end of file diff --git a/src/app/chat/views/reactions.nim b/src/app/chat/views/reactions.nim index e9fadd4b0a..2ff8e8222b 100644 --- a/src/app/chat/views/reactions.nim +++ b/src/app/chat/views/reactions.nim @@ -10,7 +10,7 @@ logScope: QtObject: type ReactionView* = ref object of QObject - messageList: ptr Table[string, ChatMessageList] + messageList: ptr OrderedTable[string, ChatMessageList] activeChannel: ChatItemView status: Status pubKey*: string @@ -21,7 +21,7 @@ QtObject: proc delete*(self: ReactionView) = self.QObject.delete - proc newReactionView*(status: Status, messageList: ptr Table[string, ChatMessageList], activeChannel: ChatItemView): ReactionView = + proc newReactionView*(status: Status, messageList: ptr OrderedTable[string, ChatMessageList], activeChannel: ChatItemView): ReactionView = new(result, delete) result = ReactionView() result.messageList = messageList diff --git a/ui/app/AppLayouts/Chat/ChatColumn.qml b/ui/app/AppLayouts/Chat/ChatColumn.qml index 4563546d46..83d939cff4 100644 --- a/ui/app/AppLayouts/Chat/ChatColumn.qml +++ b/ui/app/AppLayouts/Chat/ChatColumn.qml @@ -12,7 +12,6 @@ import "../Wallet" StackLayout { id: chatColumnLayout - property alias chatMessages: chatMessages property int chatGroupsListViewCount: 0 @@ -189,10 +188,26 @@ StackLayout { Layout.fillHeight: true Layout.alignment: Qt.AlignLeft | Qt.AlignTop spacing: 0 - ChatMessages { - id: chatMessages - messageList: chatsModel.messageList + + Connections { + target: chatsModel + onActiveChannelChanged: { + stackLayoutChatMessages.currentIndex = chatsModel.getMessageListIndex(chatsModel.activeChannelIndex) + } } + + StackLayout { + id: stackLayoutChatMessages + Repeater { + model: chatsModel + ChatMessages { + id: chatMessages + messageList: model.messages + } + } + } + + } StatusImageModal { diff --git a/ui/app/AppLayouts/Chat/ChatColumn/ChatMessages.qml b/ui/app/AppLayouts/Chat/ChatColumn/ChatMessages.qml index 88d82b0bc9..4e529a111a 100644 --- a/ui/app/AppLayouts/Chat/ChatColumn/ChatMessages.qml +++ b/ui/app/AppLayouts/Chat/ChatColumn/ChatMessages.qml @@ -137,10 +137,6 @@ ScrollView { loadingMessages = false; } - onActiveChannelChanged: { - Qt.callLater(chatLogView.scrollToBottom.bind(this, true)) - } - onSendingMessage: { chatLogView.scrollToBottom(true) } diff --git a/ui/app/AppMain.qml b/ui/app/AppMain.qml index ba67a4370b..c6a44888fe 100644 --- a/ui/app/AppMain.qml +++ b/ui/app/AppMain.qml @@ -423,10 +423,6 @@ RowLayout { timelineLayoutContainer.active = this.children[currentIndex] == timelineLayoutContainer - if(this.children[currentIndex] === chatLayoutContainer){ - chatLayoutContainer.chatColumn.chatMessages.chatLogView.scrollToBottom(true); - } - if(this.children[currentIndex] === walletLayoutContainer){ walletLayoutContainer.showSigningPhrasePopup(); }