import { Component , EventEmitter , Input , OnInit , Output } from '@angular/core' ; import { Settings } from '../../classes/settings/settings' ; import { UISettingsStorage } from '../../services/uiSettingsStorage' ; import { ModalController } from '@ionic/angular' ; import { Preparation } from '../../classes/preparation/preparation' ; import { PREPARATION_ACTION } from '../../enums/preparations/preparationAction' ; import { PreparationPopoverActionsComponent } from '../../app/preparation/preparation-popover-actions/preparation-popover-actions.component' ; import { Brew } from '../../classes/brew/brew' ; import { UIPreparationHelper } from '../../services/uiPreparationHelper' ; import { PreparationCustomParametersComponent } from '../../app/preparation/preparation-custom-parameters/preparation-custom-parameters.component' ; import { UIAlert } from '../../services/uiAlert' ; import { UIAnalytics } from '../../services/uiAnalytics' ; import { UIToast } from '../../services/uiToast' ; import { UIPreparationStorage } from '../../services/uiPreparationStorage' ; import { UIBrewStorage } from '../../services/uiBrewStorage' ; import { UIImage } from '../../services/uiImage' ; import PREPARATION_TRACKING from '../../data/tracking/preparationTracking' ; @ Component ( { selector : 'preparation-information-card' , templateUrl : './preparation-information-card.component.html' , styleUrls : [ './preparation-information-card.component.scss' ] , } ) export class PreparationInformationCardComponent implements OnInit { @ Input ( ) public preparation : Preparation ; @ Output ( ) public preparationAction : EventEmitter < any > = new EventEmitter ( ) ; constructor ( private readonly uiSettingsStorage , private readonly modalController , private readonly uiPreparationHelper , private readonly uiAlert , private readonly uiAnalytics , private readonly uiToast , private readonly uiPreparationStorage , private readonly uiBrewStorage , private readonly uiImage ) { } public ngOnInit ( ) { } public getBrewsCount ( ) { const relatedBrews = this . uiPreparationHelper . getAllBrewsForThisPreparation ( this . preparation . config . uuid ) ; return relatedBrews . length ; } public getWeightCount ( ) { const relatedBrews = this . uiPreparationHelper . getAllBrewsForThisPreparation ( this . preparation . config . uuid ) ; let grindWeight = 0 ; for ( const brew of relatedBrews ) { grindWeight += brew . grind_weight ; } return grindWeight ; } public getDrunkenQuantity ( ) { const relatedBrews = this . uiPreparationHelper . getAllBrewsForThisPreparation ( this . preparation . config . uuid ) ; let drunkenQuantity = 0 ; for ( const brew of relatedBrews ) { if ( brew . brew_beverage_quantity > 0 ) { drunkenQuantity += brew . brew_beverage_quantity ; } else { drunkenQuantity += brew . brew_quantity ; } } return drunkenQuantity / 1000 ; } public getBeansCount ( ) { const relatedBrews = this . uiPreparationHelper . getAllBrewsForThisPreparation ( this . preparation . config . uuid ) ; const distinctBeans = relatedBrews . filter ( ( bean , i , arr ) => { return arr . indexOf ( arr . find ( ( t ) => t . bean === bean . bean ) ) === i ; } ) ; return distinctBeans . length ; } private async resetSettings ( ) { const settings = this . uiSettingsStorage . getSettings ( ) ; settings . resetFilter ( ) ; await this . uiSettingsStorage . saveSettings ( settings ) ; } public async show ( ) { await this . detail ( ) ; } public async showPreparationActions ( event ) { event . stopPropagation ( ) ; event . stopImmediatePropagation ( ) ; this . uiAnalytics . trackEvent ( PREPARATION_TRACKING . TITLE , PREPARATION_TRACKING . ACTIONS . POPOVER_ACTIONS ) ; const popover = await this . modalController . create ( { component : PreparationPopoverActionsComponent , id : PreparationPopoverActionsComponent . COMPONENT_ID , componentProps : { preparation : this . preparation } , cssClass : 'popover-actions' , } ) ; await popover . present ( ) ; const data = await popover . onWillDismiss ( ) ; if ( data . role !== undefined ) { await this . internalPreparationAction ( data . role as PREPARATION_ACTION ) ; this . preparationAction . emit ( [ data . role as PREPARATION_ACTION , this . preparation ] ) ; } } public async showPhoto ( event ) { event . stopPropagation ( ) ; event . stopImmediatePropagation ( ) ; await this . viewPhotos ( ) ; } private async viewPhotos ( ) { this . uiAnalytics . trackEvent ( PREPARATION_TRACKING . TITLE , PREPARATION_TRACKING . ACTIONS . PHOTO_VIEW ) ; await this . uiImage . viewPhotos ( this . preparation ) ; } public async internalPreparationAction ( action ) { switch ( action ) { case PREPARATION_ACTION . CUSTOM_PARAMETERS : await this . customParameters ( ) ; break ; case PREPARATION_ACTION . EDIT : await this . editPreparation ( ) ; break ; case PREPARATION_ACTION . DELETE : try { await this . deletePreparation ( ) ; } catch ( ex ) { } await this . uiAlert . hideLoadingSpinner ( ) ; break ; case PREPARATION_ACTION . PHOTO_GALLERY : await this . viewPhotos ( ) ; break ; case PREPARATION_ACTION . ARCHIVE : await this . archive ( ) ; break ; case PREPARATION_ACTION . DETAIL : await this . detail ( ) ; break ; default : break ; } } public async customParameters ( ) { this . uiAnalytics . trackEvent ( PREPARATION_TRACKING . TITLE , PREPARATION_TRACKING . ACTIONS . CUSTOM_PARAMETERS ) ; const modal = await this . modalController . create ( { component : PreparationCustomParametersComponent , componentProps : { preparation : this . preparation } , id : PreparationCustomParametersComponent . COMPONENT_ID } ) ; await modal . present ( ) ; await modal . onWillDismiss ( ) ; } public async longPressEditPreparation ( event ) { event . stopPropagation ( ) ; event . stopImmediatePropagation ( ) ; await this . editPreparation ( ) ; this . preparationAction . emit ( [ PREPARATION_ACTION . EDIT , this . preparation ] ) ; } public async editPreparation ( ) { await this . uiPreparationHelper . editPreparation ( this . preparation ) ; } public async detail ( ) { await this . uiPreparationHelper . detailPreparation ( this . preparation ) ; } public async deletePreparation ( ) { await this . uiAlert . showConfirm ( 'DELETE_PREPARATION_METHOD_QUESTION' , 'SURE_QUESTION' , true ) . then ( async ( ) => { await this . uiAlert . showLoadingSpinner ( ) ; this . uiAnalytics . trackEvent ( PREPARATION_TRACKING . TITLE , PREPARATION_TRACKING . ACTIONS . DELETE ) ; await this . __deletePreparation ( ) ; this . uiToast . showInfoToast ( 'TOAST_PREPARATION_DELETED_SUCCESSFULLY' ) ; await this . resetSettings ( ) ; } , ( ) => { } ) ; } public async archive ( ) { this . uiAnalytics . trackEvent ( PREPARATION_TRACKING . TITLE , PREPARATION_TRACKING . ACTIONS . ARCHIVE ) ; this . preparation . finished = true ; await this . uiPreparationStorage . update ( this . preparation ) ; this . uiToast . showInfoToast ( 'TOAST_PREPARATION_ARCHIVED_SUCCESSFULLY' ) ; await this . resetSettings ( ) ; } private async __deletePreparation ( ) { const brews = this . uiBrewStorage . getAllEntries ( ) ; const deletingBrewIndex = [ ] ; for ( let i = 0 ; i < brews . length ; i ++ ) { if ( brews [ i ] . method_of_preparation === this . preparation . config . uuid ) { deletingBrewIndex . push ( i ) ; } } for ( let i = deletingBrewIndex . length ; i -- ; ) { await this . uiBrewStorage . removeByUUID ( brews [ deletingBrewIndex [ i ] ] . config . uuid ) ; } await this . uiPreparationStorage . removeByObject ( this . preparation ) ; } }