-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor oba service and load nearby stops
- Loading branch information
1 parent
cdfcc9a
commit 1801f77
Showing
16 changed files
with
216 additions
and
54 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,17 @@ | ||
<div fxLayout="column" fxLayoutGap="10px"> | ||
<p>Add Stop</p> | ||
<button md-raised-button color="primary" (click)="done()">Done</button> | ||
</div> | ||
<section> | ||
<md-card class="example-card"> | ||
<md-card-title>Add stop</md-card-title> | ||
<md-card-content> | ||
<md-list> | ||
<md-list-item *ngFor="let stop of nearbyStops"> | ||
<button md-button (click)="addStop(stop)"> | ||
Stop# {{ stop.code }}: {{ stop.name }} ({{ stop.formattedDirection }} bound) | ||
</button> | ||
</md-list-item> | ||
</md-list> | ||
</md-card-content> | ||
<md-card-actions> | ||
<button md-raised-button color="primary" (click)="done()">Done</button> | ||
</md-card-actions> | ||
</md-card> | ||
</section> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { TestBed, inject } from '@angular/core/testing'; | ||
|
||
import { GeoService } from './geo.service'; | ||
|
||
describe('GeoService', () => { | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [GeoService] | ||
}); | ||
}); | ||
|
||
it('should be created', inject([GeoService], (service: GeoService) => { | ||
expect(service).toBeTruthy(); | ||
})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Injectable } from '@angular/core'; | ||
|
||
@Injectable() | ||
export class GeoService { | ||
|
||
locationPromise: Promise<Coordinates>; | ||
|
||
constructor() { | ||
this.locationPromise = new Promise<Coordinates>((resolve, reject) => { | ||
if (navigator.geolocation) { | ||
navigator.geolocation.getCurrentPosition(position => { | ||
resolve(position.coords); | ||
}, error => { | ||
reject(error.message); | ||
}); | ||
} else { | ||
reject('Location not available.'); | ||
} | ||
}); | ||
} | ||
|
||
getLocation(): Promise<Coordinates> { | ||
return this.locationPromise; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export interface Region { | ||
obaBaseUrl: string; | ||
regionName: string; | ||
} | ||
|
||
export class Stop { | ||
code: string; | ||
direction: string; | ||
formattedDirection: string; | ||
id: string; | ||
name: string; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { HttpClient, HttpParams } from '@angular/common/http'; | ||
import { Region, Stop } from './oba'; | ||
import * as regionsData from './regions-v3.json'; | ||
|
||
import * as urlJoin from 'url-join'; | ||
|
||
@Injectable() | ||
export class ObaService { | ||
|
||
private allRegionsPromise: Promise<Array<Region>>; | ||
private defaultRegion: Promise<Region>; | ||
private key = 'YOUR_API_KEY_HERE'; | ||
|
||
static formatDirection(direction: string): string { | ||
return direction | ||
.replace('N', 'North') | ||
.replace('S', 'South') | ||
.replace('W', 'West') | ||
.replace('E', 'East'); | ||
} | ||
|
||
constructor(private http: HttpClient) { | ||
this.allRegionsPromise = new Promise<Array<Region>>(resolve => { | ||
resolve((<any>regionsData).data.list); | ||
}); | ||
|
||
this.defaultRegion = new Promise<Region>(resolve => { | ||
this.allRegionsPromise.then(regions => { | ||
const pugetSound = regions.filter(r => r.regionName === 'Puget Sound'); | ||
if (pugetSound.length > 0) { | ||
resolve(pugetSound[0]); | ||
} | ||
resolve(regions.length ? regions[0] : null); | ||
}); | ||
}); | ||
} | ||
|
||
getRegions(): Promise<Array<Region>> { | ||
return this.allRegionsPromise; | ||
} | ||
|
||
getDefaultRegion(): Promise<Region> { | ||
return this.defaultRegion; | ||
} | ||
|
||
getNearbyStops(region: Region, coords: Coordinates): Promise<Array<Stop>> { | ||
return new Promise<Array<Stop>>((resolve, reject) => { | ||
this.http.get( | ||
urlJoin(region.obaBaseUrl, '/api/where/stops-for-location.json'), | ||
{ | ||
params: new HttpParams() | ||
.set('key', this.key) | ||
.set('lat', coords.latitude.toString()) | ||
.set('lon', coords.longitude.toString()) | ||
} | ||
).subscribe((result: any) => { | ||
const stops: Array<Stop> = result.data.list.map(s => { | ||
s['formattedDirection'] = ObaService.formatDirection(s.direction); | ||
return s; | ||
}); | ||
resolve(stops); | ||
}, error => { | ||
reject(error); | ||
}); | ||
}); | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters