enrecipes/app/components/App.vue

275 lines
9 KiB
Vue
Raw Normal View History

2020-09-15 11:10:16 +00:00
<template>
<Page @loaded="onPageLoad" actionBarHidden="true" :androidStatusBarBackground="appTheme == 'Light' ? '#f1f3f5' : '#212529'">
<RadSideDrawer allowEdgeSwipe="true" showOverNavigation="true" ref="drawer" id="sideDrawer" drawerContentSize="270" gesturesEnabled="true" drawerTransition="SlideInOnTopTransition">
<GridLayout rows="*, auto" columns="*" ~drawerContent class="sd">
<StackLayout row="0">
<GridLayout rows="48" columns="auto, 24, *" v-for="(item, index) in topmenu" :key="index" class="sd-item orkm" :class="{
2020-10-21 17:54:45 +00:00
'selected-sd-item': currentComponent === item.component,
}">
<MDRipple row="0" colSpan="3" @tap="navigateTo(item.component, item.component, false, false)" />
<Label col="0" row="0" class="bx" :text="icon[item.icon]" />
<Label col="2" row="0" :text="`${item.title}` | L" />
</GridLayout>
<StackLayout class="hr" margin="8"></StackLayout>
<GridLayout rows="48" columns="auto, 24, *" class="sd-item orkm" :class="{
2020-11-15 21:13:06 +00:00
'selected-sd-item': currentComponent === 'MealPlanner',
}">
<MDRipple row="0" colSpan="3" @tap="navigateTo(MealPlanner, 'MealPlanner', true, false)" />
2020-11-23 09:49:58 +00:00
<Label col="0" row="0" class="bx" :text="icon.calendar" />
<Label col="2" row="0" :text="'Meal Planner' | L" />
</GridLayout>
<StackLayout class="hr" margin="8"></StackLayout>
<GridLayout class="sd-group-header orkm" rows="auto" columns="*, auto" v-if="categoriesWithRecipes.length">
<Label verticalAlignment="center" col="0" :text="'Categories' | L" />
<MDButton variant="text" @tap="toggleCatEdit" col="2" :text="`${editCategory ? 'DONE' : 'RENAME'}` | L" />
</GridLayout>
<ScrollView height="100%">
<StackLayout>
<GridLayout v-for="(item, index) in categoriesWithRecipes" :key="index" class="sd-item orkm" :class="{
2020-10-21 17:54:45 +00:00
'selected-sd-item': currentComponent == item,
}" columns="auto, *, auto">
<MDRipple row="0" colSpan="3" @tap="navigateTo(item, item, false, true)" />
<Label col="0" class="bx" :text="icon.label" margin="0 24 0 0" />
<Label col="1" :text="`${item}` | L" />
<MDButton variant="text" v-if="editCategory" @tap="renameCategory(item)" col="2" class="bx" :text="icon.edit" />
</GridLayout>
</StackLayout>
</ScrollView>
</StackLayout>
<StackLayout row="1">
<StackLayout class="hr" margin="0 8 8"></StackLayout>
<GridLayout class="sd-item orkm" :class="{
2020-10-14 19:32:32 +00:00
'selected-sd-item': currentComponent == item.title,
}" v-for="(item, index) in bottommenu" :key="index" rows="48" columns="auto, 24, *">
<MDRipple colSpan="3" @tap="navigateTo(item.component, 'item.title', true, false)" />
<Label class="bx" col="0" :text="icon[item.icon]" />
<Label col="2" :text="`${item.title}` | L" />
</GridLayout>
</StackLayout>
</GridLayout>
<Frame ~mainContent id="main-frame">
<EnRecipes ref="enrecipes" :filterFavourites="filterFavourites" :filterTrylater="filterTrylater" :selectedCategory="selectedCategory" :closeDrawer="closeDrawer" :hijackGlobalBackEvent="hijackGlobalBackEvent"
:releaseGlobalBackEvent="releaseGlobalBackEvent" />
</Frame>
</RadSideDrawer>
</Page>
2020-09-15 11:10:16 +00:00
</template>
<script>
2020-10-22 18:36:50 +00:00
import {
ApplicationSettings,
AndroidApplication,
2020-11-06 09:07:41 +00:00
Application,
Device
}
from "@nativescript/core"
2020-10-21 17:54:45 +00:00
import Theme from "@nativescript/theme"
import * as Toast from "nativescript-toast"
2020-10-22 18:36:50 +00:00
import * as application from "tns-core-modules/application"
import {
mapActions,
mapState
}
from "vuex"
2020-11-10 18:28:48 +00:00
2020-11-23 09:49:58 +00:00
import EnRecipes from "./EnRecipes"
import MealPlanner from "./MealPlanner"
import Settings from "./Settings"
import About from "./About"
2020-11-10 18:28:48 +00:00
2020-11-23 09:49:58 +00:00
import PromptDialog from "./modal/PromptDialog"
2020-09-15 11:10:16 +00:00
export default {
data() {
return {
selectedCategory: null,
filterFavourites: false,
filterTrylater: false,
2020-11-23 09:49:58 +00:00
MealPlanner: MealPlanner,
topmenu: [ {
2020-11-28 19:21:57 +00:00
title: "EnRecipes",
2020-10-21 17:54:45 +00:00
component: "EnRecipes",
icon: "home",
},
{
title: "Try Later",
component: "Try Later",
icon: "trylater",
},
2020-11-28 19:21:57 +00:00
{
title: "Favourites",
component: "Favourites",
2020-11-28 19:21:57 +00:00
icon: "heart",
},
2020-10-21 17:54:45 +00:00
],
bottommenu: [ {
2020-09-15 11:10:16 +00:00
title: "Settings",
2020-10-14 19:32:32 +00:00
component: Settings,
icon: "cog",
2020-09-15 11:10:16 +00:00
},
{
title: "About",
2020-10-14 19:32:32 +00:00
component: About,
icon: "info",
2020-09-15 11:10:16 +00:00
},
],
2020-11-10 18:28:48 +00:00
editCategory: false,
appTheme: "Light",
2020-09-15 11:10:16 +00:00
}
},
2020-11-23 09:49:58 +00:00
components: {
EnRecipes,
MealPlanner,
},
2020-09-15 11:10:16 +00:00
computed: {
...mapState( [
2020-11-02 11:36:53 +00:00
"icon",
"recipes",
"categories",
"yieldUnits",
2020-11-23 09:49:58 +00:00
"mealPlans",
2020-11-02 11:36:53 +00:00
"currentComponent",
] ),
categoriesWithRecipes() {
let arr = this.recipes.map( ( e ) => {
2020-09-15 11:10:16 +00:00
return e.category
} )
return [ ...new Set( arr ) ]
2020-09-15 11:10:16 +00:00
},
},
methods: {
...mapActions( [
2020-10-26 20:49:54 +00:00
"setCurrentComponentAction",
"initializeRecipes",
"initializeCategories",
2020-11-02 11:36:53 +00:00
"initializeYieldUnits",
2020-11-23 09:49:58 +00:00
"initializeMealPlans",
2020-10-26 20:49:54 +00:00
"renameCategoryAction",
] ),
2020-11-10 18:28:48 +00:00
onPageLoad() {
if ( this.appTheme === "Light" ) {
const View = android.view.View
const window = Application.android.startActivity.getWindow()
const decorView = window.getDecorView()
decorView.setSystemUiVisibility( View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR )
}
},
2020-11-15 21:13:06 +00:00
2020-11-10 18:28:48 +00:00
// HELPERS
2020-10-21 17:54:45 +00:00
toggleCatEdit() {
2020-11-10 18:28:48 +00:00
this.editCategory = !this.editCategory
if ( this.selectedCategory ) this.setCurrentComponentAction( "EnRecipes" )
this.filterFavourites = this.filterTrylater = false
2020-10-21 17:54:45 +00:00
this.selectedCategory = null
this.$refs.enrecipes.updateFilter()
},
renameCategory( category ) {
2020-10-21 17:54:45 +00:00
this.releaseGlobalBackEvent()
this.$showModal( PromptDialog, {
2020-10-21 17:54:45 +00:00
props: {
title: `Rename category`,
2020-11-10 18:28:48 +00:00
text: category,
2020-10-21 17:54:45 +00:00
action: "RENAME",
},
} ).then( ( newCategory ) => {
2020-10-21 17:54:45 +00:00
this.hijackGlobalBackEvent()
if ( newCategory.length ) {
this.renameCategoryAction( {
current: category,
updated: newCategory
} )
2020-11-10 18:28:48 +00:00
this.editCategory = false
this.navigateTo( newCategory, newCategory, false, true )
2020-10-21 17:54:45 +00:00
}
} )
2020-10-21 17:54:45 +00:00
},
setSelectedCategory( e ) {
2020-09-15 11:10:16 +00:00
this.selectedCategory = e.item
this.closeDrawer()
},
2020-11-23 09:49:58 +00:00
2020-11-10 18:28:48 +00:00
closeDrawer() {
this.$refs.drawer.nativeView.closeDrawer()
},
// NAVIGATION HANDLERS
2020-10-21 17:54:45 +00:00
hijackGlobalBackEvent() {
2020-10-22 18:36:50 +00:00
AndroidApplication.on(
AndroidApplication.activityBackPressedEvent,
2020-10-21 17:54:45 +00:00
this.globalBackEvent
)
},
releaseGlobalBackEvent() {
2020-10-22 18:36:50 +00:00
AndroidApplication.off(
AndroidApplication.activityBackPressedEvent,
2020-10-21 17:54:45 +00:00
this.globalBackEvent
2020-09-15 11:10:16 +00:00
)
},
globalBackEvent( args ) {
2020-10-21 17:54:45 +00:00
function preventDefault() {
args.cancel = true
}
if ( this.$refs.drawer && this.$refs.drawer.nativeView.getIsOpen() ) {
2020-10-21 17:54:45 +00:00
preventDefault()
this.closeDrawer()
2020-11-10 18:28:48 +00:00
this.editCategory = false
}
else if (
[ "Favourites", "Try Later", this.selectedCategory ].includes(
2020-10-21 17:54:45 +00:00
this.currentComponent
)
) {
preventDefault()
this.setCurrentComponentAction( "EnRecipes" )
this.filterFavourites = this.filterTrylater = false
2020-10-14 19:32:32 +00:00
this.selectedCategory = null
2020-10-21 17:54:45 +00:00
this.$refs.enrecipes.updateFilter()
this.releaseGlobalBackEvent()
2020-09-15 11:10:16 +00:00
}
},
navigateTo( to, title, isTrueComponent, isCategory ) {
if ( title !== this.currentComponent ) {
if ( isTrueComponent ) {
this.$navigateTo( to, {
frame: "main-frame",
backstackVisible: false
} )
this.closeDrawer()
}
else if ( !this.editCategory || !isCategory ) {
this.releaseGlobalBackEvent()
this.hijackGlobalBackEvent()
this.setCurrentComponentAction( to )
this.$navigateBack( {
frame: "main-frame",
backstackVisible: false
} )
this.filterFavourites = to === "Favourites" ? true : false
this.filterTrylater = to === "Try Later" ? true : false
this.selectedCategory = isCategory ? to : null
this.$refs.enrecipes.updateFilter()
this.closeDrawer()
}
this.editCategory = false
}
else {
2020-10-21 17:54:45 +00:00
this.closeDrawer()
2020-10-14 19:32:32 +00:00
}
},
2020-10-21 17:54:45 +00:00
},
created() {
this.appTheme = ApplicationSettings.getString( "appTheme", "Light" )
setTimeout( ( e ) => {
Theme.setMode( Theme[ this.appTheme ] )
}, 10 )
if ( !this.recipes.length ) this.initializeRecipes()
if ( !this.categories.length ) this.initializeCategories()
if ( !this.yieldUnits.length ) this.initializeYieldUnits()
if ( !this.mealPlans.length ) this.initializeMealPlans()
console.log( Device.language );
2020-09-15 11:10:16 +00:00
},
}
</script>