I am trying to create a Provider that will look for network connection and I will inject it in all the views I need.
Came across this nice example on how to go about it. This particular example shows how to do it using views and I am trying to do it using a Provider class.
My problem is that I am not able to figure out how to return the Observable from this Provider. Sure it is my lack of knowledge about Observable/rxjs and few other things. Still learning new things in Ionic, Angular, TypeScript, rxJS, etc. every other day.
I need to implement it quickly so would highly appreciate if somebody can please help me on how I should be implementing it or what I need to to do so that I can return an Observable from my Provider that I can subscribe to in all the views where I will be using it.
Following is my Provider code:
network-service.ts
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Network } from '@ionic-native/network';
@Injectable()
export class NetworkService {
private appOnline: boolean = true;
constructor(private network: Network) {
console.log('In NetworkService->constructor().');
}
isAppOnline(): boolean {
return this.appOnline;
}
onConnect(): Observable<any> {
this.network.onConnect().subscribe(data => {
console.log(data);
this.appOnline = true;
return Observable.create(observer => {
observer.next(data);
observer.complete();
});
});
//return this.network.onConnect(); <= If I just have this, it works but then I cannot set the 'appOnline' variable.
}
onDisconnect(): Observable<any> {
this.network.onDisconnect().subscribe(data => {
console.log(data);
this.appOnline = false;
return Observable.create(observer => {
observer.next(data);
observer.complete();
});
});
//return this.network.onDisconnect(); <= If I just have this, it works but then I cannot set the 'appOnline' variable.
}
}
When I do this, I get the following error on Observable<any>
return type of onConnect()
and onDisconnect()
methods:
A function whose declared type is neither ‘void’ nor ‘any’ must return a value.
Following is my view class that uses the above NetworkService:
home-page.ts
// Here I imported the NetworkService provider and Subscription from rxJS along with other things I needed
@Component({
selector: 'page-home',
templateUrl: 'home.page.html'
})
export class HomePage {
...
...
connected: Subscription;
disconnected: Subscription;
...
...
constructor(public navCtrl: NavController, public navParams: NavParams,
private homeService: HomePageService, private platform: Platform,
private netService: NetworkService) {
console.log('In HomePage->constructor');
}
...
...
...
ionViewDidEnter() {
this.connected = this.netService.onConnect().subscribe(
(data) => {
// DO SOMETHING WHEN THE CONNECTION EXIST
},
(error) => {
console.error('error is: ' + JSON.stringify(error));
}
);
this.disconnected = this.netService.onDisconnect().subscribe(
(data) => {
// DO SOMETHING WHEN THE CONNECTION IS LOST
},
(error) => {
console.error('error is: ' + JSON.stringify(error));
}
);
}
ionViewWillLeave(){
this.connected.unsubscribe();
this.disconnected.unsubscribe();
}
}
Any help will be highly appreciated.
Thanks!