import { Component , ViewChild } from '@angular/core' ; import { TranslateService } from 'ng2-translate/ng2-translate' ; import { Events , MenuController , AlertController , Nav , Platform } from 'ionic-angular' ; import { Storage } from '@ionic/storage' ; import { Deploy } from '@ionic/cloud-angular' ; import { SplashScreen } from '@ionic-native/splash-screen' ; import { Deeplinks } from '@ionic-native/deeplinks' ; import { TextToSpeech } from '@ionic-native/text-to-speech' ; import { AppRate } from '@ionic-native/app-rate' ; import { GoogleAnalytics } from '@ionic-native/google-analytics' ; import { Push , PushObject , PushOptions } from '@ionic-native/push' ; import { HomePage } from '../pages/home/home' ; import { CategoriesPage } from '../pages/categories/categories' ; import { ProductsPage } from '../pages/products/products' ; import { ProductDetailPage } from '../pages/product-detail/product-detail' ; import { WishlistPage } from '../pages/wishlist/wishlist' ; import { ShowcartPage } from '../pages/showcart/showcart' ; import { AboutPage } from '../pages/about/about' ; import { AccountPage } from '../pages/account/account' ; import { LoginPage } from '../pages/login/login' ; import { SignupPage } from '../pages/signup/signup' ; import { SettingsPage } from '../pages/settings/settings' ; import { WalkThroughPage } from '../pages/walkthrough/walkthrough' ; import { SupportPage } from '../pages/support/support' ; import { UserService } from '../providers/user-service' ; export interface PageInterface { title : string ; component : any ; icon : string ; logsOut ? : boolean ; index ? : number ; tabComponent ? : any ; } @ Component ( { templateUrl : 'app.template.html' , providers : [ TranslateService ] } ) export class vPlanetApp { @ ViewChild ( Nav ) nav : Nav ; appPages : PageInterface [ ] = [ { title : 'home' , component : HomePage , icon : 'home' } , { title : 'categories' , component : CategoriesPage , index : 1 , icon : 'list' } , { title : 'cart' , component : ShowcartPage , index : 2 , icon : 'cart' } , { title : 'wishlist' , component : WishlistPage , index : 3 , icon : 'heart' } ] ; loggedInPages : PageInterface [ ] = [ { title : 'account' , component : AccountPage , icon : 'person' } , { title : 'logout' , component : HomePage , icon : 'log-out' , logsOut : true } ] ; loggedOutPages : PageInterface [ ] = [ { title : 'login' , component : LoginPage , icon : 'log-in' } , { title : 'signup' , component : SignupPage , icon : 'person-add' } ] ; otherPages : PageInterface [ ] = [ { title : 'about' , component : AboutPage , icon : 'cube' } , { title : 'settings' , component : SettingsPage , icon : 'settings' } , { title : 'feedback' , component : SupportPage , icon : 'mail-open' } ] ; rootPage : any ; constructor ( public events , public userService , public menu , public platform , public storage , public translate , public deploy , public alertCtrl , private splashScreen , private deeplinks , private appRate , private ga , private push , private tts ) { this . storage . get ( 'hasSeenWalkThrough' ) . then ( ( hasSeenWalkThrough ) => { if ( hasSeenWalkThrough ) { this . rootPage = HomePage ; } else { this . rootPage = WalkThroughPage ; } this . platformReady ( ) ; } ) ; this . userService . isAuthenticated ( ) . then ( ( isAuthenticated ) => { this . enableMenu ( isAuthenticated === true ) ; } ) ; this . listenToLoginEvents ( ) ; this . initializeTranslateServiceConfig ( ) ; if ( this . platform . is ( 'cordova' ) ) { this . deploy . check ( ) . then ( ( snapshotAvailable ) => { if ( snapshotAvailable ) { let alert = this . alertCtrl . create ( { title : 'Update Available !' , message : 'Do you want to update the application now ?' , buttons : [ { text : 'Cancel' , role : 'cancel' , handler : ( ) => { console . log ( 'Cancel clicked' ) ; } } , { text : 'Update' , handler : ( ) => { this . deploy . download ( ) . then ( ( ) => { this . deploy . extract ( ) ; } ) . then ( ( ) => { this . deploy . load ( ) ; } ) ; } } ] } ) ; alert . present ( ) ; } } ) ; } } initializeTranslateServiceConfig ( ) { } openPage ( page ) { if ( page . index ) { this . nav . setRoot ( page . component , { tabIndex : page . index } ) ; } else { this . nav . setRoot ( page . component ) . catch ( ( ) => { console . log ( "Didn't set nav root" ) ; } ) ; } if ( page . logsOut === true ) { setTimeout ( ( ) => { this . userService . logout ( ) ; } , 1000 ) ; } } listenToLoginEvents ( ) { this . events . subscribe ( 'user:login' , ( ) => { this . enableMenu ( true ) ; } ) ; this . events . subscribe ( 'user:logout' , ( ) => { this . enableMenu ( false ) ; } ) ; } enableMenu ( loggedIn ) { this . menu . enable ( loggedIn , 'loggedInMenu' ) ; this . menu . enable ( ! loggedIn , 'loggedOutMenu' ) ; } platformReady ( ) { this . platform . ready ( ) . then ( ( ) => { this . splashScreen . hide ( ) ; this . deeplinks . route ( { '/about-us' : AboutPage , '/categories' : CategoriesPage , '/wishlist' : WishlistPage , '/cart' : ShowcartPage , '/login' : LoginPage , '/settings' : SettingsPage , '/categories/:categoryId' : ProductsPage , '/products/:productId' : ProductDetailPage } ) . subscribe ( ( match ) => { console . log ( 'Successfully routed' , match ) ; } , ( nomatch ) => { console . warn ( 'Unmatched Route' , nomatch ) ; } ) ; if ( this . platform . is ( 'cordova' ) ) { this . appRate . preferences . customLocale = { title : "Rate vPlanet Commerce" , message : "Would you mind taking a moment to rate it? It won’t take more than a minute. Thanks for your support!" , cancelButtonLabel : "No, Thanks" , laterButtonLabel : "Remind Me Later" , rateButtonLabel : "Rate It Now" } ; this . appRate . preferences . storeAppURL = { android : 'market://details?id=com.vplanetcommerce.demoionicpwa' } ; this . appRate . promptForRating ( false ) ; this . initPushNotification ( ) ; } return this . ga . startTrackerWithId ( "UA-92660667-1" ) . then ( ( ) => { console . log ( 'Google analytics is ready now' ) ; return this . ga . enableUncaughtExceptionReporting ( true ) ; } ) . then ( ( _success ) => { console . log ( "startTrackerWithId success" ) ; } ) . catch ( ( _error ) => { console . log ( "enableUncaughtExceptionReporting" , _error ) ; } ) ; } ) ; } isActive ( page ) { if ( this . nav . getActive ( ) && this . nav . getActive ( ) . component === page . component ) { return 'primary' ; } return ; } initPushNotification ( ) { const options = { android : { senderID : '12345679' } , ios : { alert : 'true' , badge : true , sound : 'false' } , windows : { } } ; const pushObject = this . push . init ( options ) ; let self = this ; pushObject . on ( 'notification' ) . subscribe ( ( data ) => { console . log ( 'Received a notification' , data ) ; self . tts . speak ( { text : data . message , locale : 'en-IN' , rate : 0.75 } ) . then ( ( ) => console . log ( 'Success' ) ) . catch ( ( reason ) => console . log ( reason ) ) ; if ( data . additionalData . foreground ) { alert ( data . message ) ; } self . nav . push ( AboutPage , { message : data . message } ) ; } ) ; pushObject . on ( 'registration' ) . subscribe ( ( registration ) => console . log ( 'Device registered' , registration ) ) ; pushObject . on ( 'error' ) . subscribe ( error => console . error ( 'Error with Push plugin' , error ) ) ; } }