Automatically get the number of new notifications

This commit is contained in:
Pierre HUBERT 2020-06-13 14:14:01 +02:00
parent 68d8d1b5c7
commit 38284bb2af
9 changed files with 134 additions and 1 deletions

View File

@ -20,6 +20,8 @@ SOURCES += \
loginsuccessfuldialog.cpp \ loginsuccessfuldialog.cpp \
main.cpp \ main.cpp \
loginwindow.cpp \ loginwindow.cpp \
notificationshelper.cpp \
notificationsnumber.cpp \
refreshservice.cpp \ refreshservice.cpp \
wsclient.cpp wsclient.cpp
@ -30,6 +32,8 @@ HEADERS += \
config.h \ config.h \
loginsuccessfuldialog.h \ loginsuccessfuldialog.h \
loginwindow.h \ loginwindow.h \
notificationshelper.h \
notificationsnumber.h \
refreshservice.h \ refreshservice.h \
wsclient.h wsclient.h

30
notificationshelper.cpp Normal file
View File

@ -0,0 +1,30 @@
#include "apirequest.h"
#include "notificationshelper.h"
NotificationsHelper::NotificationsHelper(QObject *parent) : QObject(parent)
{
}
void NotificationsHelper::getNewNotificationsNumbers()
{
APIRequest *req = new APIRequest("notifications/count_all_news");
req->exec();
connect(req, &APIRequest::done, this, &NotificationsHelper::getNotificationsNumberCallback);
}
void NotificationsHelper::getNotificationsNumberCallback(APIResponse response)
{
if(response.isError()) {
qDebug() << "Could not get the number of unread conversations!";
return;
}
auto obj = response.getObject();
NotificationsNumber n;
n.setNewNotifs(obj.value("notifications").toInt());
n.setUnreadConversations(obj.value("conversations").toInt());
emit onNewNumber(n);
}

33
notificationshelper.h Normal file
View File

@ -0,0 +1,33 @@
/**
* Notifications helper
*
* @author Pierre Hubert
*/
#pragma once
#include <QObject>
#include "notificationsnumber.h"
#include "apiresponse.h"
class NotificationsHelper : public QObject
{
Q_OBJECT
public:
explicit NotificationsHelper(QObject *parent = nullptr);
/**
* Count the number of new notifications
*/
void getNewNotificationsNumbers();
signals:
void onNewNumber(NotificationsNumber notifs);
private slots:
void getNotificationsNumberCallback(APIResponse response);
};

26
notificationsnumber.cpp Normal file
View File

@ -0,0 +1,26 @@
#include "notificationsnumber.h"
NotificationsNumber::NotificationsNumber()
{
}
int NotificationsNumber::unreadConversations() const
{
return mUnreadConversations;
}
void NotificationsNumber::setUnreadConversations(int unreadConversations)
{
mUnreadConversations = unreadConversations;
}
int NotificationsNumber::newNotifs() const
{
return mNewNotifs;
}
void NotificationsNumber::setNewNotifs(int newNotifs)
{
mNewNotifs = newNotifs;
}

25
notificationsnumber.h Normal file
View File

@ -0,0 +1,25 @@
/**
* Notifications number
*
* @author Pierre Hubert
*/
#pragma once
class NotificationsNumber
{
public:
NotificationsNumber();
int unreadConversations() const;
void setUnreadConversations(int unreadConversations);
int newNotifs() const;
void setNewNotifs(int newNotifs);
private:
int mNewNotifs;
int mUnreadConversations;
};

View File

@ -18,9 +18,16 @@ void RefreshService::stopService()
svc = nullptr; svc = nullptr;
} }
void RefreshService::connectedToWebSocket()
{
mNotifsHelper.getNewNotificationsNumbers();
}
RefreshService::RefreshService() RefreshService::RefreshService()
{ {
qDebug("Start refresh service"); qDebug("Start refresh service");
connect(&mWsClient, &WsClient::connected, this, &RefreshService::connectedToWebSocket);
} }
RefreshService::~RefreshService() RefreshService::~RefreshService()

View File

@ -6,6 +6,7 @@
#pragma once #pragma once
#include "notificationshelper.h"
#include "wsclient.h" #include "wsclient.h"
#include <QObject> #include <QObject>
@ -18,11 +19,15 @@ public:
static void startService(); static void startService();
static void stopService(); static void stopService();
private slots:
void connectedToWebSocket();
private: private:
RefreshService(); RefreshService();
~RefreshService(); ~RefreshService();
// Class members // Class members
static RefreshService *svc; static RefreshService *svc;
WsClient client; WsClient mWsClient;
NotificationsHelper mNotifsHelper;
}; };

View File

@ -46,6 +46,8 @@ void WsClient::getTokenCallBack(APIResponse res)
void WsClient::onConnected() void WsClient::onConnected()
{ {
qDebug() << "Connected to WebSocket"; qDebug() << "Connected to WebSocket";
emit connected();
} }
void WsClient::onMessage(const QString &msg) void WsClient::onMessage(const QString &msg)

View File

@ -22,6 +22,7 @@ public slots:
void startConnect(); void startConnect();
signals: signals:
void connected();
void newNumberNotifs(int num); void newNumberNotifs(int num);
void newNumberConvs(int num); void newNumberConvs(int num);