chatgui: add scroll-to-bottom floating button, incremental message refresh via getMessagesSince
This commit is contained in:
1 parent
b6d44bd947
commit
ce9e58a925
6 files changed
+114
-16
No files matched your search
@@ -250,8 +250,39 @@ void MessageList::loadChannel(const QString& channelId) {
|
||||
}
|
||||
|
||||
void MessageList::refresh() {
|
||||
if (!m_currentChannelId.isEmpty())
|
||||
loadChannel(m_currentChannelId);
|
||||
if (m_currentChannelId.isEmpty() || !m_db || !m_db->isOpen()) return;
|
||||
|
||||
qint64 maxId = 0;
|
||||
for (int r = 0; r < m_model->rowCount(); r++)
|
||||
if (m_model->item(r)->data(MsgIdRole).toLongLong() > maxId)
|
||||
maxId = m_model->item(r)->data(MsgIdRole).toLongLong();
|
||||
|
||||
auto newMsgs = m_db->getMessagesSince(m_currentChannelId, maxId);
|
||||
if (newMsgs.isEmpty()) return;
|
||||
|
||||
QScrollBar* sb = m_view->verticalScrollBar();
|
||||
bool wasAtBottom = sb && (sb->value() >= sb->maximum());
|
||||
|
||||
for (const auto& m : newMsgs) {
|
||||
AccountRow acc = m_db->getAccount(m.authorNodeId);
|
||||
QString author = m_db->getDisplayName(m.authorNodeId);
|
||||
QString letter = acc.avatarLetter.isEmpty() ? author.mid(0, 1) : acc.avatarLetter;
|
||||
QColor color(acc.avatarColor);
|
||||
if (!m_avatarCache.contains(m.authorNodeId))
|
||||
m_avatarCache[m.authorNodeId] = color;
|
||||
auto avatar = makeAvatar(color, letter);
|
||||
auto time = QDateTime::fromMSecsSinceEpoch(m.timestamp).toString("HH:mm");
|
||||
auto *item = new QStandardItem();
|
||||
setMsg(item, author, QString::fromUtf8(m.data), time, avatar, false,
|
||||
false, QString(), QString(), QVariantList{});
|
||||
item->setData(m.id, MsgIdRole);
|
||||
m_model->appendRow(item);
|
||||
while (m_model->rowCount() > 100)
|
||||
m_model->removeRow(0);
|
||||
}
|
||||
|
||||
if (wasAtBottom) m_view->scrollToBottom();
|
||||
m_readDebounce->start();
|
||||
}
|
||||
|
||||
void MessageList::addMessage(const QString& channelId, quint64 authorNodeId,
|
||||
|
||||
Reference in new issue
Block a user