Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
feat: load messages from the database after date
Browse files Browse the repository at this point in the history
  • Loading branch information
TriKriSta committed Jul 22, 2019
1 parent fb2957c commit b705ac8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
24 changes: 22 additions & 2 deletions src/chatlog/chatlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,22 @@ void ChatLog::insertChatlineAtBottom(ChatLine::Ptr l)
updateTypingNotification();
}

void ChatLog::insertChatlineAtBottom(const QList<ChatLine::Ptr>& newLines)
{
if (newLines.isEmpty())
return;

for (ChatLine::Ptr l : newLines) {
l->setRow(lines.size());
l->addToScene(scene);
l->visibilityChanged(false);
lines.append(l);
}

layout(lines.last()->getRow(), lines.size(), useableWidth());
startResizeWorker();
}

void ChatLog::insertChatlineOnTop(ChatLine::Ptr l)
{
if (!l.get())
Expand Down Expand Up @@ -718,8 +734,12 @@ void ChatLog::checkVisibility(bool causedByScroll)
emit firstVisibleLineChanged(visibleLines.at(0));
}

if (causedByScroll && lowerBound != lines.cend() && lowerBound->get()->row == 0) {
emit loadHistoryLower();
if (causedByScroll) {
if (lowerBound != lines.cend() && lowerBound->get()->row == 0) {
emit loadHistoryLower();
} else if (upperBound != lines.cend() && upperBound->get()->row >= lines.size() - 10) {
emit loadHistoryUpper();
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/chatlog/chatlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class ChatLog : public QGraphicsView
virtual ~ChatLog();

void insertChatlineAtBottom(ChatLine::Ptr l);
void insertChatlineAtBottom(const QList<ChatLine::Ptr>& newLines);
void insertChatlineOnTop(ChatLine::Ptr l);
void insertChatlinesOnTop(const QList<ChatLine::Ptr>& newLines);
void clearSelection();
Expand Down Expand Up @@ -73,6 +74,7 @@ class ChatLog : public QGraphicsView
void workerTimeoutFinished();
void firstVisibleLineChanged(const ChatLine::Ptr&);
void loadHistoryLower();
void loadHistoryUpper();

public slots:
void forceRelayout();
Expand Down
18 changes: 17 additions & 1 deletion src/widget/form/genericchatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ GenericChatForm::GenericChatForm(const Contact* contact, IChatLog& chatLog,
&GenericChatForm::onChatContextMenuRequested);
connect(chatWidget, &ChatLog::firstVisibleLineChanged, this, &GenericChatForm::updateShowDateInfo);
connect(chatWidget, &ChatLog::loadHistoryLower, this, &GenericChatForm::loadHistoryLower);
connect(chatWidget, &ChatLog::loadHistoryUpper, this, &GenericChatForm::loadHistoryUpper);

connect(searchForm, &SearchForm::searchInBegin, this, &GenericChatForm::searchInBegin);
connect(searchForm, &SearchForm::searchUp, this, &GenericChatForm::onSearchUp);
Expand Down Expand Up @@ -812,7 +813,10 @@ void GenericChatForm::onLoadHistory()
if (dlg.exec()) {
QDateTime time = dlg.getFromDate();
auto idx = firstItemAfterDate(dlg.getFromDate().date(), chatLog);
renderMessages(idx, chatLog.getNextIdx());
auto end = ChatLogIdx(idx.get() + 100);
chatWidget->clear();
messages.clear();
renderMessages(idx, end);
}
}

Expand Down Expand Up @@ -993,6 +997,18 @@ void GenericChatForm::loadHistoryLower()
renderMessages(begin, chatLog.getNextIdx());
}

void GenericChatForm::loadHistoryUpper()
{
auto begin = messages.end()->first;

int add = 100;
if (begin.get() + 100 > chatLog.getNextIdx().get()) {
add = chatLog.getNextIdx().get() - (begin.get() + 100);
}
auto end = ChatLogIdx(begin.get() + add);
renderMessages(begin, end);
}

void GenericChatForm::updateShowDateInfo(const ChatLine::Ptr& line)
{
const auto date = getTime(line);
Expand Down
1 change: 1 addition & 0 deletions src/widget/form/genericchatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ protected slots:
std::function<void(void)> onCompletion = std::function<void(void)>());

void loadHistoryLower();
void loadHistoryUpper();

private:
void retranslateUi();
Expand Down

0 comments on commit b705ac8

Please sign in to comment.