From 5026e7485bb7efb78999becf194f3428e2e6666d Mon Sep 17 00:00:00 2001 From: Mikhail Barashkov Date: Fri, 21 Jun 2024 16:25:45 +0300 Subject: [PATCH] Save power by adding delay between network requests in background. Also improve app initial load by delaying sticker set loading by 0.9 seconds --- qml/pages/OverviewPage.qml | 3 ++- src/tdlibreceiver.cpp | 11 +++++++++++ src/tdlibreceiver.h | 2 ++ src/tdlibwrapper.cpp | 7 ++++++- src/tdlibwrapper.h | 1 + 5 files changed, 22 insertions(+), 2 deletions(-) diff --git a/qml/pages/OverviewPage.qml b/qml/pages/OverviewPage.qml index bc057c94..22752642 100644 --- a/qml/pages/OverviewPage.qml +++ b/qml/pages/OverviewPage.qml @@ -88,7 +88,8 @@ Page { } Timer { id: updateSecondaryContentTimer - interval: 600 + interval: 1500 + repeat: false onTriggered: { chatListModel.calculateUnreadState(); tdLibWrapper.getRecentStickers(); diff --git a/src/tdlibreceiver.cpp b/src/tdlibreceiver.cpp index 44034c73..fc6d7bdc 100644 --- a/src/tdlibreceiver.cpp +++ b/src/tdlibreceiver.cpp @@ -81,6 +81,8 @@ namespace { const QString TYPE_ANIMATED_EMOJI("animatedEmoji"); const QString TYPE_INPUT_MESSAGE_REPLY_TO_MESSAGE("inputMessageReplyToMessage"); const QString TYPE_DRAFT_MESSAGE("draftMessage"); + + const double POWERSAVING_TDLIB_REQUEST_INTERVAL = 100; } static QString getChatPositionOrder(const QVariantMap &position) @@ -191,9 +193,15 @@ void TDLibReceiver::setActive(bool active) } else { LOG("Deactivating receiver loop, this may take a while..."); } + this->powerSavingMode = false; this->isActive = active; } +void TDLibReceiver::setPowerSavingMode(bool powerSavingMode) +{ + this->powerSavingMode = powerSavingMode; +} + void TDLibReceiver::receiverLoop() { LOG("Starting receiver loop"); @@ -205,6 +213,9 @@ void TDLibReceiver::receiverLoop() VERBOSE("Raw result:" << receivedJsonDocument.toJson(QJsonDocument::Indented).constData()); processReceivedDocument(receivedJsonDocument); } + if(this->powerSavingMode) { + msleep(POWERSAVING_TDLIB_REQUEST_INTERVAL); + } } LOG("Stopping receiver loop"); } diff --git a/src/tdlibreceiver.h b/src/tdlibreceiver.h index 33771b6d..b2354618 100644 --- a/src/tdlibreceiver.h +++ b/src/tdlibreceiver.h @@ -35,6 +35,7 @@ class TDLibReceiver : public QThread public: explicit TDLibReceiver(void *tdLibClient, QObject *parent = nullptr); void setActive(bool active); + void setPowerSavingMode(bool active); signals: void versionDetected(const QString &version); @@ -115,6 +116,7 @@ class TDLibReceiver : public QThread QHash handlers; void *tdLibClient; bool isActive; + bool powerSavingMode; private: static const QVariantList cleanupList(const QVariantList& list, bool *updated = Q_NULLPTR); diff --git a/src/tdlibwrapper.cpp b/src/tdlibwrapper.cpp index 70dcf650..bfd8a9b3 100644 --- a/src/tdlibwrapper.cpp +++ b/src/tdlibwrapper.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -102,7 +103,7 @@ TDLibWrapper::TDLibWrapper(AppSettings *settings, MceInterface *mce, QObject *pa connect(this->appSettings, SIGNAL(useOpenWithChanged()), this, SLOT(handleOpenWithChanged())); connect(this->appSettings, SIGNAL(storageOptimizerChanged()), this, SLOT(handleStorageOptimizerChanged())); - + connect(qGuiApp, SIGNAL(applicationStateChanged(Qt::ApplicationState)), this, SLOT(handleApplicationStateChanged(Qt::ApplicationState))); connect(networkConfigurationManager, SIGNAL(configurationChanged(QNetworkConfiguration)), this, SLOT(handleNetworkConfigurationChanged(QNetworkConfiguration))); this->setLogVerbosityLevel(); @@ -2204,6 +2205,10 @@ void TDLibWrapper::handleGetPageSourceFinished() } } +void TDLibWrapper::handleApplicationStateChanged(Qt::ApplicationState state) { + this->tdLibReceiver->setPowerSavingMode(state != Qt::ApplicationState::ApplicationActive); +} + QVariantMap& TDLibWrapper::fillTdlibParameters(QVariantMap& parameters) { parameters.insert("api_id", TDLIB_API_ID); diff --git a/src/tdlibwrapper.h b/src/tdlibwrapper.h index 2487ae5e..665f6767 100644 --- a/src/tdlibwrapper.h +++ b/src/tdlibwrapper.h @@ -373,6 +373,7 @@ public slots: void handleNetworkConfigurationChanged(const QNetworkConfiguration &config); void handleActiveEmojiReactionsUpdated(const QStringList& emojis); void handleGetPageSourceFinished(); + void handleApplicationStateChanged(Qt::ApplicationState state); private: void setOption(const QString &name, const QString &type, const QVariant &value);