Merge pull request #40 from monich/chatlist-roles
Expose more roles from ChatListModel
This commit is contained in:
commit
b62078e380
3 changed files with 189 additions and 77 deletions
|
@ -231,8 +231,8 @@ Page {
|
||||||
pageStack.push(Qt.resolvedUrl("../pages/ChatPage.qml"), { "chatInformation" : display });
|
pageStack.push(Qt.resolvedUrl("../pages/ChatPage.qml"), { "chatInformation" : display });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
showMenuOnPressAndHold: chat_id != overviewPage.ownUserId
|
||||||
menu: ContextMenu {
|
menu: ContextMenu {
|
||||||
visible: display.id !== overviewPage.ownUserId
|
|
||||||
MenuItem {
|
MenuItem {
|
||||||
onClicked: {
|
onClicked: {
|
||||||
var newNotificationSettings = display.notification_settings;
|
var newNotificationSettings = display.notification_settings;
|
||||||
|
@ -241,28 +241,12 @@ Page {
|
||||||
} else {
|
} else {
|
||||||
newNotificationSettings.mute_for = 6666666;
|
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")
|
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 {
|
Column {
|
||||||
id: chatListColumn
|
id: chatListColumn
|
||||||
width: parent.width - ( 2 * Theme.horizontalPageMargin )
|
width: parent.width - ( 2 * Theme.horizontalPageMargin )
|
||||||
|
@ -291,7 +275,7 @@ Page {
|
||||||
|
|
||||||
ProfileThumbnail {
|
ProfileThumbnail {
|
||||||
id: chatListPictureThumbnail
|
id: chatListPictureThumbnail
|
||||||
photoData: (typeof display.photo !== "undefined") ? display.photo.small : ""
|
photoData: photo_small
|
||||||
replacementStringHint: chatListNameText.text
|
replacementStringHint: chatListNameText.text
|
||||||
width: parent.width
|
width: parent.width
|
||||||
height: parent.width
|
height: parent.width
|
||||||
|
@ -306,7 +290,7 @@ Page {
|
||||||
anchors.right: parent.right
|
anchors.right: parent.right
|
||||||
anchors.bottom: parent.bottom
|
anchors.bottom: parent.bottom
|
||||||
radius: parent.width / 2
|
radius: parent.width / 2
|
||||||
visible: display.unread_count > 0
|
visible: unread_count > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
Text {
|
Text {
|
||||||
|
@ -316,7 +300,7 @@ Page {
|
||||||
color: Theme.primaryColor
|
color: Theme.primaryColor
|
||||||
anchors.centerIn: chatUnreadMessagesCountBackground
|
anchors.centerIn: chatUnreadMessagesCountBackground
|
||||||
visible: chatUnreadMessagesCountBackground.visible
|
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 {
|
Text {
|
||||||
id: chatListNameText
|
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
|
textFormat: Text.StyledText
|
||||||
font.pixelSize: Theme.fontSizeMedium
|
font.pixelSize: Theme.fontSizeMedium
|
||||||
color: Theme.primaryColor
|
color: Theme.primaryColor
|
||||||
|
@ -349,7 +333,7 @@ Page {
|
||||||
spacing: Theme.paddingSmall
|
spacing: Theme.paddingSmall
|
||||||
Text {
|
Text {
|
||||||
id: chatListLastUserText
|
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
|
font.pixelSize: Theme.fontSizeExtraSmall
|
||||||
color: Theme.highlightColor
|
color: Theme.highlightColor
|
||||||
textFormat: Text.StyledText
|
textFormat: Text.StyledText
|
||||||
|
@ -363,7 +347,7 @@ Page {
|
||||||
}
|
}
|
||||||
Text {
|
Text {
|
||||||
id: chatListLastMessageText
|
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
|
font.pixelSize: Theme.fontSizeExtraSmall
|
||||||
color: Theme.primaryColor
|
color: Theme.primaryColor
|
||||||
width: parent.width - Theme.paddingMedium - chatListLastUserText.width
|
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 {
|
Text {
|
||||||
id: messageContactTimeElapsedText
|
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
|
font.pixelSize: Theme.fontSizeTiny
|
||||||
color: Theme.secondaryColor
|
color: Theme.secondaryColor
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,29 +18,61 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "chatlistmodel.h"
|
#include "chatlistmodel.h"
|
||||||
#include <QListIterator>
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
#define LOG(x) qDebug() << "[ChatListModel]" << x
|
#define LOG(x) qDebug() << "[ChatListModel]" << x
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
const QString ID("id");
|
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 ORDER("order");
|
||||||
const QString CHAT_ID("chat_id");
|
const QString CHAT_ID("chat_id");
|
||||||
|
const QString CONTENT("content");
|
||||||
const QString LAST_MESSAGE("last_message");
|
const QString LAST_MESSAGE("last_message");
|
||||||
|
const QString SENDER_USER_ID("sender_user_id");
|
||||||
const QString UNREAD_COUNT("unread_count");
|
const QString UNREAD_COUNT("unread_count");
|
||||||
const QString NOTIFICATION_SETTINGS("notification_settings");
|
const QString NOTIFICATION_SETTINGS("notification_settings");
|
||||||
const QString LAST_READ_INBOX_MESSAGE_ID("last_read_inbox_message_id");
|
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 LAST_READ_OUTBOX_MESSAGE_ID("last_read_outbox_message_id");
|
||||||
|
|
||||||
|
const QString TYPE("@type");
|
||||||
|
const QString TYPE_MESSAGE_TEXT("messageText");
|
||||||
}
|
}
|
||||||
|
|
||||||
class ChatListModel::ChatData
|
class ChatListModel::ChatData
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
enum Role {
|
||||||
|
RoleDisplay = Qt::DisplayRole,
|
||||||
|
RoleChatId,
|
||||||
|
RoleTitle,
|
||||||
|
RolePhotoSmall,
|
||||||
|
RoleUnreadCount,
|
||||||
|
RoleLastReadInboxMessageId,
|
||||||
|
RoleLastMessageSenderId,
|
||||||
|
RoleLastMessageDate,
|
||||||
|
RoleLastMessageText
|
||||||
|
};
|
||||||
|
|
||||||
ChatData(const QVariantMap &data);
|
ChatData(const QVariantMap &data);
|
||||||
|
|
||||||
int compareTo(const ChatData *chat) const;
|
int compareTo(const ChatData *chat) const;
|
||||||
bool setOrder(const QString &order);
|
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:
|
public:
|
||||||
QVariantMap chatData;
|
QVariantMap chatData;
|
||||||
|
@ -75,6 +107,83 @@ bool ChatListModel::ChatData::setOrder(const QString &newOrder)
|
||||||
return false;
|
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)
|
ChatListModel::ChatListModel(TDLibWrapper *tdLibWrapper)
|
||||||
{
|
{
|
||||||
this->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(chatReadOutboxUpdated(QString, QString)), this, SLOT(handleChatReadOutboxUpdated(QString, QString)));
|
||||||
connect(tdLibWrapper, SIGNAL(messageSendSucceeded(QString, QString, QVariantMap)), this, SLOT(handleMessageSendSucceeded(QString, QString, QVariantMap)));
|
connect(tdLibWrapper, SIGNAL(messageSendSucceeded(QString, QString, QVariantMap)), this, SLOT(handleMessageSendSucceeded(QString, QString, QVariantMap)));
|
||||||
connect(tdLibWrapper, SIGNAL(chatNotificationSettingsUpdated(QString, QVariantMap)), this, SLOT(handleChatNotificationSettingsUpdated(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()
|
ChatListModel::~ChatListModel()
|
||||||
|
@ -93,6 +208,21 @@ ChatListModel::~ChatListModel()
|
||||||
qDeleteAll(chatList);
|
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
|
int ChatListModel::rowCount(const QModelIndex &) const
|
||||||
{
|
{
|
||||||
return chatList.size();
|
return chatList.size();
|
||||||
|
@ -101,8 +231,19 @@ int ChatListModel::rowCount(const QModelIndex &) const
|
||||||
QVariant ChatListModel::data(const QModelIndex &index, int role) const
|
QVariant ChatListModel::data(const QModelIndex &index, int role) const
|
||||||
{
|
{
|
||||||
const int row = index.row();
|
const int row = index.row();
|
||||||
if (row >= 0 && row < chatList.size() && role == Qt::DisplayRole) {
|
if (row >= 0 && row < chatList.size()) {
|
||||||
return chatList.at(row)->chatData;
|
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();
|
return QVariant();
|
||||||
}
|
}
|
||||||
|
@ -115,7 +256,7 @@ void ChatListModel::redrawModel()
|
||||||
|
|
||||||
int ChatListModel::updateChatOrder(int chatIndex)
|
int ChatListModel::updateChatOrder(int chatIndex)
|
||||||
{
|
{
|
||||||
ChatData* chat = chatList.at(chatIndex);
|
ChatData *chat = chatList.at(chatIndex);
|
||||||
|
|
||||||
const int n = chatList.size();
|
const int n = chatList.size();
|
||||||
int newIndex = chatIndex;
|
int newIndex = chatIndex;
|
||||||
|
@ -155,7 +296,7 @@ int ChatListModel::updateChatOrder(int chatIndex)
|
||||||
|
|
||||||
void ChatListModel::handleChatDiscovered(const QString &chatId, const QVariantMap &chatToBeAdded)
|
void ChatListModel::handleChatDiscovered(const QString &chatId, const QVariantMap &chatToBeAdded)
|
||||||
{
|
{
|
||||||
ChatData* chat = new ChatData(chatToBeAdded);
|
ChatData *chat = new ChatData(chatToBeAdded);
|
||||||
const int n = chatList.size();
|
const int n = chatList.size();
|
||||||
int chatIndex;
|
int chatIndex;
|
||||||
for (chatIndex = 0; chatIndex < n && chat->compareTo(chatList.at(chatIndex)) >= 0; 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);
|
chatIndexMap.insert(chatList.at(i)->chatId, i);
|
||||||
}
|
}
|
||||||
endInsertRows();
|
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)
|
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)) {
|
if (chatIndexMap.contains(chatId)) {
|
||||||
int chatIndex = chatIndexMap.value(chatId);
|
int chatIndex = chatIndexMap.value(chatId);
|
||||||
LOG("Updating last message for chat" << chatId <<" at index" << chatIndex << "new order" << order);
|
LOG("Updating last message for chat" << chatId <<" at index" << chatIndex << "new order" << order);
|
||||||
ChatData* chat = chatList.at(chatIndex);
|
ChatData *chat = chatList.at(chatIndex);
|
||||||
chat->chatData.insert(LAST_MESSAGE, lastMessage);
|
|
||||||
if (chat->setOrder(order)) {
|
if (chat->setOrder(order)) {
|
||||||
chatIndex = updateChatOrder(chatIndex);
|
chatIndex = updateChatOrder(chatIndex);
|
||||||
}
|
}
|
||||||
const QModelIndex modelIndex(index(chatIndex));
|
const QModelIndex modelIndex(index(chatIndex));
|
||||||
emit dataChanged(modelIndex, modelIndex);
|
emit dataChanged(modelIndex, modelIndex, chat->updateLastMessage(lastMessage));
|
||||||
emit chatChanged(chatId);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -192,25 +336,27 @@ void ChatListModel::handleChatOrderUpdated(const QString &chatId, const QString
|
||||||
LOG("Updating chat order of" << chatId << "to" << order);
|
LOG("Updating chat order of" << chatId << "to" << order);
|
||||||
int chatIndex = chatIndexMap.value(chatId);
|
int chatIndex = chatIndexMap.value(chatId);
|
||||||
if (chatList.at(chatIndex)->setOrder(order)) {
|
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)) {
|
if (chatIndexMap.contains(chatId)) {
|
||||||
LOG("Updating chat unread count for" << chatId << "unread messages" << unreadCount << ", last read message ID: " << lastReadInboxMessageId);
|
LOG("Updating chat unread count for" << chatId << "unread messages" << unreadCount << ", last read message ID: " << lastReadInboxMessageId);
|
||||||
const int chatIndex = chatIndexMap.value(chatId);
|
const int chatIndex = chatIndexMap.value(chatId);
|
||||||
ChatData* chat = chatList.at(chatIndex);
|
ChatData *chat = chatList.at(chatIndex);
|
||||||
chat->chatData.insert(UNREAD_COUNT, unreadCount);
|
QVector<int> changedRoles;
|
||||||
chat->chatData.insert(LAST_READ_INBOX_MESSAGE_ID, lastReadInboxMessageId);
|
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));
|
const QModelIndex modelIndex(index(chatIndex));
|
||||||
emit dataChanged(modelIndex, modelIndex);
|
emit dataChanged(modelIndex, modelIndex, changedRoles);
|
||||||
emit chatChanged(chatId);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -219,11 +365,10 @@ void ChatListModel::handleChatReadOutboxUpdated(const QString &chatId, const QSt
|
||||||
if (chatIndexMap.contains(chatId)) {
|
if (chatIndexMap.contains(chatId)) {
|
||||||
LOG("Updating last read message for" << chatId << "last ID" << lastReadOutboxMessageId);
|
LOG("Updating last read message for" << chatId << "last ID" << lastReadOutboxMessageId);
|
||||||
const int chatIndex = chatIndexMap.value(chatId);
|
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);
|
chat->chatData.insert(LAST_READ_OUTBOX_MESSAGE_ID, lastReadOutboxMessageId);
|
||||||
const QModelIndex modelIndex(index(chatIndex));
|
const QModelIndex modelIndex(index(chatIndex));
|
||||||
emit dataChanged(modelIndex, modelIndex);
|
emit dataChanged(modelIndex, modelIndex);
|
||||||
emit chatChanged(chatId);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -233,11 +378,8 @@ void ChatListModel::handleMessageSendSucceeded(const QString &messageId, const Q
|
||||||
if (chatIndexMap.contains(chatId)) {
|
if (chatIndexMap.contains(chatId)) {
|
||||||
const int chatIndex = chatIndexMap.value(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);
|
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));
|
const QModelIndex modelIndex(index(chatIndex));
|
||||||
emit dataChanged(modelIndex, modelIndex);
|
emit dataChanged(modelIndex, modelIndex, chatList.at(chatIndex)->updateLastMessage(message));
|
||||||
emit chatChanged(chatId);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -246,10 +388,17 @@ void ChatListModel::handleChatNotificationSettingsUpdated(const QString &chatId,
|
||||||
if (chatIndexMap.contains(chatId)) {
|
if (chatIndexMap.contains(chatId)) {
|
||||||
const int chatIndex = chatIndexMap.value(chatId);
|
const int chatIndex = chatIndexMap.value(chatId);
|
||||||
LOG("Updating notification settings for chat" << chatId << "at index" << chatIndex);
|
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);
|
chat->chatData.insert(NOTIFICATION_SETTINGS, chatNotificationSettings);
|
||||||
const QModelIndex modelIndex(index(chatIndex));
|
const QModelIndex modelIndex(index(chatIndex));
|
||||||
emit dataChanged(modelIndex, modelIndex);
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -21,8 +21,6 @@
|
||||||
#define CHATLISTMODEL_H
|
#define CHATLISTMODEL_H
|
||||||
|
|
||||||
#include <QAbstractListModel>
|
#include <QAbstractListModel>
|
||||||
#include <QDebug>
|
|
||||||
#include <QMutex>
|
|
||||||
#include "tdlibwrapper.h"
|
#include "tdlibwrapper.h"
|
||||||
|
|
||||||
class ChatListModel : public QAbstractListModel
|
class ChatListModel : public QAbstractListModel
|
||||||
|
@ -32,22 +30,21 @@ public:
|
||||||
ChatListModel(TDLibWrapper *tdLibWrapper);
|
ChatListModel(TDLibWrapper *tdLibWrapper);
|
||||||
~ChatListModel() override;
|
~ChatListModel() override;
|
||||||
|
|
||||||
|
virtual QHash<int,QByteArray> roleNames() const override;
|
||||||
virtual int rowCount(const QModelIndex&) const override;
|
virtual int rowCount(const QModelIndex&) const override;
|
||||||
virtual QVariant data(const QModelIndex &index, int role) const override;
|
virtual QVariant data(const QModelIndex &index, int role) const override;
|
||||||
|
|
||||||
Q_INVOKABLE void redrawModel();
|
Q_INVOKABLE void redrawModel();
|
||||||
|
|
||||||
signals:
|
|
||||||
void chatChanged(const QString &chatId);
|
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void handleChatDiscovered(const QString &chatId, const QVariantMap &chatInformation);
|
void handleChatDiscovered(const QString &chatId, const QVariantMap &chatInformation);
|
||||||
void handleChatLastMessageUpdated(const QString &chatId, const QString &order, const QVariantMap &lastMessage);
|
void handleChatLastMessageUpdated(const QString &chatId, const QString &order, const QVariantMap &lastMessage);
|
||||||
void handleChatOrderUpdated(const QString &chatId, const QString &order);
|
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 handleChatReadOutboxUpdated(const QString &chatId, const QString &lastReadOutboxMessageId);
|
||||||
void handleMessageSendSucceeded(const QString &messageId, const QString &oldMessageId, const QVariantMap &message);
|
void handleMessageSendSucceeded(const QString &messageId, const QString &oldMessageId, const QVariantMap &message);
|
||||||
void handleChatNotificationSettingsUpdated(const QString &chatId, const QVariantMap &chatNotificationSettings);
|
void handleChatNotificationSettingsUpdated(const QString &chatId, const QVariantMap &chatNotificationSettings);
|
||||||
|
void handleRelativeTimeRefreshTimer();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int updateChatOrder(int chatIndex);
|
int updateChatOrder(int chatIndex);
|
||||||
|
@ -56,6 +53,7 @@ private:
|
||||||
class ChatData;
|
class ChatData;
|
||||||
|
|
||||||
TDLibWrapper *tdLibWrapper;
|
TDLibWrapper *tdLibWrapper;
|
||||||
|
QTimer *relativeTimeRefreshTimer;
|
||||||
QList<ChatData*> chatList;
|
QList<ChatData*> chatList;
|
||||||
QHash<QString,int> chatIndexMap;
|
QHash<QString,int> chatIndexMap;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue