Sort messages by clock

This commit is contained in:
Richard Ramos 2020-06-04 15:07:07 -04:00 committed by Iuri Matias
parent f8271c0c38
commit 722ab36bf1
6 changed files with 76 additions and 10 deletions

View File

@ -12,7 +12,7 @@ type
ContentType = UserRole + 7 ContentType = UserRole + 7
Sticker = UserRole + 8 Sticker = UserRole + 8
FromAuthor = UserRole + 9 FromAuthor = UserRole + 9
Clock = UserRole + 10
QtObject: QtObject:
type type
ChatMessageList* = ref object of QAbstractListModel ChatMessageList* = ref object of QAbstractListModel
@ -47,6 +47,7 @@ QtObject:
of ChatMessageRoles.UserName: result = newQVariant(message.userName) of ChatMessageRoles.UserName: result = newQVariant(message.userName)
of ChatMessageRoles.Message: result = newQVariant(message.message) of ChatMessageRoles.Message: result = newQVariant(message.message)
of ChatMessageRoles.Timestamp: result = newQVariant(message.timestamp) of ChatMessageRoles.Timestamp: result = newQVariant(message.timestamp)
of ChatMessageRoles.Clock: result = newQVariant($message.clock)
of ChatMessageRoles.Identicon: result = newQVariant(message.identicon) of ChatMessageRoles.Identicon: result = newQVariant(message.identicon)
of ChatMessageRoles.IsCurrentUser: result = newQVariant(message.isCurrentUser) of ChatMessageRoles.IsCurrentUser: result = newQVariant(message.isCurrentUser)
of ChatMessageRoles.RepeatMessageInfo: result = newQVariant(repeatMessageInfo) of ChatMessageRoles.RepeatMessageInfo: result = newQVariant(repeatMessageInfo)
@ -59,6 +60,7 @@ QtObject:
ChatMessageRoles.UserName.int:"userName", ChatMessageRoles.UserName.int:"userName",
ChatMessageRoles.Message.int:"message", ChatMessageRoles.Message.int:"message",
ChatMessageRoles.Timestamp.int:"timestamp", ChatMessageRoles.Timestamp.int:"timestamp",
ChatMessageRoles.Clock.int:"clock",
ChatMessageRoles.Identicon.int:"identicon", ChatMessageRoles.Identicon.int:"identicon",
ChatMessageRoles.IsCurrentUser.int:"isCurrentUser", ChatMessageRoles.IsCurrentUser.int:"isCurrentUser",
ChatMessageRoles.RepeatMessageInfo.int:"repeatMessageInfo", ChatMessageRoles.RepeatMessageInfo.int:"repeatMessageInfo",

View File

