From 05bca5181415e44e392e90d7562be9a144c1bda9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=20Ku=CC=88ng?= Date: Sat, 22 Apr 2023 12:00:20 +0200 Subject: [PATCH] list component changes --- src/app/list/list.component.html | 24 ++++++++------ src/app/list/list.component.ts | 48 +++++++++++++++++++++------- src/app/service/position.service.ts | 3 +- src/app/service/readjson.service.ts | 19 +++++++++-- src/app/service/translate.service.ts | 4 +-- 5 files changed, 71 insertions(+), 27 deletions(-) diff --git a/src/app/list/list.component.html b/src/app/list/list.component.html index 2d02414..30825f7 100644 --- a/src/app/list/list.component.html +++ b/src/app/list/list.component.html @@ -1,21 +1,25 @@
- +

+ Posizione {{locationParams}} non trovata +

+
+

+ {{locations.location}} +

+

Distance: {{distance[i]}}

+
-
+

{{location.location}}

-
+ {{location.waypoints[0].name}} +

- {{waypoinst.name}} + {{waypoinst.name}}

-

Distance: {{getDistance(waypoinst.lat, location.lon)}}

+

Distance: {{distance[i]}}

- - -

- -

diff --git a/src/app/list/list.component.ts b/src/app/list/list.component.ts index 4264bd7..a9486f5 100644 --- a/src/app/list/list.component.ts +++ b/src/app/list/list.component.ts @@ -1,4 +1,4 @@ -import {Component, OnInit} from '@angular/core'; +import {Component, OnInit, SimpleChanges, OnChanges} from '@angular/core'; import {Locations} from "../interface/data"; import {ReadjsonService} from "../service/readjson.service"; import {ActivatedRoute} from "@angular/router"; @@ -9,8 +9,8 @@ import {positionService} from "../service/position.service"; templateUrl: './list.component.html', styleUrls: ['./list.component.css'] }) -export class ListComponent implements OnInit { - private locationParams: string | undefined +export class ListComponent implements OnInit, OnChanges { + locationParams: string | undefined locations: Partial[] | undefined; location: Partial | undefined; @@ -18,7 +18,8 @@ export class ListComponent implements OnInit { isNear: boolean = true; - distance: number = 0; + distance: number[] = []; + constructor(private route: ActivatedRoute, private readjsonService: ReadjsonService, private positionService: positionService) { } @@ -30,15 +31,28 @@ export class ListComponent implements OnInit { this.readjsonService.getLocations().subscribe(locations => { this.locations = locations; if (this.locationParams != null) { - this.readjsonService.getLocation(this.locationParams).subscribe(location => { + this.readjsonService.getLocation(this.locationParams ?? "").subscribe(async location => { this.location = location; - this.checkDataPopulated(); + this.readjsonService.getWaypoints(this.locationParams ?? "").subscribe(waypoints => { + if (this.location) { + this.location.waypoints = waypoints ?? [] + } + }); + await this.checkDataPopulated(); }); } }); - this.setDistance(); + } + ngOnChanges(changes: SimpleChanges) { + if (changes['positionCord'] && (changes['positionCord'])) { + console.log("onChanges") + this.setDistance(); + } + } + + private checkDataPopulated(): void { if (this.locations && this.location) { console.log("Dati popolati correttamente:", this.locations, this.location); @@ -47,6 +61,7 @@ export class ListComponent implements OnInit { this.location = this.locations[i]; console.log("Location trovata:", this.location); this.isNear = false; + this.setDistance(); break; } } @@ -54,10 +69,20 @@ export class ListComponent implements OnInit { } private setDistance(): void { - if (this.location && this.isNear) { - this.distance = this.positionService.getDistanceBetweenCoordinates(this.location.lat, this.location.lon, this.positionCord.lat, this.positionCord.lon); - console.log("Distanza: " + this.distance + " km"); - } + + const intervalId = setInterval(() => { + if (this.location) { + if (this.location?.waypoints) { + console.log("setDistance"+this.location); + for (let i = 0; i < this.location.waypoints.length; i++) { + console.log("for") + this.distance.push(this.positionService.getDistanceBetweenCoordinates(this.location.waypoints[i].lat, this.location?.lon, this.positionCord.lat, this.positionCord.lon)); + } + clearInterval(intervalId); + } + console.log("ciao" + this.distance[0]) + }}, 1000); + //da aggiungere il cambiamento in tutti i punti, forse fatto ma sono stanco } getDistance(latLocation: number | undefined, lonLocation: number | undefined): any { @@ -71,4 +96,5 @@ export class ListComponent implements OnInit { }, 1000); } + } diff --git a/src/app/service/position.service.ts b/src/app/service/position.service.ts index c741aa2..2379f5b 100644 --- a/src/app/service/position.service.ts +++ b/src/app/service/position.service.ts @@ -46,7 +46,7 @@ export class positionService{ return deg * (Math.PI / 180) } - async getLocation() { + async getLocation(): Promise { console.log('get location'); return new Promise((resolve, reject) => { if (navigator.geolocation) { @@ -65,4 +65,5 @@ export class positionService{ }); } + } diff --git a/src/app/service/readjson.service.ts b/src/app/service/readjson.service.ts index af834a9..d9b65cf 100644 --- a/src/app/service/readjson.service.ts +++ b/src/app/service/readjson.service.ts @@ -40,17 +40,32 @@ export class ReadjsonService{ } - getWaypoints(location: string, id: number): Observable { + getWaypoint(location: string, id: number): Observable { return this.locations.pipe( map((locations) => { const foundLocation: Locations | undefined = locations.find((loc: Locations) => loc.location === location); if (foundLocation?.waypoints) { return foundLocation ? foundLocation.waypoints.filter((way: waypoint) => way.id === id) : []; } else { - return []; + return [] } }), tap(data => console.log("data requested", data)) ); } + + getWaypoints(location: string): Observable { + return this.locations.pipe( + map((locations) => { + const foundLocation: Locations | undefined = locations.find((loc: Locations) => loc.location === location); + if (foundLocation?.waypoints) { + return foundLocation ? foundLocation.waypoints : []; + } else { + return [] + } + }), + tap(data => console.log("data requested", data)) + ); + } + } diff --git a/src/app/service/translate.service.ts b/src/app/service/translate.service.ts index 179d71b..5c08f17 100644 --- a/src/app/service/translate.service.ts +++ b/src/app/service/translate.service.ts @@ -5,9 +5,7 @@ import {Injectable} from "@angular/core"; providedIn: 'root' }) export class TranslateService { - constructor(private deepLService: DeepLService) { - - } + constructor(private deepLService: DeepLService) {} async getData(input: string, lang: string): Promise { const response = await this.deepLService.translate(input, lang).toPromise();