import { Component , ViewChild } from '@angular/core' ; import { Events , ionicBootstrap , MenuController , Nav , Platform } from 'ionic-angular' ; import { Splashscreen , StatusBar } from 'ionic-native' ; import { AccountPage } from './pages/account/account' ; import { ConferenceData } from './providers/conference-data' ; import { LoginPage } from './pages/login/login' ; import { SignupPage } from './pages/signup/signup' ; import { TabsPage } from './pages/tabs/tabs' ; import { UserData } from './providers/user-data' ; interface PageObj { title : string ; component : any ; icon : string ; index ? : number ; } @ Component ( { templateUrl : 'build/app.html' } ) class ConferenceApp { @ ViewChild ( Nav ) nav : Nav ; appPages : PageObj [ ] = [ { title : 'Schedule' , component : TabsPage , icon : 'calendar' } , { title : 'Speakers' , component : TabsPage , index : 1 , icon : 'contacts' } , { title : 'Map' , component : TabsPage , index : 2 , icon : 'map' } , { title : 'About' , component : TabsPage , index : 3 , icon : 'information-circle' } , ] ; loggedInPages : PageObj [ ] = [ { title : 'Account' , component : AccountPage , icon : 'person' } , { title : 'Logout' , component : TabsPage , icon : 'log-out' } ] ; loggedOutPages : PageObj [ ] = [ { title : 'Login' , component : LoginPage , icon : 'log-in' } , { title : 'Signup' , component : SignupPage , icon : 'person-add' } ] ; rootPage : any = TabsPage ; constructor ( public events , public userData , public menu , platform , private confData ) { platform . ready ( ) . then ( ( ) => { StatusBar . styleDefault ( ) ; Splashscreen . hide ( ) ; } ) ; confData . load ( ) ; this . userData . hasLoggedIn ( ) . then ( ( hasLoggedIn ) => { this . enableMenu ( hasLoggedIn === 'true' ) ; } ) ; this . listenToLoginEvents ( ) ; } openPage ( page ) { if ( page . index ) { this . nav . setRoot ( page . component , { tabIndex : page . index } ) ; } else { this . nav . setRoot ( page . component ) ; } if ( page . title === 'Logout' ) { setTimeout ( ( ) => { this . userData . logout ( ) ; } , 1000 ) ; } } listenToLoginEvents ( ) { this . events . subscribe ( 'user:login' , ( ) => { this . enableMenu ( true ) ; } ) ; this . events . subscribe ( 'user:signup' , ( ) => { this . enableMenu ( true ) ; } ) ; this . events . subscribe ( 'user:logout' , ( ) => { this . enableMenu ( false ) ; } ) ; this . events . subscribe ( 'favs:sync' , ( ) => { this . confData . load ( ) ; } ) ; } enableMenu ( loggedIn ) { this . menu . enable ( loggedIn , 'loggedInMenu' ) ; this . menu . enable ( ! loggedIn , 'loggedOutMenu' ) ; } } ionicBootstrap ( ConferenceApp , [ ConferenceData , UserData ] , { } ) ;