import { Component , ViewChild } from '@angular/core' ; import { BarcodeScanner , BarcodeScanResult } from '@fttx/barcode-scanner' ; import { TranslateService } from '@ngx-translate/core' ; import { AlertButton , AlertController , Content , NavParams , Platform , ViewController } from 'ionic-angular' ; import * as Supplant from 'supplant' ; import { OutputProfileModel } from '../../../models/output-profile.model' ; import { ScanSessionModel } from '../../../models/scan-session.model' ; import { ScanSessionsStorage } from '../../../providers/scan-sessions-storage' ; import { Utils } from '../../../providers/utils' ; import { Settings } from './../../../providers/settings' ; @ Component ( { selector : 'page-select-scanning-mode' , templateUrl : 'select-scanning-mode.html' } ) export class SelectScanningModePage { public static SCAN_MODE_ASK = '' ; public static SCAN_MODE_CONTINUE = 'continue' ; public static SCAN_MODE_SINGLE = 'single' ; public static SCAN_MODE_ENTER_MAUALLY = 'keyboard' ; public setAsDefaultMode = false ; public outputProfiles : OutputProfileModel [ ] = [ ] ; public selectedOutputProfile : OutputProfileModel ; public selectedOutputProfileIndex = 0 ; public scanSession : ScanSessionModel ; private filterErrorMessage : string = null ; @ ViewChild ( Content ) content : Content ; constructor ( public viewCtrl , public alertCtrl , private settings , private scanSessionsStorage , public navParams , private platform , private barcodeScanner , private utils , private translateService ) { this . scanSession = navParams . get ( 'scanSession' ) ; } async ionViewWillEnter ( ) { this . outputProfiles = await this . settings . getOutputProfiles ( ) ; this . selectedOutputProfileIndex = await this . settings . getSelectedOutputProfile ( ) ; if ( this . selectedOutputProfileIndex >= this . outputProfiles . length ) { this . selectedOutputProfileIndex = this . outputProfiles . length - 1 ; } if ( this . selectedOutputProfileIndex < 0 ) { this . selectedOutputProfileIndex = 0 ; } this . selectedOutputProfile = this . outputProfiles [ this . selectedOutputProfileIndex ] ; let defaultMode = await this . settings . getDefaultMode ( ) ; if ( defaultMode && defaultMode . length > 0 ) { this . viewCtrl . dismiss ( { scanMode : defaultMode , selectedOutputProfileIndex : this . selectedOutputProfileIndex , scanSession : await this . getScanSession ( ) } ) ; } this . content . resize ( ) ; } async dismiss ( scanMode ) { if ( ! scanMode ) { this . viewCtrl . dismiss ( { cancelled : true } ) ; return ; } if ( this . setAsDefaultMode ) { this . settings . setDefaultMode ( scanMode ) ; } let selectedOutputProfileIndex = this . outputProfiles . indexOf ( this . selectedOutputProfile ) ; this . settings . setSelectedOutputProfile ( selectedOutputProfileIndex ) ; this . viewCtrl . dismiss ( { scanMode : scanMode , selectedOutputProfileIndex : selectedOutputProfileIndex , scanSession : await this . getScanSession ( ) } ) ; } getBarcodeScanSessionName ( ) { return new Promise ( async ( resolve , reject ) => { this . barcodeScanner . scan ( { "showFlipCameraButton" : true , prompt : this . filterErrorMessage } ) . subscribe ( async ( scan ) => { if ( scan && scan . text ) { resolve ( scan . text ) ; } else { reject ( ) ; } } , async ( err ) => { reject ( ) ; } ) ; } ) ; } async getScanSessionName ( ) { const filterStr = await this . settings . getScanSessionFilter ( ) ; const acquireScanSessionName = ( ) : Promise < string > => { return new Promise ( async ( resolve , reject ) => { let defaultName = await this . settings . getScanSessionName ( ) ; defaultName = new Supplant ( ) . text ( defaultName , { scan_session_number : await this . scanSessionsStorage . getNextScanSessionNumber ( ) , device_name : await this . settings . getDeviceName ( ) , date : new Date ( ) . toISOString ( ) . slice ( 0 , 10 ) . replace ( /-/g , "" ) } ) ; let alwaysUseDefaultScanSessionName = await this . settings . getAlwaysUseDefaultScanSessionName ( ) ; if ( alwaysUseDefaultScanSessionName ) { resolve ( defaultName ) ; } else { let alwaysUseCameraForScanSessionName = await this . settings . getAlwaysUseCameraForScanSessionName ( ) ; let buttonClasses = alwaysUseCameraForScanSessionName ? ' alert-button-stacked' : '' ; let buttons = [ { text : await this . utils . text ( 'getScanSessionNameDialogCancelButton' ) , handler : ( ) => { reject ( ) ; } , cssClass : this . platform . is ( 'android' ) ? 'button-outline-md' + buttonClasses : null } ] ; if ( alwaysUseCameraForScanSessionName ) { buttonClasses += " alert-button-stacked" ; try { let scanSessionName = await this . getBarcodeScanSessionName ( ) ; resolve ( scanSessionName ) ; return ; } catch { } buttons . push ( { text : await this . utils . text ( 'acquireBarcodeButton' ) , handler : async ( data ) => { resolve ( await this . getScanSessionName ( ) ) ; } , cssClass : this . platform . is ( 'android' ) ? 'button-outline-md' + buttonClasses : null , } ) ; } buttons . push ( { text : await this . utils . text ( 'getScanSessionNameDialogOkButton' ) , handler : data => { if ( data . name != "" ) { resolve ( data . name ) ; } else { resolve ( defaultName ) ; } } , cssClass : this . platform . is ( 'android' ) ? 'button-outline-md button-ok' + buttonClasses : null , } ) ; let alert = this . alertCtrl . create ( { title : await this . utils . text ( 'scanSessionDialogTitle' ) , message : this . filterErrorMessage , inputs : [ { name : 'name' , placeholder : defaultName } ] , buttons : buttons , enableBackdropDismiss : false , cssClass : this . platform . is ( 'android' ) ? 'alert-big-buttons' : null , } ) ; alert . present ( ) ; } } ) ; } ; const filter = new Promise ( async ( resolve , reject ) => { const filterRegex = new RegExp ( filterStr ) ; let name ; try { do { name = await acquireScanSessionName ( ) ; this . filterErrorMessage = this . translateService . instant ( 'scanSessionFilterError' ) ; } while ( ! name . match ( filterRegex ) ) ; resolve ( name ) ; } catch { reject ( ) ; } } ) ; if ( ! filterStr && filterStr != '' ) { return acquireScanSessionName ( ) ; } return filter ; } /**
     * Creates the scanSession if not exists
     */ getScanSession ( ) { return new Promise ( async ( resolve , reject ) => { if ( ! this . scanSession ) { let date = new Date ( ) . getTime ( ) ; let scanSessionName ; try { scanSessionName = await this . getScanSessionName ( ) ; } catch ( e ) { reject ( ) ; return ; } let newScanSession = { id : date , name : scanSessionName , date : date , scannings : [ ] , selected : false , syncedWith : [ ] , } ; resolve ( newScanSession ) ; } else { resolve ( this . scanSession ) ; } } ) ; } }