import { Injectable , NgZone } from '@angular/core' ; import { Device } from '@ionic-native/device' ; import { SplashScreen } from '@ionic-native/splash-screen' ; import { Storage } from '@ionic/storage' ; import { barcodeFormatModel } from '../models/barcode-format.model' ; import { OutputProfileModel } from '../models/output-profile.model' ; import { ServerModel } from '../models/server.model' ; import { Utils } from './utils' ; @ Injectable ( ) export class Settings { private static DEFAULT_SERVER = "default_server" ; private static NO_RUNNINGS = "first_run" ; private static CONTINUE_MODE_TIMEOUT = "continuemode_timeout" ; private static RATED = "rated" ; private static MANUALLY_ADDED = "manually_added" ; private static EVER_CONNECTED = "ever_connected" ; private static ALWAYS_SKIP_WELCOME_PAGE = "always_skip_welcome_page" ; private static SCAN_MODE = 'scan_mode' ; private static DEVICE_NAME = 'device_name' ; private static REPEAT_INTERVAL = 'repeat_interval' ; private static ALWAYS_USE_DEFAULT_SCAN_SESSION_NAME = 'always_use_default_scan_session_name' ; private static PREFER_FRONT_CAMERA = 'prefer_front_camera' ; private static TORCH_ON = 'torch_on' ; private static KEEP_DISPLAY_ON = 'keep_display_on' ; private static ENABLE_BEEP = 'enable_beep' ; private static ENABLE_VIBRATION_FEEDBACK = 'enable_vibration_feedback' ; private static UPGRADED_TO_SQLITE = 'upgraded_to_sqlite_5x' ; private static LAST_VERSION = 'last_version' ; private static BARCODE_FORMATS = 'barcode_formats' ; private static ENABLE_LIMIT_BARCODE_FORMATS = 'enable_limit_barcode_formats' ; private static OUTPUT_PROFILES = 'output_profiles' ; private static SELECTED_OUTPUT_PROFILE = 'selected_output_profile' ; private static QUANTITY_ENABLED = 'quantity_enabled' ; private static QUANTITY_TYPE = 'quantity_type' ; private static SOUND_FEEDBACK_OR_DIALOG_SHOWN = 'sound_feedback_or_dialog_shown' ; private static SCAN_SESSION_NAME = 'scan_session_name' ; private static UPGRADED_DISPLAYVALUE = 'upgraded_displayvalue' ; private static OPEN_SCAN_ON_START = 'open_scan_on_start' ; private static EVENT_ON_SMARTPHONE_CHARGE_ENABLED = 'event_on_smartphone_charge_enabled' ; private static OFFLINE_MODE_ENABLED = 'offline_mode_enabled' ; private static IS_PDA_DEVICE_DIALOG_SHOWN = 'is_pda_device_dialog_shown' ; private static ALWAYS_USE_CAMERA_FOR_SCAN_SESSION_NAME = 'always_use_camera_for_scan_session_name' ; private static UNSYNCED_DELETED_SCAN_SESIONS = 'unsynced_deleted_scan_sesions' ; private static UNSYNCED_RESTORED_SCAN_SESIONS = 'unsynced_restored_scan_sesions' ; private static DUPLICATE_BARCODE_SAVE_CHOICE_SHOWN = 'duplicate_barcode_save_choice_shown' ; private static DUPLICATE_BARCODE_CHOICE = 'duplicate_barcode_choice' ; private static SCAN_SESSION_FILTER = 'scan_session_filter' ; constructor ( public storage , public device , public ngZone , public splashScreen , public utils ) { } /**
     * Used for both deleted and archived scan sessions
     */ setUnsyncedDeletedScanSesions ( serverUUID , scanSessionIds ) { return this . storage . set ( Settings . UNSYNCED_DELETED_SCAN_SESIONS + serverUUID , JSON . stringify ( scanSessionIds ) ) ; } /**
     * Used for both deleted and archived scan sessions
     */ getUnsyncedDeletedScanSesions ( serverUUID ) { return new Promise ( ( resolve , reject ) => { return this . storage . get ( Settings . UNSYNCED_DELETED_SCAN_SESIONS + serverUUID ) . then ( data => { if ( data ) { let scanSessionsIds = JSON . parse ( data ) ; resolve ( scanSessionsIds ) ; } else { resolve ( [ ] ) ; } } ) ; } ) ; } setUnsyncedRestoredScanSesions ( scanSessionIds ) { return this . storage . set ( Settings . UNSYNCED_RESTORED_SCAN_SESIONS , JSON . stringify ( scanSessionIds ) ) ; } getUnsyncedRestoredScanSesions ( ) { return new Promise ( ( resolve , reject ) => { return this . storage . get ( Settings . UNSYNCED_RESTORED_SCAN_SESIONS ) . then ( data => { if ( data ) { let scanSessionsIds = JSON . parse ( data ) ; resolve ( scanSessionsIds ) ; } else { resolve ( [ ] ) ; } } ) ; } ) ; } setDefaultServer ( server ) { if ( ! server ) { this . storage . remove ( Settings . DEFAULT_SERVER ) ; } return this . storage . set ( Settings . DEFAULT_SERVER , JSON . stringify ( server ) ) ; } getDefaultServer ( ) { return new Promise ( ( resolve , reject ) => { this . storage . get ( Settings . DEFAULT_SERVER ) . then ( ( data ) => { if ( data && data != '' && data != 'null' ) { data = JSON . parse ( data ) ; let server = new ServerModel ( data . address , data . name ) ; resolve ( server ) ; } else { resolve ( null ) ; } } ) ; } ) ; } setEverConnected ( everConnected ) { return this . storage . set ( Settings . EVER_CONNECTED , everConnected ) ; } getEverConnected ( ) { return this . storage . get ( Settings . EVER_CONNECTED ) ; } setIsPDADeviceDialogShown ( shown ) { return this . storage . set ( Settings . IS_PDA_DEVICE_DIALOG_SHOWN , shown ) ; } getIsPDADeviceDialogShown ( ) { return this . storage . get ( Settings . IS_PDA_DEVICE_DIALOG_SHOWN ) ; } setAlwaysSkipWelcomePage ( alwaysSkipWelcomePage ) { return this . storage . set ( Settings . ALWAYS_SKIP_WELCOME_PAGE , alwaysSkipWelcomePage ) ; } getAlwaysSkipWelcomePage ( ) { return this . storage . get ( Settings . ALWAYS_SKIP_WELCOME_PAGE ) ; } setOpenScanOnStart ( openscanOnStart ) { return this . storage . set ( Settings . OPEN_SCAN_ON_START , openscanOnStart ) ; } getOpenScanOnStart ( ) { return this . storage . get ( Settings . OPEN_SCAN_ON_START ) . then ( openscanOnStart => openscanOnStart || false ) ; } setNoRunnings ( noRunnings ) { return this . storage . set ( Settings . NO_RUNNINGS , noRunnings ) ; } getNoRunnings ( ) { return this . storage . get ( Settings . NO_RUNNINGS ) ; } setLastVersion ( lastVersion ) { return this . storage . set ( Settings . LAST_VERSION , lastVersion ) ; } getLastVersion ( ) { return this . storage . get ( Settings . LAST_VERSION ) . then ( version => version || '0.0.0' ) ; } setUpgradedDisplayValue ( upgradedDisplayValue ) { return this . storage . set ( Settings . UPGRADED_DISPLAYVALUE , upgradedDisplayValue ) ; } getUpgradedDisplayValue ( ) { return this . storage . get ( Settings . UPGRADED_DISPLAYVALUE ) . then ( displayValue => displayValue || null ) ; } setContinueModeTimeout ( seconds ) { return this . storage . set ( Settings . CONTINUE_MODE_TIMEOUT , seconds ) ; } getContinueModeTimeout ( ) { return this . storage . get ( Settings . CONTINUE_MODE_TIMEOUT ) ; } setRated ( rated ) { return this . storage . set ( Settings . RATED , rated ) ; } getRated ( ) { return this . storage . get ( Settings . RATED ) ; } setSoundFeedbackOrDialogShown ( soundFeedbackOrDialogShown ) { return this . storage . set ( Settings . SOUND_FEEDBACK_OR_DIALOG_SHOWN , soundFeedbackOrDialogShown ) ; } getSoundFeedbackOrDialogShown ( ) { return this . storage . get ( Settings . SOUND_FEEDBACK_OR_DIALOG_SHOWN ) ; } setSavedServers ( servers ) { return this . storage . set ( Settings . MANUALLY_ADDED , JSON . stringify ( servers ) ) ; } getSavedServers ( ) { return new Promise ( ( resolve , reject ) => { return this . storage . get ( Settings . MANUALLY_ADDED ) . then ( data => { if ( data ) { let servers = JSON . parse ( data ) . map ( x => new ServerModel ( x . address , x . name ) ) ; resolve ( servers ) ; } else { reject ( ) ; } } ) ; } ) ; } saveServer ( server ) { this . getSavedServers ( ) . then ( ( savedServers ) => { let alredyPresent = savedServers . find ( x => x . equals ( server ) ) ; if ( alredyPresent ) { savedServers . splice ( savedServers . indexOf ( alredyPresent ) , 1 ) ; } savedServers . push ( server ) ; this . setSavedServers ( savedServers ) ; } , err => { this . setSavedServers ( [ server ] ) ; } ) ; } deleteServer ( server ) { this . getSavedServers ( ) . then ( ( savedServers ) => { let deleteServer = savedServers . find ( x => x . equals ( server ) ) ; if ( deleteServer ) { savedServers . splice ( savedServers . indexOf ( deleteServer ) , 1 ) ; this . setSavedServers ( savedServers ) ; } } , err => { } ) ; } setDefaultMode ( scanMode ) { return this . storage . set ( Settings . SCAN_MODE , scanMode ) ; } getDefaultMode ( ) { return this . storage . get ( Settings . SCAN_MODE ) ; } setDeviceName ( deviceName ) { return this . storage . set ( Settings . DEVICE_NAME , deviceName ) ; } getDeviceName ( ) { return new Promise ( resolve => { this . storage . get ( Settings . DEVICE_NAME ) . then ( deviceName => { if ( ! deviceName ) { resolve ( this . device . model ) ; } else { resolve ( deviceName ) ; } } ) ; } ) ; } setScanSessionName ( scanSessionName ) { return this . storage . set ( Settings . SCAN_SESSION_NAME , scanSessionName ) ; } getScanSessionName ( ) { return new Promise ( resolve => { this . storage . get ( Settings . SCAN_SESSION_NAME ) . then ( async ( scanSessionName ) => { if ( ! scanSessionName ) { resolve ( ( await this . utils . text ( 'scanSessionName' ) ) + " {{ scan_session_number }}" ) ; } else { resolve ( scanSessionName ) ; } } ) ; } ) ; } setScanSessionFilter ( scanSessionFilter ) { return this . storage . set ( Settings . SCAN_SESSION_FILTER , scanSessionFilter ) ; } getScanSessionFilter ( ) { return new Promise ( resolve => { this . storage . get ( Settings . SCAN_SESSION_FILTER ) . then ( async ( scanSessionFilter ) => { if ( ! scanSessionFilter ) { resolve ( '' ) ; } else { resolve ( scanSessionFilter ) ; } } ) ; } ) ; } setRepeatInterval ( interval ) { return this . storage . set ( Settings . REPEAT_INTERVAL , interval ) ; } getRepeatInterval ( ) { return this . storage . get ( Settings . REPEAT_INTERVAL ) ; } setAlwaysUseDefaultScanSessionName ( alwaysUseDefaultScanSessionName ) { return this . storage . set ( Settings . ALWAYS_USE_DEFAULT_SCAN_SESSION_NAME , alwaysUseDefaultScanSessionName ) ; } getAlwaysUseDefaultScanSessionName ( ) { return this . storage . get ( Settings . ALWAYS_USE_DEFAULT_SCAN_SESSION_NAME ) . then ( result => { return result === true ; } ) ; } setAlwaysUseCameraForScanSessionName ( alwaysUseCameraForScanSessionName ) { return this . storage . set ( Settings . ALWAYS_USE_CAMERA_FOR_SCAN_SESSION_NAME , alwaysUseCameraForScanSessionName ) ; } getAlwaysUseCameraForScanSessionName ( ) { return this . storage . get ( Settings . ALWAYS_USE_CAMERA_FOR_SCAN_SESSION_NAME ) . then ( result => { return result === true ; } ) ; } setPreferFrontCamera ( preferFrontCamera ) { return this . storage . set ( Settings . PREFER_FRONT_CAMERA , preferFrontCamera ) ; } getPreferFrontCamera ( ) { return this . storage . get ( Settings . PREFER_FRONT_CAMERA ) . then ( result => { return result === true ; } ) ; } setTorchOn ( torchOn ) { return this . storage . set ( Settings . TORCH_ON , torchOn ) ; } getTorchOn ( ) { return this . storage . get ( Settings . TORCH_ON ) . then ( result => { return result === true ; } ) ; } setKeepDisplayOn ( keepDisplayOn ) { return this . storage . set ( Settings . KEEP_DISPLAY_ON , keepDisplayOn ) ; } getKeepDisplayOn ( ) { return this . storage . get ( Settings . KEEP_DISPLAY_ON ) . then ( result => { return result === true ; } ) ; } setDuplicateBarcodeSaveChoiceShown ( duplicateBarcodeSaveChoiceShown ) { return this . storage . set ( Settings . DUPLICATE_BARCODE_SAVE_CHOICE_SHOWN , duplicateBarcodeSaveChoiceShown ) ; } getDuplicateBarcodeSaveChoiceShown ( ) { return this . storage . get ( Settings . DUPLICATE_BARCODE_SAVE_CHOICE_SHOWN ) . then ( result => { return result === true ; } ) ; } setDuplicateBarcodeChoice ( choice ) { return this . storage . set ( Settings . DUPLICATE_BARCODE_CHOICE , choice ) ; } getDuplicateBarcodeChoice ( ) { return this . storage . get ( Settings . DUPLICATE_BARCODE_CHOICE ) . then ( result => { return result || 'ask' ; } ) ; } setEnableBeep ( enableBeep ) { console . log ( 'save: ' , Settings . ENABLE_BEEP , enableBeep ) ; return this . storage . set ( Settings . ENABLE_BEEP , enableBeep ) ; } getEnableBeep ( ) { return this . storage . get ( Settings . ENABLE_BEEP ) . then ( result => { return result === true ; } ) ; } setEnableVibrationFeedback ( enableVibrationFeedback ) { console . log ( 'save: ' , Settings . ENABLE_VIBRATION_FEEDBACK , enableVibrationFeedback ) ; return this . storage . set ( Settings . ENABLE_VIBRATION_FEEDBACK , enableVibrationFeedback ) ; } getEnableVibrationFeedback ( ) { return this . storage . get ( Settings . ENABLE_VIBRATION_FEEDBACK ) . then ( result => { return result === true ; } ) ; } setEnableLimitBarcodeFormats ( enableLimitBarcodeFormats ) { return this . storage . set ( Settings . ENABLE_LIMIT_BARCODE_FORMATS , enableLimitBarcodeFormats ) ; } getEnableLimitBarcodeFormats ( ) { return this . storage . get ( Settings . ENABLE_LIMIT_BARCODE_FORMATS ) . then ( result => { return result === true ; } ) ; } setBarcodeFormats ( barcodeFormats ) { return this . storage . set ( Settings . BARCODE_FORMATS , barcodeFormats ) ; } getBarcodeFormats ( ) { return new Promise ( ( resolve , reject ) => { this . storage . get ( Settings . BARCODE_FORMATS ) . then ( ( barcodeFormats ) => { if ( barcodeFormats && barcodeFormats . length ) { resolve ( barcodeFormats . map ( element => { return new barcodeFormatModel ( element . name , element . enabled ) ; } ) ) ; } else { resolve ( barcodeFormatModel . supportedBarcodeFormats ) ; } } ) . catch ( ( ) => reject ( ) ) ; } ) ; } setOutputProfiles ( outputProfiles ) { return this . storage . set ( Settings . OUTPUT_PROFILES , outputProfiles ) ; } getOutputProfiles ( ) { return new Promise < OutputProfileModel [ ] > ( async ( resolve , reject ) => { let outputProfiles = await this . storage . get ( Settings . OUTPUT_PROFILES ) ; console . log ( ' @@@ saved output profiles: ' , outputProfiles ) ; if ( ! outputProfiles ) { let defaultOutputProfile = this . generateDefaultOutputProfiles ( ) ; console . log ( ' @@@   null => generating defautl one  ' , defaultOutputProfile ) ; resolve ( defaultOutputProfile ) ; } else { resolve ( outputProfiles ) ; } } ) ; } setSelectedOutputProfile ( selectedOutputProfile ) { return this . storage . set ( Settings . SELECTED_OUTPUT_PROFILE , selectedOutputProfile ) ; } async getSelectedOutputProfile ( ) { return await this . storage . get ( Settings . SELECTED_OUTPUT_PROFILE ) . then ( result => { return result || 0 ; } ) ; } deprecated setQuantityType ( type ) { return this . storage . set ( Settings . QUANTITY_TYPE , type ) ; } deprecated getQuantityType ( ) { return this . storage . get ( Settings . QUANTITY_TYPE ) ; } /**
     * Generates the default OuputProfile based on the QuantityEnabled settings,
     * TODO: In the future versions it should return only a BARCODE + ENTER value,
     * remove the getQuantityEnabled part.
     *
     * It should be keep in sync with the server SettingsModel class.
     */ private generateDefaultOutputProfiles ( ) { return new Promise ( ( resolve , reject ) => { deprecated this . getQuantityEnabled ( ) . then ( quantityEnabled => { if ( quantityEnabled ) { resolve ( [ { name : "Output template 1" , version : null , outputBlocks : [ { name : 'BARCODE' , value : 'BARCODE' , type : 'barcode' , editable : true , skipOutput : false , label : null , enabledFormats : [ ] , filter : null , errorMessage : null } , { name : 'TAB' , value : 'tab' , type : 'key' , modifiers : [ ] } , { name : 'NUMBER' , value : 'number' , type : 'variable' , editable : true , skipOutput : false , label : null , filter : null , errorMessage : null , defaultValue : '1' } , { name : 'ENTER' , value : 'enter' , type : 'key' , modifiers : [ ] } , ] } ] ) ; } else { resolve ( [ { version : null , name : "Output template 1" , outputBlocks : [ { name : 'BARCODE' , value : 'BARCODE' , type : 'barcode' , editable : true , skipOutput : false , label : null , enabledFormats : [ ] , filter : null , errorMessage : null } , { name : 'ENTER' , value : 'enter' , type : 'key' , modifiers : [ ] } , ] } ] ) ; } } ) ; } ) ; } deprecated setQuantityEnabled ( enabled ) { return new Promise < void > ( ( resolve , reject ) => { this . storage . set ( Settings . QUANTITY_ENABLED , enabled ) . then ( async ( ) => { let defaultOutputProfiles = await this . generateDefaultOutputProfiles ( ) ; this . setOutputProfiles ( defaultOutputProfiles ) . then ( ( ) => resolve ( ) ) ; } ) ; } ) ; } deprecated getQuantityEnabled ( ) { return this . storage . get ( Settings . QUANTITY_ENABLED ) . then ( result => { return result === true ; } ) ; } setEventsOnSmartphoneChargeEnabled ( enabled ) { return this . storage . set ( Settings . EVENT_ON_SMARTPHONE_CHARGE_ENABLED , enabled ) ; } getEventsOnSmartphoneChargeEnabled ( ) { return this . storage . get ( Settings . EVENT_ON_SMARTPHONE_CHARGE_ENABLED ) . then ( result => { return result === true ; } ) ; } setOfflineModeEnabled ( enabled ) { return this . storage . set ( Settings . OFFLINE_MODE_ENABLED , enabled ) ; } getOfflineModeEnabled ( ) { return this . storage . get ( Settings . OFFLINE_MODE_ENABLED ) . then ( result => { return result === true ; } ) ; } }