blob: 620a659403a25820daa982b5e3f6cd933de52559 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
import stops from './stops.json';
export interface CachedStopList {
timestamp: number;
data: Stop[];
}
export interface Stop {
stopId: number
name: string;
latitude?: number;
longitude?: number;
lines: string[];
favourite?: boolean;
}
export class StopDataProvider {
async getStops(): Promise<Stop[]> {
const rawFavouriteStops = localStorage.getItem('favouriteStops');
let favouriteStops: number[] = [];
if (rawFavouriteStops) {
favouriteStops = JSON.parse(rawFavouriteStops) as number[];
}
return stops.map((stop: Stop) => {
return {
...stop,
favourite: favouriteStops.includes(stop.stopId)
};
});
}
addFavourite(stopId: number) {
const rawFavouriteStops = localStorage.getItem('favouriteStops');
let favouriteStops: number[] = [];
if (rawFavouriteStops) {
favouriteStops = JSON.parse(rawFavouriteStops) as number[];
}
if (!favouriteStops.includes(stopId)) {
favouriteStops.push(stopId);
localStorage.setItem('favouriteStops', JSON.stringify(favouriteStops));
}
}
removeFavourite(stopId: number) {
const rawFavouriteStops = localStorage.getItem('favouriteStops');
let favouriteStops: number[] = [];
if (rawFavouriteStops) {
favouriteStops = JSON.parse(rawFavouriteStops) as number[];
}
const newFavouriteStops = favouriteStops.filter(id => id !== stopId);
localStorage.setItem('favouriteStops', JSON.stringify(newFavouriteStops));
}
isFavourite(stopId: number): boolean {
const rawFavouriteStops = localStorage.getItem('favouriteStops');
if (rawFavouriteStops) {
const favouriteStops = JSON.parse(rawFavouriteStops) as number[];
return favouriteStops.includes(stopId);
}
return false;
}
}
|