import { Component } from '@angular/core' ; import { FirebaseAnalytics } from '@ionic-native/firebase-analytics' ; import { LaunchReview } from '@ionic-native/launch-review' ; import * as BluebirdPromise from 'bluebird' ; import { AlertController , ItemSliding , NavController , Platform , PopoverController } from 'ionic-angular' ; import { Subscription } from 'rxjs' ; import { discoveryResultModel } from '../../models/discovery-result' ; import { requestModelDeleteScanSessions } from '../../models/request.model' ; import { ScanSessionModel } from '../../models/scan-session.model' ; import { ScanModel } from '../../models/scan.model' ; import { Config } from '../../providers/config' ; import { ScanSessionsStorage } from '../../providers/scan-sessions-storage' ; import { ServerProvider } from '../../providers/server' ; import { Utils } from '../../providers/utils' ; import { ScanSessionPage } from '../scan-session/scan-session' ; import { SelectServerPage } from '../select-server/select-server' ; import { Settings } from './../../providers/settings' ; @ Component ( { selector : 'page-scannings' , templateUrl : 'scan-sessions.html' , } ) export class ScanSessionsPage { private onConnectSubscription : Subscription ; private onDisconnectSubscription : Subscription ; public scanSessions : ScanSessionModel [ ] = [ ] ; public selectedScanSessions : ScanSessionModel [ ] = [ ] ; public connected = false ; private everConnected = false ; private isWatching = false ; private preventClickTimeout = null ; private clickDisabled = false ; private unregisterBackButton = null ; constructor ( public navCtrl , private alertCtrl , private scanSessionsStorage , public popoverCtrl , private firebaseAnalytics , private settings , private launchReview , private serverProvider , private utils , public platform ) { } async ionViewDidEnter ( ) { this . isWatching = false ; this . serverProvider . stopWatchForServers ( ) ; this . firebaseAnalytics . setCurrentScreen ( 'ScanSessionsPage' ) ; this . scanSessionsStorage . getScanSessions ( ) . then ( data => { this . scanSessions = data ; } ) ; this . unregisterBackButton = this . platform . registerBackButtonAction ( ( ) => { if ( this . selectedScanSessions . length != 0 ) { this . unselectAll ( ) ; } else { this . platform . exitApp ( ) ; } } , 0 ) ; this . connected = this . serverProvider . isConnected ( ) ; this . settings . getOfflineModeEnabled ( ) . then ( offlineMode => { if ( offlineMode ) this . connected = false ; } ) ; this . utils . askWiFiEnableIfDisabled ( ) ; this . onDisconnectSubscription = this . serverProvider . onDisconnect ( ) . subscribe ( ( ) => { this . connected = false ; if ( this . isWatching ) return ; this . isWatching = true ; this . reconnect ( ) ; } ) ; this . onConnectSubscription = this . serverProvider . onConnect ( ) . subscribe ( ( ) => { this . connected = true ; this . everConnected = true ; this . serverProvider . stopWatchForServers ( ) ; this . isWatching = false ; BluebirdPromise . join ( this . settings . getNoRunnings ( ) , this . settings . getRated ( ) , async ( runnings , rated ) => { if ( runnings >= Config . NO_RUNNINGS_BEFORE_SHOW_RATING && ! rated ) { if ( this . launchReview . isRatingSupported ( ) ) { this . launchReview . rating ( ) . then ( result => { if ( result === "dismissed" ) { this . settings . setRated ( true ) ; } } ) ; } else { this . alertCtrl . create ( { title : await this . utils . text ( 'rateBarcodeToPcDialogTitle' , { "appName" : await this . utils . text ( 'appName' ) , } ) , message : await this . utils . text ( 'rateBarcodeToPcDialogMessage' , { "appName" : await this . utils . text ( 'appName' ) , } ) , buttons : [ { text : await this . utils . text ( 'rateBarcodeToPcDialogremindMeLaterButton' ) , role : 'cancel' } , { text : await this . utils . text ( 'rateBarcodeToPcDialogNoButton' ) , handler : ( ) => { this . settings . setRated ( true ) ; } } , { text : await this . utils . text ( 'rateBarcodeToPcDialogRateButton' ) , handler : ( ) => { this . launchReview . launch ( ) . then ( ( ) => { this . settings . setRated ( true ) ; } ) ; } } ] } ) . present ( ) ; } } } ) ; } ) ; this . reconnect ( ) ; } ionViewWillUnload ( ) { if ( this . onConnectSubscription != null ) { this . onConnectSubscription . unsubscribe ( ) ; } if ( this . onDisconnectSubscription != null ) { this . onDisconnectSubscription . unsubscribe ( ) ; } } async reconnect ( ) { let offlineMode = await this . settings . getOfflineModeEnabled ( ) ; if ( offlineMode ) return ; let defaultServer = await this . settings . getDefaultServer ( ) ; this . serverProvider . watchForServers ( ) . delay ( defaultServer == null ? 0 : 8000 ) . subscribe ( ( discoveryResult ) => { if ( this . connected ) return ; if ( defaultServer != null && defaultServer . name == discoveryResult . server . name && discoveryResult . server . name . length && defaultServer . address != discoveryResult . server . address ) { setTimeout ( async ( ) => { if ( this . serverProvider . isConnected ( ) ) return ; this . alertCtrl . create ( { title : await this . utils . text ( 'reconnectDialogTitle' ) , message : await this . utils . text ( 'reconnectDialogMessage' , { "defaultServerName" : defaultServer . name , "defaultServerAddress" : defaultServer . address , "discoveryServerAddress" : discoveryResult . server . address } ) , buttons : [ { text : await this . utils . text ( 'reconnectDialogNoButton' ) , role : 'cancel' , handler : ( ) => { } } , { text : await this . utils . text ( 'reconnectDialogReconnectButton' ) , handler : ( ) => { this . settings . setDefaultServer ( discoveryResult . server ) ; this . settings . getSavedServers ( ) . then ( savedServers => { this . settings . setSavedServers ( savedServers . filter ( x => x . name != discoveryResult . server . name ) . concat ( discoveryResult . server ) ) ; } ) ; this . serverProvider . connect ( discoveryResult . server , true ) ; } } ] } ) . present ( ) ; } , 3000 ) ; } else if ( defaultServer == null || ( defaultServer . name == discoveryResult . server . name && defaultServer . address == discoveryResult . server . address && this . everConnected ) ) { this . serverProvider . connect ( discoveryResult . server , true ) ; } } ) ; if ( this . serverProvider . isConnected ( ) ) { this . connected = true ; } else if ( defaultServer != null ) { this . serverProvider . connect ( defaultServer ) ; } } async ionViewDidLoad ( ) { this . settings . getOpenScanOnStart ( ) . then ( openScanOnStart => { if ( openScanOnStart ) { this . navCtrl . push ( ScanSessionPage ) ; } } ) ; } ionViewDidLeave ( ) { if ( this . unregisterBackButton != null ) { this . unregisterBackButton ( ) ; this . unregisterBackButton = null ; } if ( this . onDisconnectSubscription != null ) { this . onDisconnectSubscription . unsubscribe ( ) ; this . onDisconnectSubscription = null ; } if ( this . onConnectSubscription != null ) { this . onConnectSubscription . unsubscribe ( ) ; this . onConnectSubscription = null ; } } ionViewWillLeave ( ) { this . unselectAll ( ) ; } onSelectServerClick ( ) { this . navCtrl . push ( SelectServerPage ) ; } onScanSessionClick ( scanSession , index ) { if ( this . clickDisabled ) return ; if ( this . selectedScanSessions . length == 0 ) { this . navCtrl . push ( ScanSessionPage , { scanSession : scanSession } ) ; } else { this . select ( scanSession , index ) ; } } onSelectAllClick ( ) { this . scanSessions . forEach ( x => x . selected = true ) ; this . selectedScanSessions = [ ] . concat ( this . scanSessions ) ; } onScanSessionPress ( scanSession , index ) { if ( this . selectedScanSessions . length == 0 ) { if ( this . preventClickTimeout ) clearTimeout ( this . preventClickTimeout ) ; this . clickDisabled = true ; this . preventClickTimeout = setTimeout ( ( ) => this . clickDisabled = false , 500 ) ; } this . select ( scanSession , index ) ; } onCancelSelectionClick ( ) { this . unselectAll ( ) ; } async onDeleteClick ( scanSession , index , slidingItem ) { slidingItem . close ( ) ; this . alertCtrl . create ( { title : await this . utils . text ( 'scanSessionDeleteDialogTitle' ) , message : await this . utils . text ( 'scanSessionDialogMessage' , { "scanSessionName" : scanSession . name } ) , buttons : [ { text : await this . utils . text ( 'scanSessionDialogCancelButton' ) , role : 'cancel' } , { text : await this . utils . text ( 'scanSessionDialogDeleteButton' ) , handler : ( ) => { this . removeScanSession ( index ) ; this . save ( ) ; this . sendDeleteScanSessions ( [ scanSession ] ) ; } } ] } ) . present ( ) ; } private unselectAll ( ) { this . selectedScanSessions . forEach ( x => x . selected = false ) ; this . selectedScanSessions = [ ] ; } private select ( scanSession , index ) { if ( scanSession . selected ) { scanSession . selected = false ; if ( this . selectedScanSessions . length == 1 ) { this . selectedScanSessions = [ ] ; } else { this . selectedScanSessions . splice ( index , 1 ) ; } } else { scanSession . selected = true ; this . selectedScanSessions . push ( scanSession ) ; } } onAddClick ( ) { this . navCtrl . push ( ScanSessionPage ) ; } onArchiveSelectedClick ( ) { this . scanSessions = this . scanSessions . filter ( x => ! x . selected ) ; this . scanSessionsStorage . pushArchivedScanSessions ( this . selectedScanSessions ) ; this . save ( ) ; this . sendDeleteScanSessions ( this . selectedScanSessions ) ; this . unselectAll ( ) ; } async onDeleteSelectedClick ( ) { this . alertCtrl . create ( { title : await this . utils . text ( 'scanSessionPermanentDeleteDialogTitle' ) , message : ( await this . utils . text ( 'scanSessionPermanentDeleteDialogMessage' ) ) , buttons : [ { text : await this . utils . text ( 'scanSessionPermanentDeleteCancelButton' ) , role : 'cancel' } , { text : await this . utils . text ( 'scanSessionPermanentDeleteDeleteButton' ) , handler : ( ) => { this . scanSessions = this . scanSessions . filter ( x => ! x . selected ) ; this . save ( ) ; this . sendDeleteScanSessions ( this . selectedScanSessions ) ; this . unselectAll ( ) ; } } ] } ) . present ( ) ; } private async sendDeleteScanSessions ( scanSessions ) { if ( this . connected ) { let wsRequest = new requestModelDeleteScanSessions ( ) . fromObject ( { scanSessionIds : scanSessions . map ( x => { return x . id ; } ) } ) ; this . serverProvider . send ( wsRequest ) ; } else { let serverUUIDs = scanSessions . map ( x => x . syncedWith ) . reduce ( ( accumulator , value ) => accumulator . concat ( value ) , [ ] ) . filter ( ( value , index , self ) => { return self . indexOf ( value ) === index ; } ) ; for ( let i = 0 ; i < serverUUIDs . length ; i ++ ) { const serverUUID = serverUUIDs [ i ] ; let deletedIds = await this . settings . getUnsyncedDeletedScanSesions ( serverUUID ) ; deletedIds . push ( ... scanSessions . filter ( x => x . syncedWith . indexOf ( serverUUID ) != - 1 ) . map ( x => x . id ) ) ; this . settings . setUnsyncedDeletedScanSesions ( serverUUID , deletedIds ) ; } let restoredIds = await this . settings . getUnsyncedRestoredScanSesions ( ) ; restoredIds = restoredIds . filter ( x => scanSessions . map ( y => y . id ) . indexOf ( x ) == - 1 ) ; this . settings . setUnsyncedRestoredScanSesions ( restoredIds ) ; } } private save ( ) { this . scanSessionsStorage . setScanSessions ( this . scanSessions ) ; } private removeScanSession ( index ) { if ( this . scanSessions . length == 1 ) { this . scanSessions = [ ] ; } else { this . scanSessions . splice ( index , 1 ) ; } } }