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 \
helpers/conversationhelper.cpp \
data/conversationmessage.cpp \
widgets/conversationmessagewidget.cpp
widgets/conversationmessagewidget.cpp \
data/conversationmessageslist.cpp
HEADERS += \
helpers/accounthelper.h \
@ -62,7 +63,8 @@ HEADERS += \
data/newconversationmessage.h \
helpers/conversationhelper.h \
data/conversationmessage.h \
widgets/conversationmessagewidget.h
widgets/conversationmessagewidget.h \
data/conversationmessageslist.h
FORMS += \
widgets/loginwidget.ui \

View File

@ -33,5 +33,6 @@
* Conversations information
*/
#define CONVERSATION_MESSAGE_MIN_LENGTH 3
#define CONVERSATION_MESSAGES_REFRESH_INTERVAL 1000
#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 <algorithm>
#include <QTimer>
#include "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::getMessagesCallback, this, &ConversationWidget::getMessagesCallback);
//Get the latest message of the conversation
mConversationHelper->getMessages(conversation.iD());
//Initialize refresh timeout
mTimer = new QTimer(this);
connect(mTimer, &QTimer::timeout, this, &ConversationWidget::refreshTimeout);
emit refreshTimeout();
}
ConversationWidget::~ConversationWidget()
{
delete ui;
delete mTimer;
}
void ConversationWidget::sendMessage()
@ -63,8 +67,19 @@ void ConversationWidget::sendMessage()
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)
{
//Restart counter
mTimer->start(CONVERSATION_MESSAGES_REFRESH_INTERVAL);
if(!success){
QMessageBox::warning(this, tr("Error while getting messages list"), tr("Could not refresh messages list!"));
return;

View File

@ -14,6 +14,7 @@
#include "../data/conversation.h"
#include "../data/userslist.h"
#include "../data/conversationmessage.h"
#include "../data/conversationmessageslist.h"
namespace Ui {
class ConversationWidget;
@ -40,6 +41,12 @@ public 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
*
@ -72,10 +79,11 @@ private:
//Private fields
Ui::ConversationWidget *ui;
QTimer *mTimer;
ConversationHelper *mConversationHelper;
Conversation mConversation;
UsersList mUsersList;
QList<ConversationMessage> mMessages;
ConversationMessagesList mMessages;
};
#endif // CONVERSATIONWIDGET_H