Fix some issues with NativeStorage -> LocalStorage recovery

Skylar Ittner 2 years ago
parent a977851cff
commit 6e6a155214

@ -95,24 +95,6 @@ router.on("routeChange", function (newRoute) {
console.log("Info", "Navigating to ", newRoute.path); console.log("Info", "Navigating to ", newRoute.path);
}); });
try {
// Detect if localStorage is gone and try to restore it from NativeStorage plugin
if (getStorage("syncstateversion") == null || getStorage("syncstateversion") == 0) {
console.log("LocalStorage syncstateversion is null or zero, restoring from NativeStorage");
// "restore" localStorage
copyNativeStorageToLocalStorage();
// give it some arbitrary amount of time because I'm too lazy to do real async
setTimeout(function () {
loadSettings();
}, 60 * 1000);
} else {
// "back up" localStorage
copyLocalStorageToNativeStorage();
}
} catch (ex) {
// Well we tried
}
// Set alert radius to 100 meters by default // Set alert radius to 100 meters by default
if (getStorage("alertradius") == null) { if (getStorage("alertradius") == null) {
setStorage("alertradius", 100); setStorage("alertradius", 100);

@ -160,6 +160,25 @@ function initCordova() {
document.addEventListener("backbutton", handleBackButton, false); document.addEventListener("backbutton", handleBackButton, false);
document.addEventListener("deviceready", function () { document.addEventListener("deviceready", function () {
try {
// Detect if localStorage is gone and try to restore it from NativeStorage plugin
if (getStorage("syncstateversion") == null || getStorage("syncstateversion") == 0) {
console.log("LocalStorage syncstateversion is null or zero, restoring from NativeStorage");
// "restore" localStorage
copyNativeStorageToLocalStorage();
// give it some arbitrary amount of time because I'm too lazy to do real async
setTimeout(function () {
loadSettings();
}, 60 * 1000);
} else {
// "back up" localStorage
copyLocalStorageToNativeStorage();
}
} catch (ex) {
// Well we tried
console.error(ex);
}
loadSettings(); loadSettings();
// Make sure the status bar color is set properly // Make sure the status bar color is set properly

@ -82,10 +82,12 @@ function copyLocalStorageToNativeStorage() {
function copyNativeStorageToLocalStorage() { function copyNativeStorageToLocalStorage() {
NativeStorage.keys(function (keys) { NativeStorage.keys(function (keys) {
for (var key in keys) { for (var i = 0; i < keys.length; i++) {
NativeStorage.getItem(key, function (val) { (function (key) {
localStorage.setItem(key, val); NativeStorage.getItem(key, function (val) {
}); localStorage.setItem(key, val);
});
})(keys[i]);
} }
}); });
} }
Loading…
Cancel
Save