Implemented read locations from json, minor bug fixes and implementations

This commit is contained in:
grata
2023-04-05 13:49:27 +02:00
parent 1d32976a2e
commit 8c22716f67
3 changed files with 78 additions and 88 deletions

View File

@@ -8,15 +8,15 @@
(keydown)="selezionaSuggerimento($event)" placeholder="Type here" (keydown)="selezionaSuggerimento($event)" placeholder="Type here"
class="input input-bordered input-primary w-full max-w-xs"> class="input input-bordered input-primary w-full max-w-xs">
<ng-container *ngIf="luoghiPopup | async as luoghi;"> <ng-container *ngIf="locationsPopup | async as locations;">
<ul *ngIf="luoghi.length > 0" class="menu bg-base-200 !w-fit p-2 rounded-box" id="list"> <ul *ngIf="locations.length > 0" class="menu bg-base-200 !w-fit p-2 rounded-box" id="list">
<li class="menu-title"> <li class="menu-title">
<span>Places</span> <span>Places</span>
</li> </li>
<li *ngFor="let luogo of luoghi"> <li *ngFor="let luogo of locations" (click)="luogoSelezionato=luogo.location; cercaLuogo(luogo.location)" >
{{luogo.nome}} {{luogo.location}}
</li> </li>
<li class="menu-title"> <li class="menu-title">

View File

