import { Component , ViewChild } from '@angular/core' ; import { SplashScreen } from '@ionic-native/splash-screen' ; import { StatusBar } from '@ionic-native/status-bar' ; import { TranslateService } from '@ngx-translate/core' ; import { Config , Nav , Platform , ToastController } from 'ionic-angular' ; import { AdvertPage , FirstRunPage } from '../pages/pages' ; import { TabsPage } from "../pages/tabs/tabs" ; import { User } from '../providers/providers' ; @ Component ( { template : `
    <ion-nav [root]="rootPage"></ion-nav>` } ) export class MyApp { rootPage : any ; backButtonPressed : boolean = false ; @ ViewChild ( Nav ) nav : Nav ; constructor ( private translate , public platform , private toastCtrl , public config , private statusBar , private user , private splashScreen ) { platform . ready ( ) . then ( ( ) => { this . statusBar . hide ( ) ; this . splashScreen . hide ( ) ; this . registerBackButtonAction ( ) ; this . registerAuthentication ( ) ; } ) ; this . initTranslate ( ) ; } initTranslate ( ) { this . translate . setDefaultLang ( 'zh' ) ; if ( this . translate . getBrowserLang ( ) !== undefined ) { this . translate . use ( 'zh' ) ; } else { this . translate . use ( 'zh' ) ; } this . translate . get ( [ 'BACK_BUTTON_TEXT' ] ) . subscribe ( values => { this . config . set ( 'ios' , 'backButtonText' , values . BACK_BUTTON_TEXT ) ; } ) ; } openPage ( page ) { this . nav . setRoot ( page . component ) ; } registerAuthentication ( ) { this . user . getToken ( ) . then ( token => { if ( token ) { return this . rootPage = AdvertPage ; } return this . rootPage = FirstRunPage ; } ) ; } registerBackButtonAction ( ) { this . platform . registerBackButtonAction ( ( ) : any => { let activeVC = this . nav . getActive ( ) ; let page = activeVC . instance ; if ( ! ( page instanceof TabsPage ) ) { if ( ! this . nav . canGoBack ( ) ) { return this . showExit ( ) ; } return this . nav . pop ( ) ; } let tabs = page . tabs ; let activeNav = tabs . getSelected ( ) ; if ( ! activeNav . canGoBack ( ) ) { return this . showExit ( ) ; } return activeNav . pop ( ) ; } , 101 ) ; } showExit ( ) { if ( this . backButtonPressed ) { this . platform . exitApp ( ) ; } else { let toast = this . toastCtrl . create ( { message : '再按一次退出应用' , duration : 2000 , position : 'bottom' } ) ; toast . present ( ) ; this . backButtonPressed = true ; setTimeout ( ( ) => { this . backButtonPressed = false ; } , 2000 ) ; } } }