From 3956e77b3693939ff43255a68b7619d9552cc77b Mon Sep 17 00:00:00 2001 From: Emil Junker <19289477+EmilJunker@users.noreply.github.com> Date: Sun, 6 Mar 2022 17:06:33 +0100 Subject: [PATCH] Add ability to select backup file for import --- config.xml | 1 + package.json | 4 +- www/activities/settings/js/settings.js | 74 ++++++++++++++------------ 3 files changed, 44 insertions(+), 35 deletions(-) diff --git a/config.xml b/config.xml index f3386a0b8..bacba2260 100644 --- a/config.xml +++ b/config.xml @@ -28,6 +28,7 @@ + diff --git a/package.json b/package.json index 5c4d95b43..b105a1297 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "cordova-browser": "^6.0.0", "cordova-plugin-browsersync-gen2": "^1.1.7", "cordova-plugin-camera": "^6.0.0", + "cordova-plugin-chooser": "^1.3.2", "cordova-plugin-device": "^2.0.3", "cordova-plugin-file": "^6.0.2", "cordova-plugin-inappbrowser": "^5.0.0", @@ -30,7 +31,8 @@ "cordova-plugin-file": {}, "cordova-plugin-device": {}, "cordova-plugin-inappbrowser": {}, - "cordova-plugin-ionic-keyboard": {} + "cordova-plugin-ionic-keyboard": {}, + "cordova-plugin-chooser": {} }, "platforms": [ "browser", diff --git a/www/activities/settings/js/settings.js b/www/activities/settings/js/settings.js index ff773f59f..aaa07c8cb 100644 --- a/www/activities/settings/js/settings.js +++ b/www/activities/settings/js/settings.js @@ -317,42 +317,48 @@ app.Settings = { } }, - importDatabase: function() { - let title = app.strings.settings.integration.import || "Import"; - let text = app.strings.settings.integration["confirm-import"] || "Are you sure? This will overwrite your current database."; - - let div = document.createElement("div"); - div.className = "dialog-text"; - div.innerText = text; - - let dialog = app.f7.dialog.create({ - title: title, - content: div.outerHTML, - buttons: [{ - text: app.strings.dialogs.cancel || "Cancel", - keyCodes: [27] - }, - { - text: app.strings.dialogs.ok || "OK", - keyCodes: [13], - onClick: async () => { - let filename = "waistline_export.json"; - let json = await app.Utils.readFile(filename); - - if (json !== undefined) { - let data = JSON.parse(json); - await dbHandler.import(data); - - if (data.settings !== undefined) { - let settings = app.Settings.migrateSettings(data.settings, false); - window.localStorage.setItem("settings", JSON.stringify(settings)); - this.changeTheme(settings.appearance["dark-mode"], settings.appearance["theme"]); + importDatabase: async function() { + let file = await chooser.getFile("application/json"); + + if (file !== undefined && file.data !== undefined) { + let data; + try { + let content = new TextDecoder("utf-8").decode(file.data); + data = JSON.parse(content); + } catch (e) { } + + if (data !== undefined) { + let title = app.strings.settings.integration.import || "Import"; + let text = app.strings.settings.integration["confirm-import"] || "Are you sure? This will overwrite your current database."; + + let div = document.createElement("div"); + div.className = "dialog-text"; + div.innerText = text; + + let dialog = app.f7.dialog.create({ + title: title, + content: div.outerHTML, + buttons: [{ + text: app.strings.dialogs.cancel || "Cancel", + keyCodes: [27] + }, + { + text: app.strings.dialogs.ok || "OK", + keyCodes: [13], + onClick: async () => { + await dbHandler.import(data); + + if (data.settings !== undefined) { + let settings = app.Settings.migrateSettings(data.settings, false); + window.localStorage.setItem("settings", JSON.stringify(settings)); + this.changeTheme(settings.appearance["dark-mode"], settings.appearance["theme"]); + } } } - } - } - ] - }).open(); + ] + }).open(); + } + } }, firstTimeSetup: function() {