@@ -1,5 +1,5 @@
import {AfterViewInit, Component, ElementRef, OnDestroy, OnInit, ViewChild} from '@angular/core'; import {AfterViewInit, Component, ElementRef, OnDestroy, OnInit, ViewChild} from '@angular/core';
import {distinctUntilChanged, fromEvent, Subject, Subscription} from "rxjs"; import {distinctUntilChanged, fromEvent, Observable, Subject, Subscription} from "rxjs";
import {ReadjsonService} from "../service/readjson.service"; import {ReadjsonService} from "../service/readjson.service";
import {Locations} from "../interface/data"; import {Locations} from "../interface/data";
import * as QRCode from 'qrcode'; import * as QRCode from 'qrcode';
@@ -19,70 +19,69 @@ export class HomeComponent implements OnInit, AfterViewInit, OnDestroy {
@ViewChild('myInput') myInput?: ElementRef; @ViewChild('myInput') myInput?: ElementRef;
@ViewChild('myCanvas') myCanvas?: ElementRef<HTMLCanvasElement>; @ViewChild('myCanvas') myCanvas?: ElementRef<HTMLCanvasElement>;
luoghiPopup: Subject<Luogo[]> = new Subject<Luogo[]>() public locationsPopup: Subject<Locations[]> = new Subject<Locations[]>()
subs: Subscription[] = [] subs: Subscription[] = []
latitude: number | undefined;
longitude: number | undefined;
backgroundColor: string | undefined; backgroundColor: string | undefined;
qrCodeImage: string | undefined; qrCodeImage: string | undefined;
locations: Locations[] = [ locations: Locations[] = [];
{location: 'Locarno', region: 'Ticino', lat: 46.1704, lon: 8.7931},
{location: 'Lugano', region: 'Ticino', lat: 46.0037, lon: 8.9511},
{location: 'Luzern', region: 'Luzern', lat: 47.0502, lon: 8.3093},
{location: 'Lauterbrunnen', region: 'Bern', lat: 46.5939, lon: 7.9085}
];
luoghi: Luogo[] = [
{location: 'Locarno', lat: 46.1704, lon: 8.7931},
{location: 'Lugano', lat: 46.0037, lon: 8.9511},
{location: 'Luzern', lat: 47.0502, lon: 8.3093},
{location: 'Lauterbrunnen', lat: 46.5939, lon: 7.9085}
];
locationsFiltrati: Locations[] = []; locationsFiltrati: Locations[] = [];
luoghiFiltrati: Luogo[] = [];
luogoSelezionato: string = ''; luogoSelezionato: string = '';
suggerimentoAttivo: boolean = false; suggerimentoAttivo: boolean = false;
suggerimento: string = ''; suggerimento: string = '';
completamento: string = 'ciao'; completamento: string = 'ciao';
@ViewChild('myInput') myInput?: ElementRef;
@ViewChild('canvas') canvasRef: ElementRef | undefined; @ViewChild('canvas') canvasRef: ElementRef | undefined;
canvas: any; canvas: any;
ctx: any; ctx: any;
img: any; img: any;
constructor(private service: ReadjsonService) {} constructor(private readjsonService: ReadjsonService) {
}
ngOnInit(): void { ngOnInit(): void {
this.readjsonService.getLocations().subscribe(data => {
for (let i = 0; i < data.length; i++) {
this.locations.push(<Locations>data[i])
console.log(data[i])
}
});
console.log("home init"); console.log("home init");
this.subs.push(this.service.getLocation("Lugano").subscribe(val => console.log(val))) this.subs.push(this.readjsonService.getLocation("Lugano").subscribe(val => console.log(val)))
const text = 'https://aramisgrata.ch'; // sostituisci con la tua stringa const text = 'https://aramisgrata.ch'; // sostituisci con la tua stringa
QRCode.toDataURL(text, {errorCorrectionLevel: 'H'}, (err, url) => { QRCode.toDataURL(text, {errorCorrectionLevel: 'H'}, (err, url) => {
this.qrCodeImage = url; this.qrCodeImage = url;
}); });
} }
ngOnDestroy() {
this.subs.forEach(sub => sub.unsubscribe())
}
ngAfterViewInit() { ngAfterViewInit() {
console.log("canvas", this.myCanvas?.nativeElement);
const canvas = this.myCanvas?.nativeElement; const canvas = this.myCanvas?.nativeElement;
console.log("canvas 2", canvas);
if (canvas) if (canvas)
this.animateClouds(canvas); this.animateClouds(canvas);
if (this.locations != undefined) {
fromEvent(this.myInput?.nativeElement, 'focus').pipe( fromEvent(this.myInput?.nativeElement, 'focus').pipe(
// debounceTime(500), decommentarlo se bisogna fare una chiamata http // debounceTime(500), decommentarlo se bisogna fare una chiamata http
distinctUntilChanged() distinctUntilChanged()
).subscribe((val: any) => { ).subscribe((val: any) => {
this.luoghiPopup.next(this.locations.filter(l => l.location.toLowerCase().startsWith(val.target.value.toLowerCase()) this.locationsPopup.next(this.locations.filter(l => l.location.toLowerCase().startsWith(val.target.value.toLowerCase())))
))}) })
}
this.canvas = this.canvasRef?.nativeElement; this.canvas = this.canvasRef?.nativeElement;
if (this.canvas) {
this.ctx = this.canvas.getContext('2d'); this.ctx = this.canvas.getContext('2d');
this.img = new Image(); this.img = new Image();
@@ -100,24 +99,20 @@ export class HomeComponent implements OnInit, AfterViewInit, OnDestroy {
this.ctx.drawImage(qrCode, x, y, qrCodeSize, qrCodeSize); this.ctx.drawImage(qrCode, x, y, qrCodeSize, qrCodeSize);
} }
} }
this.img.src = 'src/assets/img/mountains.png';
this.img.src = 'src/assets/img/mountains.png';
}
fromEvent(this.myInput?.nativeElement, 'input') fromEvent(this.myInput?.nativeElement, 'input')
.pipe( .pipe(
// debounceTime(500), decommentarlo se bisogna fare una chiamata http // debounceTime(500), decommentarlo se bisogna fare una chiamata http
distinctUntilChanged() distinctUntilChanged()
).subscribe((val: any) => { ).subscribe((val: any) => {
this.luoghiPopup.next(this.luoghi.filter(l => l.location.toLowerCase().startsWith(val.target.value.toLowerCase()))) this.locationsPopup.next(this.locations.filter(l => l.location.toLowerCase().startsWith(val.target.value.toLowerCase())))
}) })
} }
ngOnDestroy() {
this.subs.forEach(sub => sub.unsubscribe())
}
animateClouds(canvas: HTMLCanvasElement): void { animateClouds(canvas: HTMLCanvasElement): void {
console.log("animating clouds")
let x = -200; let x = -200;
let y = 100; let y = 100;
let speed = 2; let speed = 2;
@@ -138,7 +133,6 @@ export class HomeComponent implements OnInit, AfterViewInit, OnDestroy {
} }
drawCloud(x: number, y: number, ctx: CanvasRenderingContext2D) { drawCloud(x: number, y: number, ctx: CanvasRenderingContext2D) {
console.log("xy:"+ x, y)
ctx.beginPath(); ctx.beginPath();
ctx.arc(x, y, 50, 0, 2 * Math.PI); ctx.arc(x, y, 50, 0, 2 * Math.PI);
ctx.arc(x + 25, y - 25, 50, 0, 2 * Math.PI); ctx.arc(x + 25, y - 25, 50, 0, 2 * Math.PI);
@@ -163,13 +157,6 @@ export class HomeComponent implements OnInit, AfterViewInit, OnDestroy {
this.myInput?.nativeElement.focus(); this.myInput?.nativeElement.focus();
} }
consigliaSuggerimento(locations: string) {
if (this.suggerimentoAttivo) {
this.luogoSelezionato = locations + "-" + this.completamento;
this.suggerimento = '';
}
}
selezionaSuggerimento(event: KeyboardEvent) { selezionaSuggerimento(event: KeyboardEvent) {
if (event.key === 'Tab' || event.key === 'Enter') { if (event.key === 'Tab' || event.key === 'Enter') {
if (this.suggerimentoAttivo) { if (this.suggerimentoAttivo) {
@@ -185,6 +172,7 @@ export class HomeComponent implements OnInit, AfterViewInit, OnDestroy {
return null; return null;
} }
protected readonly Event = Event;
} }
function stringDifference(str1: string, str2: string): string { function stringDifference(str1: string, str2: string): string {
@@ -196,4 +184,3 @@ function stringDifference(str1: string, str2: string): string {
} }
return diff; return diff;
} }

View File

@@ -10,10 +10,10 @@ import {BehaviorSubject, map, Observable, tap} from "rxjs";
export class ReadjsonService{ export class ReadjsonService{
private locations: BehaviorSubject<Locations[]> = new BehaviorSubject<Locations[]>([]); private locations: BehaviorSubject<Locations[]> = new BehaviorSubject<Locations[]>([]);
constructor(private http: HttpClient) { constructor(private http: HttpClient) {
this.http.get<Locations[]>('assets/data.json').subscribe(data => { this.http.get<Locations[]>('assets/data.json').subscribe(data => {
this.locations.next(data) this.locations.next(data)
console.log("data loaded", data)
}); });
} }
getLocations(): Observable<Partial<Locations>[]> { getLocations(): Observable<Partial<Locations>[]> {
@@ -38,7 +38,7 @@ export class ReadjsonService{
tap(data => console.log("data requested", data)) tap(data => console.log("data requested", data))
); );
} }
/*
getWaypoints(location: string, id: number): Observable<waypoint[]> { getWaypoints(location: string, id: number): Observable<waypoint[]> {
return this.locations.pipe( return this.locations.pipe(
map((locations) => { map((locations) => {
@@ -49,4 +49,7 @@ export class ReadjsonService{
); );
} }
*/
} }