Last active
March 15, 2022 17:07
-
-
Save vaibhavgehani/27a7c49a6602490deddfc1fa2ccb1639 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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