diff --git a/src/app/interfaces/bus-schedule.ts b/src/app/interfaces/bus-schedule.ts index 1d37c4f..8451d8c 100644 --- a/src/app/interfaces/bus-schedule.ts +++ b/src/app/interfaces/bus-schedule.ts @@ -53,6 +53,8 @@ export interface BusSelection { id: number; date: Date; direction: BusDirection; + startStation: string; + endStation: string; } export const BUS_LIMITS = { diff --git a/src/app/pages/coronatransport/coronatransport.page.ts b/src/app/pages/coronatransport/coronatransport.page.ts index bc5dcb3..67333bc 100644 --- a/src/app/pages/coronatransport/coronatransport.page.ts +++ b/src/app/pages/coronatransport/coronatransport.page.ts @@ -1,6 +1,6 @@ -import {Component, OnInit, Input} from '@angular/core'; -import {HttpClient} from '@angular/common/http'; -import {environment} from '../../../environments/environment'; +import { Component, OnInit, Input } from '@angular/core'; +import { HttpClient } from '@angular/common/http'; +import { environment } from '../../../environments/environment'; import { BusSchedule, BusDirection, @@ -11,11 +11,11 @@ import { BusStatus, BUS_LIMITS } from '../../interfaces/bus-schedule'; -import {FormBuilder, FormGroup, Validators} from '@angular/forms'; -import {ModalController, Platform, PopoverController, ToastController} from '@ionic/angular'; -import {CoronatransportpopoverComponent} from '../../components/coronatransportpopover/coronatransportpopover.component'; -import {CoronaBusService} from '../../services/corona-bus.service'; -import {SoapApiHelper} from 'src/app/classes/api-helper'; +import { FormBuilder, FormGroup, Validators } from '@angular/forms'; +import { ModalController, Platform, PopoverController, ToastController } from '@ionic/angular'; +import { CoronatransportpopoverComponent } from '../../components/coronatransportpopover/coronatransportpopover.component'; +import { CoronaBusService } from '../../services/corona-bus.service'; +import { SoapApiHelper } from 'src/app/classes/api-helper'; @Component({ @@ -38,11 +38,11 @@ export class CoronatransportPage implements OnInit { public limits = BUS_LIMITS; constructor(private http: HttpClient, - private _FB: FormBuilder, - private modalController: ModalController, - private popoverController: PopoverController, - private toastController: ToastController, - private busService: CoronaBusService) { + private _FB: FormBuilder, + private modalController: ModalController, + private popoverController: PopoverController, + private toastController: ToastController, + private busService: CoronaBusService) { this.arrivalForm = _FB.group({ 'time': ['', Validators.required], 'from': ['', Validators.required], @@ -57,10 +57,10 @@ export class CoronatransportPage implements OnInit { ngOnInit() { this.http.get(this.assets + '/busfahrplan.json') - .subscribe(data => { - this.busSchedule = data; - this.initForm(); - }); + .subscribe(data => { + this.busSchedule = data; + this.initForm(); + }); this.loadBusesStatus(); this.updateSelection(); } @@ -106,7 +106,7 @@ export class CoronatransportPage implements OnInit { */ public getArrivalSelection(): BusSelection { return this.selections.find(selection => selection.date.getTime() === this.date.getTime() - && selection.direction === BusDirection.ToPSI); + && selection.direction === BusDirection.ToPSI); } /** @@ -114,7 +114,7 @@ export class CoronatransportPage implements OnInit { */ public getDepartureSelection(): BusSelection { return this.selections.find(selection => selection.date.getTime() === this.date.getTime() - && selection.direction === BusDirection.FromPSI); + && selection.direction === BusDirection.FromPSI); } /** @@ -123,7 +123,7 @@ export class CoronatransportPage implements OnInit { * @param id unique id of the bus */ public getBusStatus(id: number): BusInformation { - return this.busesStatus.find(status => status.id == id) || {id, currentNb: 0, status: BusStatus.Ok}; + return this.busesStatus.find(status => status.id == id) || { id, currentNb: 0, status: BusStatus.Ok }; } /** @@ -132,7 +132,8 @@ export class CoronatransportPage implements OnInit { * @param id unique id of the selected bus. */ public selectArrivalBusSchedule(id: number) { - this.busService.selectBusAsync(this.date, id, BusDirection.ToPSI) + this.busService.selectBusAsync(this.date, id, BusDirection.ToPSI, + this.arrivalForm.controls['from'].value, this.arrivalForm.controls['to'].value) .then(_ => { this.updateSelection(); this.loadBusesStatus(); @@ -145,7 +146,8 @@ export class CoronatransportPage implements OnInit { * @param id unique id of the selected bus. */ public selectDepartureBusSchedule(id: number) { - this.busService.selectBusAsync(this.date, id, BusDirection.FromPSI) + this.busService.selectBusAsync(this.date, id, BusDirection.FromPSI, + this.departureForm.controls['from'].value, this.departureForm.controls['to'].value) .then(_ => { this.updateSelection(); this.loadBusesStatus(); @@ -178,14 +180,16 @@ export class CoronatransportPage implements OnInit { * It is currently not possible to say, where a user wanted to leave or enter the bus at PSI. */ private initForm(): void { + let initDateTime = new Date(this.date.toDateString() + ' ' + new Date().toTimeString); + this.arrivalForm.controls['time'].setValue(initDateTime.toISOString()); let arrivalSelection = this.getArrivalSelection(); if (arrivalSelection) { let bus = this.getArrivalBus(arrivalSelection.id); if (bus) { let startDate = new Date(this.date.toDateString() + ' ' + bus.start_time); this.arrivalForm.controls['time'].setValue(startDate.toISOString()); - this.arrivalForm.controls['from'].setValue(bus.start_station); - this.arrivalForm.controls['to'].setValue('e'); + this.arrivalForm.controls['from'].setValue(arrivalSelection.startStation); + this.arrivalForm.controls['to'].setValue(arrivalSelection.endStation); } } let departureSelection = this.getDepartureSelection(); @@ -194,8 +198,8 @@ export class CoronatransportPage implements OnInit { if (bus) { let startDate = new Date(this.date.toDateString() + ' ' + bus.start_time_east); this.departureForm.controls['time'].setValue(startDate.toISOString()); - this.departureForm.controls['from'].setValue('e'); - this.departureForm.controls['to'].setValue(bus.end_station); + this.departureForm.controls['from'].setValue(departureSelection.startStation); + this.departureForm.controls['to'].setValue(departureSelection.endStation); } } this.filterBusschedule(); @@ -213,7 +217,7 @@ export class CoronatransportPage implements OnInit { */ private loadBusesStatus(): void { this.busService.getBusesStatus(this.date) - .subscribe(data => this.busesStatus = data, error => SoapApiHelper.handleError(this.toastController, error)); + .subscribe(data => this.busesStatus = data, error => SoapApiHelper.handleError(this.toastController, error)); } // closes the settings modal diff --git a/src/app/services/corona-bus.service.ts b/src/app/services/corona-bus.service.ts index 40dafe1..024753f 100644 --- a/src/app/services/corona-bus.service.ts +++ b/src/app/services/corona-bus.service.ts @@ -23,7 +23,7 @@ export class CoronaBusService { * @param id unique id of the bus * @param direction direction of the bus */ - public async selectBusAsync(date: Date, id: number, direction: BusDirection): Promise { + public async selectBusAsync(date: Date, id: number, direction: BusDirection, startStation: string, endStation: string): Promise { date = new Date(date.toDateString()); return new Promise((resolve, reject) => { if (this.isOtherSelected(date)) { @@ -33,7 +33,7 @@ export class CoronaBusService { .subscribe(__ => { this.removeSelection(date, BusDirection.ToPSI); this.removeSelection(date, BusDirection.FromPSI); - this.saveSelection(date, id, direction); + this.saveSelection(date, id, direction, startStation, endStation); resolve(); }, error => reject(error)); }, error => reject(error)); @@ -43,14 +43,14 @@ export class CoronaBusService { .subscribe(_ => { this.addPersonToBus(date, id) .subscribe(__ => { - this.saveSelection(date, id, direction); + this.saveSelection(date, id, direction, startStation, endStation); resolve(); }, error => reject(error)); }, error => reject(error)); } else { this.addPersonToBus(date, id) .subscribe(_ => { - this.saveSelection(date, id, direction); + this.saveSelection(date, id, direction, startStation, endStation); resolve(); }, error => { reject(error); @@ -85,8 +85,8 @@ export class CoronaBusService { this.addPersonToNonBus(date) .subscribe(__ => { - this.saveSelection(date, 0, BusDirection.ToPSI); - this.saveSelection(date, 0, BusDirection.FromPSI); + this.saveSelection(date, 0, BusDirection.ToPSI, '', ''); + this.saveSelection(date, 0, BusDirection.FromPSI, '', ''); resolve(); }, error => reject(error)); }); @@ -144,8 +144,13 @@ export class CoronaBusService { return []; } let selections = JSON.parse(busStorage); - selections = selections.map(row => { id: row.id, date: new Date(row.date), direction: row.direction }) - .filter(selection => selection.date.getTime() >= today.getTime()); + selections = selections.map(row => { + id: row.id, + date: new Date(row.date), + direction: row.direction, + startStation: row.startStation, + endStation: row.endStation + }).filter(selection => selection.date.getTime() >= today.getTime()); return selections; } @@ -156,14 +161,15 @@ export class CoronaBusService { * @param date date of travel without hours, minutes and seconds * @param id id of the bus course */ - private saveSelection(date: Date, id: number, direction: BusDirection): void { + private saveSelection(date: Date, id: number, direction: BusDirection, startStation: string, endStation: string): void { date = new Date(date.toDateString()); // make sure the date has no time let selections = this.loadSelections(); - let index = selections.findIndex(selection => selection.date.getTime() === date.getTime() && selection.direction === direction); + let index = selections.findIndex(row => row.date.getTime() === date.getTime() && row.direction === direction); + let selection: BusSelection = { id, date, direction, startStation, endStation }; if (index >= 0) { - selections[index].id = id; + selections[index] = selection; } else { - selections.push({ id, date, direction }); + selections.push(selection); } localStorage.setItem(CoronaBusService.STORAGE_NAME, JSON.stringify(selections)); } @@ -177,7 +183,7 @@ export class CoronaBusService { private removeSelection(date: Date, direction: BusDirection): void { let selections = this.loadSelections(); let index = selections.findIndex(row => row.date.getTime() === date.getTime() && row.direction === direction); - selections = selections.splice(index, 1); + selections.splice(index, 1); localStorage.setItem(CoronaBusService.STORAGE_NAME, JSON.stringify(selections)); }