I am showing network status offline using snackbar:
showSnackbar(message: string, duration?: number, dismissTitle?: string): void {
this.snackBarRef = this.snackBar.open(message, dismissTitle, {
duration: duration
});
}
hideSnackbar(): void {
this.snackBarRef.dismiss();
}
handleNetwork() {
fromEvent(window, 'offline').subscribe(event => {
this.showSnackbar('No Internet', null, 'Dismiss');
//After Dismiss Snack-bar
this.snackBarRef.afterDismissed().subscribe(() => {
this.handleNetwork();
});
});
fromEvent(window, 'online').subscribe(event => {
this.hideSnackbar();
});
}
On each snackbar dismiss I would like to show snackbar again until the network status becomes online. I am calling handleNetwork() again after snackbar dismiss but it is not working. If anyone knows how to do it then please let me know. Thanks
CodePudding user response:
It can be done like this:
isOffline = false;
showSnackbar(message: string, duration?: number, dismissTitle?: string): void {
this.snackBarRef = this.snackBar.open(message, dismissTitle, {
duration: duration
});
}
hideSnackbar(): void {
this.snackBarRef.dismiss();
}
handleNetwork() {
fromEvent(window, 'offline').subscribe(event => {
this.isOffline = true;
this.showStatus();
});
fromEvent(window, 'online').subscribe(event => {
this.isOffline = false;
this.hideSnackbar();
});
}
showStatus() {
this.showSnackbar('No Internet', null, 'Reconnect');
this.snackBarRef.afterDismissed().subscribe(() => {
if (this.isOffline === true) {
this.showStatus();
}
});
}