import { Component , OnDestroy , OnInit } from "@angular/core" ; import { UserSettingsService } from "../shared/services/user-settings/user-settings.service" ; import * as _ from "lodash" ; import { ZONE_DEFINITIONS } from "./zone-definitions" ; import { ZonesService } from "./shared/zones.service" ; import { ActivatedRoute , Router } from "@angular/router" ; import { AppRoutesModel } from "../shared/models/app-routes.model" ; import { UserSettingsModel , UserZonesModel , ZoneModel } from "@elevate/shared/models" ; import { ZoneDefinitionModel } from "../shared/models/zone-definition.model" ; import { Subscription } from "rxjs" ; import { MatSnackBar } from "@angular/material" ; import { userSettingsData } from "@elevate/shared/data" ; @ Component ( { selector : "app-zones-settings" , templateUrl : "./zones-settings.component.html" , styleUrls : [ "./zones-settings.component.scss" ] } ) export class ZonesSettingsComponent implements OnInit , OnDestroy { public static DEFAULT_ZONE_VALUE = "heartRate" ; public zoneDefinitions : ZoneDefinitionModel [ ] = ZONE_DEFINITIONS ; public zoneDefinitionSelected : ZoneDefinitionModel ; public userZonesModel : UserZonesModel ; public currentZones : ZoneModel [ ] ; public routeParamsSubscription : Subscription ; public zonesUpdatesSubscription : Subscription ; public areZonesLoaded = false ; constructor ( public userSettingsService , public route , public router , public zonesService , public snackBar ) { } public ngOnInit ( ) { this . routeParamsSubscription = this . route . params . subscribe ( routeParams => { this . userSettingsService . fetch ( ) . then ( ( userSettingsSynced ) => { this . userZonesModel = UserZonesModel . asInstance ( userSettingsSynced . zones ) ; let zoneDefinition = null ; const hasZoneValueInRoute = ! _ . isEmpty ( routeParams . zoneValue ) ; if ( hasZoneValueInRoute && _ . has ( userSettingsData . zones , routeParams . zoneValue ) ) { zoneDefinition = this . getZoneDefinitionFromZoneValue ( routeParams . zoneValue ) ; } else { this . navigateToZone ( ZonesSettingsComponent . DEFAULT_ZONE_VALUE ) ; return ; } try { this . loadZonesFromDefinition ( zoneDefinition ) ; } catch ( error ) { const snackBarRef = this . snackBar . open ( "Your zones are corrupted. Reset your settings from advanced menu." , "Go to advanced menu" , { verticalPosition : "top" } ) ; const subscription = snackBarRef . onAction ( ) . subscribe ( ( ) => { this . router . navigate ( [ AppRoutesModel . advancedMenu ] ) ; subscription . unsubscribe ( ) ; } ) ; } } ) ; } ) ; this . zonesUpdatesSubscription = this . zonesService . zonesUpdates . subscribe ( ( updatedZones ) => { this . currentZones = updatedZones ; } ) ; } param { string } zoneValue returns { ZoneDefinitionModel } private getZoneDefinitionFromZoneValue ( zoneValue ) { return _ . find ( this . zoneDefinitions , { value : zoneValue } ) ; } param { ZoneDefinitionModel } zoneDefinition private loadZonesFromDefinition ( zoneDefinition ) { this . currentZones = this . userZonesModel . get ( zoneDefinition . value ) ; this . zonesService . currentZones = this . currentZones ; this . zonesService . zoneDefinition = zoneDefinition ; this . zoneDefinitionSelected = zoneDefinition ; _ . defer ( ( ) => { this . areZonesLoaded = true ; } ) ; } /**
     *
     */ public onZoneDefinitionSelected ( zoneDefinition ) { this . areZonesLoaded = false ; this . navigateToZone ( zoneDefinition . value ) ; } param { string } zoneValue private navigateToZone ( zoneValue ) { const selectedZoneUrl = AppRoutesModel . zonesSettings + "/" + zoneValue ; this . router . navigate ( [ selectedZoneUrl ] ) ; } /**
     *
     */ public ngOnDestroy ( ) { this . routeParamsSubscription . unsubscribe ( ) ; this . zonesUpdatesSubscription . unsubscribe ( ) ; } }