enrecipes/app/components/EnRecipes.vue

861 lines
26 KiB
Vue
Raw Normal View History

2020-09-15 11:10:16 +00:00
<template>
2021-02-28 15:10:26 +00:00
<Page @loaded="onPageLoad" @unloaded="onPageUnload">
<ActionBar androidElevation="1">
<GridLayout
v-if="showSearch"
columns="auto, *"
verticalAlignment="center"
>
<MDButton
class="er"
:text="icon.back"
variant="text"
automationText="Back"
col="0"
@tap="closeSearch"
/>
<SearchBar
col="1"
:hint="'ser' | L"
@textChange="updateList($event.value)"
@clear="clearSearch"
/>
</GridLayout>
<GridLayout v-else columns="auto, *, auto, auto, auto">
<MDButton
class="er"
col="0"
variant="text"
@tap="selectMode ? clearSelection() : showDrawer()"
:text="selectMode ? icon.x : icon.menu"
/>
<Label
v-if="selectMode"
class="title orkm"
:text="`${selection.length} ${$options.filters.L('sltd')}`"
col="1"
/>
<Label
v-else
class="title orkm"
:text="`${currentComponent}` | L"
col="1"
/>
<!-- <MDButton v-if="recipes.length" class="er" :text="layout== 1 ? icon.l1 : layout==2 ? icon.l2 : icon.l3" variant="text" col="2" @tap="switchLayout" /> -->
<MDButton
v-if="recipes.length && !selectMode"
class="er"
:text="selectMode ? icon.export : icon.sear"
variant="text"
col="3"
@tap="selectMode ? exportSelection() : openSearch()"
/>
<MDButton
v-if="recipes.length"
class="er"
:text="selectMode ? icon.del : icon.sort"
variant="text"
col="4"
@tap="selectMode ? deleteSelection() : sortDialog()"
/>
</GridLayout>
</ActionBar>
<AbsoluteLayout>
<ListView
width="100%"
height="100%"
for="recipe in filteredRecipes"
@loaded="listViewLoad"
:itemTemplateSelector="getLayout"
>
<v-template key="one">
<GridLayout class="recipeContainer" :class="isFirstItem(recipe.id)">
<GridLayout
class="recipeItem layout1 mdr"
rows="104"
columns="104, *"
androidElevation="1"
ref="recipe"
@longPress="
selectMode
? viewRecipe(recipe.id)
: addToSelection($event, recipe.id)
"
@tap="
selectMode
? addToSelection($event, recipe.id)
: viewRecipe(recipe.id)
"
>
<GridLayout class="imageHolder card" rows="104" columns="104">
<Image
row="0"
col="0"
v-if="recipe.imageSrc"
:src="recipe.imageSrc"
stretch="aspectFill"
decodeWidth="104"
decodeHeight="104"
loadMode="async"
/>
<Label
v-else
row="0"
col="0"
horizontalAlignment="center"
class="er"
fontSize="56"
:text="icon.img"
/>
2021-01-23 17:20:15 +00:00
</GridLayout>
2021-02-28 15:10:26 +00:00
<StackLayout class="recipeInfo" col="1">
<Label
:text="`${$options.filters.L(
recipe.cuisine
)} ${$options.filters.L(recipe.category)}`"
class="category"
/>
<Label :text="recipe.title" class="orkm title" />
<GridLayout columns="*" rows="auto, *, auto">
<StackLayout
class="attrContainer"
orientation="horizontal"
row="0"
>
<Label class="er small" :text="icon.star" />
<Label class="attr" :text="recipe.rating" />
<Label class="er small" :text="icon.diff" />
<Label class="attr" :text="`${recipe.difficulty}` | L" />
<Label class="er small" :text="icon.time" />
<Label
class="attr"
:text="`${
formattedTotalTime(recipe.prepTime, recipe.cookTime)
.time
}`"
/>
</StackLayout>
<StackLayout
class="tagsContainer"
orientation="horizontal"
row="2"
>
<Label
v-for="(tag, index) in recipe.tags"
:key="index"
class="tag"
:text="tag"
/>
</StackLayout>
</GridLayout>
</StackLayout>
2021-01-23 17:20:15 +00:00
</GridLayout>
2021-02-28 15:10:26 +00:00
</GridLayout>
</v-template>
<!-- <v-template key="two">
<GridLayout class="recipeContainer" :class="isFirstItem(recipe.id)">
<GridLayout
class="recipeItem layout2 mdr"
rows="auto, auto"
columns="*"
androidElevation="1"
ref="recipe"
@longPress="
selectMode
? viewRecipe(recipe.id)
: addToSelection($event, recipe.id)
"
@tap="
selectMode
? addToSelection($event, recipe.id)
: viewRecipe(recipe.id)
"
>
<GridLayout class="imageHolder card" :rows="imgWidth" columns="*">
<Image
row="0"
col="0"
v-if="recipe.imageSrc"
:src="recipe.imageSrc"
stretch="aspectFill"
:decodeWidth="imgWidth"
:decodeHeight="imgWidth"
loadMode="async"
/>
<Label
v-else
row="0"
col="0"
horizontalAlignment="center"
class="er"
:fontSize="imgWidth / 2"
:text="icon.img"
/>
</GridLayout>
<StackLayout class="recipeInfo" row="1">
<Label
:text="`${$options.filters.L(
recipe.cuisine
)} ${$options.filters.L(recipe.category)}`"
class="category"
/>
<Label :text="recipe.title" class="orkm title" />
<StackLayout
class="tagsContainer"
orientation="horizontal"
row="2"
>
<Label
v-for="(tag, index) in recipe.tags"
:key="index"
v-if="tag"
class="tag"
:text="tag"
/>
</StackLayout>
2021-01-23 17:20:15 +00:00
</StackLayout>
2021-02-28 15:10:26 +00:00
</GridLayout>
2021-01-23 17:20:15 +00:00
</GridLayout>
2021-02-28 15:10:26 +00:00
</v-template>
<v-template key="three">
<GridLayout class="recipeContainer" :class="isFirstItem(recipe.id)">
<GridLayout
class="recipeItem layout1 mdr"
rows="auto"
columns="*"
androidElevation="1"
ref="recipe"
@longPress="
selectMode
? viewRecipe(recipe.id)
: addToSelection($event, recipe.id)
"
@tap="
selectMode
? addToSelection($event, recipe.id)
: viewRecipe(recipe.id)
"
>
<StackLayout class="recipeInfo">
<Label
:text="`${$options.filters.L(
recipe.cuisine
)} ${$options.filters.L(recipe.category)}`"
class="category"
/>
<Label :text="recipe.title" class="orkm title" />
<StackLayout class="tagsContainer" orientation="horizontal">
<Label
v-for="(tag, index) in recipe.tags"
:key="index"
v-if="tag"
class="tag"
:text="tag"
/>
</StackLayout>
2021-01-23 17:20:15 +00:00
</StackLayout>
2021-02-28 15:10:26 +00:00
</GridLayout>
2021-01-23 17:20:15 +00:00
</GridLayout>
2021-02-28 15:10:26 +00:00
</v-template> -->
</ListView>
<GridLayout rows="*, auto, *, 88" columns="*" class="emptyStateContainer">
<StackLayout
row="1"
class="emptyState"
v-if="!recipes.length && !filterFavourites && !filterTrylater"
@tap="addRecipe"
>
<Label class="er icon" :text="icon.plusc" />
<Label class="title orkm" :text="'strAdd' | L" textWrap="true" />
<StackLayout orientation="horizontal" horizontalAlignment="center">
<Label :text="'plsAdd' | L" textWrap="true" />
</StackLayout>
</StackLayout>
<StackLayout
row="1"
class="emptyState"
v-if="!filteredRecipes.length && filterTrylater && !searchQuery"
>
<Label class="er icon" :text="icon.try" textWrap="true" />
<Label class="title orkm" :text="'aD' | L" textWrap="true" />
<Label :text="'tLInfo' | L" textWrap="true" />
</StackLayout>
<StackLayout
row="1"
class="emptyState"
v-if="!filteredRecipes.length && filterFavourites && !searchQuery"
>
<Label class="er icon" :text="icon.fav" textWrap="true" />
<Label class="title orkm" :text="'noFavs' | L" textWrap="true" />
<Label :text="'fsList' | L" textWrap="true" />
2020-11-10 18:28:48 +00:00
</StackLayout>
2021-02-28 15:10:26 +00:00
<StackLayout
row="1"
class="emptyState"
v-if="!filteredRecipes.length && searchQuery"
>
<Label class="er icon" :text="icon.noresult" textWrap="true" />
<Label
class="title orkm"
:text="`${noResultFor}` | L"
textWrap="true"
/>
<MDButton
v-if="filterFavourites || filterTrylater || selectedCuisine"
variant="text"
class="searchAll orkm"
:text="'trySer' | L"
@tap="searchAll"
/>
</StackLayout>
</GridLayout>
<GridLayout id="btnFabContainer" rows="*, auto" columns="*, auto">
<transition name="bounce">
<MDFloatingActionButton
v-if="showFAB"
row="1"
col="1"
class="er fab-button"
src="res://plus"
@tap="addRecipe"
/>
</transition>
</GridLayout>
</AbsoluteLayout>
</Page>
2020-09-15 11:10:16 +00:00
</template>
<script>
import {
2020-12-29 10:35:19 +00:00
ApplicationSettings,
AndroidApplication,
Utils,
2020-12-29 10:35:19 +00:00
Observable,
Device,
2021-02-28 15:10:26 +00:00
Screen,
} from "@nativescript/core";
import { localize } from "@nativescript/localize";
import { time } from "tns-core-modules/profiling";
2020-12-29 10:35:19 +00:00
import {
startAccelerometerUpdates,
stopAccelerometerUpdates,
2021-02-28 15:10:26 +00:00
} from "nativescript-accelerometer";
import { Vibrate } from "nativescript-vibrate";
2020-12-29 10:35:19 +00:00
let vibrator = new Vibrate();
2021-02-28 15:10:26 +00:00
import { mapActions, mapState } from "vuex";
import EditRecipe from "./EditRecipe.vue";
import ViewRecipe from "./ViewRecipe.vue";
import ActionDialog from "./modal/ActionDialog.vue";
import ConfirmDialog from "./modal/ConfirmDialog.vue";
2021-02-28 17:34:27 +00:00
import * as utils from "~/shared/utils.js";
2020-12-29 10:35:19 +00:00
let lastTime = 0;
let lastShake = 0;
let lastForce = 0;
let shakeCount = 0;
2021-01-13 05:02:48 +00:00
let typingTimer;
2020-10-14 19:32:32 +00:00
export default {
2021-02-28 15:10:26 +00:00
props: [
"filterFavourites",
"filterTrylater",
"closeDrawer",
"showDrawer",
"selectedCategory",
"selectedCuisine",
"selectedTag",
"hijackGlobalBackEvent",
"releaseGlobalBackEvent",
],
2020-10-14 19:32:32 +00:00
components: {
EditRecipe,
2021-02-28 15:10:26 +00:00
ViewRecipe,
2020-10-14 19:32:32 +00:00
},
2020-09-15 11:10:16 +00:00
data() {
return {
2020-10-14 19:32:32 +00:00
searchQuery: "",
2020-10-21 17:54:45 +00:00
showSearch: false,
2020-10-14 19:32:32 +00:00
rightAction: false,
deletionDialogActive: false,
2021-01-13 05:02:48 +00:00
showFAB: false,
filterDone: true,
2021-01-23 17:20:15 +00:00
selection: [],
selectMode: false,
recipeList: [],
// listView: null,
layout: 1,
};
2020-09-15 11:10:16 +00:00
},
computed: {
2021-02-28 15:10:26 +00:00
...mapState([
"sortType",
"icon",
"recipes",
"currentComponent",
"shakeEnabled",
]),
2020-09-15 11:10:16 +00:00
filteredRecipes() {
2021-02-28 15:10:26 +00:00
let ingredients = this.recipes
.map((e) => e.ingredients.map((f) => f.item.toLowerCase()).join())
.join();
2020-12-29 10:35:19 +00:00
2021-02-28 15:10:26 +00:00
let vm = this;
2021-01-13 05:02:48 +00:00
2021-02-28 15:10:26 +00:00
function getIngredients(e) {
return e.ingredients
.map((f) => f.item.toLowerCase())
.join()
.includes(vm.searchQuery);
2021-01-13 05:02:48 +00:00
}
2021-01-23 17:20:15 +00:00
// if ( this.filterDone ) {
2021-02-28 15:10:26 +00:00
if (this.filterFavourites) {
return this.recipes
.filter(
(e) =>
e.isFavorite &&
(e.title.toLowerCase().includes(this.searchQuery) ||
getIngredients(e))
)
.sort(this.sortFunction);
} else if (this.filterTrylater) {
return this.recipes
.filter(
(e) =>
!e.tried &&
(e.title.toLowerCase().includes(this.searchQuery) ||
getIngredients(e))
)
.sort(this.sortFunction);
} else if (this.selectedCuisine) {
return this.recipes
.filter((e) => {
return (
this.recipeFilter(e) &&
(e.title.toLowerCase().includes(this.searchQuery) ||
getIngredients(e))
);
})
.sort(this.sortFunction);
2020-12-14 13:48:53 +00:00
} else {
2021-02-28 15:10:26 +00:00
return this.recipes
.filter(
(e) =>
e.title.toLowerCase().includes(this.searchQuery) ||
getIngredients(e)
)
.sort(this.sortFunction);
2020-09-15 11:10:16 +00:00
}
2021-01-23 17:20:15 +00:00
// } else {
// return 0;
// }
2020-09-15 11:10:16 +00:00
},
2020-11-28 19:21:57 +00:00
noResultFor() {
2021-02-28 15:10:26 +00:00
if (this.filterFavourites) return "noRecsInFavs";
if (this.filterTrylater) return "noRecsInTL";
if (this.selectedCuisine) return "noRecsInFtr";
2021-01-13 05:02:48 +00:00
return "noRecs";
2020-12-29 10:35:19 +00:00
},
2021-01-23 17:20:15 +00:00
screenWidth() {
2021-02-28 15:10:26 +00:00
return Screen.mainScreen.widthDIPs;
2021-01-23 17:20:15 +00:00
},
imgWidth() {
2021-02-28 15:10:26 +00:00
return Screen.mainScreen.widthDIPs / 2 - 20;
2021-01-23 17:20:15 +00:00
},
2020-09-15 11:10:16 +00:00
},
methods: {
2021-02-28 15:10:26 +00:00
...mapActions([
"setCurrentComponentAction",
"setSortTypeAction",
"deleteRecipeAction",
"deleteRecipesAction",
2021-02-28 17:34:27 +00:00
"setShakeAction",
2021-02-28 15:10:26 +00:00
]),
onPageLoad(args) {
const page = args.object;
page.bindingContext = new Observable();
2021-02-28 15:10:26 +00:00
this.filterFavourites
? this.setComponent("Favourites")
: this.filterTrylater
? this.setComponent("Try Later")
: this.selectedCuisine
? this.setComponent("Filtered recipes")
: this.setComponent("EnRecipes");
if (!this.selectMode) this.showFAB = true;
2021-02-28 17:34:27 +00:00
if (this.shakeEnabled) {
if (utils.hasAccelerometer())
startAccelerometerUpdates((data) => this.onSensorData(data));
else this.setShakeAction(false);
}
2021-02-28 15:10:26 +00:00
if (this.showSearch || this.selectMode) this.hijackLocalBackEvent();
this.showDrawer();
this.closeDrawer();
},
onPageUnload() {
2021-02-28 15:10:26 +00:00
if (this.shakeEnabled) stopAccelerometerUpdates();
this.releaseGlobalBackEvent();
2021-01-13 05:02:48 +00:00
this.releaseLocalBackEvent();
},
2021-02-28 15:10:26 +00:00
listViewLoad(args) {
let e = args.object.android;
e.setSelector(new android.graphics.drawable.StateListDrawable());
e.setDivider(null);
e.setDividerHeight(1);
2020-11-23 09:49:58 +00:00
},
2021-01-23 17:20:15 +00:00
// switchLayout() {
// if ( this.layout == 2 ) this.layout = 1
// else this.layout++
// },
getLayout() {
2021-02-28 15:10:26 +00:00
switch (this.layout) {
2021-01-23 17:20:15 +00:00
case 1:
2021-02-28 15:10:26 +00:00
return "one";
2021-01-23 17:20:15 +00:00
case 2:
2021-02-28 15:10:26 +00:00
return "two";
2021-01-23 17:20:15 +00:00
case 3:
2021-02-28 15:10:26 +00:00
return "three";
2021-01-23 17:20:15 +00:00
}
},
// HELPERS
2020-10-21 17:54:45 +00:00
openSearch() {
this.showSearch = true;
this.showFAB = false;
this.hijackLocalBackEvent();
2020-10-21 17:54:45 +00:00
},
2020-11-10 18:28:48 +00:00
closeSearch() {
this.searchQuery = "";
Utils.ad.dismissSoftInput();
this.showSearch = false;
this.showFAB = true;
this.releaseLocalBackEvent();
2020-11-10 18:28:48 +00:00
},
2021-02-28 15:10:26 +00:00
setComponent(comp) {
this.setCurrentComponentAction(comp);
this.hijackGlobalBackEvent();
2020-11-10 18:28:48 +00:00
},
clearSearch() {
2021-02-28 15:10:26 +00:00
this.searchQuery = "";
},
updateList(value) {
clearTimeout(typingTimer);
typingTimer = setTimeout((e) => {
this.searchQuery = value;
}, 750);
},
formattedTotalTime(prepTime, cookTime) {
let t1 = prepTime.split(":");
let t2 = cookTime.split(":");
let minutes = parseInt(t1[1]) + parseInt(t2[1]);
let m = minutes % 60;
let h = parseInt(t1[0]) + parseInt(t2[0]) + Math.floor(minutes / 60);
let hr = localize("hr");
let min = localize("min");
let mins = h * 60 + m;
2020-11-10 18:28:48 +00:00
return {
2021-02-28 15:10:26 +00:00
time: h ? (m ? `${h} ${hr} ${m} ${min}` : `${h} ${hr}`) : `${m} ${min}`,
duration: `${mins}`,
};
2020-11-10 18:28:48 +00:00
},
2021-02-28 15:10:26 +00:00
randomRecipeID() {
// TODO: show only from selected filter
let min = 0;
let max = this.filteredRecipes.length - 1;
let randomIndex = Math.round(Math.random() * (max - min));
return this.filteredRecipes[randomIndex].id;
},
recipeFilter(e) {
let cuisineMatched = e.cuisine === this.selectedCuisine;
let allCuisines = /allCuis/.test(this.selectedCuisine);
let categoryMatched = e.category === this.selectedCategory;
let allCategories = /allCats/.test(this.selectedCategory);
let tagMatched = e.tags.includes(this.selectedTag);
let allTags = /allTs/.test(this.selectedTag);
let cuisine = cuisineMatched || allCuisines;
2020-12-29 10:35:19 +00:00
2021-02-28 15:10:26 +00:00
return this.selectedTag && !allTags
? (categoryMatched || allCategories) && cuisine && tagMatched
: this.selectedCategory && !allCategories
? cuisine && categoryMatched
: cuisine;
2020-12-29 10:35:19 +00:00
},
2021-01-13 05:02:48 +00:00
searchAll() {
2021-02-28 15:10:26 +00:00
this.$emit("backToHome");
},
sortFunction(a, b) {
const titleOrder = a.title
.toLowerCase()
.localeCompare(b.title.toLowerCase(), Device.language, {
ignorePunctuation: true,
});
let d1 = this.formattedTotalTime(a.prepTime, a.cookTime).duration;
let d2 = this.formattedTotalTime(b.prepTime, b.cookTime).duration;
let ld1 = new Date(a.lastModified);
let ld2 = new Date(b.lastModified);
let cd1 = new Date(a.created);
let cd2 = new Date(b.created);
let r1 = a.rating;
let r2 = b.rating;
2021-01-23 17:20:15 +00:00
2021-02-28 15:10:26 +00:00
function difficultyLevel(l) {
switch (l) {
2021-01-23 17:20:15 +00:00
case "Easy":
return 1;
case "Moderate":
return 2;
case "Challenging":
return 3;
}
}
2021-02-28 15:10:26 +00:00
let dl1 = difficultyLevel(a.difficulty);
let dl2 = difficultyLevel(b.difficulty);
switch (this.sortType) {
2021-01-23 17:20:15 +00:00
case "Title":
return titleOrder > 0 ? 1 : titleOrder < 0 ? -1 : 0;
break;
case "Quickest first":
return d1 > d2 ? 1 : d1 < d2 ? -1 : 0;
break;
case "Slowest first":
return d1 > d2 ? -1 : d1 < d2 ? 1 : 0;
break;
case "Rating":
return r1 > r2 ? -1 : r1 < r2 ? 1 : 0;
break;
case "Difficulty level":
return dl1 > dl2 ? 1 : dl1 < dl2 ? -1 : 0;
break;
case "Last updated":
return ld1 < ld2 ? 1 : ld1 > ld2 ? -1 : 0;
break;
case "Newest first":
return cd1 < cd2 ? 1 : cd1 > cd2 ? -1 : 0;
break;
case "Oldest first":
return cd1 < cd2 ? -1 : cd1 > cd2 ? 1 : 0;
break;
}
},
2021-02-28 15:10:26 +00:00
isFirstItem(id) {
let length = this.filteredRecipes.length;
return id == this.filteredRecipes[0].id
? "firstItem"
: id == this.filteredRecipes[length - 1].id
? "lastItem"
: "";
2021-01-23 17:20:15 +00:00
},
2021-02-28 15:10:26 +00:00
isLastItem(id) {
let length = this.filteredRecipes.length;
2021-01-23 17:20:15 +00:00
// let lastIsOdd = ( length - 1 ) % 2 == 0
// if ( this.filteredRecipes.length > 1 ) {
// if ( id == this.filteredRecipes[ length - 2 ].id ) {
// if ( !lastIsOdd ) return 'lastItem'
// }
// }
2021-02-28 15:10:26 +00:00
if (id == this.filteredRecipes[length - 1].id) return "lastItem";
2021-01-23 17:20:15 +00:00
},
2020-11-10 18:28:48 +00:00
// NAVIGATION HANDLERS
2020-10-21 17:54:45 +00:00
hijackLocalBackEvent() {
this.releaseGlobalBackEvent();
2021-02-28 15:10:26 +00:00
AndroidApplication.on(
AndroidApplication.activityBackPressedEvent,
this.localBackEvent
);
2020-10-21 17:54:45 +00:00
},
releaseLocalBackEvent() {
2021-02-28 15:10:26 +00:00
AndroidApplication.off(
AndroidApplication.activityBackPressedEvent,
this.localBackEvent
);
this.hijackGlobalBackEvent();
2020-10-21 17:54:45 +00:00
},
2021-02-28 15:10:26 +00:00
localBackEvent(args) {
args.cancel = true;
2021-02-28 15:10:26 +00:00
if (this.selectMode) this.clearSelection();
this.closeDrawer();
this.closeSearch();
2020-10-21 17:54:45 +00:00
},
2020-11-10 18:28:48 +00:00
addRecipe() {
this.showFAB = false;
this.releaseGlobalBackEvent();
2021-02-28 15:10:26 +00:00
this.$navigateTo(EditRecipe, {
2020-11-10 18:28:48 +00:00
props: {
2020-12-29 10:35:19 +00:00
selectedCuisine: this.selectedCuisine,
2020-11-10 18:28:48 +00:00
selectedCategory: this.selectedCategory,
2020-12-29 10:35:19 +00:00
selectedTag: this.selectedTag,
filterFavourites: this.filterFavourites,
filterTrylater: this.filterTrylater,
2021-02-28 15:10:26 +00:00
},
});
2020-10-21 17:54:45 +00:00
},
2021-02-28 15:10:26 +00:00
viewRecipe(recipeID) {
this.showFAB = false;
2021-02-28 15:10:26 +00:00
this.$navigateTo(ViewRecipe, {
2020-11-10 18:28:48 +00:00
props: {
filterTrylater: this.filterTrylater,
2021-02-28 15:10:26 +00:00
recipeID,
2020-11-10 18:28:48 +00:00
},
2021-02-28 15:10:26 +00:00
backstackVisible: false,
});
2020-11-10 18:28:48 +00:00
},
2020-12-29 10:35:19 +00:00
viewRandomRecipe() {
this.showFAB = false;
2021-02-28 15:10:26 +00:00
this.$navigateTo(ViewRecipe, {
2020-12-29 10:35:19 +00:00
props: {
filterTrylater: false,
2021-02-28 15:10:26 +00:00
recipeID: this.randomRecipeID(),
2020-12-29 10:35:19 +00:00
},
2021-02-28 15:10:26 +00:00
backstackVisible: false,
});
2020-12-29 10:35:19 +00:00
},
2021-01-23 17:20:15 +00:00
2020-11-10 18:28:48 +00:00
// LIST HANDLERS
2020-10-21 17:54:45 +00:00
sortDialog() {
this.releaseGlobalBackEvent();
2021-02-28 15:10:26 +00:00
this.$showModal(ActionDialog, {
2020-10-21 17:54:45 +00:00
props: {
2021-01-13 05:02:48 +00:00
title: "srt",
2021-02-28 15:10:26 +00:00
list: [
"Title",
"Quickest first",
"Slowest first",
"Rating",
"Difficulty level",
"Last updated",
"Newest first",
"Oldest first",
],
2021-01-13 05:02:48 +00:00
stretch: false,
2021-02-28 15:10:26 +00:00
helpIcon: "sort",
bgColor: "#858585",
},
}).then((action) => {
if (action && action !== "Cancel" && this.sortType !== action) {
this.setSortTypeAction(action);
ApplicationSettings.setString("sortType", action);
this.updateSort();
2020-10-21 17:54:45 +00:00
}
this.hijackGlobalBackEvent();
2021-02-28 15:10:26 +00:00
});
2020-10-21 17:54:45 +00:00
},
2020-12-29 10:35:19 +00:00
2021-01-23 17:20:15 +00:00
// DATA HANDLERS
2021-02-28 15:10:26 +00:00
addToSelection(args, id) {
this.showFAB = false;
if (!this.selectMode) this.hijackLocalBackEvent();
this.selectMode = true;
this.$emit("selectModeOn", false);
let item = args.object;
if (item.className === "selected") {
item.className = "";
this.selection.splice(this.selection.indexOf(id), 1);
this.recipeList.splice(this.selection.indexOf(id), 1);
2020-12-14 13:48:53 +00:00
} else {
2021-02-28 15:10:26 +00:00
item.className = "selected";
this.selection.push(id);
this.recipeList.push(item);
2020-10-21 17:54:45 +00:00
}
2021-02-28 15:10:26 +00:00
if (!this.selection.length) this.clearSelection();
2020-10-21 17:54:45 +00:00
},
2021-01-23 17:20:15 +00:00
clearSelection() {
2021-02-28 15:10:26 +00:00
this.selectMode = false;
this.$emit("selectModeOn", true);
this.selection = [];
this.recipeList.forEach((e) => (e.className = ""));
this.releaseLocalBackEvent();
this.showFAB = true;
2020-10-14 19:32:32 +00:00
},
2021-01-23 17:20:15 +00:00
deleteSelection() {
2021-02-28 15:10:26 +00:00
this.selection.length === 1
? this.deleteRecipe(this.selection[0])
: this.deleteRecipes(this.selection);
2020-10-14 19:32:32 +00:00
},
2021-01-23 17:20:15 +00:00
exportSelection() {},
2021-02-28 15:10:26 +00:00
deleteRecipe(id) {
this.deletionDialogActive = true;
2021-02-28 15:10:26 +00:00
let index = this.recipes.findIndex((e) => e.id === id);
this.$showModal(ConfirmDialog, {
2020-10-21 17:54:45 +00:00
props: {
2021-02-28 15:10:26 +00:00
title: localize("conf"),
description: `${localize("delRecInfo")} "${
this.recipes[index].title
}"`,
2021-01-13 05:02:48 +00:00
cancelButtonText: "cBtn",
okButtonText: "dBtn",
2021-02-28 15:10:26 +00:00
helpIcon: "del",
bgColor: "#c92a2a",
},
}).then((action) => {
if (action) {
this.deleteRecipeAction({
index,
2021-02-28 15:10:26 +00:00
id,
});
if (!this.filteredRecipes.length) this.$emit("backToHome");
this.clearSelection();
2021-01-23 17:20:15 +00:00
}
this.deletionDialogActive = false;
2021-02-28 15:10:26 +00:00
});
2021-01-23 17:20:15 +00:00
},
2021-02-28 15:10:26 +00:00
deleteRecipes(idsArr) {
2021-01-23 17:20:15 +00:00
this.deletionDialogActive = true;
2021-02-28 15:10:26 +00:00
this.$showModal(ConfirmDialog, {
2021-01-23 17:20:15 +00:00
props: {
2021-02-28 15:10:26 +00:00
title: localize("conf"),
description: `${localize("delRecsInfo")} ${
this.selection.length
} ${localize("recs")}`,
2021-01-23 17:20:15 +00:00
cancelButtonText: "cBtn",
okButtonText: "dBtn",
2021-02-28 15:10:26 +00:00
helpIcon: "del",
bgColor: "#c92a2a",
},
}).then((action) => {
if (action) {
this.deleteRecipesAction(idsArr);
if (!this.filteredRecipes.length) this.$emit("backToHome");
this.clearSelection();
}
this.deletionDialogActive = false;
2021-02-28 15:10:26 +00:00
});
2020-12-29 10:35:19 +00:00
},
2021-01-23 17:20:15 +00:00
2020-12-29 10:35:19 +00:00
// SHAKE DETECTOR
2021-02-28 15:10:26 +00:00
onSensorData({ x, y, z }) {
x = x.toFixed(2);
y = y.toFixed(2);
z = z.toFixed(2);
2020-12-29 10:35:19 +00:00
const FORCE_THRESHOLD = 1;
const TIME_THRESHOLD = 150;
const SHAKE_TIMEOUT = 600;
const SHAKE_THROTTLE = 600;
const SHAKE_COUNT = 3;
2021-02-28 15:10:26 +00:00
const now = time();
if (now - lastForce > SHAKE_TIMEOUT) {
2020-12-29 10:35:19 +00:00
shakeCount = 0;
}
let timeDelta = now - lastTime;
2021-02-28 15:10:26 +00:00
if (timeDelta > TIME_THRESHOLD) {
let forceVector = Math.abs(
Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2)) - 1
);
if (forceVector > FORCE_THRESHOLD) {
2020-12-29 10:35:19 +00:00
shakeCount++;
2021-02-28 15:10:26 +00:00
if (shakeCount >= SHAKE_COUNT && now - lastShake > SHAKE_THROTTLE) {
2020-12-29 10:35:19 +00:00
lastShake = now;
shakeCount = 0;
2021-02-28 15:10:26 +00:00
if (this.filteredRecipes.length) {
vibrator.vibrate(100);
this.viewRandomRecipe();
2020-12-29 10:35:19 +00:00
}
}
lastForce = now;
}
lastTime = now;
}
},
2020-09-15 11:10:16 +00:00
},
mounted() {
2021-02-28 15:10:26 +00:00
this.showFAB = true;
},
};
2020-09-15 11:10:16 +00:00
</script>