From f942f8df78a97d302d4ccca8a87e8a410f845dbe Mon Sep 17 00:00:00 2001 From: Ariel Costas Guerrero <94913521+arielcostas@users.noreply.github.com> Date: Mon, 9 Sep 2024 18:56:46 +0200 Subject: Add recent stop list --- src/data/StopDataProvider.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/data') diff --git a/src/data/StopDataProvider.ts b/src/data/StopDataProvider.ts index 620a659..6245e7a 100644 --- a/src/data/StopDataProvider.ts +++ b/src/data/StopDataProvider.ts @@ -62,4 +62,30 @@ export class StopDataProvider { } return false; } + + RECENT_STOPS_LIMIT = 10; + + pushRecent(stopId: number) { + const rawRecentStops = localStorage.getItem('recentStops'); + let recentStops: Set = new Set(); + if (rawRecentStops) { + recentStops = new Set(JSON.parse(rawRecentStops) as number[]); + } + + recentStops.add(stopId); + if (recentStops.size > this.RECENT_STOPS_LIMIT) { + const iterator = recentStops.values(); + recentStops.delete(iterator.next().value); + } + + localStorage.setItem('recentStops', JSON.stringify(Array.from(recentStops))); + } + + getRecent(): number[] { + const rawRecentStops = localStorage.getItem('recentStops'); + if (rawRecentStops) { + return JSON.parse(rawRecentStops) as number[]; + } + return []; + } } \ No newline at end of file -- cgit v1.3