/**
* a convenience NativeScript shim to approximate the Ionic Platform Service
*/ import { Injectable } from "@angular/core" ; import { ios , android , displayedEvent , exitEvent , launchEvent , lowMemoryEvent , orientationChangedEvent , resumeEvent , suspendEvent , uncaughtErrorEvent , ApplicationEventData , LaunchEventData , OrientationChangedEventData , UnhandledErrorEventData , on as applicationOn , run as applicationRun } from "tns-core-modules/application" ; import { EventsService } from './events.service' ; import { Subject } from 'rxjs/Subject' ; event event @ Injectable ( ) export class PlatformService { public paused : boolean ; /**
    * platform service ready promise
    */ public readyPromise : Promise < any > ; public readyPromiseResolver : any ; link constructor ( private events ) { this . readyPromise = new Promise ( ( resolve , reject ) => { this . readyPromiseResolver = resolve ; } ) ; applicationOn ( launchEvent , ( args ) => { if ( args . android ) { console . log ( "PlatformService:constructor(): Launched Android application with the following intent: " + args . android + "." ) ; } else if ( args . ios !== undefined ) { console . log ( "PlatformService:constructor(): Launched iOS application with options: " + args . ios ) ; } this . events . publish ( 'platform:launched' , { } ) ; } ) ; applicationOn ( suspendEvent , ( args ) => { if ( args . android ) { console . log ( "PlatformService:constructor(): Activity suspend: " + args . android ) ; } else if ( args . ios ) { console . log ( "PlatformService:constructor(): UIApplication suspend: " + args . ios ) ; } this . paused = true ; this . events . publish ( 'platform:pause' , { } ) ; } ) ; applicationOn ( resumeEvent , ( args ) => { if ( args . android ) { console . log ( "PlatformService:constructor(): Activity resume: " + args . android ) ; } else if ( args . ios ) { console . log ( "PlatformService:constructor(): UIApplication resume: " + args . ios ) ; } this . paused = false ; this . events . publish ( 'platform:resume' , { } ) ; } ) ; applicationOn ( displayedEvent , ( args ) => { console . log ( "PlatformService:constructor(): displayedEvent" ) ; this . events . publish ( 'platform:displayed' , { } ) ; } ) ; applicationOn ( orientationChangedEvent , ( args ) => { console . log ( args . newValue ) ; } ) ; applicationOn ( exitEvent , ( args ) => { if ( args . android ) { console . log ( "PlatformService:constructor(): Activity exit: " + args . android ) ; } else if ( args . ios ) { console . log ( "PlatformService:constructor(): UIApplication exit: " + args . ios ) ; } this . events . publish ( 'platform:exit' , { } ) ; } ) ; applicationOn ( lowMemoryEvent , ( args ) => { if ( args . android ) { console . log ( "PlatformService:constructor(): Activity low memory: " + args . android ) ; } else if ( args . ios ) { console . log ( "PlatformService:constructor(): UIApplication low memory: " + args . ios ) ; } this . events . publish ( 'platform:lowmemory' , { } ) ; } ) ; applicationOn ( uncaughtErrorEvent , function ( args ) { console . log ( "PlatformService:constructor(): Error: " + args . error ) ; } ) ; } /**
    * declare the platform ready
    *
    * FIXME: We're simulating an Ionic style platform here. This method needs
    * to be called from the angular app component.
    */ declareReady ( ) { this . readyPromiseResolver ( true ) ; this . events . publish ( 'platform:ready' , { } ) ; } /**
    * return the platform ready promise
    */ ready ( ) { return this . readyPromise ; } /**
    * whether we are android or ios
    */ is ( platformName ) { switch ( platformName ) { case 'ios' : return ios ; break ; case 'android' : return android ; break ; default : throw new Error ( 'unsupported platform' ) ; break ; } } isPaused ( ) { return this . paused ; } }