enrecipes/app/components/Settings.vue

223 lines
6.5 KiB
Vue
Raw Normal View History

2020-09-15 11:10:16 +00:00
<template>
2020-10-22 18:36:50 +00:00
<Page @loaded="initializePage">
2020-10-14 19:32:32 +00:00
<ActionBar :flat="viewIsScrolled ? false : true">
2020-11-02 11:36:53 +00:00
<GridLayout rows="*" columns="auto, *">
2020-10-14 19:32:32 +00:00
<Label
2020-11-02 11:36:53 +00:00
class="bx"
2020-10-14 19:32:32 +00:00
:text="icon.menu"
2020-11-02 11:36:53 +00:00
automationText="Back"
2020-10-14 19:32:32 +00:00
@tap="showDrawer"
col="0"
/>
2020-10-21 17:54:45 +00:00
<Label class="title orkm" text="Settings" col="1" />
2020-10-14 19:32:32 +00:00
</GridLayout>
</ActionBar>
2020-11-02 11:36:53 +00:00
<ScrollView scrollBarIndicatorVisible="false" @scroll="onScroll">
2020-10-14 19:32:32 +00:00
<StackLayout class="main-container">
<Label text="Interface" class="group-header" />
<StackLayout
orientation="horizontal"
class="option"
@tap="selectThemes"
>
<Label verticalAlignment="center" class="bx" :text="icon.theme" />
<StackLayout>
<Label text="Theme" class="option-title" />
<Label :text="themeName" class="option-info" textWrap="true" />
</StackLayout>
</StackLayout>
<StackLayout class="hr m-10"></StackLayout>
<Label text="Backup/Restore" class="group-header" />
<StackLayout orientation="horizontal" class="option" @tap="backupData">
2020-11-02 11:36:53 +00:00
<Label class="bx" :text="icon.save" />
<Label text="Backup data" />
2020-10-14 19:32:32 +00:00
</StackLayout>
<StackLayout orientation="horizontal" class="option" @tap="restoreData">
<Label class="bx" :text="icon.restore" />
2020-11-02 11:36:53 +00:00
<Label text="Restore data" />
2020-10-14 19:32:32 +00:00
</StackLayout>
2020-09-15 11:10:16 +00:00
</StackLayout>
2020-10-14 19:32:32 +00:00
</ScrollView>
</Page>
2020-09-15 11:10:16 +00:00
</template>
<script>
import {
ApplicationSettings,
path,
getFileAccess,
knownFolders,
} from "@nativescript/core"
2020-09-15 11:10:16 +00:00
import * as permissions from "nativescript-permissions"
import { Zip } from "nativescript-zip"
2020-10-26 20:49:54 +00:00
import * as Toast from "nativescript-toast"
import * as filepicker from "nativescript-plugin-filepicker"
2020-10-14 19:32:32 +00:00
import Theme from "@nativescript/theme"
2020-10-21 17:54:45 +00:00
import ActionDialog from "./modal/ActionDialog.vue"
import ConfirmDialog from "./modal/ConfirmDialog.vue"
2020-10-14 19:32:32 +00:00
import { mapState, mapActions } from "vuex"
2020-09-15 11:10:16 +00:00
export default {
2020-10-21 17:54:45 +00:00
props: [
"highlight",
"showDrawer",
"restartApp",
"hijackGlobalBackEvent",
"releaseGlobalBackEvent",
],
2020-09-15 11:10:16 +00:00
data() {
return {
2020-11-02 11:36:53 +00:00
viewIsScrolled: false,
2020-10-14 19:32:32 +00:00
interface: {
theme: {
title: "Theme",
subTitle: "Light",
icon: "\ued09",
},
2020-09-15 11:10:16 +00:00
},
2020-10-14 19:32:32 +00:00
backupRestore: [
{
title: "EnRecipes Backup Directory",
subTitle: "/storage/emulated/0/EnRecipes",
icon: "\ued7c",
},
{
title: "Backup Data",
subTitle: null,
icon: "\uee48",
},
{
title: "Restore Data",
subTitle: null,
icon: "\ueadc",
},
],
2020-09-15 11:10:16 +00:00
themeName: "Light",
2020-10-14 19:32:32 +00:00
themesArray: ["Light", "Dark"],
2020-09-15 11:10:16 +00:00
}
},
2020-10-14 19:32:32 +00:00
computed: {
2020-10-21 17:54:45 +00:00
...mapState(["icon", "currentComponent"]),
2020-10-14 19:32:32 +00:00
},
2020-09-15 11:10:16 +00:00
methods: {
2020-10-22 18:36:50 +00:00
...mapActions(["setCurrentComponentAction"]),
initializePage() {
this.setCurrentComponentAction("Settings")
2020-10-21 17:54:45 +00:00
this.releaseGlobalBackEvent()
},
2020-11-02 11:36:53 +00:00
onScroll(args) {
args.scrollY
? (this.viewIsScrolled = true)
: (this.viewIsScrolled = false)
2020-10-21 17:54:45 +00:00
},
2020-09-15 11:10:16 +00:00
selectThemes(args) {
2020-09-15 18:04:33 +00:00
this.highlight(args)
2020-10-21 17:54:45 +00:00
this.$showModal(ActionDialog, {
props: {
title: "Theme",
list: ["Light", "Dark"],
2020-11-02 11:36:53 +00:00
height: "108",
2020-10-21 17:54:45 +00:00
},
}).then((action) => {
if (action && action !== "Cancel" && this.themeName !== action) {
this.$showModal(ConfirmDialog, {
props: {
title: "App Reload Required",
description:
"The app needs to be reloaded for the theme change to take effect.",
cancelButtonText: "CANCEL",
okButtonText: "RELOAD",
},
}).then((result) => {
if (result) {
this.interface.theme.subTitle = this.themeName = action
2020-10-22 18:36:50 +00:00
ApplicationSettings.setString("application-theme", action)
2020-10-21 17:54:45 +00:00
setTimeout((e) => this.restartApp(), 250)
}
})
}
2020-09-15 11:10:16 +00:00
})
},
selectBackupDir(args) {
2020-09-15 18:04:33 +00:00
this.highlight(args)
2020-09-15 11:10:16 +00:00
let btn = args.object
permissions
.requestPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
.then(() => {
alert("select backup directory")
})
.catch(() => {
console.log("Uh oh, no permissions - plan B time!")
})
},
backupData(args) {
let btn = args.object
2020-09-15 18:04:33 +00:00
this.highlight(args)
2020-09-15 11:10:16 +00:00
permissions
.requestPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
.then(() => {
const sdDownloadPath = android.os.Environment.getExternalStoragePublicDirectory(
android.os.Environment.DIRECTORY_DOWNLOADS
).toString()
2020-10-26 20:49:54 +00:00
let date = new Date()
2020-11-02 11:36:53 +00:00
let formattedDate = `${date.getFullYear()}${date.getMonth()}${date.getDate()}_${date.getHours()}-${date.getMinutes()}-${date.getSeconds()}`
let fromPath = path.join(knownFolders.documents().path, "enrecipes")
2020-10-26 20:49:54 +00:00
let destPath = path.join(
sdDownloadPath,
2020-11-02 11:36:53 +00:00
`EnRecipes-${formattedDate}.zip`
2020-10-26 20:49:54 +00:00
)
console.log(fromPath, destPath, sdDownloadPath)
Zip.zip({
directory: fromPath,
archive: destPath,
})
.then((success) => {
2020-10-26 20:49:54 +00:00
Toast.makeText(
"Backup file successfully saved to Downloads",
"long"
).show()
console.log("success:" + success)
})
.catch((err) => {
console.log(err)
})
// console.log(fromPath, destPath, sdDownloadPath)
// alert("Backup successful!")
2020-09-15 11:10:16 +00:00
})
.catch(() => {
console.log("Uh oh, no permissions - plan B time!")
})
},
restoreData(args) {
let btn = args.object
2020-09-15 18:04:33 +00:00
this.highlight(args)
2020-10-26 20:49:54 +00:00
let vm = this
let context = filepicker.create({
mode: "single", // use "multiple" for multiple selection
extensions: ["zip"],
})
context
.authorize()
.then(function() {
return context.present()
2020-09-15 11:10:16 +00:00
})
2020-10-26 20:49:54 +00:00
.then(function(selection) {
let result = selection
console.log(result)
})
.catch(function(e) {
console.log(e)
2020-09-15 11:10:16 +00:00
})
},
},
2020-10-14 19:32:32 +00:00
created() {
2020-10-22 18:36:50 +00:00
this.interface.theme.subTitle = this.themeName = ApplicationSettings.getString(
2020-10-14 19:32:32 +00:00
"application-theme",
"Light"
)
},
2020-09-15 11:10:16 +00:00
}
</script>