2021-05-22 08:56:31 +00:00
|
|
|
<template>
|
2021-06-15 11:04:42 +00:00
|
|
|
<RGridLayout
|
|
|
|
:rtl="RTL"
|
2021-05-22 08:56:31 +00:00
|
|
|
rows="auto, auto"
|
|
|
|
columns="auto, *, auto, auto, auto"
|
2021-06-15 11:04:42 +00:00
|
|
|
class="timer"
|
2021-05-22 08:56:31 +00:00
|
|
|
>
|
|
|
|
<Button
|
2021-06-18 12:52:03 +00:00
|
|
|
class="ico si rtl"
|
2021-05-22 08:56:31 +00:00
|
|
|
:text="done ? icon.ring : timer.isPaused ? icon.start : icon.pause"
|
|
|
|
@tap="!done && toggleProgress()"
|
|
|
|
/>
|
2021-06-15 11:04:42 +00:00
|
|
|
<StackLayout
|
|
|
|
col="1"
|
|
|
|
class="info"
|
|
|
|
:colSpan="timer.isPaused || !timer.preset ? 1 : 2"
|
|
|
|
>
|
|
|
|
<RLabel :text="timer.label" class="tb title tw a" />
|
|
|
|
<RLabel
|
|
|
|
:hidden="!timer.recipeID && done"
|
2021-06-18 12:52:03 +00:00
|
|
|
@touch="!timer.recipeID && !done && touch($event)"
|
2021-05-22 08:56:31 +00:00
|
|
|
:text="getRecipeTitle"
|
2021-06-15 11:04:42 +00:00
|
|
|
class="a"
|
|
|
|
:class="timer.recipeID ? 'sub' : 'accent'"
|
|
|
|
/>
|
|
|
|
<RLabel
|
|
|
|
:text="
|
|
|
|
progress == 0
|
|
|
|
? countUp
|
|
|
|
? getCount
|
|
|
|
: formattedTime(timer.time)
|
|
|
|
: getCount
|
|
|
|
"
|
2021-05-22 08:56:31 +00:00
|
|
|
/>
|
|
|
|
</StackLayout>
|
|
|
|
<Button
|
|
|
|
col="2"
|
2021-06-15 11:04:42 +00:00
|
|
|
class="ico rtl"
|
|
|
|
:hidden="(!timer.isPaused && timer.preset) || done || countUp"
|
|
|
|
:text="isReset || timer.preset ? icon.reset : icon.addTo"
|
|
|
|
@tap="isReset || timer.preset ? resetTimer() : addPreset()"
|
2021-05-22 08:56:31 +00:00
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
col="3"
|
|
|
|
class="ico"
|
2021-06-15 11:04:42 +00:00
|
|
|
@tap="countUp ? addPreset() : addDelay()"
|
|
|
|
:text="countUp ? icon.addTo : icon.delay"
|
2021-05-22 08:56:31 +00:00
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
col="4"
|
2021-06-18 12:52:03 +00:00
|
|
|
class="ico si"
|
2021-05-22 08:56:31 +00:00
|
|
|
:text="icon.x"
|
2021-06-15 11:04:42 +00:00
|
|
|
@tap="removeTimer(timer.id, done)"
|
2021-05-22 08:56:31 +00:00
|
|
|
/>
|
2021-06-15 11:04:42 +00:00
|
|
|
<Progress @loaded="pLoaded" row="1" colSpan="5" />
|
|
|
|
</RGridLayout>
|
2021-05-22 08:56:31 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2021-06-15 11:04:42 +00:00
|
|
|
import { ApplicationSettings } from "@nativescript/core";
|
2021-05-22 08:56:31 +00:00
|
|
|
import { localize } from "@nativescript/localize";
|
|
|
|
import { mapState, mapActions } from "vuex";
|
2021-05-25 14:32:53 +00:00
|
|
|
import ActionWithSearch from "../modals/ActionWithSearch";
|
2021-06-18 12:52:03 +00:00
|
|
|
import EditRecipe from "../EditRecipe";
|
2021-05-22 08:56:31 +00:00
|
|
|
import * as utils from "~/shared/utils";
|
2021-06-15 11:04:42 +00:00
|
|
|
import { EvtBus } from "~/main";
|
2021-05-22 08:56:31 +00:00
|
|
|
export default {
|
|
|
|
props: [
|
|
|
|
"timer",
|
|
|
|
"formattedTime",
|
|
|
|
"removeTimer",
|
|
|
|
"togglePause",
|
2021-06-15 11:04:42 +00:00
|
|
|
"timerAlert",
|
|
|
|
"showToast",
|
2021-05-22 08:56:31 +00:00
|
|
|
],
|
|
|
|
data() {
|
|
|
|
return {
|
2021-06-15 11:04:42 +00:00
|
|
|
pBar: 0,
|
|
|
|
count: 0,
|
|
|
|
delay: 0,
|
2021-05-22 08:56:31 +00:00
|
|
|
progress: 0,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
computed: {
|
2021-06-15 11:04:42 +00:00
|
|
|
...mapState(["icon", "recipes", "timerDelay", "timerPresets", "RTL"]),
|
2021-05-22 08:56:31 +00:00
|
|
|
getRecipeTitle() {
|
|
|
|
let { recipeID } = this.timer;
|
|
|
|
if (recipeID) {
|
2021-06-15 11:04:42 +00:00
|
|
|
let recipe = this.recipes.filter((e) => e.id == this.timer.recipeID)[0];
|
|
|
|
if (recipe) return recipe.title;
|
|
|
|
else {
|
|
|
|
this.timer.recipeID = null;
|
|
|
|
return localize("fwr");
|
|
|
|
}
|
2021-05-22 08:56:31 +00:00
|
|
|
} else return localize("fwr");
|
|
|
|
},
|
|
|
|
done() {
|
2021-06-15 11:04:42 +00:00
|
|
|
return this.timer.done;
|
|
|
|
},
|
|
|
|
countUp() {
|
|
|
|
return this.timer.mode == 0;
|
|
|
|
},
|
|
|
|
isReset() {
|
|
|
|
return this.timer.isPaused && this.progress != 0;
|
2021-05-22 08:56:31 +00:00
|
|
|
},
|
2021-06-15 11:04:42 +00:00
|
|
|
getTotalTime() {
|
|
|
|
return this.delay + this.actualTime;
|
|
|
|
},
|
|
|
|
actualTime() {
|
2021-05-22 08:56:31 +00:00
|
|
|
let t = this.timer.time.split(":");
|
|
|
|
return +t[0] * 60 * 60 + +t[1] * 60 + +t[2];
|
|
|
|
},
|
2021-06-15 11:04:42 +00:00
|
|
|
getCount() {
|
|
|
|
let c = this.count;
|
|
|
|
let s = Math.abs(c);
|
|
|
|
return (
|
|
|
|
(c < 0 ? "-" : "") +
|
|
|
|
new Date(s * 1000)
|
|
|
|
.toISOString()
|
|
|
|
.slice(
|
|
|
|
s < 10
|
|
|
|
? 18
|
|
|
|
: s < 60
|
|
|
|
? 17
|
|
|
|
: s < 600
|
|
|
|
? 15
|
|
|
|
: s < 3600
|
|
|
|
? 14
|
|
|
|
: s < 36000
|
|
|
|
? 12
|
|
|
|
: s < 86400
|
|
|
|
? 11
|
|
|
|
: 0,
|
|
|
|
19
|
|
|
|
)
|
|
|
|
);
|
|
|
|
},
|
2021-05-22 08:56:31 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2021-06-15 11:04:42 +00:00
|
|
|
...mapActions([
|
|
|
|
"removeActiveTimer",
|
|
|
|
"addTimerPreset",
|
|
|
|
"deleteTimerPreset",
|
|
|
|
"sortActiveTimers",
|
|
|
|
]),
|
|
|
|
pLoaded({ object }) {
|
|
|
|
this.pBar = object.android;
|
|
|
|
this.pBar.setRotation(
|
|
|
|
this.RTL && utils.sysRTL() ? 0 : this.RTL || utils.sysRTL() ? 180 : 0
|
|
|
|
);
|
|
|
|
this.initTimer();
|
|
|
|
},
|
2021-05-22 08:56:31 +00:00
|
|
|
attachRecipe() {
|
2021-05-25 14:32:53 +00:00
|
|
|
this.$showModal(ActionWithSearch, {
|
2021-05-22 08:56:31 +00:00
|
|
|
props: {
|
|
|
|
title: "selRec",
|
|
|
|
recipes: this.recipes,
|
2021-06-15 11:04:42 +00:00
|
|
|
action: "aNBtn",
|
2021-05-22 08:56:31 +00:00
|
|
|
},
|
2021-06-15 11:04:42 +00:00
|
|
|
}).then((res) => {
|
|
|
|
if (res == "aNBtn") {
|
|
|
|
this.$navigateTo(EditRecipe, {
|
|
|
|
animated: false,
|
|
|
|
});
|
|
|
|
} else if (res) {
|
|
|
|
let timer = this.timer;
|
|
|
|
timer.recipeID = res;
|
|
|
|
this.sortActiveTimers();
|
|
|
|
}
|
2021-05-22 08:56:31 +00:00
|
|
|
});
|
|
|
|
},
|
2021-06-15 11:04:42 +00:00
|
|
|
setNum(type, val) {
|
|
|
|
ApplicationSettings.setNumber(`${this.timer.id}${type}`, val);
|
|
|
|
},
|
|
|
|
setProgress() {
|
|
|
|
this.progress = 100 - (this.count / this.getTotalTime) * 100;
|
|
|
|
this.pBar.setProgress(this.progress, true);
|
|
|
|
},
|
|
|
|
initTimer() {
|
|
|
|
this.resetInterval();
|
|
|
|
this.setProgress();
|
|
|
|
!this.timer.isPaused && this.pBar.setIndeterminate(this.countUp);
|
|
|
|
if (this.progress < 100 || !this.timer.timerInt) {
|
|
|
|
this.timer.timerInt = setInterval(() => {
|
2021-05-22 08:56:31 +00:00
|
|
|
if (!this.timer.isPaused) {
|
2021-06-15 11:04:42 +00:00
|
|
|
this.setNum("c", this.countUp ? this.count++ : --this.count);
|
|
|
|
this.setProgress();
|
|
|
|
} else this.resetInterval();
|
|
|
|
if (this.progress >= 100 && this.count >= 0) {
|
|
|
|
this.timer.done = 1;
|
|
|
|
this.timerAlert();
|
2021-05-22 08:56:31 +00:00
|
|
|
}
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
resetInterval() {
|
2021-06-15 11:04:42 +00:00
|
|
|
clearInterval(this.timer.timerInt);
|
|
|
|
this.timer.timerInt = 0;
|
|
|
|
this.pBar.setIndeterminate(false);
|
2021-05-22 08:56:31 +00:00
|
|
|
},
|
|
|
|
resetTimer() {
|
2021-06-15 11:04:42 +00:00
|
|
|
this.count = this.actualTime;
|
|
|
|
this.progress = this.delay = this.timer.done = 0;
|
|
|
|
ApplicationSettings.remove(`${this.timer.id}d`);
|
|
|
|
this.setNum("c", this.count);
|
|
|
|
this.toggleProgress(1);
|
|
|
|
this.pBar.setProgress(0, true);
|
|
|
|
},
|
|
|
|
toggleProgress(n) {
|
|
|
|
this.togglePause(this.timer, n);
|
|
|
|
this.timer.isPaused ? this.resetInterval() : this.initTimer();
|
2021-05-22 08:56:31 +00:00
|
|
|
},
|
|
|
|
addPreset() {
|
2021-06-15 11:04:42 +00:00
|
|
|
let exist = this.timerPresets.some((e) => e.id == this.timer.id);
|
2021-05-22 08:56:31 +00:00
|
|
|
this.timer.preset = 1;
|
2021-06-15 11:04:42 +00:00
|
|
|
if (this.countUp) {
|
|
|
|
this.timer.time = new Date(this.count * 1000)
|
|
|
|
.toISOString()
|
|
|
|
.substr(11, 8);
|
|
|
|
}
|
|
|
|
let timer = JSON.parse(JSON.stringify(this.timer));
|
|
|
|
let { recipeID, timerInt, isPaused, preset, done, mode, ...presetTimer } =
|
|
|
|
timer;
|
|
|
|
this.addTimerPreset(presetTimer);
|
|
|
|
exist ? this.showToast("prstTU") : this.showToast("aTPrst");
|
2021-05-22 08:56:31 +00:00
|
|
|
},
|
2021-06-15 11:04:42 +00:00
|
|
|
addDelay() {
|
|
|
|
this.timer.done = 0;
|
|
|
|
let td = this.timerDelay;
|
|
|
|
let delayDur =
|
|
|
|
this.getLocaleN(td) + " " + localize(td > 1 ? "minutes" : "minute");
|
|
|
|
this.showToast(localize("wDBy", this.timer.label, delayDur));
|
|
|
|
let delay = td * 60;
|
|
|
|
if (this.done) this.delay = delay;
|
|
|
|
else this.delay += delay;
|
|
|
|
if (this.count >= 0) this.count += delay;
|
|
|
|
else this.count = this.delay;
|
|
|
|
this.setNum("d", this.delay);
|
|
|
|
this.setNum("c", this.count);
|
2021-05-22 08:56:31 +00:00
|
|
|
this.resetInterval();
|
2021-06-15 11:04:42 +00:00
|
|
|
this.initTimer();
|
|
|
|
this.timerAlert();
|
2021-05-22 08:56:31 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// HELPERS
|
|
|
|
touch({ object, action }) {
|
2021-06-18 12:52:03 +00:00
|
|
|
this.touchFade(object, action);
|
2021-05-22 08:56:31 +00:00
|
|
|
if (action == "up") this.attachRecipe();
|
|
|
|
},
|
|
|
|
},
|
2021-06-15 11:04:42 +00:00
|
|
|
created() {
|
|
|
|
this.delay = ApplicationSettings.getNumber(`${this.timer.id}d`, 0);
|
|
|
|
this.count = ApplicationSettings.getNumber(
|
|
|
|
`${this.timer.id}c`,
|
|
|
|
this.actualTime
|
2021-05-22 08:56:31 +00:00
|
|
|
);
|
2021-06-15 11:04:42 +00:00
|
|
|
let bID = "timer" + this.timer.id;
|
|
|
|
EvtBus.$off(bID);
|
|
|
|
EvtBus.$on(bID, (e) => {
|
2021-05-22 08:56:31 +00:00
|
|
|
switch (e) {
|
|
|
|
case "delay":
|
2021-06-15 11:04:42 +00:00
|
|
|
this.addDelay();
|
|
|
|
break;
|
|
|
|
case "dismiss":
|
|
|
|
this.removeTimer(this.timer.id, 1);
|
2021-05-22 08:56:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|