Skip to content

Commit

Permalink
fix(network): connection message after leaving the tab on the phone (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
drekss authored and mbarbeau committed May 11, 2020
1 parent 30d5573 commit b652565
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions packages/core/src/lib/network/network-ionic.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, EventEmitter, OnDestroy, Injector } from '@angular/core';
import { Injectable, EventEmitter, OnDestroy, Injector, ViewChild } from '@angular/core';
import { Observable, Subscription, fromEvent } from 'rxjs';
import { debounceTime, startWith } from 'rxjs/operators';

Expand All @@ -21,6 +21,8 @@ export class NetworkIonicService implements OnDestroy {
connection: window.navigator.onLine
};

private previousState: boolean = !window.navigator.onLine;

constructor(
private messageService: MessageService,
private injector: Injector,
Expand Down Expand Up @@ -61,30 +63,36 @@ export class NetworkIonicService implements OnDestroy {
private checkNetworkStateMobile() {
this.offlineSubscription = this.network.onDisconnect().subscribe(() => {
this.state.connection = false;
setTimeout(() => {
if (!this.state.connection) {
const translate = this.injector.get(LanguageService).translate;
const message = translate.instant('igo.core.network.offline.message');
const title = translate.instant('igo.core.network.offline.title');
this.messageService.info(message, title);
this.state.connection = false;
this.emitEvent();
}
}, 10000);
if (this.previousState !== this.state.connection) {
setTimeout(() => {
if (!this.state.connection) {
const translate = this.injector.get(LanguageService).translate;
const message = translate.instant('igo.core.network.offline.message');
const title = translate.instant('igo.core.network.offline.title');
this.messageService.info(message, title);
this.state.connection = false;
this.emitEvent();
this.previousState = this.state.connection;
}
}, 10000);
}
});

this.onlineSubscription = this.network.onConnect().subscribe(() => {
this.state.connection = true;
setTimeout(() => {
if (this.state.connection) {
const translate = this.injector.get(LanguageService).translate;
const message = translate.instant('igo.core.network.online.message');
const title = translate.instant('igo.core.network.online.title');
this.messageService.info(message, title);
this.state.connection = true;
this.emitEvent();
}
}, 10000);
if (this.previousState !== this.state.connection) {
setTimeout(() => {
if (this.state.connection) {
const translate = this.injector.get(LanguageService).translate;
const message = translate.instant('igo.core.network.online.message');
const title = translate.instant('igo.core.network.online.title');
this.messageService.info(message, title);
this.state.connection = true;
this.emitEvent();
this.previousState = this.state.connection;
}
}, 10000);
}
});
}

Expand Down

0 comments on commit b652565

Please sign in to comment.