enrecipes/app/components/Settings/Database.vue

460 lines
14 KiB
Vue
Raw Normal View History

2021-03-21 17:02:04 +00:00
<template>
2021-04-01 10:55:35 +00:00
<Page @loaded="onPageLoad" actionBarHidden="true">
<GridLayout rows="*, auto" columns="auto, *">
<ListView
colSpan="2"
rowSpan="2"
class="options-list"
for="item in items"
>
<v-template if="$index == 0">
<Label class="pageTitle" :text="'db' | L" />
</v-template>
2021-04-07 17:18:38 +00:00
<v-template if="$index == 4">
2021-04-01 10:55:35 +00:00
<StackLayout class="listSpace"> </StackLayout>
</v-template>
<v-template>
<GridLayout
columns="auto, *"
class="option"
@touch="touch($event, item.action)"
>
<Label class="ico" :text="icon[item.icon]" />
2021-04-07 17:18:38 +00:00
<StackLayout col="1" verticalAlignment="center">
2021-04-01 10:55:35 +00:00
<Label :text="item.title | L" class="info" />
2021-04-07 17:18:38 +00:00
<Label v-if="item.subTitle" :text="item.subTitle" class="sub" />
2021-04-01 10:55:35 +00:00
</StackLayout>
</GridLayout>
</v-template>
</ListView>
2021-04-07 17:18:38 +00:00
<GridLayout
2021-04-12 18:09:48 +00:00
v-show="!toast && !progress"
2021-04-07 17:18:38 +00:00
row="1"
class="appbar"
rows="*"
columns="auto, *"
>
<Button class="ico" :text="icon.back" @tap="$navigateBack()" />
</GridLayout>
<GridLayout
v-show="toast"
row="1"
colSpan="2"
class="appbar snackBar"
columns="*"
@tap="toast = null"
>
<FlexboxLayout minHeight="48" alignItems="center">
<Label class="title msg" :text="toast" />
</FlexboxLayout>
</GridLayout>
<GridLayout
2021-04-12 18:09:48 +00:00
v-show="progress"
2021-04-07 17:18:38 +00:00
row="1"
colSpan="2"
class="appbar snackBar"
2021-04-12 18:09:48 +00:00
columns="auto, *"
2021-04-07 17:18:38 +00:00
>
2021-04-12 18:09:48 +00:00
<ActivityIndicator :busy="progress ? true : false" />
<Label col="1" class="title" :text="progress" textWrap="true" />
2021-03-21 17:02:04 +00:00
</GridLayout>
2021-04-01 10:55:35 +00:00
</GridLayout>
2021-03-21 17:02:04 +00:00
</Page>
</template>
<script>
import {
ApplicationSettings,
path,
knownFolders,
File,
Folder,
Observable,
2021-04-07 17:18:38 +00:00
Application,
2021-03-21 17:02:04 +00:00
} from "@nativescript/core";
import { localize } from "@nativescript/localize";
import ConfirmDialog from "../modal/ConfirmDialog.vue";
import { mapState, mapActions } from "vuex";
import * as utils from "~/shared/utils";
export default {
data() {
return {
2021-04-07 17:18:38 +00:00
backupFolder: null,
2021-04-12 18:09:48 +00:00
progress: null,
2021-04-07 17:18:38 +00:00
toast: null,
2021-03-21 17:02:04 +00:00
};
},
computed: {
...mapState([
"icon",
"recipes",
"cuisines",
"categories",
"yieldUnits",
"units",
"mealPlans",
"importSummary",
]),
2021-04-01 10:55:35 +00:00
items() {
return [
{},
2021-04-07 17:18:38 +00:00
{
icon: "folder",
title: "Backup folder",
subTitle: this.backupFolder,
action: this.setBackupFolder,
},
2021-04-01 10:55:35 +00:00
{
icon: "exp",
title: "expBu",
2021-04-07 17:18:38 +00:00
subTitle: localize("buInfo"),
2021-04-01 10:55:35 +00:00
action: this.exportCheck,
},
{
icon: "imp",
title: "impBu",
2021-04-07 17:18:38 +00:00
subTitle: localize("impInfo"),
action: this.openZipFile,
2021-04-01 10:55:35 +00:00
},
{},
];
},
2021-03-21 17:02:04 +00:00
},
methods: {
...mapActions([
"importListItemsAction",
"importRecipesAction",
"importMealPlansAction",
"unlinkBrokenImages",
2021-04-12 18:09:48 +00:00
"clearImportSummary",
2021-03-21 17:02:04 +00:00
]),
onPageLoad(args) {
const page = args.object;
page.bindingContext = new Observable();
2021-04-12 18:09:48 +00:00
const ContentResolver = Application.android.nativeApp.getContentResolver();
this.backupFolder = ApplicationSettings.getString("backupFolder");
if (
!this.backupFolder ||
ContentResolver.getPersistedUriPermissions().isEmpty()
) {
this.backupFolder = null;
}
2021-04-07 17:18:38 +00:00
},
// BACKUP FOLDER PICKER
2021-04-12 18:09:48 +00:00
setBackupFolder(startExport) {
const ContentResolver = Application.android.nativeApp.getContentResolver();
const FLAGS =
android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION |
android.content.Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
utils.getBackupFolder().then((uri) => {
if (uri != null) {
if (this.backupFolder)
ContentResolver.releasePersistableUriPermission(
new android.net.Uri.parse(this.backupFolder),
FLAGS
);
this.backupFolder = uri.toString();
2021-04-07 17:18:38 +00:00
ApplicationSettings.setString("backupFolder", this.backupFolder);
2021-04-12 18:09:48 +00:00
// PERSIST PERMISSIONS
ContentResolver.takePersistableUriPermission(uri, FLAGS);
if (startExport && this.backupFolder) {
this.exportBackup();
}
2021-04-07 17:18:38 +00:00
}
});
2021-03-21 17:02:04 +00:00
},
2021-04-12 18:09:48 +00:00
2021-03-21 17:02:04 +00:00
// EXPORT HANDLERS
exportCheck() {
2021-04-12 18:09:48 +00:00
const ContentResolver = Application.android.nativeApp.getContentResolver();
2021-03-21 17:02:04 +00:00
if (!this.recipes.length) {
2021-04-07 17:18:38 +00:00
this.toast = localize("aFBu");
utils.timer(5, (val) => {
if (!val) this.toast = val;
});
2021-03-21 17:02:04 +00:00
} else {
2021-04-12 18:09:48 +00:00
if (
!this.backupFolder ||
ContentResolver.getPersistedUriPermissions().isEmpty()
) {
this.setBackupFolder(true);
} else this.exportBackup();
2021-03-21 17:02:04 +00:00
}
},
exportBackup() {
2021-04-12 18:09:48 +00:00
this.progress = localize("expip");
2021-03-21 17:02:04 +00:00
this.exportFiles("create");
let date = new Date();
let formattedDate =
date.getFullYear() +
"-" +
("0" + (date.getMonth() + 1)).slice(-2) +
"-" +
("0" + date.getDate()).slice(-2) +
"_" +
("0" + date.getHours()).slice(-2) +
("0" + date.getMinutes()).slice(-2) +
("0" + date.getSeconds()).slice(-2);
2021-04-07 17:18:38 +00:00
let filename = `EnRecipes_${formattedDate}.zip`;
2021-03-21 17:02:04 +00:00
let fromPath = path.join(knownFolders.documents().path, "EnRecipes");
2021-04-12 18:09:48 +00:00
utils.Zip.zip(fromPath, this.backupFolder, filename)
.then((res) => {
if (res) this.showExportSummary(filename);
})
.catch((err) => console.log("Backup error: ", err));
2021-03-21 17:02:04 +00:00
},
exportFiles(option) {
const folder = path.join(knownFolders.documents().path, "EnRecipes");
const EnRecipesFile = File.fromPath(path.join(folder, "recipes.json"));
let userCuisinesFile,
userCategoriesFile,
userYieldUnitsFile,
userUnitsFile,
mealPlansFile;
if (this.cuisines.length)
userCuisinesFile = File.fromPath(
path.join(folder, "userCuisines.json")
);
if (this.categories.length)
userCategoriesFile = File.fromPath(
path.join(folder, "userCategories.json")
);
if (this.yieldUnits.length)
userYieldUnitsFile = File.fromPath(
path.join(folder, "userYieldUnits.json")
);
if (this.units.length)
userUnitsFile = File.fromPath(path.join(folder, "userUnits.json"));
if (this.mealPlans.length)
mealPlansFile = File.fromPath(path.join(folder, "mealPlans.json"));
switch (option) {
case "create":
this.writeDataToFile(EnRecipesFile, this.recipes);
this.cuisines.length &&
this.writeDataToFile(userCuisinesFile, this.cuisines);
this.categories.length &&
this.writeDataToFile(userCategoriesFile, this.categories);
this.yieldUnits.length &&
this.writeDataToFile(userYieldUnitsFile, this.yieldUnits);
this.units.length && this.writeDataToFile(userUnitsFile, this.units);
this.mealPlans.length &&
this.writeDataToFile(mealPlansFile, this.mealPlans);
break;
case "delete":
EnRecipesFile.remove();
this.cuisines.length && userCuisinesFile.remove();
this.categories.length && userCategoriesFile.remove();
this.yieldUnits.length && userYieldUnitsFile.remove();
this.units.length && userUnitsFile.remove();
this.mealPlans.length && mealPlansFile.remove();
break;
}
},
writeDataToFile(file, data) {
file.writeText(JSON.stringify(data));
},
2021-04-12 18:09:48 +00:00
showExportSummary(filename) {
this.progress = null;
this.$showModal(ConfirmDialog, {
props: {
title: "expSuc",
description: `Backed up to ${filename}`,
okButtonText: "OK",
},
});
2021-03-21 17:02:04 +00:00
},
2021-04-07 17:18:38 +00:00
2021-04-12 18:09:48 +00:00
// IMPORT HANDLERS
openZipFile() {
2021-04-07 17:18:38 +00:00
utils.getBackupFile().then((uri) => {
2021-04-14 09:27:40 +00:00
console.log(uri);
if (uri) {
let dest = path.join(knownFolders.temp().path, "tempUnZip");
utils.Zip.unzip(uri, dest)
.then((res) => {
if (res) this.validateZipContent(res, uri);
})
.catch(() => this.failedImport(localize("buInc")));
}
2021-04-07 17:18:38 +00:00
});
2021-03-21 17:02:04 +00:00
},
2021-04-12 18:09:48 +00:00
validateZipContent(extractedFolderPath, uri) {
this.progress = localize("impip");
let cacheFolderPath = extractedFolderPath + "/EnRecipes";
const EnRecipesFilePath = cacheFolderPath + "/recipes.json";
const ImagesFolderPath = cacheFolderPath + "/Images";
const userCuisinesFilePath = cacheFolderPath + "/userCuisines.json";
const userCategoriesFilePath = cacheFolderPath + "/userCategories.json";
const userYieldUnitsFilePath = cacheFolderPath + "/userYieldUnits.json";
const userUnitsFilePath = cacheFolderPath + "/userUnits.json";
const mealPlansFilePath = cacheFolderPath + "/mealPlans.json";
if (Folder.exists(cacheFolderPath)) {
this.isFileDataValid([
{
path: EnRecipesFilePath,
db: "EnRecipesDB",
file: "recipes.json",
},
{
path: userCuisinesFilePath,
db: "userCuisinesDB",
file: "userCuisines.json",
},
{
path: userCategoriesFilePath,
db: "userCategoriesDB",
file: "userCategories.json",
},
{
path: userYieldUnitsFilePath,
db: "userYieldUnitsDB",
file: "userYieldUnits.json",
},
{
path: userUnitsFilePath,
db: "userUnitsDB",
file: "userUnits.json",
},
{
path: mealPlansFilePath,
db: "mealPlansDB",
file: "mealPlans.json",
},
]);
} else {
Folder.fromPath(extractedFolderPath).remove();
this.progress = null;
this.failedImport(localize("buInc"));
2021-03-21 17:02:04 +00:00
}
2021-04-12 18:09:48 +00:00
if (Folder.exists(ImagesFolderPath)) {
const timer = setInterval(() => {
if (this.importSummary.found) {
this.importImages(uri, extractedFolderPath);
clearInterval(timer);
}
}, 100);
2021-03-21 17:02:04 +00:00
}
},
isFileDataValid(file) {
const files = file.filter((e) => File.exists(e.path));
if (files.length) {
let isValid = files.map((e) => false);
files.forEach((file, i) => {
File.fromPath(file.path)
.readText()
.then((data) => {
isValid[i] = this.hasValidJSON(data);
if (!isValid[i]) {
this.failedImport(
`${localize("buMod")}\n\n${localize("invFile")}: ${file.file}`
);
return 0;
}
if (isValid.every((e) => e === true)) {
2021-04-12 18:09:48 +00:00
files.forEach((file) => {
2021-03-21 17:02:04 +00:00
File.fromPath(file.path)
.readText()
.then((data) => {
2021-04-12 18:09:48 +00:00
this.importDataToDB(JSON.parse(data), file.db);
2021-03-21 17:02:04 +00:00
});
});
}
});
});
} else {
this.failedImport(localize("buEmp"));
}
},
failedImport(description) {
this.$showModal(ConfirmDialog, {
props: {
title: "impFail",
description,
okButtonText: "OK",
},
});
},
2021-04-12 18:09:48 +00:00
hasValidJSON(data) {
try {
JSON.parse(data) && Array.isArray(JSON.parse(data));
} catch (e) {
return false;
}
return true;
},
importDataToDB(data, db) {
switch (db) {
case "EnRecipesDB":
this.importRecipesAction(data);
break;
case "userCuisinesDB":
this.importListItemsAction({
data,
listName: "cuisines",
});
break;
case "userCategoriesDB":
this.importListItemsAction({
data,
listName: "categories",
});
break;
case "userYieldUnitsDB":
this.importListItemsAction({
data,
listName: "yieldUnits",
});
break;
case "userUnitsDB":
this.importListItemsAction({
data,
listName: "units",
});
break;
case "mealPlansDB":
this.importMealPlansAction(data);
break;
}
},
importImages(uri, extractedFolderPath) {
let destPath = knownFolders.documents().path;
Folder.fromPath(destPath);
utils.Zip.unzip(uri, destPath).then((res) => {
if (res) {
this.showImportSummary();
this.unlinkBrokenImages();
this.exportFiles("delete");
2021-03-21 17:02:04 +00:00
Folder.fromPath(extractedFolderPath).remove();
}
});
},
showImportSummary() {
let { found, imported, updated } = this.importSummary;
let exists = Math.abs(found - imported - updated) + updated;
let importedNote = `\n${imported} ${localize("recI")}`;
let existsNote = `\n${exists} ${localize("recE")}`;
let updatedNote = `\n${updated} ${localize("recU")}`;
2021-04-12 18:09:48 +00:00
this.progress = null;
2021-03-21 17:02:04 +00:00
this.$showModal(ConfirmDialog, {
props: {
title: "impSuc",
description: `${found} ${localize(
"recF"
)}${importedNote}${existsNote}${updatedNote}`,
okButtonText: "OK",
},
2021-04-12 18:09:48 +00:00
}).then(() => this.clearImportSummary());
2021-03-21 17:02:04 +00:00
},
2021-04-12 18:09:48 +00:00
2021-04-01 10:55:35 +00:00
// HELPERS
touch({ object, action }, method) {
object.className = action.match(/down|move/) ? "option fade" : "option";
if (action == "up") method();
},
2021-03-21 17:02:04 +00:00
},
};
</script>