Commit a144293d authored by Nicolas Fella's avatar Nicolas Fella

Move location cache to own class

parent 0a68ebd3
......@@ -4,6 +4,7 @@ set(ktrip_SRCS
main.cpp
querycontroller.cpp
locationquerymodel.cpp
locationcache.cpp
)
if (ANDROID)
......
/**
* Copyright 2019 Nicolas Fella <nicolas.fella@gmx.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "locationcache.h"
#include <QDebug>
#include <QDateTime>
#include <QStandardPaths>
#include <QJsonDocument>
#include <QJsonObject>
LocationCache::LocationCache(QObject *parent)
: QObject(parent), m_locationCacheFile(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QStringLiteral("/locations.cache")), m_cachedLocationsJson()
{
if (!m_locationCacheFile.open(QIODevice::ReadWrite)) {
qWarning() << "Could not open location cache file" << m_locationCacheFile.fileName();
}
loadLocationsFromCache();
}
QVariantList LocationCache::cachedLocations() const
{
return m_cachedLocations;
}
void LocationCache::setCachedLocations(const QVariantList &locations)
{
if (locations != m_cachedLocations) {
m_cachedLocations = locations;
Q_EMIT cachedLocationsChanged();
}
}
void LocationCache::addCachedLocation(const KPublicTransport::Location location)
{
if (m_cachedLocations.contains(QVariant::fromValue(location))) {
return;
}
m_cachedLocations.append(QVariant::fromValue(location));
m_cachedLocationsJson.append(KPublicTransport::Location::toJson(location));
QJsonDocument doc(m_cachedLocationsJson);
m_locationCacheFile.resize(0);
m_locationCacheFile.write(doc.toJson());
m_locationCacheFile.flush();
}
void LocationCache::loadLocationsFromCache()
{
m_cachedLocationsJson = QJsonDocument::fromJson(m_locationCacheFile.readAll()).array();
for (const QJsonValue &val : qAsConst(m_cachedLocationsJson)) {
m_cachedLocations.append(QVariant::fromValue(KPublicTransport::Location::fromJson(val.toObject())));
}
}
/**
* Copyright 2019 Nicolas Fella <nicolas.fella@gmx.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <QObject>
#include <QFile>
#include <QJsonArray>
#include <KPublicTransport/Location>
class LocationCache : public QObject
{
Q_OBJECT
Q_PROPERTY(QVariantList cachedLocations READ cachedLocations WRITE setCachedLocations NOTIFY cachedLocationsChanged)
public:
explicit LocationCache(QObject *parent = nullptr);
QVariantList cachedLocations() const;
void setCachedLocations(const QVariantList &locations);
Q_INVOKABLE void addCachedLocation(const KPublicTransport::Location location);
Q_SIGNALS:
void cachedLocationsChanged();
private:
void loadLocationsFromCache();
QVariantList m_cachedLocations;
QFile m_locationCacheFile;
QJsonArray m_cachedLocationsJson;
};
......@@ -21,6 +21,7 @@
#include "androidutils.h"
#include "locationquerymodel.h"
#include "querycontroller.h"
#include "locationcache.h"
#include <QQmlApplicationEngine>
#include <QQmlContext>
......@@ -62,6 +63,9 @@ int main(int argc, char *argv[])
QueryController queryController;
engine.rootContext()->setContextProperty(QStringLiteral("_queryController"), &queryController);
LocationCache locationCache;
engine.rootContext()->setContextProperty(QStringLiteral("_locationCache"), &locationCache);
KPublicTransport::Manager manager;
engine.rootContext()->setContextProperty(QStringLiteral("_manager"), &manager);
......
......@@ -51,13 +51,13 @@ Kirigami.Page
ListView {
anchors.fill: parent
visible: showCached
model: _queryController.cachedLocations
model: _locationCache.cachedLocations
delegate: Kirigami.BasicListItem {
text: modelData.name
reserveSpaceForIcon: false
onClicked: {
_queryController.addCachedLocation(modelData)
_locationCache.addCachedLocation(modelData)
if (type == "start") {
_queryController.start = modelData
......@@ -82,7 +82,7 @@ Kirigami.Page
text: name
reserveSpaceForIcon: false
onClicked: {
_queryController.addCachedLocation(object)
_locationCache.addCachedLocation(object)
if (type == "start") {
_queryController.start = object
......
......@@ -27,16 +27,10 @@
#include <QJsonObject>
QueryController::QueryController(QObject *parent)
: QObject(parent), m_start(), m_destination(), m_locationCacheFile(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QStringLiteral("/locations.cache")), m_cachedLocationsJson()
: QObject(parent), m_start(), m_destination()
{
if (!m_locationCacheFile.open(QIODevice::ReadWrite)) {
qWarning() << "Could not open location cache file" << m_locationCacheFile.fileName();
}
m_departureDate = QDate::currentDate().toString(Qt::ISODate);
m_departureTime = QTime::currentTime().toString(Qt::SystemLocaleShortDate);
loadLocationsFromCache();
}
void QueryController::setStart(const KPublicTransport::Location start)
......@@ -99,41 +93,3 @@ void QueryController::setDepartureTime(const QString &time)
Q_EMIT departureTimeChanged();
}
}
QVariantList QueryController::cachedLocations() const
{
return m_cachedLocations;
}
void QueryController::setCachedLocations(const QVariantList &locations)
{
if (locations != m_cachedLocations) {
m_cachedLocations = locations;
Q_EMIT cachedLocationsChanged();
}
}
void QueryController::addCachedLocation(const KPublicTransport::Location location)
{
if (m_cachedLocations.contains(QVariant::fromValue(location))) {
return;
}
m_cachedLocations.append(QVariant::fromValue(location));
m_cachedLocationsJson.append(KPublicTransport::Location::toJson(location));
QJsonDocument doc(m_cachedLocationsJson);
m_locationCacheFile.resize(0);
m_locationCacheFile.write(doc.toJson());
m_locationCacheFile.flush();
}
void QueryController::loadLocationsFromCache()
{
m_cachedLocationsJson = QJsonDocument::fromJson(m_locationCacheFile.readAll()).array();
for (const QJsonValue &val : qAsConst(m_cachedLocationsJson)) {
m_cachedLocations.append(QVariant::fromValue(KPublicTransport::Location::fromJson(val.toObject())));
}
}
......@@ -21,8 +21,6 @@
#pragma once
#include <QObject>
#include <QFile>
#include <QJsonArray>
#include <KPublicTransport/Location>
#include <KPublicTransport/JourneyRequest>
......@@ -35,7 +33,6 @@ class QueryController : public QObject
Q_PROPERTY(KPublicTransport::Location destination READ destination WRITE setDestination NOTIFY destinationChanged)
Q_PROPERTY(QString departureDate READ departureDate WRITE setDepartureDate NOTIFY departureDateChanged)
Q_PROPERTY(QString departureTime READ departureTime WRITE setDepartureTime NOTIFY departureTimeChanged)
Q_PROPERTY(QVariantList cachedLocations READ cachedLocations WRITE setCachedLocations NOTIFY cachedLocationsChanged)
public:
explicit QueryController(QObject *parent = nullptr);
......@@ -52,10 +49,6 @@ public:
QString departureTime() const;
void setDepartureTime(const QString &time);
QVariantList cachedLocations() const;
void setCachedLocations(const QVariantList &locations);
Q_INVOKABLE void addCachedLocation(const KPublicTransport::Location location);
Q_INVOKABLE KPublicTransport::JourneyRequest createJourneyRequest();
Q_SIGNALS:
......@@ -63,16 +56,11 @@ Q_SIGNALS:
void destinationChanged();
void departureDateChanged();
void departureTimeChanged();
void cachedLocationsChanged();
private:
void loadLocationsFromCache();
KPublicTransport::Location m_start;
KPublicTransport::Location m_destination;
QString m_departureDate;
QString m_departureTime;
QVariantList m_cachedLocations;
QFile m_locationCacheFile;
QJsonArray m_cachedLocationsJson;
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment