Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
Utilities
KTrip
Commits
a144293d
Commit
a144293d
authored
Jul 28, 2019
by
Nicolas Fella
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move location cache to own class
parent
0a68ebd3
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
136 additions
and
60 deletions
+136
-60
src/CMakeLists.txt
src/CMakeLists.txt
+1
-0
src/locationcache.cpp
src/locationcache.cpp
+75
-0
src/locationcache.h
src/locationcache.h
+52
-0
src/main.cpp
src/main.cpp
+4
-0
src/qml/LocationQueryPage.qml
src/qml/LocationQueryPage.qml
+3
-3
src/querycontroller.cpp
src/querycontroller.cpp
+1
-45
src/querycontroller.h
src/querycontroller.h
+0
-12
No files found.
src/CMakeLists.txt
View file @
a144293d
...
...
@@ -4,6 +4,7 @@ set(ktrip_SRCS
main.cpp
querycontroller.cpp
locationquerymodel.cpp
locationcache.cpp
)
if
(
ANDROID
)
...
...
src/locationcache.cpp
0 → 100644
View file @
a144293d
/**
* 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
())));
}
}
src/locationcache.h
0 → 100644
View file @
a144293d
/**
* 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
;
};
src/main.cpp
View file @
a144293d
...
...
@@ -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
);
...
...
src/qml/LocationQueryPage.qml
View file @
a144293d
...
...
@@ -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
...
...
src/querycontroller.cpp
View file @
a144293d
...
...
@@ -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
())));
}
}
src/querycontroller.h
View file @
a144293d
...
...
@@ -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
;
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment