enrecipes/app/components/ViewRecipe.vue

505 lines
15 KiB
Vue
Raw Normal View History

2020-10-14 19:32:32 +00:00
<template>
2020-10-22 18:36:50 +00:00
<Page @loaded="initializePage">
<ActionBar height="152" margin="0" flat="true">
2020-10-14 19:32:32 +00:00
<GridLayout
rows="24, 64, 64"
2020-10-21 17:54:45 +00:00
columns="auto, *, auto,auto, auto"
2020-10-14 19:32:32 +00:00
class="actionBarContainer"
>
<Label
row="1"
2020-10-14 19:32:32 +00:00
col="0"
class="bx leftAction"
:text="icon.back"
automationText="Back"
2020-10-21 17:54:45 +00:00
@tap="$navigateBack()"
2020-10-14 19:32:32 +00:00
/>
2020-10-21 17:54:45 +00:00
<ScrollView
row="2"
2020-10-14 19:32:32 +00:00
col="1"
2020-10-21 17:54:45 +00:00
colSpan="3"
orientation="horizontal"
scrollBarIndicatorVisible="false"
>
<Label
class="title orkm"
:text="recipe.title"
verticalAlignment="bottom"
/>
</ScrollView>
2020-10-26 20:49:54 +00:00
<Label
row="1"
2020-10-14 19:32:32 +00:00
col="3"
class="bx"
:text="recipe.isFavorite ? icon.heart : icon.heartOutline"
@tap="toggleFavorite"
2020-10-21 17:54:45 +00:00
/>
<Label
v-if="!filterTrylater"
row="1"
2020-10-21 17:54:45 +00:00
col="4"
class="bx"
:text="recipe.tried ? icon.trylaterOutline : icon.trylater"
@tap="toggleTrylater"
/>
<Label
v-if="!busy"
row="1"
col="2"
class="bx"
:text="icon.edit"
@tap="editRecipe"
2020-10-14 19:32:32 +00:00
/>
<ActivityIndicator v-else row="1" col="4" :busy="busy" />
2020-10-14 19:32:32 +00:00
</GridLayout>
</ActionBar>
<AbsoluteLayout>
<TabView androidElevation="0" width="100%" height="100%">
<TabViewItem title="Overview">
<ScrollView scrollBarIndicatorVisible="false">
2020-10-21 17:54:45 +00:00
<StackLayout>
2020-10-14 19:32:32 +00:00
<StackLayout
width="100%"
:height="screenWidth"
verticalAlignment="center"
class="view-imageHolder"
>
<Image
v-if="recipe.imageSrc"
:src="recipe.imageSrc"
stretch="aspectFill"
width="100%"
:height="screenWidth"
/>
<Label
v-else
horizontalAlignment="center"
class="bx"
fontSize="160"
2020-10-21 17:54:45 +00:00
:text="icon.image"
2020-10-14 19:32:32 +00:00
/>
</StackLayout>
2020-10-21 17:54:45 +00:00
<StackLayout margin="16 16 144">
2020-10-14 19:32:32 +00:00
<Label class="view-cat orkm" :text="recipe.category" />
<Label
2020-10-21 17:54:45 +00:00
class="view-title orkm"
2020-10-14 19:32:32 +00:00
:text="recipe.title"
textWrap="true"
/>
<Label
class="view-other"
:text="`Preparation time: ${getTime(recipe.prepTime)}`"
/>
<Label
class="view-other"
:text="`Cooking time: ${getTime(recipe.cookTime)}`"
/>
</StackLayout>
</StackLayout>
</ScrollView>
</TabViewItem>
<TabViewItem title="Ingredients">
2020-10-14 19:32:32 +00:00
<ScrollView scrollBarIndicatorVisible="false">
<Label
v-if="!recipe.ingredients.length"
class="noResults"
text="Click the edit button to add ingredients to this recipe"
textWrap="true"
/>
<StackLayout v-else padding="16 16 124">
2020-10-14 19:32:32 +00:00
<AbsoluteLayout class="inputField">
<TextField
width="165"
2020-10-14 19:32:32 +00:00
v-model="portionScale"
keyboardType="number"
/>
<Label top="0" class="fieldLabel" text="Set portion size" />
</AbsoluteLayout>
<StackLayout margin="24 0 8 0">
<Label
2020-10-21 17:54:45 +00:00
class="view-title orkm"
2020-10-14 19:32:32 +00:00
:text="
`Ingredients for ${portionScale}${
portionScale > 1
? ' portions'
: portionScale == 0
? '1 portion'
: ' portion'
}`
"
textWrap="true"
/>
<Label
class="view-ingredient"
v-for="(item, index) in recipe.ingredients"
:key="index"
:text="
`${roundedQuantity(item.quantity)}${
item.unit ? ' ' + item.unit : ''
} ${item.item}`
"
textWrap="true"
/>
</StackLayout>
</StackLayout>
</ScrollView>
</TabViewItem>
<TabViewItem title="Instructions">
2020-10-14 19:32:32 +00:00
<ScrollView scrollBarIndicatorVisible="false">
<Label
v-if="!recipe.instructions.length"
class="noResults"
text="Click the edit button to add instructions to this recipe"
textWrap="true"
/>
<StackLayout v-else padding="32 16 132">
2020-10-14 19:32:32 +00:00
<GridLayout
columns="auto ,*"
v-for="(instruction, index) in recipe.instructions"
:key="index"
>
<Label
col="0"
colSpan="2"
class="view-instruction"
:class="{
instructionWOBorder:
index === recipe.instructions.length - 1,
}"
:text="instruction"
textWrap="true"
/>
<Label
verticalAlignment="top"
horizontalAlignment="center"
class="view-count orkb"
col="0"
:text="index + 1"
/>
</GridLayout>
</StackLayout>
</ScrollView>
</TabViewItem>
<TabViewItem title="Notes">
2020-10-14 19:32:32 +00:00
<ScrollView scrollBarIndicatorVisible="false">
<Label
v-if="!recipe.notes.length"
class="noResults"
text="Click the edit button to add notes to this recipe"
textWrap="true"
/>
<StackLayout v-else padding="32 16 132">
2020-10-14 19:32:32 +00:00
<GridLayout
columns="auto ,*"
v-for="(note, index) in recipe.notes"
:key="index"
>
<Label
col="0"
colSpan="2"
class="view-note"
:text="note"
textWrap="true"
/>
<Label
verticalAlignment="top"
horizontalAlignment="center"
class="view-count note orkb"
2020-10-14 19:32:32 +00:00
col="0"
:text="index + 1"
/>
</GridLayout>
</StackLayout>
</ScrollView>
</TabViewItem>
<TabViewItem title="References">
2020-10-14 19:32:32 +00:00
<ScrollView scrollBarIndicatorVisible="false">
<Label
v-if="!recipe.references.length"
class="noResults"
text="Click the edit button to add references to this recipe"
textWrap="true"
/>
<StackLayout v-else padding="10 0 132">
<StackLayout
2020-10-14 19:32:32 +00:00
v-for="(reference, index) in recipe.references"
:key="index"
>
<GridLayout
v-if="isValidURL(reference)"
columns="*, auto"
class="view-reference-container"
androidElevation="1"
>
<Label
col="0"
verticalAlignment="center"
class="view-reference"
:text="reference"
textWrap="false"
@tap="openURL($event, reference)"
/>
<Label
col="1"
class="view-copyReference bx"
:text="icon.copy"
@tap="copyURL($event, reference)"
/>
</GridLayout>
2020-10-14 19:32:32 +00:00
<Label
v-else
class="view-reference-text"
2020-10-14 19:32:32 +00:00
:text="reference"
textWrap="true"
/>
</StackLayout>
2020-10-14 19:32:32 +00:00
</StackLayout>
</ScrollView>
</TabViewItem>
</TabView>
<GridLayout id="btnFabContainer" rows="*,auto" columns="*,auto">
2020-10-14 19:32:32 +00:00
<Label
row="1"
col="1"
2020-10-22 18:36:50 +00:00
class="bx fab-button"
:text="icon.unchecked"
@tap="recipeTried"
v-if="filterTrylater"
2020-10-14 19:32:32 +00:00
/>
<transition name="dolly" appear>
<Label
row="1"
col="1"
class="bx fab-button"
:text="icon.share"
@tap="shareRecipe"
v-if="!filterTrylater && showFab"
/>
</transition>
2020-10-14 19:32:32 +00:00
</GridLayout>
</AbsoluteLayout>
</Page>
</template>
<script>
import { Screen, Utils, ImageSource, Device, File } from "@nativescript/core"
import { Feedback, FeedbackType, FeedbackPosition } from "nativescript-feedback"
import * as application from "tns-core-modules/application"
2020-10-21 17:54:45 +00:00
import * as Toast from "nativescript-toast"
import * as SocialShare from "nativescript-social-share-ns-7"
import { setText } from "nativescript-clipboard"
2020-10-21 17:54:45 +00:00
2020-10-14 19:32:32 +00:00
import { mapState, mapActions } from "vuex"
2020-10-22 18:36:50 +00:00
import EditRecipe from "./EditRecipe.vue"
2020-10-14 19:32:32 +00:00
export default {
2020-10-26 20:49:54 +00:00
props: [
"filterTrylater",
2020-10-26 20:49:54 +00:00
"recipeIndex",
"recipeID",
"hijackGlobalBackEvent",
"releaseGlobalBackEvent",
],
2020-10-14 19:32:32 +00:00
data() {
return {
2020-10-21 17:54:45 +00:00
busy: false,
2020-10-14 19:32:32 +00:00
portionScale: 1,
recipe: null,
showFab: false,
2020-10-14 19:32:32 +00:00
}
},
computed: {
2020-10-26 20:49:54 +00:00
...mapState(["icon", "recipes"]),
2020-10-14 19:32:32 +00:00
screenWidth() {
2020-10-22 18:36:50 +00:00
return Screen.mainScreen.widthDIPs
2020-10-14 19:32:32 +00:00
},
isPortionScalePositive() {
return this.portionScale && !isNaN(this.portionScale)
? parseFloat(this.portionScale)
: 1
},
},
methods: {
2020-10-26 20:49:54 +00:00
...mapActions(["toggleStateAction", "setCurrentComponentAction"]),
initializePage() {
this.releaseGlobalBackEvent()
this.busy = false
setTimeout((e) => {
this.setCurrentComponentAction("ViewRecipe")
}, 500)
2020-10-26 20:49:54 +00:00
this.portionScale = this.recipe.portionSize
this.showFab = true
this.showInfo()
},
niceDates(time) {
let date = new Date(time)
let diff = (new Date().getTime() - date.getTime()) / 1000
console.log(diff)
let dayDiff = Math.ceil(diff / 86400)
console.log(dayDiff)
if (isNaN(dayDiff) || dayDiff < 0) return ""
return (
(dayDiff == 0 &&
((diff < 60 && "just now") ||
(diff < 120 && "1 minute ago") ||
(diff < 3600 && Math.floor(diff / 60) + " minutes ago") ||
(diff < 7200 && "1 hour ago") ||
(diff < 86400 && Math.floor(diff / 3600) + " hours ago"))) ||
(dayDiff == 1 && "yesterday") ||
(dayDiff < 7 && dayDiff + " days ago") ||
(dayDiff < 31 && Math.ceil(dayDiff / 7) + " week(s) ago") ||
(dayDiff > 30 &&
dayDiff < 365 &&
Math.round(dayDiff / 30) + " month(s) ago") ||
(dayDiff > 364 && Math.round(dayDiff / 365) + " year(s) ago")
)
},
showInfo() {
let feedback = new Feedback()
feedback.show({
type: FeedbackType.Info,
message: `You tried this recipe ${this.niceDates(
this.recipe.triedOn
)}!`,
})
},
highlight(args) {
let temp = args.object.className
args.object.className = `${temp} option-highlight`
setTimeout(() => (args.object.className = temp), 100)
},
2020-10-26 20:49:54 +00:00
roundedQuantity(quantity) {
return (
Math.round(
(quantity / this.recipe.portionSize) *
this.isPortionScalePositive *
100
) / 100
)
2020-10-14 19:32:32 +00:00
},
editRecipe() {
this.showFab = false
2020-10-21 17:54:45 +00:00
this.busy = true
this.$navigateTo(EditRecipe, {
// transition: {
// name: "slide",
// duration: 250,
// curve: "easeOut",
// },
2020-10-21 17:54:45 +00:00
props: {
2020-10-26 20:49:54 +00:00
recipeIndex: this.recipeIndex,
recipeID: this.recipeID,
2020-10-21 17:54:45 +00:00
},
// backstackVisible: false,
})
2020-10-14 19:32:32 +00:00
},
shareRecipe() {
let overview = `${
this.recipe.title
} Recipe\n\nPreparation time: ${this.getTime(
this.recipe.prepTime
)}\nCooking time: ${this.getTime(this.recipe.cookTime)}\n`
let shareContent = overview
if (this.recipe.ingredients.length) {
let ingredients = `\n\nIngredients for ${this.recipe.portionSize} ${
this.recipe.portionSize === 1 ? "postion:" : "portions:"
}\n\n`
this.recipe.ingredients.forEach((e) => {
ingredients += `- ${this.roundedQuantity(e.quantity)} ${e.unit} ${
e.item
}\n`
})
shareContent += ingredients
}
if (this.recipe.instructions.length) {
let instructions = `\n\nInstructions:\n\n`
this.recipe.instructions.forEach((e, i) => {
instructions += `${i + 1}. ${e}\n\n`
})
shareContent += instructions
}
if (this.recipe.notes.length) {
let notes = `\nNotes:\n\n`
this.recipe.notes.forEach((e, i) => {
notes += `${i + 1}. ${e}\n\n`
})
shareContent += notes
}
if (this.recipe.references.length) {
let references = `\nReferences:\n\n`
this.recipe.references.forEach((e, i) => {
references += `${e}\n\n`
})
shareContent += references
}
let sharenote =
"\nCreated and shared via EnRecipes.\n\nDownload the app on f-droid:\nhttps://www.vishnuraghav.com/"
shareContent += sharenote
SocialShare.shareText(
shareContent,
"How would you like to share this recipe?"
)
},
toggle(key) {
2020-10-26 20:49:54 +00:00
this.toggleStateAction({
index: this.recipeIndex,
id: this.recipeID,
recipe: this.recipe,
key,
})
},
2020-10-14 19:32:32 +00:00
toggleFavorite() {
2020-10-21 17:54:45 +00:00
this.recipe.isFavorite
? Toast.makeText("Removed from Favorites").show()
: Toast.makeText("Added to Favorites").show()
this.toggle("isFavorite")
2020-10-21 17:54:45 +00:00
},
toggleTrylater() {
2020-10-21 17:54:45 +00:00
this.recipe.tried
? this.filterTrylater
? Toast.makeText("Added back to Try later").show()
: Toast.makeText("Added to Try later").show()
: this.filterTrylater
? Toast.makeText("You tried this recipe").show()
: Toast.makeText("Removed from Try later").show()
// : Toast.makeText("You tried this recipe").show()
this.toggle("tried")
},
recipeTried() {
this.toggle("tried")
this.$navigateBack()
2020-10-14 19:32:32 +00:00
},
getTime(time) {
let t = time.split(":")
let h = t[0]
let m = t[1]
return h !== "00" ? `${h}h ${m}m` : `${m}m`
},
isValidURL(string) {
let pattern = new RegExp("^https?|www", "ig")
return pattern.test(string)
},
2020-10-14 19:32:32 +00:00
openURL(args, url) {
// this.highlight(args)
2020-10-22 18:36:50 +00:00
Utils.openUrl(url)
2020-10-14 19:32:32 +00:00
},
copyURL(args, url) {
setText(url).then((e) => {
Toast.makeText("URL Copied").show()
})
},
},
created() {
2020-10-26 20:49:54 +00:00
this.recipe = this.recipes.filter((e) => e.id === this.recipeID)[0]
2020-10-14 19:32:32 +00:00
},
mounted() {
this.showFab = true
},
2020-10-14 19:32:32 +00:00
}
</script>