Commit 098b3fc8 authored by Volker Krause's avatar Volker Krause

Consider live data when computing element progress

Also, consider boarding time for flights when determining if an element
is current.
parent fd601092
......@@ -6,8 +6,10 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config-itinerary.h.in ${CMAKE_CURRENT
set(itinerary_srcs
applicationcontroller.cpp
countryinformation.cpp
livedatamanager.cpp
pkpassmanager.cpp
pkpassimageprovider.cpp
publictransport.cpp
reservationmanager.cpp
timelinedelegatecontroller.cpp
timelinemodel.cpp
......@@ -24,12 +26,16 @@ ecm_qt_declare_logging_category(itinerary_srcs
add_library(itinerary STATIC ${itinerary_srcs})
target_link_libraries(itinerary PUBLIC
itinerary-weather
KPublicTransport
KPim::Itinerary
KPim::PkPass
KF5::I18n
Qt5::Network
Qt5::Quick
)
if (TARGET KF5::Notifications)
target_link_libraries(itinerary PUBLIC KF5::Notifications)
endif()
if (Qt5QuickCompiler_FOUND)
qtquick_compiler_add_resources(qml_srcs qml.qrc)
......@@ -48,9 +54,7 @@ set(itinerary_app_srcs
${qml_srcs}
brightnessmanager.cpp
lockmanager.cpp
livedatamanager.cpp
journeyquerymodel.cpp
publictransport.cpp
)
if (ANDROID)
list(APPEND itinerary_app_srcs
......@@ -73,11 +77,7 @@ target_link_libraries(itinerary-app PRIVATE
itinerary
KF5::Contacts
KF5::CoreAddons
KPublicTransport
)
if (TARGET KF5::Notifications)
target_link_libraries(itinerary-app PRIVATE KF5::Notifications)
endif()
if (ANDROID)
target_include_directories(itinerary-app PRIVATE ${Qt5Core_PRIVATE_INCLUDE_DIRS})
# explicitly add runtime dependencies and transitive link dependencies,
......
......@@ -46,6 +46,7 @@ Kirigami.AbstractCard {
TimelineDelegateController {
id: controller
reservationManager: _reservationManager
liveDataManager: _liveDataManager
}
header: Rectangle {
......
......@@ -17,10 +17,16 @@
#include "timelinedelegatecontroller.h"
#include "livedatamanager.h"
#include "reservationmanager.h"
#include <KItinerary/Flight>
#include <KItinerary/LocationUtil>
#include <KItinerary/SortUtil>
#include <KItinerary/Reservation>
#include <KItinerary/TrainTrip>
#include <KPublicTransport/Departure>
#include <QCoreApplication>
#include <QDateTime>
......@@ -53,8 +59,9 @@ QObject* TimelineDelegateController::reservationManager() const
void TimelineDelegateController::setReservationManager(QObject *resMgr)
{
if (m_resMgr == resMgr)
if (m_resMgr == resMgr) {
return;
}
m_resMgr = qobject_cast<ReservationManager*>(resMgr);
emit setupChanged();
......@@ -65,6 +72,26 @@ void TimelineDelegateController::setReservationManager(QObject *resMgr)
checkForUpdate(m_batchId);
}
QObject* TimelineDelegateController::liveDataManager() const
{
return m_liveDataMgr;
}
void TimelineDelegateController::setLiveDataManager(QObject* liveDataMgr)
{
if (m_liveDataMgr == liveDataMgr) {
return;
}
m_liveDataMgr = qobject_cast<LiveDataManager*>(liveDataMgr);
emit setupChanged();
connect(m_liveDataMgr, &LiveDataManager::arrivalUpdated, this, &TimelineDelegateController::checkForUpdate);
connect(m_liveDataMgr, &LiveDataManager::departureUpdated, this, &TimelineDelegateController::checkForUpdate);
checkForUpdate(m_batchId);
}
QString TimelineDelegateController::batchId() const
{
return m_batchId;
......@@ -125,9 +152,8 @@ float TimelineDelegateController::progress() const
}
const auto res = m_resMgr->reservation(m_batchId);
// TODO this needs to consider live data
const auto startTime = SortUtil::startDateTime(res);
const auto endTime = SortUtil::endtDateTime(res);
const auto startTime = liveStartDateTime(res);
const auto endTime = liveEndDateTime(res);
const auto tripLength = startTime.secsTo(endTime);
if (tripLength <= 0) {
......@@ -150,9 +176,8 @@ void TimelineDelegateController::checkForUpdate(const QString& batchId)
const auto res = m_resMgr->reservation(batchId);
const auto now = QDateTime::currentDateTime();
// TODO this needs to consider live data and boarding times
const auto startTime = SortUtil::startDateTime(res);
const auto endTime = SortUtil::endtDateTime(res);
const auto startTime = relevantStartDateTime(res);
const auto endTime = liveEndDateTime(res);
setCurrent(startTime < now && now < endTime, res);
......@@ -163,6 +188,43 @@ void TimelineDelegateController::checkForUpdate(const QString& batchId)
}
}
QDateTime TimelineDelegateController::relevantStartDateTime(const QVariant &res) const
{
auto startTime = SortUtil::startDateTime(res);
if (JsonLd::isA<FlightReservation>(res)) {
const auto flight = res.value<FlightReservation>().reservationFor().value<Flight>();
if (flight.boardingTime().isValid()) {
startTime = flight.boardingTime();
}
} else if (JsonLd::isA<TrainReservation>(res) || JsonLd::isA<BusReservation>(res)) {
startTime = startTime.addSecs(-5 * 60);
}
return startTime;
}
QDateTime TimelineDelegateController::liveStartDateTime(const QVariant& res) const
{
if (m_liveDataMgr) {
const auto dep = m_liveDataMgr->departure(m_batchId).value<KPublicTransport::Departure>();
if (dep.expectedDepartureTime().isValid()) {
return dep.expectedDepartureTime();
}
}
return SortUtil::startDateTime(res);
}
QDateTime TimelineDelegateController::liveEndDateTime(const QVariant& res) const
{
if (m_liveDataMgr) {
const auto arr = m_liveDataMgr->arrival(m_batchId).value<KPublicTransport::Departure>();
if (arr.expectedArrivalTime().isValid()) {
return arr.expectedArrivalTime();
}
}
return SortUtil::endtDateTime(res);
}
void TimelineDelegateController::scheduleNextUpdate(std::chrono::milliseconds ms)
{
if (s_currentTimer->isActive() && s_currentTimer->remainingTimeAsDuration() < ms) {
......
......@@ -23,8 +23,10 @@
#include <chrono>
class QDateTime;
class QTimer;
class LiveDataManager;
class ReservationManager;
/** C++ side logic for timeline delegates. */
......@@ -32,6 +34,7 @@ class TimelineDelegateController : public QObject
{
Q_OBJECT
Q_PROPERTY(QObject* reservationManager READ reservationManager WRITE setReservationManager NOTIFY setupChanged)
Q_PROPERTY(QObject* liveDataManager READ liveDataManager WRITE setLiveDataManager NOTIFY setupChanged)
Q_PROPERTY(QString batchId READ batchId WRITE setBatchId NOTIFY contentChanged)
Q_PROPERTY(bool isCurrent READ isCurrent NOTIFY currentChanged)
......@@ -41,8 +44,10 @@ public:
TimelineDelegateController(QObject *parent = nullptr);
~TimelineDelegateController();
QObject *reservationManager() const;
QObject* reservationManager() const;
void setReservationManager(QObject *resMgr);
QObject* liveDataManager() const;
void setLiveDataManager(QObject *liveDataMgr);
QString batchId() const;
void setBatchId(const QString &batchId);
......@@ -59,8 +64,14 @@ Q_SIGNALS:
private:
void setCurrent(bool current, const QVariant &res = {});
void checkForUpdate(const QString &batchId);
/** Time at which we consider @p res "current". */
QDateTime relevantStartDateTime(const QVariant &res) const;
/** Time at which the event starts/stops based on realtime data. */
QDateTime liveStartDateTime(const QVariant &res) const;
QDateTime liveEndDateTime(const QVariant &res) const;
ReservationManager *m_resMgr = nullptr; // ### should this be static?
LiveDataManager *m_liveDataMgr = nullptr;
QString m_batchId;
bool m_isCurrent = false;
......
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