Add network verification
The app doesn't try to get or update the data if the phone is not online.
This commit is contained in:
5
mobile/package-lock.json
generated
5
mobile/package-lock.json
generated
@ -274,6 +274,11 @@
|
|||||||
"resolved": "https://registry.npmjs.org/cordova-plugin-device/-/cordova-plugin-device-2.0.3.tgz",
|
"resolved": "https://registry.npmjs.org/cordova-plugin-device/-/cordova-plugin-device-2.0.3.tgz",
|
||||||
"integrity": "sha512-Jb3V72btxf3XHpkPQsGdyc8N6tVBYn1vsxSFj43fIz9vonJDUThYPCJJHqk6PX6N4dJw6I4FjxkpfCR4LDYMlw=="
|
"integrity": "sha512-Jb3V72btxf3XHpkPQsGdyc8N6tVBYn1vsxSFj43fIz9vonJDUThYPCJJHqk6PX6N4dJw6I4FjxkpfCR4LDYMlw=="
|
||||||
},
|
},
|
||||||
|
"cordova-plugin-network-information": {
|
||||||
|
"version": "2.0.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/cordova-plugin-network-information/-/cordova-plugin-network-information-2.0.2.tgz",
|
||||||
|
"integrity": "sha512-NwO3qDBNL/vJxUxBTPNOA1HvkDf9eTeGH8JSZiwy1jq2W2mJKQEDBwqWkaEQS19Yd/MQTiw0cykxg5D7u4J6cQ=="
|
||||||
|
},
|
||||||
"cordova-plugin-whitelist": {
|
"cordova-plugin-whitelist": {
|
||||||
"version": "1.3.4",
|
"version": "1.3.4",
|
||||||
"resolved": "https://registry.npmjs.org/cordova-plugin-whitelist/-/cordova-plugin-whitelist-1.3.4.tgz",
|
"resolved": "https://registry.npmjs.org/cordova-plugin-whitelist/-/cordova-plugin-whitelist-1.3.4.tgz",
|
||||||
|
@ -16,7 +16,8 @@
|
|||||||
"cordova-android": "^8.1.0",
|
"cordova-android": "^8.1.0",
|
||||||
"cordova-browser": "^6.0.0",
|
"cordova-browser": "^6.0.0",
|
||||||
"cordova-ios": "^5.1.1",
|
"cordova-ios": "^5.1.1",
|
||||||
"cordova-plugin-device": "^2.0.3"
|
"cordova-plugin-device": "^2.0.3",
|
||||||
|
"cordova-plugin-network-information": "^2.0.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"cordova-plugin-whitelist": "^1.3.4"
|
"cordova-plugin-whitelist": "^1.3.4"
|
||||||
@ -24,7 +25,8 @@
|
|||||||
"cordova": {
|
"cordova": {
|
||||||
"plugins": {
|
"plugins": {
|
||||||
"cordova-plugin-whitelist": {},
|
"cordova-plugin-whitelist": {},
|
||||||
"cordova-plugin-device": {}
|
"cordova-plugin-device": {},
|
||||||
|
"cordova-plugin-network-information": {}
|
||||||
},
|
},
|
||||||
"platforms": [
|
"platforms": [
|
||||||
"android",
|
"android",
|
||||||
|
@ -104,13 +104,13 @@
|
|||||||
|
|
||||||
<!-- Your custom app scripts -->
|
<!-- Your custom app scripts -->
|
||||||
<script src="js/app.js"></script>
|
<script src="js/app.js"></script>
|
||||||
|
<script src="js/index.js"></script>
|
||||||
<script src="js/database.js"></script>
|
<script src="js/database.js"></script>
|
||||||
<script src="js/graph.js"></script>
|
<script src="js/graph.js"></script>
|
||||||
<script src="js/feclass.js"></script>
|
<script src="js/feclass.js"></script>
|
||||||
<script src="js/stats.js"></script>
|
<script src="js/stats.js"></script>
|
||||||
<script src="js/feclass-stats.js"></script>
|
<script src="js/feclass-stats.js"></script>
|
||||||
<script src="js/comparator.js"></script>
|
<script src="js/comparator.js"></script>
|
||||||
<script src="js/index.js"></script>
|
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -6,6 +6,17 @@ Description : Communicates with the server to ensure that the data is up to date
|
|||||||
const BASE_URL = "http://localhost/";
|
const BASE_URL = "http://localhost/";
|
||||||
let feData = JSON.parse(localStorage.getItem("feData"));
|
let feData = JSON.parse(localStorage.getItem("feData"));
|
||||||
|
|
||||||
|
function isOnline() {
|
||||||
|
const networkState = (navigator.connection) ? navigator.connection.type : null;
|
||||||
|
|
||||||
|
if (networkState != null && networkState != "none") {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the data from the server or update the data in the localstorage if needed
|
||||||
|
function getOrUpdateData() {
|
||||||
if (feData != null) {
|
if (feData != null) {
|
||||||
let body = new FormData();
|
let body = new FormData();
|
||||||
body.append('version', feData.version);
|
body.append('version', feData.version);
|
||||||
@ -44,7 +55,9 @@ if (feData != null) {
|
|||||||
console.log(err);
|
console.log(err);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the data in the localstorage
|
||||||
function updateData(data) {
|
function updateData(data) {
|
||||||
// If the data received from the server are not empty then we can save them in the local storage
|
// If the data received from the server are not empty then we can save them in the local storage
|
||||||
if (data != null && Object.keys(data).length > 0) {
|
if (data != null && Object.keys(data).length > 0) {
|
||||||
@ -55,3 +68,18 @@ function updateData(data) {
|
|||||||
// End of the update process, we can now display the data
|
// End of the update process, we can now display the data
|
||||||
displayCharacters();
|
displayCharacters();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Doesn't try to get or update the data if the user is not connected to the internet
|
||||||
|
if (isOnline()) {
|
||||||
|
getOrUpdateData();
|
||||||
|
} else {
|
||||||
|
// If the user is not connected to internet but the data are already present in the app, then we display them
|
||||||
|
if (feData != null) {
|
||||||
|
displayCharacters();
|
||||||
|
} else {
|
||||||
|
alert("Please connect to the internet to download the data");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create an event to get/update the data when the user goes online
|
||||||
|
document.addEventListener("online", getOrUpdateData, false);
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user