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 { Mill } from '../../classes/mill/mill' ; import { MILL_ACTION } from '../../enums/mills/millActions' ; import { MillPopoverActionsComponent } from '../../app/mill/mill-popover-actions/mill-popover-actions.component' ; import { Brew } from '../../classes/brew/brew' ; import { UIMillHelper } from '../../services/uiMillHelper' ; import { UIBrewHelper } from '../../services/uiBrewHelper' ; import { MillEditComponent } from '../../app/mill/mill-edit/mill-edit.component' ; import { MillDetailComponent } from '../../app/mill/mill-detail/mill-detail.component' ; import { UIToast } from '../../services/uiToast' ; import { UIAlert } from '../../services/uiAlert' ; import { UIMillStorage } from '../../services/uiMillStorage' ; import { UIBrewStorage } from '../../services/uiBrewStorage' ; import { UIAnalytics } from '../../services/uiAnalytics' ; import { UIImage } from '../../services/uiImage' ; import MILL_TRACKING from '../../data/tracking/millTracking' ; @ Component ( { selector : 'mill-information-card' , templateUrl : './mill-information-card.component.html' , styleUrls : [ './mill-information-card.component.scss' ] , } ) export class MillInformationCardComponent implements OnInit { @ Input ( ) public mill : Mill ; @ Output ( ) public millAction : EventEmitter < any > = new EventEmitter ( ) ; constructor ( private readonly uiSettingsStorage , private readonly modalController , private readonly uiMillHelper , private readonly uiToast , private readonly uiAlert , private readonly uiMillStorage , private readonly uiBrewStorage , private readonly uiAnalytics , private readonly uiImage ) { } public ngOnInit ( ) { } public getBrewsCount ( ) { const relatedBrews = this . uiMillHelper . getAllBrewsForThisMill ( this . mill . config . uuid ) ; return relatedBrews . length ; } public getWeightCount ( ) { const relatedBrews = this . uiMillHelper . getAllBrewsForThisMill ( this . mill . config . uuid ) ; let grindWeight = 0 ; for ( const brew of relatedBrews ) { grindWeight += brew . grind_weight ; } return grindWeight ; } public getBeansCount ( ) { const relatedBrews = this . uiMillHelper . getAllBrewsForThisMill ( this . mill . config . uuid ) ; const distinctBeans = relatedBrews . filter ( ( bean , i , arr ) => { return arr . indexOf ( arr . find ( ( t ) => t . bean === bean . bean ) ) === i ; } ) ; return distinctBeans . length ; } public lastUsed ( ) { let relatedBrews = this . uiMillHelper . getAllBrewsForThisMill ( this . mill . config . uuid ) ; if ( relatedBrews . length > 0 ) { relatedBrews = UIBrewHelper . sortBrews ( relatedBrews ) ; return relatedBrews [ 0 ] . config . unix_timestamp ; } return - 1 ; } public async show ( ) { await this . detail ( ) ; } public async showPhoto ( event ) { event . stopPropagation ( ) ; event . stopImmediatePropagation ( ) ; await this . viewPhotos ( ) ; } private async viewPhotos ( ) { this . uiAnalytics . trackEvent ( MILL_TRACKING . TITLE , MILL_TRACKING . ACTIONS . PHOTO_VIEW ) ; await this . uiImage . viewPhotos ( this . mill ) ; } public async internalMillAction ( action ) { switch ( action ) { case MILL_ACTION . EDIT : await this . edit ( ) ; break ; case MILL_ACTION . DELETE : try { await this . delete ( ) ; } catch ( ex ) { } await this . uiAlert . hideLoadingSpinner ( ) ; break ; case MILL_ACTION . ARCHIVE : await this . archive ( ) ; break ; case MILL_ACTION . DETAIL : await this . detail ( ) ; break ; case MILL_ACTION . PHOTO_GALLERY : await this . viewPhotos ( ) ; break ; default : break ; } } private async resetSettings ( ) { const settings = this . uiSettingsStorage . getSettings ( ) ; settings . resetFilter ( ) ; await this . uiSettingsStorage . saveSettings ( settings ) ; } public async longPressEditMill ( event ) { event . stopPropagation ( ) ; event . stopImmediatePropagation ( ) ; await this . edit ( ) ; this . millAction . emit ( [ MILL_ACTION . EDIT , this . mill ] ) ; } public async edit ( ) { await this . uiMillHelper . editMill ( this . mill ) ; } public async detail ( ) { await this . uiMillHelper . detailMill ( this . mill ) ; } public async delete ( ) { await this . uiAlert . showConfirm ( 'DELETE_MILL_QUESTION' , 'SURE_QUESTION' , true ) . then ( async ( ) => { await this . uiAlert . showLoadingSpinner ( ) ; this . uiAnalytics . trackEvent ( MILL_TRACKING . TITLE , MILL_TRACKING . ACTIONS . DELETE ) ; await this . __deleteMill ( ) ; this . uiToast . showInfoToast ( 'TOAST_MILL_DELETED_SUCCESSFULLY' ) ; await this . resetSettings ( ) ; } , ( ) => { } ) ; } private async __deleteMill ( ) { const brews = this . uiBrewStorage . getAllEntries ( ) ; const deletingBrewIndex = [ ] ; for ( let i = 0 ; i < brews . length ; i ++ ) { if ( brews [ i ] . mill === this . mill . config . uuid ) { deletingBrewIndex . push ( i ) ; } } for ( let i = deletingBrewIndex . length ; i -- ; ) { await this . uiBrewStorage . removeByUUID ( brews [ deletingBrewIndex [ i ] ] . config . uuid ) ; } await this . uiMillStorage . removeByObject ( this . mill ) ; } public async archive ( ) { this . uiAnalytics . trackEvent ( MILL_TRACKING . TITLE , MILL_TRACKING . ACTIONS . ARCHIVE ) ; this . mill . finished = true ; await this . uiMillStorage . update ( this . mill ) ; this . uiToast . showInfoToast ( 'TOAST_MILL_ARCHIVED_SUCCESSFULLY' ) ; await this . resetSettings ( ) ; } public async showMillActions ( event ) { event . stopPropagation ( ) ; event . stopImmediatePropagation ( ) ; this . uiAnalytics . trackEvent ( MILL_TRACKING . TITLE , MILL_TRACKING . ACTIONS . POPOVER_ACTIONS ) ; const popover = await this . modalController . create ( { component : MillPopoverActionsComponent , componentProps : { mill : this . mill } , id : MillPopoverActionsComponent . COMPONENT_ID , cssClass : 'popover-actions' , } ) ; await popover . present ( ) ; const data = await popover . onWillDismiss ( ) ; if ( data . role !== undefined ) { await this . internalMillAction ( data . role as MILL_ACTION ) ; this . millAction . emit ( [ data . role as MILL_ACTION , this . mill ] ) ; } } }