MessagesAdapter: avoid repeat queries of locale

MessageAdapter's getFormattedDay and getFormattedTime functions would
preivously query the AppSettingsManger's current locale for each call.
The locale can be refactored as a class member to avoid repeat calls.

Change-Id: Id438ab6387f63e8ae4269a2a8617a522fa99ec6d
This commit is contained in:
Ilyas Erdogan
2025-11-04 10:33:12 -05:00
parent 8bbdd3ba53
commit 2da0f18213
4 changed files with 12 additions and 4 deletions

View File

@@ -59,6 +59,10 @@ void
AppSettingsManager::setValue(const QString& key, const QVariant& value)
{
settings_->setValue(key, value);
if (key == QString("LANG")) {
Q_EMIT localeChanged();
}
}
QVariant

View File

@@ -156,6 +156,7 @@ public:
Q_SIGNALS:
void retranslate();
void reloadHistory();
void localeChanged();
private:
QSettings* settings_;

View File

@@ -49,6 +49,7 @@ MessagesAdapter::MessagesAdapter(AppSettingsManager* settingsManager,
, filteredMsgListModel_(new FilteredMsgListModel(this))
, mediaInteractions_(std::make_unique<MessageListModel>(nullptr))
, timestampTimer_(new QTimer(this))
, curLocale_(QLocale(settingsManager_->getLanguage()))
{
setObjectName(typeid(*this).name());
@@ -74,6 +75,9 @@ MessagesAdapter::MessagesAdapter(AppSettingsManager* settingsManager,
connect(messageParser_, &MessageParser::messageParsed, this, &MessagesAdapter::onMessageParsed);
connect(messageParser_, &MessageParser::linkInfoReady, this, &MessagesAdapter::onLinkInfoReady);
connect(settingsManager_, &AppSettingsManager::localeChanged, this, [this]() {
curLocale_ = QLocale((settingsManager_->getLanguage()));
});
connect(timestampTimer_, &QTimer::timeout, this, &MessagesAdapter::timestampUpdated);
timestampTimer_->start(timestampUpdateIntervalMs_);
@@ -629,9 +633,8 @@ MessagesAdapter::getFormattedTime(const quint64 timestamp)
const auto currentTime = QDateTime::currentDateTime();
const auto seconds = currentTime.toSecsSinceEpoch() - timestamp;
if (seconds > 60) {
auto curLocal = QLocale(settingsManager_->getLanguage());
auto curTime = QDateTime::fromSecsSinceEpoch(timestamp).time();
return curLocal.toString(curTime, curLocal.ShortFormat).toLower();
return curLocale_.toString(curTime, curLocale_.ShortFormat).toLower();
}
return QObject::tr("Just now");
}
@@ -656,8 +659,7 @@ MessagesAdapter::getFormattedDay(const quint64 timestamp)
if (timestampDate.daysTo(currentDate) == 1)
return QObject::tr("Yesterday");
auto curLocal = QLocale(settingsManager_->getLanguage());
return curLocal.toString(timestampDate, curLocal.ShortFormat);
return curLocale_.toString(timestampDate, curLocale_.ShortFormat);
}
void

View File

@@ -203,4 +203,5 @@ private:
QTimer* timestampTimer_;
static constexpr const int loadChunkSize_ {20};
static constexpr const int timestampUpdateIntervalMs_ {1000};
QLocale curLocale_;
};