Automatically fetch new messages.

This commit is contained in:
Pierre HUBERT 2018-12-17 20:24:24 +01:00
parent 41cd02dfec
commit 6e7645f17b
6 changed files with 75 additions and 5 deletions

View File

@ -30,7 +30,8 @@ SOURCES += \
data/newconversationmessage.cpp \ data/newconversationmessage.cpp \
helpers/conversationhelper.cpp \ helpers/conversationhelper.cpp \
data/conversationmessage.cpp \ data/conversationmessage.cpp \
widgets/conversationmessagewidget.cpp widgets/conversationmessagewidget.cpp \
data/conversationmessageslist.cpp
HEADERS += \ HEADERS += \
helpers/accounthelper.h \ helpers/accounthelper.h \
@ -62,7 +63,8 @@ HEADERS += \
data/newconversationmessage.h \ data/newconversationmessage.h \
helpers/conversationhelper.h \ helpers/conversationhelper.h \
data/conversationmessage.h \ data/conversationmessage.h \
widgets/conversationmessagewidget.h widgets/conversationmessagewidget.h \
data/conversationmessageslist.h
FORMS += \ FORMS += \
widgets/loginwidget.ui \ widgets/loginwidget.ui \

View File

@ -33,5 +33,6 @@
* Conversations information * Conversations information
*/ */
#define CONVERSATION_MESSAGE_MIN_LENGTH 3 #define CONVERSATION_MESSAGE_MIN_LENGTH 3
#define CONVERSATION_MESSAGES_REFRESH_INTERVAL 1000
#endif // CONFIG_H #endif // CONFIG_H

View File

@ -0,0 +1,16 @@
#include "conversationmessageslist.h"
ConversationMessagesList::ConversationMessagesList()
{
}
int ConversationMessagesList::getLastMessageID()
{
//Return -1 by default if there is not any message in the conversation
if(count() == 0)
return -1;
return at(count() -1).iD();
}

View File

@ -0,0 +1,28 @@
/**
* Conversations messages list container
*
* @author Pierre HUBERT
*/
#ifndef CONVERSATIONMESSAGESLIST_H
#define CONVERSATIONMESSAGESLIST_H
#include <QList>
#include "conversationmessage.h"
class ConversationMessagesList : public QList<ConversationMessage>
{
public:
ConversationMessagesList();
/**
* Get the ID of the oldest message of
* the conversation
*
* @return The ID of the message / -1 in case of failure
*/
int getLastMessageID();
};
#endif // CONVERSATIONMESSAGESLIST_H

View File

@ -1,5 +1,6 @@
#include <QMessageBox> #include <QMessageBox>
#include <algorithm> #include <algorithm>
#include <QTimer>
#include "conversationwidget.h" #include "conversationwidget.h"
#include "ui_conversationwidget.h" #include "ui_conversationwidget.h"
@ -27,13 +28,16 @@ ConversationWidget::ConversationWidget(const Conversation &conversation, const U
connect(mConversationHelper, &ConversationHelper::sendMessageCallback, this, &ConversationWidget::sendMessageCallback); connect(mConversationHelper, &ConversationHelper::sendMessageCallback, this, &ConversationWidget::sendMessageCallback);
connect(mConversationHelper, &ConversationHelper::getMessagesCallback, this, &ConversationWidget::getMessagesCallback); connect(mConversationHelper, &ConversationHelper::getMessagesCallback, this, &ConversationWidget::getMessagesCallback);
//Get the latest message of the conversation //Initialize refresh timeout
mConversationHelper->getMessages(conversation.iD()); mTimer = new QTimer(this);
connect(mTimer, &QTimer::timeout, this, &ConversationWidget::refreshTimeout);
emit refreshTimeout();
} }
ConversationWidget::~ConversationWidget() ConversationWidget::~ConversationWidget()
{ {
delete ui; delete ui;
delete mTimer;
} }
void ConversationWidget::sendMessage() void ConversationWidget::sendMessage()
@ -63,8 +67,19 @@ void ConversationWidget::sendMessage()
mConversationHelper->sendMessage(newMessage); mConversationHelper->sendMessage(newMessage);
} }
void ConversationWidget::refreshTimeout()
{
mTimer->stop();
//Get the latest message of the conversation
mConversationHelper->getMessages(mConversation.iD(), mMessages.getLastMessageID());
}
void ConversationWidget::getMessagesCallback(bool success, QList<ConversationMessage> list) void ConversationWidget::getMessagesCallback(bool success, QList<ConversationMessage> list)
{ {
//Restart counter
mTimer->start(CONVERSATION_MESSAGES_REFRESH_INTERVAL);
if(!success){ if(!success){
QMessageBox::warning(this, tr("Error while getting messages list"), tr("Could not refresh messages list!")); QMessageBox::warning(this, tr("Error while getting messages list"), tr("Could not refresh messages list!"));
return; return;

View File

@ -14,6 +14,7 @@
#include "../data/conversation.h" #include "../data/conversation.h"
#include "../data/userslist.h" #include "../data/userslist.h"
#include "../data/conversationmessage.h" #include "../data/conversationmessage.h"
#include "../data/conversationmessageslist.h"
namespace Ui { namespace Ui {
class ConversationWidget; class ConversationWidget;
@ -40,6 +41,12 @@ public slots:
private slots: private slots:
/**
* This slot is called at regular interval in order to
* update regulary the list of messages
*/
void refreshTimeout();
/** /**
* Method called once we have got the list of messages * Method called once we have got the list of messages
* *
@ -72,10 +79,11 @@ private:
//Private fields //Private fields
Ui::ConversationWidget *ui; Ui::ConversationWidget *ui;
QTimer *mTimer;
ConversationHelper *mConversationHelper; ConversationHelper *mConversationHelper;
Conversation mConversation; Conversation mConversation;
UsersList mUsersList; UsersList mUsersList;
QList<ConversationMessage> mMessages; ConversationMessagesList mMessages;
}; };
#endif // CONVERSATIONWIDGET_H #endif // CONVERSATIONWIDGET_H