import { Component, OnInit, OnDestroy, ViewChild, AfterViewInit } from "@angular/core"; import { ApiClientService } from "../../api"; import { MatDialog, MatSnackBar } from "@angular/material"; import { ActivatedRoute, Router } from "@angular/router"; import { MessageDialogComponent } from "../widgets/message-dialog.component"; import { HttpErrorResponse } from "@angular/common/http"; import { HttpStatusCode } from "../utils/httpcodes"; import { Subscription, BehaviorSubject } from "rxjs"; import { Device, Location, ShareInfo } from "../../api/models"; import { Snackbars } from "../utils/snackbars"; import * as L from 'leaflet'; import { TileMapComponent } from "../widgets/tile-map.component"; import { MarkerMapComponent } from "../widgets/marker-map.component"; import { ResourceService } from "../services/resource.service"; @Component({ selector: 'share', templateUrl: './share.component.html', host: { class:'match-parent' }, }) export class ShareComponent implements AfterViewInit, OnDestroy { @ViewChild(MarkerMapComponent, { static: true }) map: MarkerMapComponent; token: string; info = new BehaviorSubject(null); location = new BehaviorSubject(null); private infoSubscription: Subscription; private locationSubscription: Subscription; constructor( public res: ResourceService, private snackbar: MatSnackBar, private apiClientService: ApiClientService, private router: Router, private route: ActivatedRoute ) { } ngOnInit() { this.token = this.route.snapshot.paramMap.get("token"); this.updateInfo(); } ngAfterViewInit() { } isUpdatingInfo():boolean { return this.infoSubscription != null; } isUpdatingLocation():boolean { return this.infoSubscription != null || this.locationSubscription != null; } isOutside():boolean { if (this.route.parent != null) { return this.route.parent.snapshot.url[0].path != "privileges"; } return false; } addShare():void { this.router.navigate(["/privileges", { add: this.token}]); } updateInfo():void { if (this.infoSubscription) return; this.infoSubscription = this.apiClientService.getShareInfo(this.token).subscribe((result)=>{ this.info.next(result.body); this.infoSubscription = null; this.updateLocation(); }, (error)=>{ Snackbars.showError(this.snackbar, this.res.strings('share_no_update_info')); this.infoSubscription = null; }); } updateLocation():void { if (this.locationSubscription) return; this.locationSubscription = this.apiClientService.getShareLocation(this.token).subscribe((result)=>{ this.location.next(result.body); this.map.setMarkerPosition(result.body.coordinate); this.locationSubscription = null; }, (error)=>{ Snackbars.showError(this.snackbar, this.res.strings('share_no_update_location')); this.map.setMarkerPosition(null); this.locationSubscription = null; }); } ngOnDestroy() { if (this.locationSubscription) this.locationSubscription.unsubscribe(); if (this.infoSubscription) this.infoSubscription.unsubscribe(); } }