Skip to content

Instantly share code, notes, and snippets.

@vaibhavgehani
Last active March 15, 2022 17:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaibhavgehani/27a7c49a6602490deddfc1fa2ccb1639 to your computer and use it in GitHub Desktop.
Save vaibhavgehani/27a7c49a6602490deddfc1fa2ccb1639 to your computer and use it in GitHub Desktop.
import { Network } from '@ionic-native/network/ngx';
import { AlertController, Platform } from '@ionic/angular';
export class InitUserProvider {
disconnectSubscription: any;
connectSubscription: any;
networkStatus = true;
constructor(private network: Network, private platform: Platform) {
this.platform.ready().then(() => {
this.checkInternetConnection();
});
}
checkInternetConnection() {
this.disconnectSubscription = this.network.onDisconnect().subscribe(async () => {
this.networkStatus = false;
});
this.connectSubscription = this.network.onConnect().subscribe(() => {
this.networkStatus = true;
});
}
async load() {
if (this.networkStatus) {
// We have the Internet, so we can fetch the users data from the server
const onlineUser = await this.api.getUserFromServer(this.userid);
this.api.setLoggedInUser(onlineUser);
} else {
const localUser = await this.data.getStoredUserData();
this.api.setLoggedInUser(localUser);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment