import { Actions , ofActionSuccessful , Store } from '@ngxs/store' ; import { Inject , Injectable , Renderer2 , RendererFactory2 } from '@angular/core' ; import { SwPush } from '@angular/service-worker' ; import { EventCategory , GoogleAnalyticsService } from '../services/google-analytics.service' ; import { AppState , ChangeInstallStatus , ChangeOnlineStatus , IsOffline , IsOnline , SetInstallPrompt } from './app.state' ; import { WINDOW } from '../services/window.token' ; import { LoginSuccess } from '@ngx-starter-kit/auth' ; import { FetchProfile } from '../state/profile.state' ; dynamic @ Injectable ( { providedIn : 'root' , } ) export class AppHandler { private renderer : Renderer2 ; constructor ( private actions$ , private store , private readonly swPush , private analytics , private rendererFactory , @ Inject ( WINDOW ) private readonly window ) { this . renderer = rendererFactory . createRenderer ( this . window , null ) ; this . renderer . listen ( this . window , 'online' , ( ) => { this . store . dispatch ( [ new ChangeOnlineStatus ( ) , new IsOnline ( ) ] ) ; } ) ; this . renderer . listen ( this . window , 'offline' , ( ) => { this . store . dispatch ( [ new ChangeOnlineStatus ( ) , new IsOffline ( ) ] ) ; } ) ; const installed = this . store . selectSnapshot ( AppState . installed ) ; if ( ! installed ) { this . renderer . listen ( this . window , 'beforeinstallprompt' , ( event ) => { event . preventDefault ( ) ; this . analytics . emitEvent ( EventCategory . Install , 'available' ) ; this . store . dispatch ( new SetInstallPrompt ( event ) ) ; } ) ; this . renderer . listen ( this . window , 'appinstalled' , ( event ) => { event . preventDefault ( ) ; this . analytics . emitEvent ( EventCategory . Install , 'installed' ) ; this . store . dispatch ( new ChangeInstallStatus ( true ) ) ; } ) ; this . actions$ . pipe ( ofActionSuccessful ( SetInstallPrompt ) ) . subscribe ( async ( action ) => { try { console . log ( 'platforms' , action . payload . platforms ) ; action . payload . prompt ( ) ; const choiceResult = await action . payload . userChoice ; console . log ( 'choiceResult' , choiceResult ) ; this . analytics . emitEvent ( EventCategory . Install , choiceResult ) ; } catch ( installError ) { this . analytics . emitEvent ( EventCategory . Install , 'errored' ) ; } } ) ; this . actions$ . pipe ( ofActionSuccessful ( LoginSuccess ) ) . subscribe ( ( action ) => { this . analytics . setUsername ( action . payload . preferred_username ) ; console . log ( 'setting action.payload.preferred_username' , action . payload . preferred_username ) ; this . store . dispatch ( new FetchProfile ( ) ) ; } ) ; } if ( this . swPush . isEnabled ) { this . swPush . messages . subscribe ( ( message ) => { console . log ( 'received push notification' , message ) ; } ) ; } } }