@ -37,7 +37,7 @@ proc toMessage*(jsonMsg: JsonNode): Message =
result = Message( result = Message(
alias: jsonMsg{"alias"}.getStr, alias: jsonMsg{"alias"}.getStr,
chatId: jsonMsg{"chatId"}.getStr, chatId: jsonMsg{"chatId"}.getStr,
clock: $jsonMsg{"clock"}.getInt, clock: jsonMsg{"clock"}.getInt,
contentType: jsonMsg{"contentType"}.getInt, contentType: jsonMsg{"contentType"}.getInt,
ensName: jsonMsg{"ensName"}.getStr, ensName: jsonMsg{"ensName"}.getStr,
fromAuthor: jsonMsg{"from"}.getStr, fromAuthor: jsonMsg{"from"}.getStr,

View File

@ -20,7 +20,7 @@ type WalletSignal* = ref object of Signal
type Message* = object type Message* = object
alias*: string alias*: string
chatId*: string chatId*: string
clock*: string clock*: int
# commandParameters*: # ??? # commandParameters*: # ???
contentType*: int # ??? contentType*: int # ???
ensName*: string # ??? ensName*: string # ???

View File

@ -7,6 +7,7 @@ type ChatMessage* = ref object
message*: string message*: string
fromAuthor*: string fromAuthor*: string
timestamp*: string timestamp*: string
clock*: int
identicon*: string identicon*: string
isCurrentUser*: bool isCurrentUser*: bool
contentType*: int contentType*: int
@ -20,6 +21,7 @@ proc newChatMessage*(): ChatMessage =
result.userName = "" result.userName = ""
result.message = "" result.message = ""
result.fromAuthor = "" result.fromAuthor = ""
result.clock = 0
result.timestamp = "0" result.timestamp = "0"
result.identicon = "" result.identicon = ""
result.isCurrentUser = false result.isCurrentUser = false
@ -31,6 +33,7 @@ proc toChatMessage*(payload: JsonNode): ChatMessage =
userName: payload["alias"].str, userName: payload["alias"].str,
message: payload["text"].str, message: payload["text"].str,
timestamp: $payload["timestamp"], timestamp: $payload["timestamp"],
clock: payload["clock"].getInt,
identicon: payload["identicon"].str, identicon: payload["identicon"].str,
isCurrentUser: false, isCurrentUser: false,
contentType: payload["contentType"].getInt, contentType: payload["contentType"].getInt,
@ -40,6 +43,7 @@ proc toChatMessage*(payload: JsonNode): ChatMessage =
proc toChatMessage*(message: Message): ChatMessage = proc toChatMessage*(message: Message): ChatMessage =
result = ChatMessage( result = ChatMessage(
userName: message.alias, userName: message.alias,
clock: message.clock,
fromAuthor: message.fromAuthor, fromAuthor: message.fromAuthor,
message: message.text, message: message.text,
timestamp: message.timestamp, timestamp: message.timestamp,

View File

@ -17,13 +17,12 @@ ScrollView {
ScrollBar.vertical.policy: chatLogView.contentHeight > chatLogView.height ? ScrollBar.AlwaysOn : ScrollBar.AlwaysOff ScrollBar.vertical.policy: chatLogView.contentHeight > chatLogView.height ? ScrollBar.AlwaysOn : ScrollBar.AlwaysOff
ScrollBar.horizontal.policy: ScrollBar.AlwaysOff ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
ListView { SortFilterModel {
anchors.fill: parent id: messageListDelegate
spacing: 4 lessThan: function(left, right) {
id: chatLogView return left.clock < right.clock;
}
model: messageList model: messageList
Layout.fillWidth: true
Layout.fillHeight: true
delegate: Message { delegate: Message {
userName: model.userName userName: model.userName
message: model.message message: model.message
@ -34,8 +33,16 @@ ScrollView {
sticker: model.sticker sticker: model.sticker
contentType: model.contentType contentType: model.contentType
} }
highlightFollowsCurrentItem: true }
ListView {
anchors.fill: parent
spacing: 4
id: chatLogView
model: messageListDelegate
Layout.fillWidth: true
Layout.fillHeight: true
highlightFollowsCurrentItem: true
onCountChanged: { onCountChanged: {
if (!this.atYEnd) { if (!this.atYEnd) {
// User has scrolled up, we don't want to scroll back // User has scrolled up, we don't want to scroll back

View File

@ -0,0 +1,53 @@
import QtQuick 2.9
import QtQml.Models 2.3
// https://martin.rpdev.net/2019/01/15/using-delegatemodel-in-qml-for-sorting-and-filtering.html
DelegateModel {
id: delegateModel
property var lessThan: function(left, right) { return true; }
property var filterAcceptsItem: function(item) { return true; }
function update() {
if (items.count > 0) {
items.setGroups(0, items.count, "items");
}
// Step 1: Filter items
var visible = [];
for (var i = 0; i < items.count; ++i) {
var item = items.get(i);
if (filterAcceptsItem(item.model)) {
visible.push(item);
}
}
// Step 2: Sort the list of visible items
visible.sort(function(a, b) {
return lessThan(a.model, b.model) ? -1 : 1;
});
// Step 3: Add all items to the visible group:
for (i = 0; i < visible.length; ++i) {
item = visible[i];
item.inVisible = true;
if (item.visibleIndex !== i) {
visibleItems.move(item.visibleIndex, i, 1);
}
}
}
items.onChanged: update()
onLessThanChanged: update()
onFilterAcceptsItemChanged: update()
groups: DelegateModelGroup {
id: visibleItems
name: "visible"
includeByDefault: false
}
filterOnGroup: "visible"
}