[gnome-maps/wip/mlundblad/transit-plugin-resrobot: 2/5] transitPlan: Add method to insert new itineraries



commit 26213b14547ddb5b91899137e35d662eea29e8d4
Author: Marcus Lundblad <ml update uu se>
Date:   Sun Oct 6 09:36:23 2019 +0200

    transitPlan: Add method to insert new itineraries
    
    Adds a method to add new itineraris, either setting
    a fresh set, or extending the previous. Sorted either
    on departure time ascending, or arrival time descending.
    This was previously done in the OpenTripPlanner plugin,
    but moved to avoid code duplication in new plugins.

 src/transitPlan.js | 47 +++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 45 insertions(+), 2 deletions(-)
---
diff --git a/src/transitPlan.js b/src/transitPlan.js
index 0229c4d..909e721 100644
--- a/src/transitPlan.js
+++ b/src/transitPlan.js
@@ -130,6 +130,29 @@ var Plan = GObject.registerClass({
         this.emit('update');
     }
 
+    /**
+     * Update plan with new itineraries, setting the new itineraries if it's
+     * the first fetch for a query, or extending the existing ones if it's
+     * a request to load more
+     */
+    updateWithNewItineraries(itineraries, arriveBy, extendPrevious) {
+        /* sort itineraries, by departure time ascending if querying
+         * by leaving time, by arrival time descending when querying
+         * by arriving time
+         */
+        if (arriveBy)
+            itineraries.sort(sortItinerariesByArrivalDesc);
+        else
+            itineraries.sort(sortItinerariesByDepartureAsc);
+
+        let newItineraries =
+            extendPrevious ? this.itineraries.concat(itineraries) : itineraries;
+
+        this.update(newItineraries);
+    }
+
+
+
     reset() {
         this._itineraries = [];
         this.bbox = null;
@@ -384,6 +407,10 @@ class Itinerary extends GObject.Object {
     get transitArrivalTimezoneOffset() {
         return this._getTransitArrivalLeg().timezoneOffset;
     }
+
+    get isWalkingOnly() {
+        return this.legs.length === 1 && !this.legs[0].isTransit;
+    }
 });
 
 var Leg = class Leg {
@@ -761,9 +788,25 @@ var Stop = class Stop {
 };
 
 function sortItinerariesByDepartureAsc(first, second) {
-    return first.departure > second.departure;
+    /* always sort walk-only itineraries first, as they would always be
+     * starting at the earliest possible departure time
+     */
+    if (first.isWalkingOnly)
+        return -1;
+    else if (second.isWalkingOnly)
+        return 1;
+    else
+        return first.departure > second.departure;
 }
 
 function sortItinerariesByArrivalDesc(first, second) {
-    return first.arrival < second.arrival;
+    /* always sort walk-only itineraries first, as they would always be
+     * ending at the latest possible arrival time
+     */
+    if (first.isWalkingOnly)
+        return -1;
+    else if (second.isWalkingOnly)
+        return 1;
+    else
+        return first.arrival < second.arrival;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]