mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-06-22 02:01:08 +07:00
Remove news caching
This commit is contained in:
@@ -102,10 +102,8 @@ void CoreController::initModels()
|
|||||||
m_apiDevicesModel.reset(new ApiDevicesModel(m_settings, this));
|
m_apiDevicesModel.reset(new ApiDevicesModel(m_settings, this));
|
||||||
m_engine->rootContext()->setContextProperty("ApiDevicesModel", m_apiDevicesModel.get());
|
m_engine->rootContext()->setContextProperty("ApiDevicesModel", m_apiDevicesModel.get());
|
||||||
|
|
||||||
m_newsModel.reset(new NewsModel(this));
|
m_newsModel.reset(new NewsModel(m_settings, this));
|
||||||
m_engine->rootContext()->setContextProperty("NewsModel", m_newsModel.get());
|
m_engine->rootContext()->setContextProperty("NewsModel", m_newsModel.get());
|
||||||
QObject::connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit,
|
|
||||||
m_newsModel.get(), &NewsModel::saveLocalNews);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CoreController::initControllers()
|
void CoreController::initControllers()
|
||||||
|
|||||||
@@ -578,3 +578,13 @@ void Settings::setAllowedDnsServers(const QStringList &servers)
|
|||||||
{
|
{
|
||||||
setValue("Conf/allowedDnsServers", servers);
|
setValue("Conf/allowedDnsServers", servers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QStringList Settings::readNewsIds() const
|
||||||
|
{
|
||||||
|
return value("News/readIds").toStringList();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Settings::setReadNewsIds(const QStringList &ids)
|
||||||
|
{
|
||||||
|
setValue("News/readIds", ids);
|
||||||
|
}
|
||||||
|
|||||||
@@ -236,6 +236,9 @@ public:
|
|||||||
QStringList allowedDnsServers() const;
|
QStringList allowedDnsServers() const;
|
||||||
void setAllowedDnsServers(const QStringList &servers);
|
void setAllowedDnsServers(const QStringList &servers);
|
||||||
|
|
||||||
|
QStringList readNewsIds() const;
|
||||||
|
void setReadNewsIds(const QStringList &ids);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void saveLogsChanged(bool enabled);
|
void saveLogsChanged(bool enabled);
|
||||||
void screenshotsEnabledChanged(bool enabled);
|
void screenshotsEnabledChanged(bool enabled);
|
||||||
|
|||||||
@@ -9,10 +9,11 @@
|
|||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
NewsModel::NewsModel(QObject *parent)
|
NewsModel::NewsModel(const std::shared_ptr<Settings> &settings, QObject *parent)
|
||||||
: QAbstractListModel(parent)
|
: QAbstractListModel(parent)
|
||||||
|
, m_settings(settings)
|
||||||
{
|
{
|
||||||
loadLocalNews();
|
loadReadIds();
|
||||||
}
|
}
|
||||||
|
|
||||||
int NewsModel::rowCount(const QModelIndex &parent) const
|
int NewsModel::rowCount(const QModelIndex &parent) const
|
||||||
@@ -63,6 +64,8 @@ void NewsModel::markAsRead(int index)
|
|||||||
return;
|
return;
|
||||||
if (!m_items[index].read) {
|
if (!m_items[index].read) {
|
||||||
m_items[index].read = true;
|
m_items[index].read = true;
|
||||||
|
m_readIds.insert(m_items[index].id);
|
||||||
|
saveReadIds();
|
||||||
QModelIndex idx = createIndex(index, 0);
|
QModelIndex idx = createIndex(index, 0);
|
||||||
emit dataChanged(idx, idx, {IsReadRole});
|
emit dataChanged(idx, idx, {IsReadRole});
|
||||||
emit hasUnreadChanged();
|
emit hasUnreadChanged();
|
||||||
@@ -101,7 +104,7 @@ void NewsModel::updateModel(const QJsonArray &serverItems)
|
|||||||
item.title = obj.value("title").toString();
|
item.title = obj.value("title").toString();
|
||||||
item.content = obj.value("content").toString();
|
item.content = obj.value("content").toString();
|
||||||
item.timestamp = QDateTime::fromString(obj.value("timestamp").toString(), Qt::ISODate);
|
item.timestamp = QDateTime::fromString(obj.value("timestamp").toString(), Qt::ISODate);
|
||||||
item.read = false; // New news is always unread
|
item.read = m_readIds.contains(id);
|
||||||
newItems.append(item);
|
newItems.append(item);
|
||||||
existingIds.insert(id);
|
existingIds.insert(id);
|
||||||
}
|
}
|
||||||
@@ -128,68 +131,13 @@ bool NewsModel::hasUnread() const
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString NewsModel::localFilePath() const
|
void NewsModel::loadReadIds()
|
||||||
{
|
{
|
||||||
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
QStringList ids = m_settings->readNewsIds();
|
||||||
QDir dir(path);
|
m_readIds = QSet<QString>(ids.begin(), ids.end());
|
||||||
if (!dir.exists()) {
|
|
||||||
dir.mkpath(".");
|
|
||||||
}
|
|
||||||
return path + "/news.json";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NewsModel::loadLocalNews()
|
void NewsModel::saveReadIds() const
|
||||||
{
|
{
|
||||||
QFile file(localFilePath());
|
m_settings->setReadNewsIds(QStringList(m_readIds.begin(), m_readIds.end()));
|
||||||
if (!file.exists() || !file.open(QIODevice::ReadOnly)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
|
|
||||||
file.close();
|
|
||||||
|
|
||||||
if (!doc.isArray()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
beginResetModel();
|
|
||||||
m_items.clear();
|
|
||||||
|
|
||||||
QJsonArray newsArray = doc.array();
|
|
||||||
for (const QJsonValue &value : newsArray) {
|
|
||||||
if (!value.isObject())
|
|
||||||
continue;
|
|
||||||
const QJsonObject obj = value.toObject();
|
|
||||||
NewsItem item;
|
|
||||||
item.id = obj.value("id").toString();
|
|
||||||
item.title = obj.value("title").toString();
|
|
||||||
item.content = obj.value("content").toString();
|
|
||||||
item.timestamp = QDateTime::fromString(obj.value("timestamp").toString(), Qt::ISODate);
|
|
||||||
item.read = obj.value("read").toBool();
|
|
||||||
m_items.append(item);
|
|
||||||
}
|
|
||||||
endResetModel();
|
|
||||||
}
|
|
||||||
|
|
||||||
void NewsModel::saveLocalNews() const
|
|
||||||
{
|
|
||||||
QJsonArray newsArray;
|
|
||||||
for (const auto &item : m_items) {
|
|
||||||
QJsonObject obj;
|
|
||||||
obj["id"] = item.id;
|
|
||||||
obj["title"] = item.title;
|
|
||||||
obj["content"] = item.content;
|
|
||||||
obj["timestamp"] = item.timestamp.toString(Qt::ISODate);
|
|
||||||
obj["read"] = item.read;
|
|
||||||
newsArray.append(obj);
|
|
||||||
}
|
|
||||||
|
|
||||||
QJsonDocument doc(newsArray);
|
|
||||||
QFile file(localFilePath());
|
|
||||||
if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
|
||||||
file.write(doc.toJson());
|
|
||||||
file.close();
|
|
||||||
} else {
|
|
||||||
qWarning() << "Could not save news to" << localFilePath();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -7,6 +7,8 @@
|
|||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
#include <QSet>
|
#include <QSet>
|
||||||
|
#include <memory>
|
||||||
|
#include "settings.h"
|
||||||
|
|
||||||
struct NewsItem {
|
struct NewsItem {
|
||||||
QString id;
|
QString id;
|
||||||
@@ -28,9 +30,8 @@ public:
|
|||||||
IsReadRole,
|
IsReadRole,
|
||||||
IsProcessedRole
|
IsProcessedRole
|
||||||
};
|
};
|
||||||
explicit NewsModel(QObject *parent = nullptr);
|
explicit NewsModel(const std::shared_ptr<Settings> &settings, QObject *parent = nullptr);
|
||||||
Q_INVOKABLE void markAsRead(int index);
|
Q_INVOKABLE void markAsRead(int index);
|
||||||
Q_INVOKABLE void saveLocalNews() const;
|
|
||||||
|
|
||||||
Q_PROPERTY(int processedIndex READ processedIndex WRITE setProcessedIndex NOTIFY processedIndexChanged)
|
Q_PROPERTY(int processedIndex READ processedIndex WRITE setProcessedIndex NOTIFY processedIndexChanged)
|
||||||
Q_PROPERTY(bool hasUnread READ hasUnread NOTIFY hasUnreadChanged)
|
Q_PROPERTY(bool hasUnread READ hasUnread NOTIFY hasUnreadChanged)
|
||||||
@@ -51,8 +52,10 @@ signals:
|
|||||||
private:
|
private:
|
||||||
QVector<NewsItem> m_items;
|
QVector<NewsItem> m_items;
|
||||||
int m_processedIndex = -1;
|
int m_processedIndex = -1;
|
||||||
void loadLocalNews();
|
std::shared_ptr<Settings> m_settings;
|
||||||
QString localFilePath() const;
|
QSet<QString> m_readIds;
|
||||||
|
void loadReadIds();
|
||||||
|
void saveReadIds() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // NEWSMODEL_H
|
#endif // NEWSMODEL_H
|
||||||
Reference in New Issue
Block a user