2020-09-15 11:10:16 +00:00
< template >
2020-12-07 14:45:00 +00:00
< Page @loaded ="onPageLoad" @unloaded ="onPageUnload" >
< ActionBar : androidElevation = "viewIsScrolled ? 4 : 0" >
< GridLayout v-if = "showSearch" columns="auto, *" verticalAlignment="center" >
< MDButton class = "bx" :text = "icon.back" variant = "text" automationText = "Back" col = "0" @tap ="closeSearch" / >
2021-01-13 05:02:48 +00:00
< SearchBar col = "1" : hint = "'ser' | L" v-model = "searchQuery" @textChange="callUpdateFilter" @clear="clearSearch" / >
2020-12-07 14:45:00 +00:00
< / GridLayout >
< GridLayout v -else columns = "auto, *, auto, auto" >
< MDButton class = "bx" col = "0" variant = "text" @tap ="showDrawer" :text = "icon.menu" automationText = "Back" / >
< Label class = "title orkm" : text = "`${currentComponent}` | L" col = "1" / >
< MDButton v-if = "recipes.length" class="bx" :text="icon.search" variant="text" col="2" @tap="openSearch" / >
< MDButton v-if = "recipes.length" class="bx" :text="icon.sort" variant="text" col="3" @tap="sortDialog" / >
< / GridLayout >
< / ActionBar >
< AbsoluteLayout >
2020-12-29 10:35:19 +00:00
< RadListView ref = "listView" itemHeight = "104" for = "recipe in recipes" swipeActions = "true" @itemSwipeProgressChanged ="onSwiping" @itemSwipeProgressEnded ="onSwipeEnded" @scrolled ="onScroll" :filteringFunction = "filterFunction"
2020-12-07 14:45:00 +00:00
: sortingFunction = "sortFunction" >
2020-12-29 10:35:19 +00:00
< v-template name = "header" >
< StackLayout height = "4" > < / StackLayout >
< / v-template >
2020-12-07 14:45:00 +00:00
< v-template >
2020-12-29 10:35:19 +00:00
< GridLayout class = "recipeItem" rows = "104" columns = "104, *" androidElevation = "1" >
2020-12-07 14:45:00 +00:00
< MDRipple colSpan = "2" @tap ="viewRecipe(recipe.id)" / >
2020-12-29 10:35:19 +00:00
< 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" / >
2020-12-07 14:45:00 +00:00
< Label v -else row = "0" col = "0" horizontalAlignment = "center" class = "bx" fontSize = "56" :text = "icon.image" / >
< / GridLayout >
< StackLayout class = "recipeInfo" col = "1" >
2020-12-29 10:35:19 +00:00
< Label : text = "`${$options.filters.L(recipe.cuisine)} • ${$options.filters.L(recipe.category)}`" class = "category" / >
2020-12-07 14:45:00 +00:00
< Label :text = "recipe.title" class = "orkm title" / >
2020-12-29 10:35:19 +00:00
< GridLayout columns = "*" rows = "auto, *, auto" >
< StackLayout class = "attrContainer" orientation = "horizontal" row = "0" >
< Label class = "bx small" :text = "icon.starLine" / >
< Label class = "attr" :text = "recipe.rating" / >
2021-01-13 05:02:48 +00:00
< Label class = "bx small" :text = "icon.meterLine" / >
2020-12-29 10:35:19 +00:00
< Label class = "attr" : text = "`${recipe.difficulty}` | L" / >
2021-01-13 05:02:48 +00:00
< Label class = "bx small" :text = "icon.timeLine" / >
2020-12-29 10:35:19 +00:00
< Label class = "attr" : text = "
` ${
formattedTotalTime ( recipe . prepTime , recipe . cookTime ) . time
} `
" / >
< / StackLayout >
< FlexboxLayout class = "tagsContainer" flexWrap = "wrap" row = "2" >
< Label v-for = "(tag, index) in recipe.tags" :key="index" v-if="tag && index < 2" class="tag" :text="tag" / >
< Label class = "collapsedTagsCount" v-if = "recipe.tags.length > 2" :text="recipe.tags.length - 2 +'+'" / >
< / FlexboxLayout >
< / GridLayout >
2020-12-07 14:45:00 +00:00
< / StackLayout >
< / GridLayout >
< / v-template >
< v-template name = "itemswipe" >
2020-12-29 10:35:19 +00:00
< GridLayout columns = "*, auto" padding = "0" >
2020-12-07 14:45:00 +00:00
< StackLayout id = "delete-action" col = "1" class = "swipe-item right" >
2020-12-29 10:35:19 +00:00
< Label class = "bx" padding = "8" :text = "icon.trash" / >
2020-12-07 14:45:00 +00:00
< / StackLayout >
< / GridLayout >
< / v-template >
< v-template name = "footer" >
2020-12-29 10:35:19 +00:00
< StackLayout height = "84" > < / StackLayout >
2020-12-07 14:45:00 +00:00
< / v-template >
< / RadListView >
< GridLayout rows = "*, auto, *, 88" columns = "*" class = "emptyStateContainer" >
< StackLayout row = "1" class = "emptyState" v -if = "
2020-11-10 18:28:48 +00:00
! recipes . length &&
2020-12-07 14:45:00 +00:00
! filterFavourites &&
2020-12-29 10:35:19 +00:00
! filterTrylater
2020-12-07 14:45:00 +00:00
" @tap=" addRecipe " >
< Label class = "bx icon" :text = "icon.plusCircle" / >
2021-01-13 05:02:48 +00:00
< Label class = "title orkm" : text = "'strAdd' | L" textWrap = "true" / >
2020-12-07 14:45:00 +00:00
< StackLayout orientation = "horizontal" horizontalAlignment = "center" >
2021-01-13 05:02:48 +00:00
< Label : text = "'plsAdd' | L" textWrap = "true" / >
2020-11-10 18:28:48 +00:00
< / StackLayout >
2020-12-07 14:45:00 +00:00
< / StackLayout >
< StackLayout row = "1" class = "emptyState" v-if = "!filteredRecipes.length && filterTrylater && !searchQuery" >
2020-12-29 10:35:19 +00:00
< Label class = "bx icon" :text = "icon.trylaterLine" textWrap = "true" / >
2021-01-13 05:02:48 +00:00
< Label class = "title orkm" : text = "'aD' | L" textWrap = "true" / >
< Label : text = "'tLInfo' | L" textWrap = "true" / >
2020-12-07 14:45:00 +00:00
< / StackLayout >
< StackLayout row = "1" class = "emptyState" v-if = "!filteredRecipes.length && filterFavourites && !searchQuery" >
2020-12-29 10:35:19 +00:00
< Label class = "bx icon" :text = "icon.heartLine" textWrap = "true" / >
2021-01-13 05:02:48 +00:00
< Label class = "title orkm" : text = "'noFavs' | L" textWrap = "true" / >
< Label : text = "'fsList' | L" textWrap = "true" / >
2020-12-07 14:45:00 +00:00
< / StackLayout >
< StackLayout row = "1" class = "emptyState" v-if = "!filteredRecipes.length && searchQuery" >
< Label class = "bx icon" :text = "icon.search" textWrap = "true" / >
2021-01-13 05:02:48 +00:00
< 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" / >
2020-12-07 14:45:00 +00:00
< / StackLayout >
< / GridLayout >
< GridLayout id = "btnFabContainer" rows = "*, auto" columns = "*, auto" >
< transition name = "bounce" >
< MDFloatingActionButton v-if = "showFAB" row="1" col="1" class="bx fab-button" src="res://plus" @tap="addRecipe" / >
< / transition >
< / GridLayout >
< / AbsoluteLayout >
< / Page >
2020-09-15 11:10:16 +00:00
< / template >
< script >
2020-12-07 14:45:00 +00:00
import {
2021-01-13 05:02:48 +00:00
Frame ,
2020-12-29 10:35:19 +00:00
ApplicationSettings ,
2020-12-07 14:45:00 +00:00
AndroidApplication ,
Utils ,
2020-12-29 10:35:19 +00:00
Observable ,
Device ,
2020-12-07 14:45:00 +00:00
}
from "@nativescript/core" ;
import {
localize
}
from "@nativescript/localize"
2020-12-29 10:35:19 +00:00
import {
time
} from "tns-core-modules/profiling"
import {
startAccelerometerUpdates ,
stopAccelerometerUpdates ,
} from "nativescript-accelerometer"
import {
Vibrate
} from 'nativescript-vibrate' ;
let vibrator = new Vibrate ( ) ;
2020-12-07 14:45:00 +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" ;
import * as utils from "~/shared/utils" ;
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 {
2020-12-29 10:35:19 +00:00
props : [ "filterFavourites" , "filterTrylater" , "closeDrawer" , "selectedCategory" , "selectedCuisine" , "selectedTag" , "hijackGlobalBackEvent" , "releaseGlobalBackEvent" ] ,
2020-10-14 19:32:32 +00:00
components : {
EditRecipe ,
2020-12-07 14:45:00 +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 : "" ,
viewIsScrolled : false ,
2020-10-21 17:54:45 +00:00
showSearch : false ,
2020-10-14 19:32:32 +00:00
rightAction : false ,
2020-10-24 18:02:35 +00:00
deletionDialogActive : false ,
2021-01-13 05:02:48 +00:00
showFAB : false ,
filterDone : true ,
2020-12-07 14:45:00 +00:00
} ;
2020-09-15 11:10:16 +00:00
} ,
computed : {
2020-12-29 10:35:19 +00:00
... mapState ( [ "sortType" , "icon" , "recipes" , "currentComponent" , "shakeEnabled" ] ) ,
2020-09-15 11:10:16 +00:00
filteredRecipes ( ) {
2020-12-14 13:48:53 +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-01-13 05:02:48 +00:00
let vm = this
function getIngredients ( e ) {
return e . ingredients . map ( f => f . item . toLowerCase ( ) ) . join ( ) . includes ( vm . searchQuery ) ;
}
if ( this . filterDone ) {
if ( this . filterFavourites ) {
return this . recipes . filter ( e => e . isFavorite && ( e . title . toLowerCase ( ) . includes ( this . searchQuery ) || getIngredients ( e ) ) )
} else if ( this . filterTrylater ) {
return this . recipes . filter ( e => ! e . tried && ( e . title . toLowerCase ( ) . includes ( this . searchQuery ) || getIngredients ( e ) ) )
} else if ( this . selectedCuisine ) {
return this . recipes . filter ( e => {
return this . recipeFilter ( e ) && ( e . title . toLowerCase ( ) . includes ( this . searchQuery ) || getIngredients ( e ) )
} )
} else {
return this . recipes . filter ( e => e . title . toLowerCase ( ) . includes ( this . searchQuery ) || getIngredients ( e ) )
}
2020-12-14 13:48:53 +00:00
} else {
2021-01-13 05:02:48 +00:00
return "A" ;
2020-09-15 11:10:16 +00:00
}
} ,
2020-11-28 19:21:57 +00:00
noResultFor ( ) {
2021-01-13 05:02:48 +00:00
if ( this . filterFavourites ) return "noRecsInFavs" ;
if ( this . filterTrylater ) return "noRecsInTL" ;
if ( this . selectedCuisine ) return "noRecsInFtr" ;
return "noRecs" ;
2020-12-29 10:35:19 +00:00
} ,
2020-09-15 11:10:16 +00:00
} ,
methods : {
2020-12-29 10:35:19 +00:00
... mapActions ( [ "setCurrentComponentAction" , "setSortTypeAction" , "deleteRecipeAction" ] ) ,
2020-12-07 14:45:00 +00:00
onPageLoad ( args ) {
const page = args . object ;
page . bindingContext = new Observable ( ) ;
2021-01-13 05:02:48 +00:00
this . filterFavourites ? this . setComponent ( "Favourites" ) : this . filterTrylater ? this . setComponent ( "Try Later" ) : this . selectedCuisine ? this . setComponent ( "Filtered recipes" ) : this . setComponent ( "EnRecipes" ) ;
2020-12-07 14:45:00 +00:00
this . showFAB = true ;
2020-12-29 10:35:19 +00:00
if ( this . shakeEnabled ) startAccelerometerUpdates ( data => this . onSensorData ( data ) )
2021-01-13 05:02:48 +00:00
if ( this . showSearch )
this . hijackLocalBackEvent ( )
2020-12-07 14:45:00 +00:00
} ,
onPageUnload ( ) {
2020-12-29 10:35:19 +00:00
if ( this . shakeEnabled ) stopAccelerometerUpdates ( ) ;
2020-12-07 14:45:00 +00:00
this . releaseGlobalBackEvent ( ) ;
2021-01-13 05:02:48 +00:00
this . releaseLocalBackEvent ( ) ;
2020-10-24 18:02:35 +00:00
} ,
2020-11-10 18:28:48 +00:00
// HELPERS
2020-11-23 09:49:58 +00:00
showDrawer ( ) {
2020-12-07 14:45:00 +00:00
utils . showDrawer ( ) ;
2020-11-23 09:49:58 +00:00
} ,
2020-10-21 17:54:45 +00:00
openSearch ( ) {
2020-12-07 14:45:00 +00:00
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 ( ) {
2020-12-07 14:45:00 +00:00
if ( this . searchQuery ) this . updateFilter ( ) ;
this . searchQuery = "" ;
Utils . ad . dismissSoftInput ( ) ;
this . showSearch = false ;
this . showFAB = true ;
this . releaseLocalBackEvent ( ) ;
2020-11-10 18:28:48 +00:00
} ,
2020-12-07 14:45:00 +00:00
setComponent ( comp ) {
this . setCurrentComponentAction ( comp ) ;
this . hijackGlobalBackEvent ( ) ;
2020-11-10 18:28:48 +00:00
} ,
clearSearch ( ) {
2020-12-07 14:45:00 +00:00
if ( this . searchQuery !== "" ) {
this . updateFilter ( ) ;
2020-11-10 18:28:48 +00:00
}
} ,
2020-12-07 14:45:00 +00:00
formattedTotalTime ( prepTime , cookTime ) {
let t1 = prepTime . split ( ":" ) ;
let t2 = cookTime . split ( ":" ) ;
2020-12-29 10:35:19 +00:00
let minutes = parseInt ( t1 [ 1 ] ) + parseInt ( t2 [ 1 ] )
let m = minutes % 60
let h = parseInt ( t1 [ 0 ] ) + parseInt ( t2 [ 0 ] ) + Math . floor ( minutes / 60 ) ;
2020-12-07 14:45:00 +00:00
let hr = localize ( 'hr' )
let min = localize ( 'min' )
2020-11-10 18:28:48 +00:00
return {
2020-12-07 14:45:00 +00:00
time : h ? ( m ? ` ${ h } ${ hr } ${ m } ${ min } ` : ` ${ h } ${ hr } ` ) : ` ${ m } ${ min } ` ,
duration : ` ${ h } ${ m } `
} ;
2020-11-10 18:28:48 +00:00
} ,
2020-12-07 14:45:00 +00:00
onScroll ( args ) {
this . viewIsScrolled = args . scrollOffset ? true : false ;
2020-11-10 18:28:48 +00:00
} ,
2020-12-29 10:35:19 +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
2021-01-13 05:02:48 +00:00
let allCuisines = /allCuis/ . test ( this . selectedCuisine )
2020-12-29 10:35:19 +00:00
let categoryMatched = e . category === this . selectedCategory
2021-01-13 05:02:48 +00:00
let allCategories = /allCats/ . test ( this . selectedCategory )
2020-12-29 10:35:19 +00:00
let tagMatched = e . tags . includes ( this . selectedTag )
2021-01-13 05:02:48 +00:00
let allTags = /allTs/ . test ( this . selectedTag )
2020-12-29 10:35:19 +00:00
let cuisine = cuisineMatched || allCuisines
return this . selectedTag && ! allTags ? ( categoryMatched || allCategories ) && cuisine && tagMatched : this . selectedCategory && ! allCategories ? cuisine && categoryMatched : cuisine
} ,
2021-01-13 05:02:48 +00:00
searchAll ( ) {
this . $emit ( "backToHome" )
} ,
2020-11-10 18:28:48 +00:00
// NAVIGATION HANDLERS
2020-10-21 17:54:45 +00:00
hijackLocalBackEvent ( ) {
2020-12-07 14:45:00 +00:00
this . releaseGlobalBackEvent ( ) ;
AndroidApplication . on ( AndroidApplication . activityBackPressedEvent , this . searchBackEvent ) ;
2020-10-21 17:54:45 +00:00
} ,
releaseLocalBackEvent ( ) {
2020-12-07 14:45:00 +00:00
AndroidApplication . off ( AndroidApplication . activityBackPressedEvent , this . searchBackEvent ) ;
this . hijackGlobalBackEvent ( ) ;
2020-10-21 17:54:45 +00:00
} ,
2020-12-07 14:45:00 +00:00
searchBackEvent ( args ) {
args . cancel = true ;
this . closeDrawer ( ) ;
this . closeSearch ( ) ;
2020-10-21 17:54:45 +00:00
} ,
2020-11-10 18:28:48 +00:00
addRecipe ( ) {
2020-12-07 14:45:00 +00:00
this . showFAB = false ;
this . releaseGlobalBackEvent ( ) ;
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 ,
2020-12-07 14:45:00 +00:00
}
} ) ;
2020-10-21 17:54:45 +00:00
} ,
2020-12-07 14:45:00 +00:00
viewRecipe ( recipeID ) {
this . showFAB = false ;
this . $navigateTo ( ViewRecipe , {
2020-11-10 18:28:48 +00:00
props : {
filterTrylater : this . filterTrylater ,
2020-12-07 14:45:00 +00:00
recipeID
2020-11-10 18:28:48 +00:00
} ,
2020-12-07 14:45:00 +00:00
backstackVisible : false
} ) ;
2020-11-10 18:28:48 +00:00
} ,
2020-12-29 10:35:19 +00:00
viewRandomRecipe ( ) {
this . showFAB = false ;
this . $navigateTo ( ViewRecipe , {
props : {
filterTrylater : false ,
recipeID : this . randomRecipeID ( )
} ,
backstackVisible : false
} ) ;
} ,
2020-11-10 18:28:48 +00:00
// LIST HANDLERS
2020-10-21 17:54:45 +00:00
sortDialog ( ) {
2020-12-07 14:45:00 +00:00
this . releaseGlobalBackEvent ( ) ;
this . $showModal ( ActionDialog , {
2020-10-21 17:54:45 +00:00
props : {
2021-01-13 05:02:48 +00:00
title : "srt" ,
2020-12-29 10:35:19 +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 ,
helpIcon : 'sort' ,
bgColor : '#adb5bd' ,
2020-12-07 14:45:00 +00:00
}
} ) . then ( action => {
if ( action && action !== "Cancel" && this . sortType !== action ) {
2020-12-29 10:35:19 +00:00
this . setSortTypeAction ( action )
ApplicationSettings . setString ( "sortType" , action )
2020-12-07 14:45:00 +00:00
this . updateSort ( ) ;
2020-10-21 17:54:45 +00:00
}
2020-12-07 14:45:00 +00:00
this . hijackGlobalBackEvent ( ) ;
} ) ;
2020-10-21 17:54:45 +00:00
} ,
updateSort ( ) {
2020-12-07 14:45:00 +00:00
let listView = this . $refs . listView . nativeView ;
listView . sortingFunction = undefined ;
listView . sortingFunction = this . sortFunction ;
2020-10-21 17:54:45 +00:00
} ,
2020-12-07 14:45:00 +00:00
sortFunction ( item , otherItem ) {
2020-12-29 10:35:19 +00:00
const titleOrder = item . title . toLowerCase ( ) . localeCompare ( otherItem . title . toLowerCase ( ) , Device . language , {
2020-12-07 14:45:00 +00:00
ignorePunctuation : true
} ) ;
let d1 = this . formattedTotalTime ( item . prepTime , item . cookTime ) . duration ;
let d2 = this . formattedTotalTime ( otherItem . prepTime , otherItem . cookTime ) . duration ;
let ld1 = new Date ( item . lastModified ) ;
let ld2 = new Date ( otherItem . lastModified ) ;
2020-12-29 10:35:19 +00:00
let cd1 = new Date ( item . created ) ;
let cd2 = new Date ( otherItem . created ) ;
let r1 = item . rating
let r2 = otherItem . rating
function difficultyLevel ( level ) {
switch ( level ) {
case "Easy" :
return 1 ;
case "Moderate" :
return 2 ;
case "Challenging" :
return 3 ;
}
}
let dl1 = difficultyLevel ( item . difficulty )
let dl2 = difficultyLevel ( otherItem . difficulty )
2020-12-07 14:45:00 +00:00
switch ( this . sortType ) {
2020-10-21 17:54:45 +00:00
case "Title" :
2020-12-07 14:45:00 +00:00
return titleOrder > 0 ? - 1 : titleOrder < 0 ? 1 : 0 ;
break ;
2020-12-29 10:35:19 +00:00
case "Quickest first" :
2020-12-07 14:45:00 +00:00
return d1 > d2 ? - 1 : d1 < d2 ? 1 : 0 ;
break ;
2020-12-29 10:35:19 +00:00
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" :
2020-12-07 14:45:00 +00:00
return ld1 < ld2 ? - 1 : ld1 > ld2 ? 1 : 0 ;
break ;
2020-12-29 10:35:19 +00:00
case "Newest first" :
return cd1 < cd2 ? - 1 : cd1 > cd2 ? 1 : 0 ;
break ;
case "Oldest first" :
return cd1 < cd2 ? 1 : cd1 > cd2 ? - 1 : 0 ;
2020-12-07 14:45:00 +00:00
break ;
2020-10-21 17:54:45 +00:00
}
} ,
2021-01-13 05:02:48 +00:00
callUpdateFilter ( ) {
clearTimeout ( typingTimer )
this . filterDone = false
typingTimer = setTimeout ( e => {
this . updateFilter ( )
} , 750 )
} ,
2020-10-21 17:54:45 +00:00
updateFilter ( ) {
2020-12-07 14:45:00 +00:00
let listView = this . $refs . listView . nativeView ;
setTimeout ( e => {
listView . filteringFunction = undefined ;
listView . filteringFunction = this . filterFunction ;
} , 1 ) ;
2021-01-13 05:02:48 +00:00
this . filterDone = true
2020-10-21 17:54:45 +00:00
} ,
2020-12-29 10:35:19 +00:00
filterFunction ( e ) {
2021-01-13 05:02:48 +00:00
let ingredients = e . ingredients . map ( e => e . item . toLowerCase ( ) ) . join ( ) . includes ( this . searchQuery )
2020-12-07 14:45:00 +00:00
if ( this . filterFavourites ) {
2021-01-13 05:02:48 +00:00
return e . isFavorite ? e . title . toLowerCase ( ) . includes ( this . searchQuery ) || ingredients : false ;
2020-12-14 13:48:53 +00:00
} else if ( this . filterTrylater ) {
2021-01-13 05:02:48 +00:00
return e . tried ? false : e . title . toLowerCase ( ) . includes ( this . searchQuery ) || ingredients
2020-12-29 10:35:19 +00:00
} else if ( this . selectedCuisine ) {
2021-01-13 05:02:48 +00:00
return this . recipeFilter ( e ) ? e . title . toLowerCase ( ) . includes ( this . searchQuery ) || ingredients : false ;
2020-12-14 13:48:53 +00:00
} else {
2021-01-13 05:02:48 +00:00
return e . title . toLowerCase ( ) . includes ( this . searchQuery ) || ingredients
2020-10-21 17:54:45 +00:00
}
} ,
2020-12-07 14:45:00 +00:00
onSwiping ( {
data ,
object
} ) {
const swipeLimits = data . swipeLimits ;
const swipeView = object ;
const rightItem = swipeView . getViewById ( "delete-action" ) ;
2020-12-29 10:35:19 +00:00
swipeLimits . right = rightItem . getMeasuredWidth ( ) - 8 ;
swipeLimits . threshold = swipeLimits . right - 4 ;
2020-12-07 14:45:00 +00:00
if ( data . x < - swipeLimits . threshold ) {
this . rightAction = true ;
swipeView . notifySwipeToExecuteFinished ( ) ;
2020-10-14 19:32:32 +00:00
}
} ,
2020-12-07 14:45:00 +00:00
onSwipeEnded ( {
index
} ) {
let recipeID = this . recipes [ index ] . id ;
if ( this . rightAction && ! this . deletionDialogActive ) this . deleteRecipe ( index , recipeID ) ;
this . rightAction = false ;
2020-10-14 19:32:32 +00:00
} ,
2020-11-10 18:28:48 +00:00
// DATA HANDLERS
2020-12-07 14:45:00 +00:00
deleteRecipe ( index , recipeID ) {
this . deletionDialogActive = true ;
this . $showModal ( ConfirmDialog , {
2020-10-21 17:54:45 +00:00
props : {
2021-01-13 05:02:48 +00:00
title : localize ( "conf" ) ,
description : ` ${ localize ( 'delRecInfo' ) } " ${ this . recipes [ index ] . title } " ` ,
cancelButtonText : "cBtn" ,
okButtonText : "dBtn" ,
helpIcon : 'trash' ,
bgColor : '#c92a2a' ,
2020-10-21 17:54:45 +00:00
}
2020-12-07 14:45:00 +00:00
} ) . then ( action => {
if ( action ) {
this . deleteRecipeAction ( {
index ,
id : recipeID
} ) ;
2021-01-13 05:02:48 +00:00
if ( ! this . filteredRecipes . length )
this . $emit ( 'backToHome' )
2020-12-07 14:45:00 +00:00
}
this . deletionDialogActive = false ;
} ) ;
2020-12-29 10:35:19 +00:00
} ,
// SHAKE DETECTOR
onSensorData ( {
x ,
y ,
z
} ) {
x = x . toFixed ( 2 )
y = y . toFixed ( 2 )
z = z . toFixed ( 2 )
const FORCE _THRESHOLD = 1 ;
const TIME _THRESHOLD = 150 ;
const SHAKE _TIMEOUT = 600 ;
const SHAKE _THROTTLE = 600 ;
const SHAKE _COUNT = 3 ;
const now = time ( )
if ( ( now - lastForce ) > SHAKE _TIMEOUT ) {
shakeCount = 0 ;
}
let timeDelta = now - lastTime ;
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 ) {
shakeCount ++ ;
if ( ( shakeCount >= SHAKE _COUNT ) && ( now - lastShake > SHAKE _THROTTLE ) ) {
lastShake = now ;
shakeCount = 0 ;
if ( this . filteredRecipes . length ) {
vibrator . vibrate ( 100 )
this . viewRandomRecipe ( )
}
}
lastForce = now ;
}
lastTime = now ;
}
} ,
2020-09-15 11:10:16 +00:00
} ,
2020-10-29 20:12:53 +00:00
mounted ( ) {
2020-12-07 14:45:00 +00:00
this . showFAB = true ;
}
} ;
2020-09-15 11:10:16 +00:00
< / script >