working message offline

This commit is contained in:
Drew Ansbacher
2021-06-24 16:35:15 -06:00
committed by Aiden McClelland
parent fdbe00aef9
commit ce514646ff
2 changed files with 47 additions and 17 deletions

View File

@@ -3,7 +3,7 @@ import { Storage } from '@ionic/storage'
import { AuthService, AuthState } from './services/auth.service' import { AuthService, AuthState } from './services/auth.service'
import { ApiService } from './services/api/api.service' import { ApiService } from './services/api/api.service'
import { Router, RoutesRecognized } from '@angular/router' import { Router, RoutesRecognized } from '@angular/router'
import { distinctUntilChanged, filter, finalize, takeWhile } from 'rxjs/operators' import { distinctUntilChanged, filter, finalize, map, takeWhile } from 'rxjs/operators'
import { AlertController, ToastController } from '@ionic/angular' import { AlertController, ToastController } from '@ionic/angular'
import { LoaderService } from './services/loader.service' import { LoaderService } from './services/loader.service'
import { Emver } from './services/emver.service' import { Emver } from './services/emver.service'
@@ -24,6 +24,7 @@ export class AppComponent {
showMenu = false showMenu = false
selectedIndex = 0 selectedIndex = 0
untilLoaded = true untilLoaded = true
offlineToast: HTMLIonToastElement
appPages = [ appPages = [
{ {
title: 'Services', title: 'Services',
@@ -112,6 +113,14 @@ export class AppComponent {
takeWhile(() => auth === AuthState.VERIFIED), takeWhile(() => auth === AuthState.VERIFIED),
) )
.subscribe(c => { .subscribe(c => {
if (!c.network || !c.internet) {
this.presentToastOffline()
} else {
if (this.offlineToast) {
this.offlineToast.dismiss()
this.offlineToast = undefined
}
}
console.log('CONNECTION CHANGED', c) console.log('CONNECTION CHANGED', c)
}) })
} }
@@ -216,6 +225,25 @@ export class AppComponent {
await toast.present() await toast.present()
} }
private async presentToastOffline () {
this.offlineToast = await this.toastCtrl.create({
header: 'No Internet',
message: `Please check your Internet connection and try again.`,
position: 'bottom',
duration: 0,
buttons: [
{
side: 'start',
icon: 'close',
handler: () => {
return true
},
},
],
})
await this.offlineToast.present()
}
private async setError (e: Error) { private async setError (e: Error) {
console.error(e) console.error(e)
await this.presentError(e.message) await this.presentError(e.message)

View File

@@ -7,9 +7,9 @@ import { ApiService } from './api/api.service'
providedIn: 'root', providedIn: 'root',
}) })
export class ConnectionService { export class ConnectionService {
private offlineSubscription: Subscription private offlineSubscription$: Subscription
private onlineSubscription: Subscription private onlineSubscription$: Subscription
private httpSubscription: Subscription private httpSubscription$: Subscription
private readonly currentState: ConnectionState = { private readonly currentState: ConnectionState = {
network: true, network: true,
internet: true, internet: true,
@@ -25,9 +25,9 @@ export class ConnectionService {
ngOnDestroy (): void { ngOnDestroy (): void {
try { try {
this.offlineSubscription.unsubscribe() this.offlineSubscription$.unsubscribe()
this.onlineSubscription.unsubscribe() this.onlineSubscription$.unsubscribe()
this.httpSubscription.unsubscribe() this.httpSubscription$.unsubscribe()
} catch (e) { } catch (e) {
console.error(e.message) console.error(e.message)
} }
@@ -44,34 +44,36 @@ export class ConnectionService {
} }
private checkNetworkState (): void { private checkNetworkState (): void {
this.onlineSubscription = fromEvent(window, 'online').subscribe(() => { this.onlineSubscription$ = fromEvent(window, 'online').subscribe(() => {
this.currentState.network = true this.currentState.network = true
this.checkInternetState() this.checkInternetState()
this.emitEvent() this.emitEvent()
}) })
this.offlineSubscription = fromEvent(window, 'offline').subscribe(() => { this.offlineSubscription$ = fromEvent(window, 'offline').subscribe(() => {
this.currentState.network = true this.currentState.network = false
this.checkInternetState() if (this.httpSubscription$) {
this.httpSubscription$.unsubscribe()
}
this.emitEvent() this.emitEvent()
}) })
} }
private checkInternetState (): void { private checkInternetState (): void {
if (this.httpSubscription) { if (this.httpSubscription$) {
this.httpSubscription.unsubscribe() this.httpSubscription$.unsubscribe()
} }
// ping server every 10 seconds // ping server every 10 seconds
this.httpSubscription = timer(0, 10000) this.httpSubscription$ = timer(0, 10000)
.pipe( .pipe(
switchMap(() => this.apiService.echo()), switchMap(() => this.apiService.echo()),
retryWhen(errors => retryWhen(errors =>
errors.pipe( errors.pipe(
tap(val => { tap(val => {
console.error('Echo error: ', val) console.error('Echo error: ', val)
this.currentState.internet = true this.currentState.internet = false
this.emitEvent() this.emitEvent()
}), }),
// restart after 2 seconds // restart after 2 seconds
@@ -86,7 +88,7 @@ export class ConnectionService {
} }
private emitEvent (): void { private emitEvent (): void {
this.stateChangeEventEmitter.next(this.currentState) this.stateChangeEventEmitter.next({ ...this.currentState })
} }
} }