Files
Progetto-m152-Angular/src/app/component/list/list.component.ts
2023-05-05 11:35:33 +02:00

125 lines
3.9 KiB
TypeScript

import {Component, OnChanges, OnInit, SimpleChanges} from '@angular/core';
import {ActivatedRoute} from "@angular/router";
import {positionService} from "../../service/position.service";
import {listTranslations} from "../../interface/translations";
import {TranslateService} from "../../service/language/translate.service";
import {ReadTranslateJsonService} from "../../service/language/readTranslateJson.service";
import {LocationService} from "../../service/http/location.service";
import {LocationEntity} from "../../interface/LocationEntity";
import {WaypointsEntity} from "../../interface/WaypointsEntity";
import {WaypointService} from "../../service/http/waypoint.service"
import {cookieService} from "../../service/cookie.service";
@Component({
selector: 'app-list',
templateUrl: './list.component.html',
styleUrls: ['./list.component.css']
})
export class ListComponent implements OnInit, OnChanges {
username: string = '';
locationParams: string | undefined
locations: LocationEntity[] | undefined;
location: LocationEntity | undefined;
waypoints: WaypointsEntity[] | undefined;
positionCord: any;
isNear: boolean = true;
translations: listTranslations = {} as listTranslations
positionNotFound: boolean = false;
constructor(
private route: ActivatedRoute,
private positionService: positionService,
private locationService: LocationService,
private waypointService: WaypointService,
private cookieService: cookieService,
) {
}
async ngOnInit() {
this.username = this.cookieService.getUsername();
this.route.params.subscribe(params => {
this.locationParams = params['location'];
});
this.locationService.getLocation(this.locationParams ?? "").subscribe(location => {
this.location = location;
if (this.location.location != null || this.location.location != undefined) {
this.isNear = false;
this.waypointService.getWaypoints(this.location.location).subscribe(waypoints => {
this.waypoints = waypoints;
console.log("waypoints", this.waypoints)
this.setDistance()
});
}
});
this.locationService.getLocations().subscribe(locations => {
this.locations = locations;
console.log("locations", this.locations)
this.setDistance()
});
this.getPosition();
this.positionNotFoundFunction();
}
positionNotFoundFunction() {
if (!this.positionNotFound) {
setTimeout(() => {
if (!this.positionCord) {
this.positionNotFound = true;
}
}, 5000);
}
}
ngOnChanges(changes: SimpleChanges) {
if (changes['positionCord'] && (changes['positionCord'])) {
console.log("onChanges")
this.setDistance();
}
}
getPosition(): any {
setInterval(async () => {
this.positionCord = await this.positionService.getLocation();
this.setDistance();
}, 2000);
}
private checkDataPopulated(): void {
if (this.locations && this.location) {
console.log("Dati popolati correttamente:", this.locations, this.location);
for (let i = 0; i < this.locations.length; i++) {
if (this.locations[i].location === this.locationParams) {
this.location = this.locations[i];
console.log("Location trovata:", this.location);
this.isNear = false;
this.setDistance();
break;
}
}
}
}
private setDistance(): void {
if (this.waypoints) {
for (let i = 0; i < this.waypoints.length; i++) {
this.waypoints[i].distance = this.positionService.getDistanceBetweenCoordinates(this.waypoints[i].lat, this.waypoints[i].lon, this.positionCord.lat, this.positionCord.lon);
}
} else {
if (this.locations) {
for (let i = 0; i < this.locations.length; i++) {
this.locations[i].distance = this.positionService.getDistanceBetweenCoordinates(this.locations[i].lat, this.locations[i].lon, this.positionCord.lat, this.positionCord.lon);
}
}
}
}
}