import { ChangeDetectorRef , Component , ElementRef , HostListener , OnInit , ViewChild } from '@angular/core' ; import { Water } from '../../../classes/water/water' ; import { AgVirtualSrollComponent } from 'ag-virtual-scroll' ; import { Settings } from '../../../classes/settings/settings' ; import { ModalController } from '@ionic/angular' ; import { UIWaterStorage } from '../../../services/uiWaterStorage' ; import { UIAlert } from '../../../services/uiAlert' ; import { UIBrewStorage } from '../../../services/uiBrewStorage' ; import { UISettingsStorage } from '../../../services/uiSettingsStorage' ; import { UIAnalytics } from '../../../services/uiAnalytics' ; import { GREEN_BEAN_ACTION } from '../../../enums/green-beans/greenBeanAction' ; import { GreenBean } from '../../../classes/green-bean/green-bean' ; import WATER_TRACKING from '../../../data/tracking/waterTracking' ; import { WaterAddComponent } from './water-add/water-add.component' ; import { UIWaterHelper } from '../../../services/uiWaterHelper' ; @ Component ( { selector : 'app-water' , templateUrl : './water.page.html' , styleUrls : [ './water.page.scss' ] , } ) export class WaterPage implements OnInit { private waters : Array < Water > = [ ] ; public openWaters : Array < Water > = [ ] ; public finishedWaters : Array < Water > = [ ] ; @ ViewChild ( 'waterContent' , { read : ElementRef } ) public waterContent : ElementRef ; @ ViewChild ( 'openScroll' , { read : AgVirtualSrollComponent , static : false } ) public openScroll : AgVirtualSrollComponent ; @ ViewChild ( 'archivedScroll' , { read : AgVirtualSrollComponent , static : false } ) public archivedScroll : AgVirtualSrollComponent ; public segment : string = 'open' ; public settings : Settings ; constructor ( public modalCtrl , private readonly changeDetectorRef , private readonly uiWaterStorage , private readonly uiAlert , private readonly uiBrewStorage , private readonly uiSettingsStorage , private readonly uiAnalytics , private readonly uiWaterHelper ) { this . settings = this . uiSettingsStorage . getSettings ( ) ; } public ionViewWillEnter ( ) { this . load ( ) ; } public load ( ) { this . __initialize ( ) ; this . changeDetectorRef . detectChanges ( ) ; this . retriggerScroll ( ) ; } public segmentChanged ( ) { this . retriggerScroll ( ) ; } @ HostListener ( 'window:resize' ) @ HostListener ( 'window:orientationchange' , [ '$event' ] ) public onOrientationChange ( event ) { this . retriggerScroll ( ) ; } private retriggerScroll ( ) { setTimeout ( async ( ) => { const el = this . waterContent . nativeElement ; let scrollComponent ; if ( this . openScroll !== undefined ) { scrollComponent = this . openScroll ; } else { scrollComponent = this . archivedScroll ; } scrollComponent . el . style . height = ( el . offsetHeight - scrollComponent . el . offsetTop ) + 'px' ; } , 250 ) ; } public async triggerAction ( action , bean ) { this . load ( ) ; } public async add ( ) { await this . uiWaterHelper . addWater ( ) ; this . load ( ) ; } private __initializeView ( _type ) { const copyObj = [ ... this . waters ] ; const isOpen = ( _type === 'open' ) ; let sortedBeans ; if ( isOpen ) { sortedBeans = copyObj . filter ( ( w ) => ! w . finished ) ; } else { sortedBeans = copyObj . filter ( ( w ) => w . finished ) ; } if ( isOpen ) { this . openWaters = sortedBeans ; } else { this . finishedWaters = sortedBeans ; } this . retriggerScroll ( ) ; } public ngOnInit ( ) { } private __initialize ( ) { this . waters = this . uiWaterStorage . getAllEntries ( ) . sort ( ( a , b ) => a . name . localeCompare ( b . name ) ) ; this . openWaters = [ ] ; this . finishedWaters = [ ] ; this . __initializeView ( 'open' ) ; this . __initializeView ( 'archiv' ) ; } }