import { Component , ViewChild } from '@angular/core' ; import { SplashScreen } from '@ionic-native/splash-screen' ; import { StatusBar } from '@ionic-native/status-bar' ; import { App , IonicApp , Nav , Platform , ToastController } from 'ionic-angular' ; import { LnLoadingController } from '../common/ln-loading-controller' ; import { AppUpdateService } from '../providers/app-update-service' ; import { DownloadService } from '../providers/download-service' ; import { EpubService } from '../providers/epub-service' ; import { ReaderSettingsService } from '../providers/reader-settings-service' ; import { SettingsService } from '../providers/settings-service' ; import { LastReadChapterService } from '../providers/last-read-chapter-service' ; @ Component ( { templateUrl : "app.html" } ) export class MyApp { @ ViewChild ( Nav ) nav : Nav ; rootPage : any ; overallPages : Array < any > ; userPages : Array < any > ; miscPages : Array < any > ; constructor ( public platform , public statusBar , public splashScreen , private ionicApp , private app , private readerSettingsService , private loadingController , private toastCtrl , private downloadService , private epubService , private settingsService , private lastReadChapterService , private appUpdateService ) { this . initializeApp ( ) ; this . overallPages = [ { title : "All novels" , icon : "home" , component : "LnList" } , ] ; this . userPages = [ { title : "Favorites" , icon : "heart" , component : "LnFavorites" } , { title : "Recently viewed" , icon : "book" , component : "LnRecentNovelsPage" } , { title : "Downloaded" , icon : "cloud-done" , component : "LnDownloadsListPage" } , { title : "Downloading queue" , icon : "cloud-download" , component : "LnDownloadsQueuePage" } ] ; this . miscPages = [ { title : "Settings" , icon : "settings" , component : "LnSettings" } , { title : "About" , icon : "body" , component : "LnAbout" } ] ; } initializeApp ( ) { this . platform . ready ( ) . then ( ( ) => { this . appUpdateService . checkForUpdate ( ) ; this . statusBar . styleDefault ( ) ; this . splashScreen . hide ( ) ; let settingsLoaded = this . settingsService . init ( ) ; this . readerSettingsService . init ( ) ; this . loadingController . init ( ) ; this . lastReadChapterService . init ( ) ; this . registerBackButtonHandler ( ) ; settingsLoaded . then ( ( settings ) => { this . downloadService . init ( ) ; this . epubService . init ( ) ; this . rootPage = settings . startupScreen ; } ) ; } ) ; } get allPages ( ) { return [ { header : "Catalog" , pages : this . overallPages } , { header : "My picks" , pages : this . userPages } , { header : "Miscellaneous" , pages : this . miscPages } ] ; } openPage ( page ) { this . nav . setRoot ( page . component ) ; } private registerBackButtonHandler ( ) { var lastTimeBackPress = 0 ; var timePeriodToExit = 2000 ; this . platform . registerBackButtonAction ( ( ) => { let activePortal = this . ionicApp . _loadingPortal . getActive ( ) || this . ionicApp . _modalPortal . getActive ( ) || this . ionicApp . _overlayPortal . getActive ( ) ; if ( activePortal ) { activePortal . dismiss ( ) ; return ; } let view = this . nav . getActive ( ) ; if ( this . nav . canGoBack ( ) || view && view . isOverlay ) { this . nav . pop ( ) ; } else { if ( new Date ( ) . getTime ( ) - lastTimeBackPress < timePeriodToExit ) { this . platform . exitApp ( ) ; } else { let toast = this . toastCtrl . create ( { message : "Press back again to exit the application." , duration : timePeriodToExit , position : "bottom" } ) ; toast . present ( ) ; lastTimeBackPress = new Date ( ) . getTime ( ) ; } } } , 0 ) ; } }