Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save power by adding delay between network requests in background. #573

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion qml/pages/OverviewPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ Page {
}
Timer {
id: updateSecondaryContentTimer
interval: 600
interval: 1500
repeat: false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need for this, repeat defaults to false

Copy link
Contributor Author

@mbarashkov mbarashkov Jul 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need for this, repeat defaults to false

Above this another method states "repeat: false" expicitly. I assumed that's a code style. After all it's better readable this way..

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And is this necessary at all, to increase the interval? This is a one-time action, it won't have any impact on power usage or anything like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, yes, it's logically unrelated, I've just noticed it improves app startup performance.. Ok, I will redo the PR again.

onTriggered: {
chatListModel.calculateUnreadState();
tdLibWrapper.getRecentStickers();
Expand Down
11 changes: 11 additions & 0 deletions src/tdlibreceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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");
Expand All @@ -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");
}
Expand Down
2 changes: 2 additions & 0 deletions src/tdlibreceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -115,6 +116,7 @@ class TDLibReceiver : public QThread
QHash<QString, Handler> handlers;
void *tdLibClient;
bool isActive;
bool powerSavingMode;

private:
static const QVariantList cleanupList(const QVariantList& list, bool *updated = Q_NULLPTR);
Expand Down
7 changes: 6 additions & 1 deletion src/tdlibwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QGuiApplication>
#include <QLocale>
#include <QProcess>
#include <QSysInfo>
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/tdlibwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down