Commit 123ccfc1 authored by Volker Krause's avatar Volker Krause
Browse files

Give the transfer anchor time methods more specific names

Also, let them handle event reservations and consider flight boarding
times when present.
parent 5cec5f75
......@@ -71,7 +71,7 @@ private Q_SLOTS:
auto batchId = resMgr.batches().at(0);
auto transfer = mgr.transfer(batchId, Transfer::Before);
QCOMPARE(transfer.state(), Transfer::Pending);
QCOMPARE(transfer.anchorTime(), QDateTime({2017, 9, 10}, {6, 45}, QTimeZone("Europe/Berlin")));
QCOMPARE(transfer.anchorTime(), QDateTime({2017, 9, 10}, {6, 15}, QTimeZone("Europe/Berlin")));
QCOMPARE(transfer.anchorTimeDelta(), 3600);
QCOMPARE(transfer.alignment(), Transfer::Before);
QCOMPARE(transfer.reservationId(), batchId);
......@@ -90,7 +90,7 @@ private Q_SLOTS:
TransferManager mgr2;
transfer = mgr2.transfer(batchId, Transfer::Before);
QCOMPARE(transfer.state(), Transfer::Pending);
QCOMPARE(transfer.anchorTime(), QDateTime({2017, 9, 10}, {6, 45}, QTimeZone("Europe/Berlin")));
QCOMPARE(transfer.anchorTime(), QDateTime({2017, 9, 10}, {6, 15}, QTimeZone("Europe/Berlin")));
QCOMPARE(transfer.alignment(), Transfer::Before);
QCOMPARE(transfer.reservationId(), batchId);
QVERIFY(transfer.hasLocations());
......@@ -107,8 +107,8 @@ private Q_SLOTS:
KPublicTransport::Journey jny;
KPublicTransport::JourneySection section;
section.setScheduledDepartureTime(QDateTime({2017, 9, 10}, {5, 30}, QTimeZone("Europe/Berlin")));
section.setScheduledArrivalTime(QDateTime({2017, 9, 10}, {6, 0}, QTimeZone("Europe/Berlin")));
section.setScheduledDepartureTime(QDateTime({2017, 9, 10}, {5, 0}, QTimeZone("Europe/Berlin")));
section.setScheduledArrivalTime(QDateTime({2017, 9, 10}, {5, 30}, QTimeZone("Europe/Berlin")));
jny.setSections({section});
mgr.setJourneyForTransfer(transfer, jny);
QCOMPARE(addSpy.size(), 0);
......@@ -118,7 +118,7 @@ private Q_SLOTS:
transfer = mgr.transfer(batchId, Transfer::Before);
QCOMPARE(transfer.state(), Transfer::Selected);
QCOMPARE(transfer.journey().sections().size(), 1);
QCOMPARE(transfer.journey().scheduledArrivalTime(), QDateTime({2017, 9, 10}, {6, 0}, QTimeZone("Europe/Berlin")));
QCOMPARE(transfer.journey().scheduledArrivalTime(), QDateTime({2017, 9, 10}, {5, 30}, QTimeZone("Europe/Berlin")));
QCOMPARE(transfer.anchorTimeDelta(), 45*60);
addSpy.clear();
......@@ -135,7 +135,7 @@ private Q_SLOTS:
transfer = mgr.addTransfer(batchId, Transfer::Before);
QCOMPARE(transfer.state(), Transfer::Pending);
QCOMPARE(transfer.anchorTime(), QDateTime({2017, 9, 10}, {6, 45}, QTimeZone("Europe/Berlin")));
QCOMPARE(transfer.anchorTime(), QDateTime({2017, 9, 10}, {6, 15}, QTimeZone("Europe/Berlin")));
QCOMPARE(transfer.alignment(), Transfer::Before);
QCOMPARE(transfer.reservationId(), batchId);
QVERIFY(transfer.hasLocations());
......
......@@ -36,6 +36,7 @@ target_sources(itinerary PRIVATE
tripgroupmanager.cpp
tripgroupproxymodel.cpp
transfer.cpp
transferhelper.cpp
transfermanager.cpp
util.cpp
)
......
......@@ -63,34 +63,6 @@ bool ReservationHelper::equals(const QVariant &lhs, const QVariant &rhs)
return false;
}
QDateTime ReservationHelper::departureTime(const QVariant &res)
{
if (JsonLd::isA<TrainReservation>(res)) {
return res.value<TrainReservation>().reservationFor().value<TrainTrip>().departureTime();
}
if (JsonLd::isA<BusReservation>(res)) {
return res.value<BusReservation>().reservationFor().value<BusTrip>().departureTime();
}
if (JsonLd::isA<FlightReservation>(res)) {
return res.value<FlightReservation>().reservationFor().value<Flight>().departureTime();
}
return {};
}
QDateTime ReservationHelper::arrivalTime(const QVariant &res)
{
if (JsonLd::isA<TrainReservation>(res)) {
return res.value<TrainReservation>().reservationFor().value<TrainTrip>().arrivalTime();
}
if (JsonLd::isA<BusReservation>(res)) {
return res.value<BusReservation>().reservationFor().value<BusTrip>().arrivalTime();
}
if (JsonLd::isA<FlightReservation>(res)) {
return res.value<FlightReservation>().reservationFor().value<Flight>().arrivalTime();
}
return {};
}
QString ReservationHelper::uicCompanyCode(const QVariant &res)
{
QString id;
......
......@@ -25,12 +25,6 @@ namespace ReservationHelper
bool equals(const QVariant &lhs, const QVariant &rhs);
/** Returns the arrival/departure time if available.
* This returns an invalid time for unbound train tickets or bare flight boarding passes.
*/
QDateTime departureTime(const QVariant &res);
QDateTime arrivalTime(const QVariant &res);
/** Returns the UIC company code for @p res, if any. */
QString uicCompanyCode(const QVariant &res);
}
......
/*
SPDX-FileCopyrightText: 2020-2021 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "transferhelper.h"
#include <KItinerary/BusTrip>
#include <KItinerary/Event>
#include <KItinerary/Flight>
#include <KItinerary/Reservation>
#include <KItinerary/TrainTrip>
#include <QDateTime>
#include <QVariant>
using namespace KItinerary;
QDateTime TransferHelper::anchorTimeBefore(const QVariant &res)
{
if (JsonLd::isA<TrainReservation>(res)) {
return res.value<TrainReservation>().reservationFor().value<TrainTrip>().departureTime();
}
if (JsonLd::isA<BusReservation>(res)) {
return res.value<BusReservation>().reservationFor().value<BusTrip>().departureTime();
}
if (JsonLd::isA<FlightReservation>(res)) {
const auto flight = res.value<FlightReservation>().reservationFor().value<Flight>();
if (flight.boardingTime().isValid()) {
return flight.boardingTime();
}
return flight.departureTime();
}
if (JsonLd::isA<EventReservation>(res)) {
const auto event = res.value<EventReservation>().reservationFor().value<Event>();
if (event.doorTime().isValid()) {
return event.doorTime();
}
return event.startDate();
}
return {};
}
QDateTime TransferHelper::anchorTimeAfter(const QVariant &res)
{
if (JsonLd::isA<TrainReservation>(res)) {
return res.value<TrainReservation>().reservationFor().value<TrainTrip>().arrivalTime();
}
if (JsonLd::isA<BusReservation>(res)) {
return res.value<BusReservation>().reservationFor().value<BusTrip>().arrivalTime();
}
if (JsonLd::isA<FlightReservation>(res)) {
return res.value<FlightReservation>().reservationFor().value<Flight>().arrivalTime();
}
if (JsonLd::isA<EventReservation>(res)) {
return res.value<EventReservation>().reservationFor().value<Event>().endDate();
}
return {};
}
/*
SPDX-FileCopyrightText: 2020-2021 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef TRANSFERHELPERS_H
#define TRANSFERHELPERS_H
class QDateTime;
class QVariant;
/** Helper methods for TransferManager. */
namespace TransferHelper
{
/** Return the transfer anchor time before/after the reservation @p res. */
QDateTime anchorTimeBefore(const QVariant &res);
QDateTime anchorTimeAfter(const QVariant &res);
}
#endif // TRANSFERHELPERS_H
......@@ -8,8 +8,8 @@
#include "logging.h"
#include "favoritelocationmodel.h"
#include "publictransport.h"
#include "reservationhelper.h"
#include "reservationmanager.h"
#include "transferhelper.h"
#include "tripgroup.h"
#include "tripgroupmanager.h"
......@@ -199,11 +199,11 @@ void TransferManager::checkReservation(const QString &resId)
const auto res = m_resMgr->reservation(resId);
const auto now = currentDateTime();
if (ReservationHelper::arrivalTime(res) < now) {
if (TransferHelper::anchorTimeAfter(res) < now) {
return;
}
checkReservation(resId, res, Transfer::After);
if (ReservationHelper::departureTime(res) < now) {
if (TransferHelper::anchorTimeBefore(res) < now) {
return;
}
checkReservation(resId, res, Transfer::Before);
......@@ -236,7 +236,7 @@ void TransferManager::checkReservation(const QString &resId, const QVariant &res
TransferManager::CheckTransferResult TransferManager::checkTransferBefore(const QString &resId, const QVariant &res, Transfer &transfer) const
{
transfer.setAnchorTime(ReservationHelper::departureTime(res));
transfer.setAnchorTime(TransferHelper::anchorTimeBefore(res));
const auto isLocationChange = LocationUtil::isLocationChange(res);
QVariant toLoc;
if (isLocationChange) {
......@@ -292,7 +292,7 @@ TransferManager::CheckTransferResult TransferManager::checkTransferBefore(const
TransferManager::CheckTransferResult TransferManager::checkTransferAfter(const QString &resId, const QVariant &res, Transfer &transfer) const
{
transfer.setAnchorTime(ReservationHelper::arrivalTime(res));
transfer.setAnchorTime(TransferHelper::anchorTimeAfter(res));
const auto isLocationChange = LocationUtil::isLocationChange(res);
QVariant fromLoc;
if (isLocationChange) {
......
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