import { StorageService } from 'src/app/services/storage.service'; import { Component } from '@angular/core'; import { Platform } from '@ionic/angular'; import { SplashScreen } from '@ionic-native/splash-screen/ngx'; import { StatusBar } from '@ionic-native/status-bar/ngx'; import { RouterService } from 'src/app/services/router.service'; import { ScreenOrientation } from '@ionic-native/screen-orientation/ngx'; import { AuthService } from './auth/auth.service'; import { Subscription } from 'rxjs'; import { Plugins, Capacitor, AppState } from '@capacitor/core'; import { take } from 'rxjs/operators'; import { UserService } from './services/user.service'; @Component({ selector: 'app-root', templateUrl: 'app.component.html', styleUrls: ['app.component.scss'] }) export class AppComponent { username: any; Lang: any; private authSub: Subscription; private previousAuthState = false; constructor( private screenOrientation:ScreenOrientation, private storage:StorageService, private router:RouterService, private platform: Platform, private splashScreen: SplashScreen, private statusBar: StatusBar, private authService: UserService ) { this.initializeApp(); } initializeApp() { this.platform.ready().then(() => { this.statusBar.styleDefault(); this.splashScreen.hide(); this.storage.get("username").then(data=>{console.log(data,'username'); this.username = data; this.storage.get("lang").then(data=>{console.log(data,'lang'); this.Lang=data; console.log('lang'+this.Lang); if(this.Lang === null || this.Lang==='') this.Lang =1 ; if(data==2) { document.dir='rtl' } else { document.dir='ltr' } }, error=>{console.log(error,'langError');}); }, error=>{console.log(error,'langError');}); // this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.PORTRAIT); this.router.navByUrl("splash"); }); } logout() { this.authService.logout(); this.router.navByUrl("auth"); } ngOnInit() { this.authSub = this.authService.username.subscribe(username => { this.username = username; }); Plugins.App.addListener( 'appStateChange', this.checkAuthOnResume.bind(this) ); } ngOnDestroy() { if (this.authSub) { this.authSub.unsubscribe(); } // Plugins.App.removeListener('appStateChange', this.checkAuthOnResume); } ionViewDidEnter() { this.router.navByUrl("splash"); } private checkAuthOnResume(state: AppState) { if (state.isActive) { this.authService .autoLogin() .pipe(take(1)) .subscribe(success => { if (!success) { this.logout(); } }); } } }