Merge pull request #40 from monich/chatlist-roles

Expose more roles from ChatListModel
This commit is contained in:
Sebastian Wolf 2020-10-03 20:50:00 +02:00 committed by GitHub
commit b62078e380
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 189 additions and 77 deletions

View file

@ -231,8 +231,8 @@ Page {
pageStack.push(Qt.resolvedUrl("../pages/ChatPage.qml"), { "chatInformation" : display });
}
showMenuOnPressAndHold: chat_id != overviewPage.ownUserId
menu: ContextMenu {
visible: display.id !== overviewPage.ownUserId
MenuItem {
onClicked: {
var newNotificationSettings = display.notification_settings;
@ -241,28 +241,12 @@ Page {
} else {
newNotificationSettings.mute_for = 6666666;
}
tdLibWrapper.setChatNotificationSettings(display.id, newNotificationSettings);
tdLibWrapper.setChatNotificationSettings(chat_id, newNotificationSettings);
}
text: display.notification_settings.mute_for > 0 ? qsTr("Unmute Chat") : qsTr("Mute Chat")
}
}
Connections {
target: chatListModel
onChatChanged: {
if (overviewPage.chatListCreated) {
// Force update of all list item elements. dataChanged() doesn't seem to trigger them all :(
chatListPictureThumbnail.photoData = (typeof display.photo !== "undefined") ? display.photo.small : "";
chatUnreadMessagesCountBackground.visible = display.unread_count > 0;
chatUnreadMessagesCount.text = display.unread_count > 99 ? "99+" : display.unread_count;
chatListNameText.text = display.title !== "" ? Emoji.emojify(display.title, Theme.fontSizeMedium) + ( display.notification_settings.mute_for > 0 ? Emoji.emojify(" 🔇", Theme.fontSizeMedium) : "" ) : qsTr("Unknown");
chatListLastUserText.text = (typeof display.last_message !== "undefined") ? ( display.last_message.sender_user_id !== overviewPage.ownUserId ? Emoji.emojify(Functions.getUserName(tdLibWrapper.getUserInformation(display.last_message.sender_user_id)), Theme.fontSizeExtraSmall) : qsTr("You") ) : qsTr("Unknown");
chatListLastMessageText.text = (typeof display.last_message !== "undefined") ? Emoji.emojify(Functions.getMessageText(display.last_message, true, display.last_message.sender_user_id === overviewPage.ownUserId), Theme.fontSizeExtraSmall) : qsTr("Unknown");
messageContactTimeElapsedText.text = (typeof display.last_message !== "undefined") ? Functions.getDateTimeElapsed(display.last_message.date) : qsTr("Unknown");
}
}
}
Column {
id: chatListColumn
width: parent.width - ( 2 * Theme.horizontalPageMargin )
@ -291,7 +275,7 @@ Page {
ProfileThumbnail {
id: chatListPictureThumbnail
photoData: (typeof display.photo !== "undefined") ? display.photo.small : ""
photoData: photo_small
replacementStringHint: chatListNameText.text
width: parent.width
height: parent.width
@ -306,7 +290,7 @@ Page {
anchors.right: parent.right
anchors.bottom: parent.bottom
radius: parent.width / 2
visible: display.unread_count > 0
visible: unread_count > 0
}
Text {
@ -316,7 +300,7 @@ Page {
color: Theme.primaryColor
anchors.centerIn: chatUnreadMessagesCountBackground
visible: chatUnreadMessagesCountBackground.visible
text: display.unread_count > 99 ? "99+" : display.unread_count
text: unread_count > 99 ? "99+" : unread_count
}
}
}
@ -328,7 +312,7 @@ Page {
Text {
id: chatListNameText
text: display.title !== "" ? Emoji.emojify(display.title, Theme.fontSizeMedium) + ( display.notification_settings.mute_for > 0 ? Emoji.emojify(" 🔇", Theme.fontSizeMedium) : "" ) : qsTr("Unknown")
text: title ? Emoji.emojify(title, Theme.fontSizeMedium) + ( display.notification_settings.mute_for > 0 ? Emoji.emojify(" 🔇", Theme.fontSizeMedium) : "" ) : qsTr("Unknown")
textFormat: Text.StyledText
font.pixelSize: Theme.fontSizeMedium
color: Theme.primaryColor
@ -349,7 +333,7 @@ Page {
spacing: Theme.paddingSmall
Text {
id: chatListLastUserText
text: (typeof display.last_message !== "undefined") ? ( display.last_message.sender_user_id !== overviewPage.ownUserId ? Emoji.emojify(Functions.getUserName(tdLibWrapper.getUserInformation(display.last_message.sender_user_id)), font.pixelSize) : qsTr("You") ) : qsTr("Unknown")
text: last_message_sender_id ? ( last_message_sender_id !== overviewPage.ownUserId ? Emoji.emojify(Functions.getUserName(tdLibWrapper.getUserInformation(last_message_sender_id)), font.pixelSize) : qsTr("You") ) : qsTr("Unknown")
font.pixelSize: Theme.fontSizeExtraSmall
color: Theme.highlightColor
textFormat: Text.StyledText
@ -363,7 +347,7 @@ Page {
}
Text {
id: chatListLastMessageText
text: (typeof display.last_message !== "undefined") ? Emoji.emojify(Functions.getMessageText(display.last_message, true, display.last_message.sender_user_id === overviewPage.ownUserId), Theme.fontSizeExtraSmall) : qsTr("Unknown")
text: last_message_text ? Emoji.emojify(last_message_text, Theme.fontSizeExtraSmall) : qsTr("Unknown")
font.pixelSize: Theme.fontSizeExtraSmall
color: Theme.primaryColor
width: parent.width - Theme.paddingMedium - chatListLastUserText.width
@ -379,28 +363,9 @@ Page {
}
}
Timer {
id: messageContactTimeUpdater
interval: 60000
running: true
repeat: true
onTriggered: {
if (typeof display.last_message !== "undefined") {
messageContactTimeElapsedText.text = Functions.getDateTimeElapsed(display.last_message.date);
// Force update of all list item elements. dataChanged() doesn't seem to trigger them all :(
chatListPictureThumbnail.photoData = (typeof display.photo !== "undefined") ? display.photo.small : "";
chatUnreadMessagesCountBackground.visible = display.unread_count > 0;
chatUnreadMessagesCount.text = display.unread_count > 99 ? "99+" : display.unread_count;
chatListNameText.text = display.title !== "" ? Emoji.emojify(display.title, Theme.fontSizeMedium) + ( display.notification_settings.mute_for > 0 ? Emoji.emojify(" 🔇", Theme.fontSizeMedium) : "" ) : qsTr("Unknown");
chatListLastUserText.text = (typeof display.last_message !== "undefined") ? ( display.last_message.sender_user_id !== overviewPage.ownUserId ? Emoji.emojify(Functions.getUserName(tdLibWrapper.getUserInformation(display.last_message.sender_user_id)), Theme.fontSizeExtraSmall) : qsTr("You") ) : qsTr("Unknown");
chatListLastMessageText.text = (typeof display.last_message !== "undefined") ? Emoji.emojify(Functions.getMessageText(display.last_message, true, display.last_message.sender_user_id === overviewPage.ownUserId), Theme.fontSizeExtraSmall) : qsTr("Unknown");
}
}
}
Text {
id: messageContactTimeElapsedText
text: (typeof display.last_message !== "undefined") ? Functions.getDateTimeElapsed(display.last_message.date) : qsTr("Unknown")
text: last_message_date ? Functions.getDateTimeElapsed(last_message_date) : qsTr("Unknown")
font.pixelSize: Theme.fontSizeTiny
color: Theme.secondaryColor
}

View file

@ -18,29 +18,61 @@
*/
#include "chatlistmodel.h"
#include <QListIterator>
#include <QDebug>
#define LOG(x) qDebug() << "[ChatListModel]" << x
namespace {
const QString ID("id");
const QString DATE("date");
const QString TEXT("text");
const QString TITLE("title");
const QString PHOTO("photo");
const QString SMALL("small");
const QString ORDER("order");
const QString CHAT_ID("chat_id");
const QString CONTENT("content");
const QString LAST_MESSAGE("last_message");
const QString SENDER_USER_ID("sender_user_id");
const QString UNREAD_COUNT("unread_count");
const QString NOTIFICATION_SETTINGS("notification_settings");
const QString LAST_READ_INBOX_MESSAGE_ID("last_read_inbox_message_id");
const QString LAST_READ_OUTBOX_MESSAGE_ID("last_read_outbox_message_id");
const QString TYPE("@type");
const QString TYPE_MESSAGE_TEXT("messageText");
}
class ChatListModel::ChatData
{
public:
enum Role {
RoleDisplay = Qt::DisplayRole,
RoleChatId,
RoleTitle,
RolePhotoSmall,
RoleUnreadCount,
RoleLastReadInboxMessageId,
RoleLastMessageSenderId,
RoleLastMessageDate,
RoleLastMessageText
};
ChatData(const QVariantMap &data);
int compareTo(const ChatData *chat) const;
bool setOrder(const QString &order);
const QVariant lastMessage(const QString &key) const;
QString title() const;
int unreadCount() const;
QVariant photoSmall() const;
qlonglong lastReadInboxMessageId() const;
qlonglong senderUserId() const;
qlonglong senderMessageDate() const;
QString senderMessageText() const;
bool updateUnreadCount(int unreadCount);
bool updateLastReadInboxMessageId(qlonglong messageId);
QVector<int> updateLastMessage(const QVariantMap &message);
public:
QVariantMap chatData;
@ -75,6 +107,83 @@ bool ChatListModel::ChatData::setOrder(const QString &newOrder)
return false;
}
inline const QVariant ChatListModel::ChatData::lastMessage(const QString &key) const
{
return chatData.value(LAST_MESSAGE).toMap().value(key);
}
QString ChatListModel::ChatData::title() const
{
return chatData.value(TITLE).toString();
}
int ChatListModel::ChatData::unreadCount() const
{
return chatData.value(UNREAD_COUNT).toInt();
}
QVariant ChatListModel::ChatData::photoSmall() const
{
return chatData.value(PHOTO).toMap().value(SMALL);
}
qlonglong ChatListModel::ChatData::lastReadInboxMessageId() const
{
return chatData.value(LAST_READ_INBOX_MESSAGE_ID).toLongLong();
}
qlonglong ChatListModel::ChatData::senderUserId() const
{
return lastMessage(SENDER_USER_ID).toLongLong();
}
qlonglong ChatListModel::ChatData::senderMessageDate() const
{
return lastMessage(DATE).toLongLong();
}
QString ChatListModel::ChatData::senderMessageText() const
{
const QVariantMap content(lastMessage(CONTENT).toMap());
return (content.value(TYPE).toString() == TYPE_MESSAGE_TEXT) ? content.value(TEXT).toMap().value(TEXT).toString() : QString();
}
bool ChatListModel::ChatData::updateUnreadCount(int count)
{
const int prevUnreadCount(unreadCount());
chatData.insert(UNREAD_COUNT, count);
return prevUnreadCount != unreadCount();
}
bool ChatListModel::ChatData::updateLastReadInboxMessageId(qlonglong messageId)
{
const qlonglong prevLastReadInboxMessageId(lastReadInboxMessageId());
chatData.insert(LAST_READ_INBOX_MESSAGE_ID, messageId);
return prevLastReadInboxMessageId != lastReadInboxMessageId();
}
QVector<int> ChatListModel::ChatData::updateLastMessage(const QVariantMap &message)
{
const qlonglong prevSenderUserId(senderUserId());
const qlonglong prevSenderMessageDate(senderMessageDate());
const QString prevSenderMessageText(senderMessageText());
chatData.insert(LAST_MESSAGE, message);
QVector<int> changedRoles;
changedRoles.append(RoleDisplay);
if (prevSenderUserId != senderUserId()) {
changedRoles.append(RoleLastMessageSenderId);
}
if (prevSenderMessageDate != senderMessageDate()) {
changedRoles.append(RoleLastMessageDate);
}
if (prevSenderMessageText != senderMessageText()) {
changedRoles.append(RoleLastMessageText);
}
return changedRoles;
}
ChatListModel::ChatListModel(TDLibWrapper *tdLibWrapper)
{
this->tdLibWrapper = tdLibWrapper;
@ -85,6 +194,12 @@ ChatListModel::ChatListModel(TDLibWrapper *tdLibWrapper)
connect(tdLibWrapper, SIGNAL(chatReadOutboxUpdated(QString, QString)), this, SLOT(handleChatReadOutboxUpdated(QString, QString)));
connect(tdLibWrapper, SIGNAL(messageSendSucceeded(QString, QString, QVariantMap)), this, SLOT(handleMessageSendSucceeded(QString, QString, QVariantMap)));
connect(tdLibWrapper, SIGNAL(chatNotificationSettingsUpdated(QString, QVariantMap)), this, SLOT(handleChatNotificationSettingsUpdated(QString, QVariantMap)));
// Don't start the timer until we have at least one chat
relativeTimeRefreshTimer = new QTimer(this);
relativeTimeRefreshTimer->setSingleShot(false);
relativeTimeRefreshTimer->setInterval(30000);
connect(relativeTimeRefreshTimer, SIGNAL(timeout()), SLOT(handleRelativeTimeRefreshTimer()));
}
ChatListModel::~ChatListModel()
@ -93,6 +208,21 @@ ChatListModel::~ChatListModel()
qDeleteAll(chatList);
}
QHash<int,QByteArray> ChatListModel::roleNames() const
{
QHash<int,QByteArray> roles;
roles.insert(ChatData::RoleDisplay, "display");
roles.insert(ChatData::RoleChatId, "chat_id");
roles.insert(ChatData::RoleTitle, "title");
roles.insert(ChatData::RolePhotoSmall, "photo_small");
roles.insert(ChatData::RoleUnreadCount, "unread_count");
roles.insert(ChatData::RoleLastReadInboxMessageId, "last_read_inbox_message_id");
roles.insert(ChatData::RoleLastMessageSenderId, "last_message_sender_id");
roles.insert(ChatData::RoleLastMessageDate, "last_message_date");
roles.insert(ChatData::RoleLastMessageText, "last_message_text");
return roles;
}
int ChatListModel::rowCount(const QModelIndex &) const
{
return chatList.size();
@ -101,8 +231,19 @@ int ChatListModel::rowCount(const QModelIndex &) const
QVariant ChatListModel::data(const QModelIndex &index, int role) const
{
const int row = index.row();
if (row >= 0 && row < chatList.size() && role == Qt::DisplayRole) {
return chatList.at(row)->chatData;
if (row >= 0 && row < chatList.size()) {
const ChatData *data = chatList.at(row);
switch ((ChatData::Role)role) {
case ChatData::RoleDisplay: return data->chatData;
case ChatData::RoleChatId: return data->chatId;
case ChatData::RoleTitle: return data->title();
case ChatData::RolePhotoSmall: return data->photoSmall();
case ChatData::RoleUnreadCount: return data->unreadCount();
case ChatData::RoleLastReadInboxMessageId: return data->lastReadInboxMessageId();
case ChatData::RoleLastMessageSenderId: return data->senderUserId();
case ChatData::RoleLastMessageText: return data->senderMessageText();
case ChatData::RoleLastMessageDate: return data->senderMessageDate();
}
}
return QVariant();
}
@ -115,7 +256,7 @@ void ChatListModel::redrawModel()
int ChatListModel::updateChatOrder(int chatIndex)
{
ChatData* chat = chatList.at(chatIndex);
ChatData *chat = chatList.at(chatIndex);
const int n = chatList.size();
int newIndex = chatIndex;
@ -155,7 +296,7 @@ int ChatListModel::updateChatOrder(int chatIndex)
void ChatListModel::handleChatDiscovered(const QString &chatId, const QVariantMap &chatToBeAdded)
{
ChatData* chat = new ChatData(chatToBeAdded);
ChatData *chat = new ChatData(chatToBeAdded);
const int n = chatList.size();
int chatIndex;
for (chatIndex = 0; chatIndex < n && chat->compareTo(chatList.at(chatIndex)) >= 0; chatIndex++);
@ -168,6 +309,11 @@ void ChatListModel::handleChatDiscovered(const QString &chatId, const QVariantMa
chatIndexMap.insert(chatList.at(i)->chatId, i);
}
endInsertRows();
// Start timestamp refresh timer when the first chat is discovered
if (!relativeTimeRefreshTimer->isActive()) {
relativeTimeRefreshTimer->start();
}
}
void ChatListModel::handleChatLastMessageUpdated(const QString &chatId, const QString &order, const QVariantMap &lastMessage)
@ -175,14 +321,12 @@ void ChatListModel::handleChatLastMessageUpdated(const QString &chatId, const QS
if (chatIndexMap.contains(chatId)) {
int chatIndex = chatIndexMap.value(chatId);
LOG("Updating last message for chat" << chatId <<" at index" << chatIndex << "new order" << order);
ChatData* chat = chatList.at(chatIndex);
chat->chatData.insert(LAST_MESSAGE, lastMessage);
ChatData *chat = chatList.at(chatIndex);
if (chat->setOrder(order)) {
chatIndex = updateChatOrder(chatIndex);
}
const QModelIndex modelIndex(index(chatIndex));
emit dataChanged(modelIndex, modelIndex);
emit chatChanged(chatId);
emit dataChanged(modelIndex, modelIndex, chat->updateLastMessage(lastMessage));
}
}
@ -192,25 +336,27 @@ void ChatListModel::handleChatOrderUpdated(const QString &chatId, const QString
LOG("Updating chat order of" << chatId << "to" << order);
int chatIndex = chatIndexMap.value(chatId);
if (chatList.at(chatIndex)->setOrder(order)) {
chatIndex = updateChatOrder(chatIndex);
updateChatOrder(chatIndex);
}
const QModelIndex modelIndex(index(chatIndex));
emit dataChanged(modelIndex, modelIndex);
emit chatChanged(chatId);
}
}
void ChatListModel::handleChatReadInboxUpdated(const QString &chatId, const QString &lastReadInboxMessageId, const int &unreadCount)
void ChatListModel::handleChatReadInboxUpdated(const QString &chatId, const QString &lastReadInboxMessageId, int unreadCount)
{
if (chatIndexMap.contains(chatId)) {
LOG("Updating chat unread count for" << chatId << "unread messages" << unreadCount << ", last read message ID: " << lastReadInboxMessageId);
const int chatIndex = chatIndexMap.value(chatId);
ChatData* chat = chatList.at(chatIndex);
chat->chatData.insert(UNREAD_COUNT, unreadCount);
chat->chatData.insert(LAST_READ_INBOX_MESSAGE_ID, lastReadInboxMessageId);
ChatData *chat = chatList.at(chatIndex);
QVector<int> changedRoles;
changedRoles.append(ChatData::RoleDisplay);
if (chat->updateUnreadCount(unreadCount)) {
changedRoles.append(ChatData::RoleUnreadCount);
}
if (chat->updateLastReadInboxMessageId(lastReadInboxMessageId.toLongLong())) {
changedRoles.append(ChatData::RoleLastReadInboxMessageId);
}
const QModelIndex modelIndex(index(chatIndex));
emit dataChanged(modelIndex, modelIndex);
emit chatChanged(chatId);
emit dataChanged(modelIndex, modelIndex, changedRoles);
}
}
@ -219,11 +365,10 @@ void ChatListModel::handleChatReadOutboxUpdated(const QString &chatId, const QSt
if (chatIndexMap.contains(chatId)) {
LOG("Updating last read message for" << chatId << "last ID" << lastReadOutboxMessageId);
const int chatIndex = chatIndexMap.value(chatId);
ChatData* chat = chatList.at(chatIndex);
ChatData *chat = chatList.at(chatIndex);
chat->chatData.insert(LAST_READ_OUTBOX_MESSAGE_ID, lastReadOutboxMessageId);
const QModelIndex modelIndex(index(chatIndex));
emit dataChanged(modelIndex, modelIndex);
emit chatChanged(chatId);
}
}
@ -233,11 +378,8 @@ void ChatListModel::handleMessageSendSucceeded(const QString &messageId, const Q
if (chatIndexMap.contains(chatId)) {
const int chatIndex = chatIndexMap.value(chatId);
LOG("Updating last message for chat" << chatId << "at index" << chatIndex << ", as message was sent, old ID:" << oldMessageId << ", new ID:" << messageId);
ChatData* chat = chatList.at(chatIndex);
chat->chatData.insert(LAST_MESSAGE, message);
const QModelIndex modelIndex(index(chatIndex));
emit dataChanged(modelIndex, modelIndex);
emit chatChanged(chatId);
emit dataChanged(modelIndex, modelIndex, chatList.at(chatIndex)->updateLastMessage(message));
}
}
@ -246,10 +388,17 @@ void ChatListModel::handleChatNotificationSettingsUpdated(const QString &chatId,
if (chatIndexMap.contains(chatId)) {
const int chatIndex = chatIndexMap.value(chatId);
LOG("Updating notification settings for chat" << chatId << "at index" << chatIndex);
ChatData* chat = chatList.at(chatIndex);
ChatData *chat = chatList.at(chatIndex);
chat->chatData.insert(NOTIFICATION_SETTINGS, chatNotificationSettings);
const QModelIndex modelIndex(index(chatIndex));
emit dataChanged(modelIndex, modelIndex);
emit chatChanged(chatId);
}
}
void ChatListModel::handleRelativeTimeRefreshTimer()
{
LOG("Refreshing timestamps");
QVector<int> roles;
roles.append(ChatData::RoleLastMessageDate);
emit dataChanged(index(0), index(chatList.size() - 1), roles);
}

View file

@ -21,8 +21,6 @@
#define CHATLISTMODEL_H
#include <QAbstractListModel>
#include <QDebug>
#include <QMutex>
#include "tdlibwrapper.h"
class ChatListModel : public QAbstractListModel
@ -32,22 +30,21 @@ public:
ChatListModel(TDLibWrapper *tdLibWrapper);
~ChatListModel() override;
virtual QHash<int,QByteArray> roleNames() const override;
virtual int rowCount(const QModelIndex&) const override;
virtual QVariant data(const QModelIndex &index, int role) const override;
Q_INVOKABLE void redrawModel();
signals:
void chatChanged(const QString &chatId);
private slots:
void handleChatDiscovered(const QString &chatId, const QVariantMap &chatInformation);
void handleChatLastMessageUpdated(const QString &chatId, const QString &order, const QVariantMap &lastMessage);
void handleChatOrderUpdated(const QString &chatId, const QString &order);
void handleChatReadInboxUpdated(const QString &chatId, const QString &lastReadInboxMessageId, const int &unreadCount);
void handleChatReadInboxUpdated(const QString &chatId, const QString &lastReadInboxMessageId, int unreadCount);
void handleChatReadOutboxUpdated(const QString &chatId, const QString &lastReadOutboxMessageId);
void handleMessageSendSucceeded(const QString &messageId, const QString &oldMessageId, const QVariantMap &message);
void handleChatNotificationSettingsUpdated(const QString &chatId, const QVariantMap &chatNotificationSettings);
void handleRelativeTimeRefreshTimer();
private:
int updateChatOrder(int chatIndex);
@ -56,6 +53,7 @@ private:
class ChatData;
TDLibWrapper *tdLibWrapper;
QTimer *relativeTimeRefreshTimer;
QList<ChatData*> chatList;
QHash<QString,int> chatIndexMap;
};