import { Injectable } from '@angular/core'; import { Vibration } from '@ionic-native/vibration/ngx'; import { AlertController } from '@ionic/angular'; import { container } from '../models/container.model'; import { Count } from '../models/count.model'; import { PouchdbService } from './pouchdb.service'; import { TranslateService } from "@ngx-translate/core"; import { Settings } from '../models/settings.model'; @Injectable({ providedIn: 'root', }) export class CountService { public currentCount: Count; //Holds the values for the current count public loadedCounts: Count[]; //last 5 Counts public lastCountValue: number; //Used for check() function holds the previous value of currentCount.count public containers: container[] = []; public selectedContainer: container = new container(0); public singleMode: boolean = true; constructor( private pouch: PouchdbService, private vibration: Vibration, private alertCtrl: AlertController, private translate: TranslateService ) { this.currentCount = new Count(); this.loadedCounts = []; this.lastCountValue = 0; this.containers.push(new container(1,0)); this.selectedContainer = this.containers[0]; } //getter which returns the Color of the div depending on the state of the current count get MainDivColor() { if (this.currentCount.state === 0) { return {'backgrond-color':'var(--ion-color-secondary)'}; } if (this.currentCount.state === 1) { return {'backgrond-color':'var(--ion-color-success)'}; } if (this.currentCount.state === 2) { return {'backgrond-color':'var(--ion-color-danger)'}; } } //Raises count async raiseCount() { this.lastCountValue = 0; this.currentCount.count = this.currentCount.count + 1; this.check(); } //resets all current values to 0 resetUI() { let packing = this.currentCount.packing; this.currentCount = new Count(); this.lastCountValue = 0; this.currentCount.packing = packing; } resetContainers() { this.containers = []; this.containers.push(new container(1,0)); this.selectedContainer = this.containers[0]; } //checks current values and plays audio if certain limit is reached check() { this.pouch.getSettings().then((settings: Settings) => { if (this.currentCount.packing != 0) { if (this.currentCount.count < this.lastCountValue) { } if (this.currentCount.count > this.lastCountValue) { if ( this.currentCount.return >= this.currentCount.packing - 5 && this.currentCount.return < this.currentCount.packing ) { if (settings.vibration) { this.vibration.vibrate(500); } } if (this.currentCount.return === this.currentCount.packing) { this.alertCtrl.create({ header: this.translate.instant("FULL_H"), subHeader: this.translate.instant("STOPPED_AT")+':'+this.currentCount.return.valueOf()+' '+this.translate.instant("NOW")+':'+this.currentCount.return, message: this.translate.instant("FULL_MSG"), cssClass: 'full-alert', buttons: [ { text: this.translate.instant("OK") } ] }).then(alert =>{ alert.present(); }); if (settings.vibration) { this.vibration.vibrate([500, 50, 500]); } } if (this.currentCount.return > this.currentCount.packing) { if (settings.vibration) { this.vibration.vibrate(1000); } } } this.lastCountValue = this.currentCount.count; } }); } selectSingleMode() { this.singleMode = true; } selectDoubleMode() { this.singleMode = false; } /* plusStart() { this.currentCount.start = this.currentCount.start + 1; } minusStart() { this.currentCount.start = this.currentCount.start - 1; } plusPacking() { this.currentCount.packing = this.currentCount.packing + 1; } minusPacking() { this.currentCount.packing = this.currentCount.packing - 1; } minusReturn() { this.currentCount.return = this.currentCount.return - 1; this.currentCount.count = this.currentCount.count -1; }*/ }