import { Component , EventEmitter , Input , OnInit , Output , ViewChild } from '@angular/core' ; import { ModalController } from '@ionic/angular' ; import { UIBeanHelper } from '../../services/uiBeanHelper' ; import { GreenBean } from '../../classes/green-bean/green-bean' ; import { GreenBeanPopoverActionsComponent } from '../../app/roasting-section/green-beans/green-bean-popover-actions/green-bean-popover-actions.component' ; import { Bean } from '../../classes/bean/bean' ; import { GreenBeanAddComponent } from '../../app/roasting-section/green-beans/green-bean-add/green-bean-add.component' ; import { BeansAddComponent } from '../../app/beans/beans-add/beans-add.component' ; import { Brew } from '../../classes/brew/brew' ; import { UIBrewStorage } from '../../services/uiBrewStorage' ; import { UIGreenBeanStorage } from '../../services/uiGreenBeanStorage' ; import { UIAnalytics } from '../../services/uiAnalytics' ; import { UISettingsStorage } from '../../services/uiSettingsStorage' ; import { UIToast } from '../../services/uiToast' ; import { UIImage } from '../../services/uiImage' ; import { UIAlert } from '../../services/uiAlert' ; import { Settings } from '../../classes/settings/settings' ; import { GREEN_BEAN_ACTION } from '../../enums/green-beans/greenBeanAction' ; import { UIBeanStorage } from '../../services/uiBeanStorage' ; import GREEN_BEAN_TRACKING from '../../data/tracking/greenBeanTracking' ; import { NgxStarsComponent } from 'ngx-stars' ; import { UIGreenBeanHelper } from '../../services/uiGreenBeanHelper' ; @ Component ( { selector : 'green-bean-information' , templateUrl : './green-bean-information.component.html' , styleUrls : [ './green-bean-information.component.scss' ] , } ) export class GreenBeanInformationComponent implements OnInit { @ Input ( ) public greenBean : GreenBean ; @ Output ( ) public greenBeanAction : EventEmitter < any > = new EventEmitter ( ) ; @ ViewChild ( 'greenBeanRating' , { read : NgxStarsComponent , static : false } ) public greenBeanRating : NgxStarsComponent ; constructor ( private readonly uiBeanHelper , private readonly uiGreenBeanStorage , private readonly uiBrewStorage , private readonly modalController , private readonly uiAnalytics , private readonly uiSettingsStorage , private readonly uiAlert , private readonly uiToast , private readonly uiImage , private readonly uiBeanStorage , private readonly uiGreenBeanHelper ) { } public ngOnInit ( ) { } public ngAfterViewInit ( ) { this . resetRenderingRating ( ) ; } public ngOnChanges ( ) { this . resetRenderingRating ( ) ; } private resetRenderingRating ( ) { setTimeout ( ( ) => { if ( this . greenBeanRating && this . greenBean . rating !== 0 ) { this . greenBeanRating . setRating ( this . greenBean . rating ) ; } } , 250 ) ; } public daysOld ( ) { return this . greenBean . beanAgeInDays ( ) ; } public async showGreenBean ( ) { await this . detailBean ( ) ; } public async showBeanActions ( event ) { event . stopPropagation ( ) ; event . stopImmediatePropagation ( ) ; this . uiAnalytics . trackEvent ( GREEN_BEAN_TRACKING . TITLE , GREEN_BEAN_TRACKING . ACTIONS . POPOVER_ACTIONS ) ; const popover = await this . modalController . create ( { component : GreenBeanPopoverActionsComponent , componentProps : { 'green-bean' : this . greenBean } , id : GreenBeanPopoverActionsComponent . COMPONENT_ID , cssClass : 'popover-actions' , } ) ; await popover . present ( ) ; const data = await popover . onWillDismiss ( ) ; if ( data . role !== undefined ) { await this . internalBeanAction ( data . role as GREEN_BEAN_ACTION ) ; this . greenBeanAction . emit ( [ data . role as GREEN_BEAN_ACTION , this . greenBean ] ) ; } } private async internalBeanAction ( action ) { switch ( action ) { case GREEN_BEAN_ACTION . DETAIL : await this . detailBean ( ) ; break ; case GREEN_BEAN_ACTION . REPEAT : await this . repeatBean ( ) ; break ; case GREEN_BEAN_ACTION . EDIT : await this . editBean ( ) ; break ; case GREEN_BEAN_ACTION . DELETE : try { await this . deleteBean ( ) ; } catch ( ex ) { } await this . uiAlert . hideLoadingSpinner ( ) ; break ; case GREEN_BEAN_ACTION . BEANS_CONSUMED : await this . beansConsumed ( ) ; break ; case GREEN_BEAN_ACTION . PHOTO_GALLERY : await this . viewPhotos ( ) ; break ; case GREEN_BEAN_ACTION . TRANSFER_ROAST : await this . transferRoast ( ) ; break ; default : break ; } } public getUsedWeightCount ( ) { let usedWeightCount = 0 ; const relatedRoastingBeans = this . uiBeanHelper . getAllRoastedBeansForThisGreenBean ( this . greenBean . config . uuid ) ; for ( const roast of relatedRoastingBeans ) { usedWeightCount += roast . bean_roast_information . green_bean_weight ; } return usedWeightCount ; } public roastCount ( ) { const relatedRoastingBeans = this . uiBeanHelper . getAllRoastedBeansForThisGreenBean ( this . greenBean . config . uuid ) ; return relatedRoastingBeans . length ; } public async detailBean ( ) { await this . uiGreenBeanHelper . detailGreenBean ( this . greenBean ) ; } public async showPhoto ( event ) { event . stopPropagation ( ) ; event . stopImmediatePropagation ( ) ; await this . viewPhotos ( ) ; } public async viewPhotos ( ) { this . uiAnalytics . trackEvent ( GREEN_BEAN_TRACKING . TITLE , GREEN_BEAN_TRACKING . ACTIONS . PHOTO_VIEW ) ; await this . uiImage . viewPhotos ( this . greenBean ) ; } public async transferRoast ( ) { this . uiAnalytics . trackEvent ( GREEN_BEAN_TRACKING . TITLE , GREEN_BEAN_TRACKING . ACTIONS . TRANSFER_ROAST ) ; await this . uiBeanHelper . addRoastedBean ( this . greenBean ) ; } public async beansConsumed ( ) { this . uiAnalytics . trackEvent ( GREEN_BEAN_TRACKING . TITLE , GREEN_BEAN_TRACKING . ACTIONS . ARCHIVE ) ; this . greenBean . finished = true ; await this . uiGreenBeanStorage . update ( this . greenBean ) ; this . uiToast . showInfoToast ( 'TOAST_GREEN_BEAN_ARCHIVED_SUCCESSFULLY' ) ; await this . resetSettings ( ) ; } private async resetSettings ( ) { const settings = this . uiSettingsStorage . getSettings ( ) ; settings . resetFilter ( ) ; await this . uiSettingsStorage . saveSettings ( settings ) ; } public async longPressEditBean ( event ) { event . stopPropagation ( ) ; event . stopImmediatePropagation ( ) ; await this . editBean ( ) ; this . greenBeanAction . emit ( [ GREEN_BEAN_ACTION . EDIT , this . greenBean ] ) ; } public async editBean ( ) { await this . uiGreenBeanHelper . editGreenBean ( this . greenBean ) ; } public async deleteBean ( ) { return new Promise ( async ( resolve , reject ) => { this . uiAlert . showConfirm ( 'DELETE_GREEN_BEAN_QUESTION' , 'SURE_QUESTION' , true ) . then ( async ( ) => { await this . uiAlert . showLoadingSpinner ( ) ; this . uiAnalytics . trackEvent ( GREEN_BEAN_TRACKING . TITLE , GREEN_BEAN_TRACKING . ACTIONS . DELETE ) ; await this . __deleteBean ( ) ; this . uiToast . showInfoToast ( 'TOAST_GREEN_BEAN_DELETED_SUCCESSFULLY' ) ; await this . resetSettings ( ) ; resolve ( undefined ) ; } , ( ) => { reject ( ) ; } ) ; } ) ; } public async repeatBean ( ) { this . uiAnalytics . trackEvent ( GREEN_BEAN_TRACKING . TITLE , GREEN_BEAN_TRACKING . ACTIONS . REPEAT ) ; await this . uiGreenBeanHelper . repeatGreenBean ( this . greenBean ) ; } private async __deleteBean ( ) { const brews = this . uiBrewStorage . getAllEntries ( ) ; const relatedRoastingBeans = this . uiBeanHelper . getAllRoastedBeansForThisGreenBean ( this . greenBean . config . uuid ) ; let deletingBrews = [ ] ; for ( const roastedBean of relatedRoastingBeans ) { const filteredBrews = brews . filter ( ( e ) => e . bean === roastedBean . config . uuid ) ; deletingBrews = [ ... deletingBrews , ... filteredBrews ] ; } for ( const brew of deletingBrews ) { await this . uiBrewStorage . removeByUUID ( brew . config . uuid ) ; } for ( const bean of relatedRoastingBeans ) { await this . uiBeanStorage . removeByUUID ( bean . config . uuid ) ; } await this . uiGreenBeanStorage . removeByObject ( this . greenBean ) ; } public hasPhotos ( ) { return this . greenBean . attachments . length > 0 ; } }