enrecipes/app/components/EnRecipes.vue

866 lines
26 KiB
Vue
Raw Normal View History

2020-09-15 11:10:16 +00:00
<template>
2021-03-23 09:59:00 +00:00
<Page @loaded="onPageLoad" @unloaded="onPageUnload" actionBarHidden="true">
<GridLayout rows="*, auto" columns="*">
2021-02-28 18:49:36 +00:00
<CollectionView
2021-03-23 09:59:00 +00:00
rowSpan="2"
2021-02-28 15:10:26 +00:00
width="100%"
height="100%"
for="recipe in filteredRecipes"
2021-02-28 18:49:36 +00:00
ref="listview"
2021-02-28 15:10:26 +00:00
:itemTemplateSelector="getLayout"
2021-03-21 17:02:04 +00:00
:colWidth="layout == 'grid' ? '50%' : '100%'"
2021-02-28 15:10:26 +00:00
>
2021-03-23 09:59:00 +00:00
<v-template name="header">
<StackLayout> <Label text="Title" textWrap="true" /> </StackLayout>
</v-template>
2021-03-21 17:02:04 +00:00
<v-template name="detailed">
<GridLayout class="recipeContainer" :class="getItemPos(recipe.id)">
2021-02-28 15:10:26 +00:00
<GridLayout
class="recipeItem layout1 mdr"
2021-03-23 06:16:25 +00:00
rows="96"
columns="96, *"
2021-02-28 15:10:26 +00:00
ref="recipe"
@longPress="
selectMode
? viewRecipe(recipe.id)
: addToSelection($event, recipe.id)
"
@tap="
selectMode
? addToSelection($event, recipe.id)
: viewRecipe(recipe.id)
"
>
2021-03-23 06:16:25 +00:00
<GridLayout class="imageHolder card" rows="96" columns="96">
2021-02-28 15:10:26 +00:00
<Image
row="0"
col="0"
v-if="recipe.imageSrc"
:src="recipe.imageSrc"
stretch="aspectFill"
2021-03-23 06:16:25 +00:00
decodeWidth="96"
decodeHeight="96"
2021-02-28 15:10:26 +00:00
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"
/>
2021-03-23 06:16:25 +00:00
<Label :text="recipe.title" class="tb title" />
2021-03-21 17:02:04 +00:00
<GridLayout columns="*" rows="auto, auto">
2021-02-28 15:10:26 +00:00
<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"
2021-03-21 17:02:04 +00:00
row="1"
2021-02-28 15:10:26 +00:00
>
<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>
2021-03-21 17:02:04 +00:00
<v-template name="simple">
<GridLayout class="recipeContainer" :class="getItemPos(recipe.id)">
2021-02-28 15:10:26 +00:00
<GridLayout
2021-03-21 17:02:04 +00:00
class="recipeItem layout1 layout2 mdr"
rows="auto"
2021-02-28 15:10:26 +00:00
columns="*"
ref="recipe"
@longPress="
selectMode
? viewRecipe(recipe.id)
: addToSelection($event, recipe.id)
"
@tap="
selectMode
? addToSelection($event, recipe.id)
: viewRecipe(recipe.id)
"
>
2021-03-21 17:02:04 +00:00
<StackLayout class="recipeInfo">
2021-02-28 15:10:26 +00:00
<Label
:text="`${$options.filters.L(
recipe.cuisine
)} ${$options.filters.L(recipe.category)}`"
class="category"
/>
2021-03-23 06:16:25 +00:00
<Label :text="recipe.title" class="tb title" />
2021-02-28 15:10:26 +00:00
<StackLayout
class="tagsContainer"
2021-03-21 17:02:04 +00:00
v-if="recipe.tags.length"
2021-02-28 15:10:26 +00:00
orientation="horizontal"
>
<Label
v-for="(tag, index) in recipe.tags"
:key="index"
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>
2021-03-21 17:02:04 +00:00
<v-template name="grid">
<GridLayout class="recipeContainer" :class="getItemPos(recipe.id)">
2021-02-28 15:10:26 +00:00
<GridLayout
2021-03-21 17:02:04 +00:00
class="recipeItem layout3 mdr"
rows="auto, auto"
2021-02-28 15:10:26 +00:00
columns="*"
ref="recipe"
@longPress="
selectMode
? viewRecipe(recipe.id)
: addToSelection($event, recipe.id)
"
@tap="
selectMode
? addToSelection($event, recipe.id)
: viewRecipe(recipe.id)
"
>
2021-03-21 17:02:04 +00:00
<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">
2021-02-28 15:10:26 +00:00
<Label
:text="`${$options.filters.L(
recipe.cuisine
)} ${$options.filters.L(recipe.category)}`"
class="category"
/>
2021-03-23 06:16:25 +00:00
<Label :text="recipe.title" class="tb title" />
2021-03-21 17:02:04 +00:00
<StackLayout
class="tagsContainer"
orientation="horizontal"
row="2"
>
2021-02-28 15:10:26 +00:00
<Label
v-for="(tag, index) in recipe.tags"
:key="index"
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 18:49:36 +00:00
</v-template>
</CollectionView>
2021-03-23 09:59:00 +00:00
<GridLayout
rowSpan="2"
rows="*, auto, *, 88"
columns="*"
class="emptyStateContainer"
>
2021-02-28 15:10:26 +00:00
<StackLayout
row="1"
class="emptyState"
v-if="!recipes.length && !filterFavourites && !filterTrylater"
@tap="addRecipe"
>
<Label class="er icon" :text="icon.plusc" />
2021-03-23 06:16:25 +00:00
<Label class="title tb" :text="'strAdd' | L" textWrap="true" />
2021-02-28 15:10:26 +00:00
<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" />
2021-03-23 06:16:25 +00:00
<Label class="title tb" :text="'aD' | L" textWrap="true" />
2021-02-28 15:10:26 +00:00
<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" />
2021-03-23 06:16:25 +00:00
<Label class="title tb" :text="'noFavs' | L" textWrap="true" />
2021-02-28 15:10:26 +00:00
<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
2021-03-23 06:16:25 +00:00
class="title tb"
2021-02-28 15:10:26 +00:00
:text="`${noResultFor}` | L"
textWrap="true"
/>
<MDButton
v-if="filterFavourites || filterTrylater || selectedCuisine"
variant="text"
2021-03-23 06:16:25 +00:00
class="searchAll tb"
2021-02-28 15:10:26 +00:00
:text="'trySer' | L"
@tap="searchAll"
/>
</StackLayout>
</GridLayout>
2021-03-23 09:59:00 +00:00
<GridLayout
row="1"
class="appbar"
rows="auto"
columns="auto, *, auto, auto, auto"
>
<MDButton
class="er menu"
variant="text"
@tap="
showSearch
? closeSearch()
: selectMode
? clearSelection()
: showDrawer()
"
:text="showSearch ? icon.back : selectMode ? icon.x : icon.menu"
/>
<TextField
class="searchBar"
@loaded="focusField"
v-if="showSearch"
col="1"
colSpan="4"
:hint="'ser' | L"
@textChange="updateList($event.value)"
/>
<Label
v-if="selectMode"
class="title tb"
:text="`${selection.length} ${$options.filters.L('sltd')}`"
col="1"
/>
<MDButton
v-if="recipes.length && !selectMode && !showSearch"
class="er"
:text="selectMode ? icon.export : icon.sear"
variant="text"
col="2"
@tap="selectMode ? exportSelection() : openSearch()"
/>
<MDButton
v-if="recipes.length && !showSearch && !selectMode"
class="er"
:text="icon.sort"
variant="text"
col="3"
@tap="sortDialog()"
/>
2021-02-28 15:10:26 +00:00
<transition name="bounce">
2021-03-23 09:59:00 +00:00
<MDButton
2021-02-28 15:10:26 +00:00
v-if="showFAB"
2021-03-23 09:59:00 +00:00
class="er fab"
:text="icon.plus"
variant="text"
col="4"
@tap="showFAB && addRecipe()"
2021-02-28 15:10:26 +00:00
/>
</transition>
2021-03-23 09:59:00 +00:00
<MDButton
v-if="selectMode"
class="er"
:text="icon.del"
variant="text"
col="4"
@tap="deleteSelection()"
/>
2021-02-28 15:10:26 +00:00
</GridLayout>
2021-03-23 09:59:00 +00:00
</GridLayout>
2021-02-28 15:10:26 +00:00
</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-03-23 06:16:25 +00:00
} from "@triniwiz/nativescript-accelerometer";
2021-02-28 15:10:26 +00:00
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,
2021-03-23 09:59:00 +00:00
searchBar: null,
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,
};
2020-09-15 11:10:16 +00:00
},
computed: {
2021-02-28 15:10:26 +00:00
...mapState([
"sortType",
"icon",
"recipes",
"currentComponent",
"shakeEnabled",
2021-03-21 17:02:04 +00:00
"layout",
2021-02-28 15:10:26 +00:00
]),
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-03-23 06:16:25 +00:00
return Screen.mainScreen.widthDIPs / 2 - 16;
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
getLayout() {
2021-03-21 17:02:04 +00:00
return this.layout;
2021-01-23 17:20:15 +00:00
},
// HELPERS
2020-10-21 17:54:45 +00:00
openSearch() {
this.showFAB = false;
2021-03-23 09:59:00 +00:00
this.showSearch = true;
this.hijackLocalBackEvent();
2020-10-21 17:54:45 +00:00
},
2021-03-23 09:59:00 +00:00
focusField(args) {
setTimeout((e) => {
args.object.focus();
setTimeout((e) => Utils.ad.showSoftInput(args.object.android), 100);
}, 100);
},
2020-11-10 18:28:48 +00:00
closeSearch() {
2021-03-23 09:59:00 +00:00
this.showFAB = true;
this.searchQuery = "";
Utils.ad.dismissSoftInput();
this.showSearch = false;
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
},
2021-02-28 15:10:26 +00:00
updateList(value) {
clearTimeout(typingTimer);
typingTimer = setTimeout((e) => {
2021-03-23 09:59:00 +00:00
this.searchQuery = value.toLowerCase();
2021-02-28 15:10:26 +00:00
}, 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-03-21 17:02:04 +00:00
getItemPos(id) {
2021-02-28 15:10:26 +00:00
let length = this.filteredRecipes.length;
2021-03-21 17:02:04 +00:00
let l2 = this.layout == "grid";
let itemPos =
id == this.filteredRecipes[0].id ||
(length > 1 && l2 && id == this.filteredRecipes[1].id)
? "firstItem"
: id == this.filteredRecipes[length - 1].id ||
(length > 1 &&
l2 &&
this.oddOrEven(id) == " odd" &&
id == this.filteredRecipes[length - 2].id)
? "lastItem"
: "";
return l2 ? itemPos + this.oddOrEven(id) : itemPos;
},
oddOrEven(id) {
return this.filteredRecipes.map((e) => e.id).indexOf(id) % 2 === 0
? " odd"
: " even";
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: {
2021-03-21 17:02:04 +00:00
filterTrylater: true,
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",
2021-03-21 17:02:04 +00:00
count: 8,
2021-02-28 15:10:26 +00:00
},
}).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;
2021-02-28 18:49:36 +00:00
let classes = item.className;
if (classes.includes("selected")) {
item.className = classes.replace(/selected/g, "");
2021-02-28 15:10:26 +00:00
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 18:49:36 +00:00
item.className = classes + " selected";
2021-02-28 15:10:26 +00:00
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-03-23 09:59:00 +00:00
this.showFAB = true;
2021-02-28 15:10:26 +00:00
this.selectMode = false;
this.$emit("selectModeOn", true);
this.selection = [];
2021-02-28 18:49:36 +00:00
this.recipeList.forEach((e) => {
e.className = e.className.replace(/selected/g, "");
});
2021-02-28 15:10:26 +00:00
this.releaseLocalBackEvent();
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",
2021-03-21 17:02:04 +00:00
iconColor: "#c92a2a",
2021-02-28 15:10:26 +00:00
},
}).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",
2021-03-21 17:02:04 +00:00
iconColor: "#c92a2a",
2021-02-28 15:10:26 +00:00
},
}).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>