Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(network): new ionic network service #490

Merged
merged 11 commits into from
Oct 31, 2019
2 changes: 2 additions & 0 deletions packages/core/src/lib/network/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from './network.service';
export * from './network-ionic.service';
export * from './network.interfaces';
111 changes: 111 additions & 0 deletions packages/core/src/lib/network/network-ionic.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
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';
import { ConnectionState } from './network.interfaces';

@Injectable({
providedIn: 'root'
})
export class NetworkIonicService implements OnDestroy {
private stateChangeEventEmitter = new EventEmitter<ConnectionState>();
private onlineSubscription: Subscription;
private offlineSubscription: Subscription;

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

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

private checkNetworkState() {
this.onlineSubscription = fromEvent(window, 'online').subscribe(() => {
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.offlineSubscription = fromEvent(window, 'offline').subscribe(() => {
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();
});
}

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

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

ngOnDestroy(): void {
try {
this.offlineSubscription.unsubscribe();
this.onlineSubscription.unsubscribe();
} catch (e) {}
}

currentState(reportState = true): Observable<ConnectionState> {
return reportState
? this.stateChangeEventEmitter.pipe(
debounceTime(300),
startWith(this.state)
)
: this.stateChangeEventEmitter.pipe(debounceTime(300));
}
}
3 changes: 3 additions & 0 deletions packages/core/src/lib/network/network.interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface ConnectionState {
connection: boolean;
}
57 changes: 2 additions & 55 deletions packages/core/src/lib/network/network.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +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';

export interface ConnectionState {
connection: boolean;
}
import { ConnectionState } from './network.interfaces';

@Injectable({
providedIn: 'root'
Expand All @@ -27,20 +21,8 @@ export class NetworkService implements OnDestroy {
constructor(
private messageService: MessageService,
private injector: Injector
// private network: Network,
// private platform: Platform
) {
this.checkNetworkState();
// this.platform.ready().then(() => {
// if (this.platform.is('cordova')) {
// if (this.platform.is('android')) {
// this.checkNetworkStateMobile();
// }
// } else {
// console.log('browser');
// this.checkNetworkState();
// }
// });
this.checkNetworkState();
}

private checkNetworkState() {
Expand All @@ -63,41 +45,6 @@ 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