import * as _ from "lodash" ; import * as moment from "moment" ; import { Component , Inject , OnInit } from "@angular/core" ; import { MAT_DIALOG_DATA , MatDialogRef , MatSnackBar } from "@angular/material" ; import { HeartRateImpulseMode } from "../shared/enums/heart-rate-impulse-mode.enum" ; import { FitnessTrendConfigDialogData } from "../shared/models/fitness-trend-config-dialog-data.model" ; @ Component ( { selector : "app-fitness-trend-config-dialog" , templateUrl : "./fitness-trend-config-dialog.component.html" , styleUrls : [ "./fitness-trend-config-dialog.component.scss" ] } ) export class FitnessTrendConfigDialogComponent implements OnInit { public static readonly MAX_WIDTH : string = "80%" ; public static readonly MIN_WIDTH : string = "40%" ; public readonly HeartRateImpulseMode = HeartRateImpulseMode ; public ignoreActivityNamePatterns : string = null ; public ignoreBeforeDate : Date = null ; public ignoreBeforeMaxDate : Date = null ; public isEstimatedPowerStressScoreToggleEnabled : boolean ; public isEstimatedRunningStressScoreToggleEnabled : boolean ; public initialized = false ; constructor ( public dialogRef , @ Inject ( MAT_DIALOG_DATA ) public fitnessTrendConfigDialogData , public snackBar ) { } public ngOnInit ( ) { if ( this . fitnessTrendConfigDialogData . fitnessTrendConfigModel . ignoreActivityNamePatterns ) { this . ignoreActivityNamePatterns = this . formatPatternsForDisplay ( this . fitnessTrendConfigDialogData . fitnessTrendConfigModel . ignoreActivityNamePatterns ) ; } if ( this . fitnessTrendConfigDialogData . fitnessTrendConfigModel . ignoreBeforeDate ) { this . ignoreBeforeDate = moment ( this . fitnessTrendConfigDialogData . fitnessTrendConfigModel . ignoreBeforeDate ) . toDate ( ) ; } if ( this . fitnessTrendConfigDialogData . lastFitnessActiveDate ) { this . ignoreBeforeMaxDate = this . fitnessTrendConfigDialogData . lastFitnessActiveDate ; } this . updateEstimatedStressScoresToggles ( ) ; _ . defer ( ( ) => { this . initialized = true ; } ) ; } public updateEstimatedStressScoresToggles ( ) { this . isEstimatedPowerStressScoreToggleEnabled = this . fitnessTrendConfigDialogData . isPowerMeterEnabled && this . fitnessTrendConfigDialogData . fitnessTrendConfigModel . heartRateImpulseMode === HeartRateImpulseMode . HRSS ; this . isEstimatedRunningStressScoreToggleEnabled = this . fitnessTrendConfigDialogData . fitnessTrendConfigModel . heartRateImpulseMode === HeartRateImpulseMode . HRSS ; } public onSave ( ) { this . dialogRef . close ( this . fitnessTrendConfigDialogData . fitnessTrendConfigModel ) ; } public onCancel ( ) { this . dialogRef . close ( ) ; } public onModeChange ( ) { this . fitnessTrendConfigDialogData . fitnessTrendConfigModel . heartRateImpulseMode = Number ( this . fitnessTrendConfigDialogData . fitnessTrendConfigModel . heartRateImpulseMode ) ; this . updateEstimatedStressScoresToggles ( ) ; } public onInitialFitnessChange ( ) { if ( this . fitnessTrendConfigDialogData . fitnessTrendConfigModel . initializedFitnessTrendModel . ctl < 0 ) { this . fitnessTrendConfigDialogData . fitnessTrendConfigModel . initializedFitnessTrendModel . ctl = null ; this . snackBar . open ( "Invalid value entered. Reset to default value." , "Close" , { duration : 2500 } ) ; } } public onInitialFatigueChange ( ) { if ( this . fitnessTrendConfigDialogData . fitnessTrendConfigModel . initializedFitnessTrendModel . atl < 0 ) { this . fitnessTrendConfigDialogData . fitnessTrendConfigModel . initializedFitnessTrendModel . atl = null ; this . snackBar . open ( "Invalid value entered. Reset to default value." , "Close" , { duration : 2500 } ) ; } } public onIgnoreActivityNamePatternsChange ( ) { this . fitnessTrendConfigDialogData . fitnessTrendConfigModel . ignoreActivityNamePatterns = this . formatPatternsForStorage ( this . ignoreActivityNamePatterns ) ; } public onIgnoreBeforeDateChange ( ) { this . fitnessTrendConfigDialogData . fitnessTrendConfigModel . ignoreBeforeDate = ( this . ignoreBeforeDate ) ? this . ignoreBeforeDate . toISOString ( ) : null ; } public formatPatternsForStorage ( userInputPatterns ) { let patternsList = userInputPatterns . split ( /\r?\n/ ) ; patternsList = _ . map ( patternsList , ( pattern ) => { return pattern . trim ( ) ; } ) ; patternsList = _ . compact ( patternsList ) ; patternsList = _ . uniq ( patternsList ) ; return ( patternsList . length > 0 ) ? patternsList : null ; } public formatPatternsForDisplay ( patterns ) { return patterns . join ( "\n" ) ; } }