From 38284bb2af611c7929dc2e8cf668b7615a6b8bf7 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 13 Jun 2020 14:14:01 +0200 Subject: [PATCH] Automatically get the number of new notifications --- ComunicWatcher.pro | 4 ++++ notificationshelper.cpp | 30 ++++++++++++++++++++++++++++++ notificationshelper.h | 33 +++++++++++++++++++++++++++++++++ notificationsnumber.cpp | 26 ++++++++++++++++++++++++++ notificationsnumber.h | 25 +++++++++++++++++++++++++ refreshservice.cpp | 7 +++++++ refreshservice.h | 7 ++++++- wsclient.cpp | 2 ++ wsclient.h | 1 + 9 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 notificationshelper.cpp create mode 100644 notificationshelper.h create mode 100644 notificationsnumber.cpp create mode 100644 notificationsnumber.h diff --git a/ComunicWatcher.pro b/ComunicWatcher.pro index 35d7924..538f972 100644 --- a/ComunicWatcher.pro +++ b/ComunicWatcher.pro @@ -20,6 +20,8 @@ SOURCES += \ loginsuccessfuldialog.cpp \ main.cpp \ loginwindow.cpp \ + notificationshelper.cpp \ + notificationsnumber.cpp \ refreshservice.cpp \ wsclient.cpp @@ -30,6 +32,8 @@ HEADERS += \ config.h \ loginsuccessfuldialog.h \ loginwindow.h \ + notificationshelper.h \ + notificationsnumber.h \ refreshservice.h \ wsclient.h diff --git a/notificationshelper.cpp b/notificationshelper.cpp new file mode 100644 index 0000000..3c41bf8 --- /dev/null +++ b/notificationshelper.cpp @@ -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); +} diff --git a/notificationshelper.h b/notificationshelper.h new file mode 100644 index 0000000..f1f6d44 --- /dev/null +++ b/notificationshelper.h @@ -0,0 +1,33 @@ +/** + * Notifications helper + * + * @author Pierre Hubert + */ + +#pragma once + +#include + +#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); +}; + diff --git a/notificationsnumber.cpp b/notificationsnumber.cpp new file mode 100644 index 0000000..f8b92f1 --- /dev/null +++ b/notificationsnumber.cpp @@ -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; +} diff --git a/notificationsnumber.h b/notificationsnumber.h new file mode 100644 index 0000000..38f9f87 --- /dev/null +++ b/notificationsnumber.h @@ -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; +}; + diff --git a/refreshservice.cpp b/refreshservice.cpp index cb5c393..376163a 100644 --- a/refreshservice.cpp +++ b/refreshservice.cpp @@ -18,9 +18,16 @@ void RefreshService::stopService() svc = nullptr; } +void RefreshService::connectedToWebSocket() +{ + mNotifsHelper.getNewNotificationsNumbers(); +} + RefreshService::RefreshService() { qDebug("Start refresh service"); + + connect(&mWsClient, &WsClient::connected, this, &RefreshService::connectedToWebSocket); } RefreshService::~RefreshService() diff --git a/refreshservice.h b/refreshservice.h index aef201f..0d56692 100644 --- a/refreshservice.h +++ b/refreshservice.h @@ -6,6 +6,7 @@ #pragma once +#include "notificationshelper.h" #include "wsclient.h" #include @@ -18,11 +19,15 @@ public: static void startService(); static void stopService(); +private slots: + void connectedToWebSocket(); + private: RefreshService(); ~RefreshService(); // Class members static RefreshService *svc; - WsClient client; + WsClient mWsClient; + NotificationsHelper mNotifsHelper; }; diff --git a/wsclient.cpp b/wsclient.cpp index 51e93fb..8d545bc 100644 --- a/wsclient.cpp +++ b/wsclient.cpp @@ -46,6 +46,8 @@ void WsClient::getTokenCallBack(APIResponse res) void WsClient::onConnected() { qDebug() << "Connected to WebSocket"; + + emit connected(); } void WsClient::onMessage(const QString &msg) diff --git a/wsclient.h b/wsclient.h index 2845a70..eaf0487 100644 --- a/wsclient.h +++ b/wsclient.h @@ -22,6 +22,7 @@ public slots: void startConnect(); signals: + void connected(); void newNumberNotifs(int num); void newNumberConvs(int num);