Can get older conversation messages

This commit is contained in:
Pierre HUBERT 2019-01-10 03:29:22 +01:00
parent 22eec97796
commit bdfbe8ba48
7 changed files with 88 additions and 1 deletions

View File

@ -34,6 +34,7 @@
*/
#define CONVERSATION_MESSAGE_MIN_LENGTH 3
#define CONVERSATION_MESSAGES_REFRESH_INTERVAL 1000
#define NUMBER_OF_OLDER_MESSAGES_TO_GET 10
/**
* Images load manager information

View File

@ -5,6 +5,14 @@ ConversationMessagesList::ConversationMessagesList()
}
int ConversationMessagesList::getOldestMessageID()
{
if(count() == 0)
return -1;
return at(0).iD();
}
int ConversationMessagesList::getLastMessageID()
{

View File

@ -16,6 +16,13 @@ class ConversationMessagesList : public QList<ConversationMessage>
public:
ConversationMessagesList();
/**
* Get and return the oldest message of the conversation
*
* @return The ID of the oldest message / -1 if none found
*/
int getOldestMessageID();
/**
* Get the ID of the newest message of
* the conversation

View File

@ -5,6 +5,7 @@
#include "conversationhelper.h"
#include "../data/apirequest.h"
#include "../utils/filesutils.h"
#include "../config.h"
ConversationHelper::ConversationHelper(QObject *parent) : QObject(parent)
{
@ -41,6 +42,18 @@ void ConversationHelper::getMessages(int conversationID, int last_message_id)
mAPIHelper->execute(request);
}
void ConversationHelper::getOlderMessages(int conversationID, int oldest_message_id)
{
APIRequest *request = new APIRequest;
request->setURI("conversations/get_older_messages");
request->addInt("conversationID", conversationID);
request->addInt("oldest_message_id", oldest_message_id);
request->addInt("limit", NUMBER_OF_OLDER_MESSAGES_TO_GET);
connect(request, &APIRequest::finished, this, &ConversationHelper::getMessagesFinished);
mAPIHelper->execute(request);
}
void ConversationHelper::sendMessageFinished(int code)
{
//Delete sender

View File

@ -40,6 +40,14 @@ public:
*/
void getMessages(int conversationID, int last_message_id = -1);
/**
* Get older messages of a conversation
*
* @param conversationID The ID of the target conversation
* @param oldest_message_id The ID of the last known message
*/
void getOlderMessages(int conversationID, int oldest_message_id);
signals:
/**

View File

@ -24,6 +24,7 @@ ConversationWidget::ConversationWidget(const Conversation &conversation, const U
//Initialize UI
QString convTitle = ConversationsListHelper::getConversationDisplayName(conversation, list);
ui->convName->setText(convTitle);
connect(ui->scrollArea->verticalScrollBar(), &QScrollBar::valueChanged, this, &ConversationWidget::messagesListScrolled);
//Initalize helpers
mConversationHelper = new ConversationHelper(this);
@ -100,6 +101,11 @@ void ConversationWidget::sendMessage()
void ConversationWidget::refreshTimeout()
{
//Check if messages are already being loaded
if(mIsLoadingMessages)
return;
mIsLoadingMessages = true;
mTimer->stop();
//Get the latest message of the conversation
@ -110,6 +116,7 @@ void ConversationWidget::getMessagesCallback(bool success, QList<ConversationMes
{
//Restart counter
mTimer->start(CONVERSATION_MESSAGES_REFRESH_INTERVAL);
mIsLoadingMessages = false;
if(!success){
QMessageBox::warning(this, tr("Error while getting messages list"), tr("Could not refresh messages list!"));
@ -117,8 +124,16 @@ void ConversationWidget::getMessagesCallback(bool success, QList<ConversationMes
}
//Stop now if the list of messages is empty
if(list.empty())
if(list.empty()){
//Check if we were loading older messages
if(mIsLoadingOlderMessages){
mGotOldestConversationMessage = true;
mIsLoadingOlderMessages = false;
}
return;
}
mMessages.append(list);
@ -141,6 +156,10 @@ void ConversationWidget::getMessagesCallback(bool success, QList<ConversationMes
//Scroll to the end of the widget if required
if(list.at(list.count()-1).iD() == mMessages.getLastMessageID())
QTimer::singleShot(1000, this, &ConversationWidget::scrollToBottom);
//Else we can check if we reached the top of the conversation
else if(list.count() < NUMBER_OF_OLDER_MESSAGES_TO_GET)
mGotOldestConversationMessage = true;
}
void ConversationWidget::scrollToBottom()
@ -162,6 +181,27 @@ void ConversationWidget::sendMessageCallback(bool success)
resetSendMessageForm();
}
void ConversationWidget::messagesListScrolled(int value)
{
//Check if the user reached the top of the conversation
if(value > 0)
return; //Nothing to be done
//Check if the conversation does not contains any message yet
// or if we already retrieved the oldest message of the conversation
if(mMessages.count() == 0 || mGotOldestConversationMessage)
return;
//Check if messsages are already being loaded
if(mIsLoadingMessages)
return;
mIsLoadingMessages = true;
mIsLoadingOlderMessages = true;
//Get older messages
mConversationHelper->getOlderMessages(mConversation.iD(), mMessages.getOldestMessageID());
}
void ConversationWidget::on_sendMessageButton_clicked()
{
sendMessage();

View File

@ -72,6 +72,13 @@ private slots:
*/
void sendMessageCallback(bool success);
/**
* Slot called when the user scroll the conversation
*
* @param value The new scroll value
*/
void messagesListScrolled(int value);
void on_sendMessageButton_clicked();
void on_messageContentInput_returnPressed();
@ -105,6 +112,9 @@ private:
Conversation mConversation;
UsersList mUsersList;
ConversationMessagesList mMessages;
bool mIsLoadingMessages = false;
bool mGotOldestConversationMessage = false;
bool mIsLoadingOlderMessages = false;
};
#endif // CONVERSATIONWIDGET_H