Skip to content

Commit

Permalink
remember followRoads per-segment
Browse files Browse the repository at this point in the history
  • Loading branch information
vincens2005 committed Dec 10, 2021
1 parent e43c73e commit 52531d4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/current-run.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ describe('CurrentRun class', () => {
let currentRun = new CurrentRun(new RunStart({} as LngLat));

let initialExpectedDistance = 500;
let firstSegment = new RunSegment('some-uuid', {} as LngLat, initialExpectedDistance, {} as LineString);
let firstSegment = new RunSegment('some-uuid', {} as LngLat, initialExpectedDistance, {} as LineString, false);
let marker = getMockMarker();
currentRun.addSegment(firstSegment, marker);

expect(currentRun.distance).toBe(initialExpectedDistance, 'Distance was not set correctly from the distance response.');
expect(firstSegment.marker).toBe(marker);
expect(firstSegment.followsRoads).toBe(false);

let secondDistance = 1337;
let secondSegment = new RunSegment('different-uuid', {} as LngLat, secondDistance, {} as LineString);
let secondSegment = new RunSegment('different-uuid', {} as LngLat, secondDistance, {} as LineString, true);
currentRun.addSegment(secondSegment, getMockMarker());
expect(currentRun.distance).toBe(initialExpectedDistance + secondDistance, 'Distance did not correctly add the incoming distance response value.');
});
Expand All @@ -52,7 +53,7 @@ describe('CurrentRun class', () => {

let expectedLngLat = { lng: 101, lat: 202 } as LngLat;
let expectedDistance = 100;
let segment = new RunSegment('some-uuid', expectedLngLat, expectedDistance, {} as LineString);
let segment = new RunSegment('some-uuid', expectedLngLat, expectedDistance, {} as LineString, false);
let marker = getMockMarker();
spyOn(marker, 'remove').and.stub();
currentRun.addSegment(segment, marker);
Expand Down
4 changes: 3 additions & 1 deletion src/current-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ export class RunSegment extends RunStart {
public id: string;
public distance: number; // in meters
public geometry: LineString;
public followsRoads: boolean;

constructor(id: string, lngLat: LngLat, distance: number, geometry: LineString) {
constructor(id: string, lngLat: LngLat, distance: number, geometry: LineString, followsRoads: boolean) {
super(lngLat);
this.id = id;
this.distance = distance;
this.geometry = geometry;
this.followsRoads = followsRoads;
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ function runToJson(run: CurrentRun): string {
for (let i in run.segments) {
runJSON.segments.push({
lng: run.segments[i].lngLat.lng,
lat: run.segments[i].lngLat.lat
})
lat: run.segments[i].lngLat.lat,
followsRoads: run.segments[i].followsRoads
});
}
return JSON.stringify(runJSON);
}
Expand All @@ -172,7 +173,7 @@ function jsonToRun(json: string) {
let prev = lngLat;
for (let i = 0; i < runJSON.segments.length; i++) {
let lngLat = new LngLat(runJSON.segments[i].lng, runJSON.segments[i].lat);
if (runJSON.followRoads) {
if (runJSON.segments[i].followsRoads) {
addSegmentFromDirectionsResponse(prev, lngLat, false);
} else {
addSegmentFromStraightLine(prev, lngLat, false);
Expand Down
6 changes: 4 additions & 2 deletions src/next-segment-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export class NextSegmentService {
uuid(),
nextLngLat,
route.distance,
route.geometry as LineString
route.geometry as LineString,
true
);
} else {
throw new Error(`Non-successful status code when getting directions: ${JSON.stringify(res)}`);
Expand All @@ -76,7 +77,8 @@ export class NextSegmentService {
uuid(),
nextLngLat,
distance,
line
line,
false
);
}
}

0 comments on commit 52531d4

Please sign in to comment.