import { Injectable , NgZone } from '@angular/core' ; import { ActionSheetController , NavController , Platform , MenuController , ModalController } from '@ionic/angular' ; import { AppMinimize } from '@ionic-native/app-minimize/ngx' ; import { PlatformLocation } from '@angular/common' ; import { NavigationEnd , NavigationStart , Router } from '@angular/router' ; import { BehaviorSubject , Subject } from 'rxjs' ; declare var window ; declare var StatusBar ; @ Injectable ( { providedIn : 'root' } ) export class ViewService { viewMode = 'home' ; swipeEnable = true ; menuSwipeEnable = false ; showSpeechModal = false ; devicePageIsRoot = false ; statusBarHeight = new BehaviorSubject ( 0 ) ; appReady = new BehaviorSubject ( false ) ; get isIos ( ) { return this . platform . is ( 'ios' ) ; } constructor ( private actionSheetController , private navCtrl , private platform , private platformLocation , private menu , private appMinimize , private router , private modalCtrl , private ngzone ) { } async init ( ) { this . platform . ready ( ) . then ( result => { if ( result ) this . appReady . next ( true ) ; } ) ; this . getStatusBarHeight ( ) . then ( value => { this . statusBarHeight . next ( value ) ; } ) ; this . listenRouter ( ) ; this . listenBackButton ( ) ; this . checkShortcut ( ) ; } listenBackButton ( ) { this . platform . backButton . subscribeWithPriority ( 9999 , async ( ) => { if ( typeof await this . modalCtrl . getTop ( ) != 'undefined' ) { console . log ( 'close modal' ) ; this . modalCtrl . dismiss ( ) ; } else if ( this . menuSwipeEnable && this . platformLocation . pathname . indexOf ( '/view' ) > - 1 ) { console . log ( 'close menu' ) ; this . menu . close ( ) ; } else if ( this . showSpeechModal ) { this . showSpeechModal = false ; } else if ( this . platformLocation . pathname . indexOf ( '/view' ) > - 1 ) { this . appMinimize . minimize ( ) ; } else if ( this . devicePageIsRoot && this . platformLocation . pathname . indexOf ( '/device/' ) > - 1 ) { this . appMinimize . minimize ( ) ; } else { this . navCtrl . pop ( ) ; } } ) ; } disableSwipeBack ( ) { setTimeout ( ( ) => { this . swipeEnable = false ; } ) ; } enableSwipeBack ( ) { setTimeout ( ( ) => { this . swipeEnable = true ; } ) ; } disableMenuSwipe ( ) { this . menuSwipeEnable = false ; } enableMenuSwipe ( ) { this . menuSwipeEnable = true ; } async changeView ( ) { const actionSheet = await this . actionSheetController . create ( { header : '视图模式切换' , buttons : [ { text : '常规视图' , handler : ( ) => { this . viewMode = 'home' ; this . navCtrl . navigateRoot ( '/view/home' ) ; } } , { text : '地理视图' , handler : ( ) => { this . viewMode = 'gis' ; this . navCtrl . navigateRoot ( '/view/gis' ) ; } } , ] } ) ; await actionSheet . present ( ) ; } currentStatusBar = 'dark' ; setLightStatusBar ( ) { if ( ! this . platform . is ( 'cordova' ) ) return ; StatusBar . styleLightContent ( ) ; this . currentStatusBar = 'light' ; } setDarkStatusBar ( ) { if ( ! this . platform . is ( 'cordova' ) ) return ; StatusBar . styleDefault ( ) ; this . currentStatusBar = 'dark' ; } listenRouter ( ) { this . changeStatusBar ( this . router . url ) ; this . router . events . subscribe ( e => { if ( e instanceof NavigationEnd ) { this . changeStatusBar ( e . url ) ; } } ) ; } changeStatusBar ( url ) { console . log ( 'url:' , url ) ; setTimeout ( ( ) => { let els = document . querySelectorAll ( 'ion-header' ) ; if ( typeof els [ els . length - 1 ] . attributes [ 'whitebg' ] != 'undefined' ) { this . setDarkStatusBar ( ) ; } else { this . setLightStatusBar ( ) ; } } , 800 ) ; } newIntentData = new Subject < any > ( ) ; checkShortcut ( ) { if ( ! this . platform . is ( "cordova" ) ) return ; this . getIntentData ( ) . then ( url => { if ( url != null ) { this . devicePageIsRoot = true ; this . navCtrl . navigateRoot ( url ) ; } } ) ; this . getNewIntentData ( ) ; this . newIntentData . subscribe ( url => { console . log ( url ) ; this . ngzone . run ( ( ) => { if ( url == null ) { if ( this . platformLocation . pathname . indexOf ( '/device/' ) > - 1 ) { this . navCtrl . navigateRoot ( '/' ) ; } } else { if ( this . platformLocation . pathname . indexOf ( '/device/' ) > - 1 && this . devicePageIsRoot ) { this . navCtrl . navigateRoot ( url ) ; setTimeout ( ( ) => { this . devicePageIsRoot = true ; } , 500 ) ; } else this . router . navigate ( [ url ] ) ; } } ) ; } ) ; } getIntentData ( ) { return new Promise ( ( resolve , reject ) => { window . plugins . Shortcuts . getIntent ( intent => { if ( typeof intent . data != 'undefined' ) { resolve ( intent . data ) ; } else resolve ( null ) ; } ) ; } ) ; } getNewIntentData ( ) { window . plugins . Shortcuts . onNewIntent ( intent => { console . log ( intent ) ; if ( typeof intent . data != 'undefined' ) { this . newIntentData . next ( intent . data ) ; } } ) ; } getStatusBarHeight ( ) { return new Promise ( ( resolve , reject ) => { if ( ! this . platform . is ( "cordova" ) ) resolve ( 0 ) ; else this . platform . ready ( ) . then ( ( ) => { StatusBar . height ( ( height ) => { resolve ( height ) ; } , ( error ) => { console . log ( error ) ; resolve ( 0 ) ; } ) ; } ) ; } ) ; } }