Skip to content

Commit

Permalink
feat(network): now works with cordova (#393)
Browse files Browse the repository at this point in the history
  • Loading branch information
drekss authored and mbarbeau committed Sep 23, 2019
1 parent 860e287 commit 30d07dd
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@
"@angular/platform-browser": "^7.2.6",
"@angular/platform-browser-dynamic": "^7.2.6",
"@angular/router": "^7.2.6",
"@ionic-native/core": "^5.10.0",
"@ionic-native/network": "^5.12.0",
"@ionic/angular": "^4.6.2",
"@mat-datetimepicker/core": "^3.0.0-beta.0",
"@mdi/angular-material": "^3.6.95",
"@ngx-translate/core": "^10.0.1",
Expand Down
57 changes: 54 additions & 3 deletions packages/core/src/lib/network/network.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { Injectable, EventEmitter, OnDestroy, Injector } from '@angular/core';
import { Observable, Subscription, fromEvent } from 'rxjs';
import { debounceTime, startWith } from 'rxjs/operators';

import { Network } from '@ionic-native/network/ngx';
import { Platform } from '@ionic/angular';

import { MessageService } from '../message/shared/message.service';
import { LanguageService } from '../language/shared/language.service';

Expand All @@ -17,17 +20,30 @@ export class NetworkService implements OnDestroy {
private stateChangeEventEmitter = new EventEmitter<ConnectionState>();
private onlineSubscription: Subscription;
private offlineSubscription: Subscription;
private connectionType: string;

private state: ConnectionState = {
connection: window.navigator.onLine
};

constructor(
private messageService: MessageService,
private injector: Injector
private injector: Injector,
private network: Network,
private platform: Platform
) {
this.checkNetworkState();
}
this.checkNetworkState();
this.platform.ready().then(() => {
if (this.platform.is('cordova')) {
if (this.platform.is('android')) {
this.checkNetworkStateMobile();
}
} else {
console.log('browser');
this.checkNetworkState();
}
});
}

private checkNetworkState() {
this.onlineSubscription = fromEvent(window, 'online').subscribe(() => {
Expand All @@ -49,6 +65,41 @@ export class NetworkService implements OnDestroy {
});
}

private checkNetworkStateMobile() {
if (this.network.type !== this.network.Connection.NONE) {
this.connectionType = this.network.type;
this.state.connection = true;
}

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);
});

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);
});
}

private emitEvent() {
this.stateChangeEventEmitter.emit(this.state);
}
Expand Down

0 comments on commit 30d07dd

Please sign in to comment.