import { ChangeDetectorRef , Component , OnInit , ViewChild } from '@angular/core' ; import { UIStatistic } from '../../services/uiStatistic' ; import { ModalController } from '@ionic/angular' ; import { Brew } from '../../classes/brew/brew' ; import { UIBrewStorage } from '../../services/uiBrewStorage' ; import { UIBrewHelper } from '../../services/uiBrewHelper' ; import { BREW_ACTION } from '../../enums/brews/brewAction' ; import { Router } from '@angular/router' ; import { UIBeanStorage } from '../../services/uiBeanStorage' ; import { Bean } from '../../classes/bean/bean' ; import { UIBeanHelper } from '../../services/uiBeanHelper' ; @ Component ( { selector : 'dashboard' , templateUrl : './dashboard.page.html' , styleUrls : [ './dashboard.page.scss' ] , } ) export class DashboardPage implements OnInit { public brews : Array < Brew > = [ ] ; private leftOverBeansWeight : number = undefined ; public flowProfileChartEl : any = undefined ; constructor ( public uiStatistic , private readonly modalCtrl , private readonly uiBrewStorage , private readonly uiBrewHelper , private readonly changeDetectorRef , private readonly router , private readonly uiBeanStorage , private readonly uiBeanHelper ) { } public ngOnInit ( ) { this . uiBrewStorage . attachOnEvent ( ) . subscribe ( ( _val ) => { this . leftOverBeansWeight = undefined ; } ) ; this . uiBeanStorage . attachOnEvent ( ) . subscribe ( ( _val ) => { this . leftOverBeansWeight = undefined ; } ) ; } public ionViewWillEnter ( ) { this . loadBrews ( ) ; } public loadBrews ( ) { this . brews = this . uiBrewStorage . getAllEntries ( ) ; this . brews = UIBrewHelper . sortBrews ( this . brews ) ; this . brews = this . brews . slice ( 0 , 10 ) ; this . changeDetectorRef . detectChanges ( ) ; } public async addBrew ( ) { await this . uiBrewHelper . addBrew ( ) ; this . loadBrews ( ) ; this . router . navigate ( [ '/home/brews' ] ) ; } public async longPressAdd ( event ) { event . preventDefault ( ) ; event . cancelBubble = true ; event . stopPropagation ( ) ; event . stopImmediatePropagation ( ) ; await this . uiBrewHelper . longPressAddBrew ( ) ; this . loadBrews ( ) ; this . router . navigate ( [ '/home/brews' ] ) ; } public getBrews ( ) { return this . brews ; } public async brewAction ( action , brew ) { this . loadBrews ( ) ; } public openBeansLeftOverCount ( ) { if ( this . leftOverBeansWeight === undefined ) { let leftOverCount = 0 ; const openBeans = this . uiBeanStorage . getAllEntries ( ) . filter ( ( bean ) => ! bean . finished ) ; for ( const bean of openBeans ) { if ( bean . weight > 0 ) { leftOverCount += ( bean . weight - this . getUsedWeightCount ( bean ) ) ; } } this . leftOverBeansWeight = leftOverCount ; } if ( this . leftOverBeansWeight < 1000 ) { return ( Math . round ( this . leftOverBeansWeight * 100 ) / 100 ) + ' g' ; } else { return ( Math . round ( ( this . leftOverBeansWeight / 1000 ) * 100 ) / 100 ) + ' kg' ; } } public getUsedWeightCount ( _bean ) { let usedWeightCount = 0 ; const relatedBrews = this . uiBeanHelper . getAllBrewsForThisBean ( _bean . config . uuid ) ; for ( const brew of relatedBrews ) { usedWeightCount += brew . grind_weight ; } return usedWeightCount ; } }