enrecipes/app/main.ts

150 lines
4.1 KiB
TypeScript
Raw Permalink Normal View History

2021-06-15 11:04:42 +00:00
import Vue from 'nativescript-vue'
import store from './store'
2021-06-05 18:08:36 +00:00
import {
Application,
AndroidApplication,
Utils,
Frame,
} from '@nativescript/core'
2021-06-18 12:52:03 +00:00
import {
setNumber,
getNumber,
getString,
} from '@nativescript/core/application-settings'
2021-06-15 11:04:42 +00:00
import { localize } from '@nativescript/localize'
2021-06-05 18:08:36 +00:00
import EnRecipes from './components/EnRecipes.vue'
import EditRecipe from './components/EditRecipe.vue'
import MealPlanner from './components/MealPlanner.vue'
import CookingTimer from './components/CookingTimer.vue'
2021-06-15 11:04:42 +00:00
// import GroceryList from './components/GroceryList.vue'
2021-06-05 18:08:36 +00:00
import * as utils from '~/shared/utils'
2021-06-15 11:04:42 +00:00
export const EvtBus = new Vue()
2021-06-05 18:08:36 +00:00
2021-06-18 12:52:03 +00:00
let renderView: any = EnRecipes
2021-06-05 18:08:36 +00:00
import CollectionView from '@nativescript-community/ui-collectionview/vue'
Vue.use(CollectionView)
2021-06-15 11:04:42 +00:00
2021-06-18 12:52:03 +00:00
import { RGridLayout, RStackLayout, RLabel } from './rtl-ui'
2021-06-15 11:04:42 +00:00
Vue.registerElement('RGridLayout', () => RGridLayout)
Vue.registerElement('RStackLayout', () => RStackLayout)
Vue.registerElement('RLabel', () => RLabel)
2021-06-05 18:08:36 +00:00
import { myMixin } from './shared/mixins'
Vue.mixin(myMixin)
2021-06-18 12:52:03 +00:00
Vue.filter('L', localize)
2021-06-05 18:08:36 +00:00
const initFrame = () => {
2021-06-18 12:52:03 +00:00
const s = store
2021-06-15 11:04:42 +00:00
// MainInit
2021-06-18 18:37:01 +00:00
s.commit('setT', getString('theme', 'sysDef'))
s.commit('initRs')
s.commit('initMPs')
s.commit('initLIs')
s.commit('initTPs')
if (!Object.keys(s.state.timerS).length) {
let hasTimerSound = getString('timerS', null)
2021-06-18 12:52:03 +00:00
s.commit(
2021-06-18 18:37:01 +00:00
'setTS',
2021-06-05 18:08:36 +00:00
hasTimerSound ? JSON.parse(hasTimerSound) : utils.getTones().defaultTone
)
}
2021-06-15 11:04:42 +00:00
// InitFrame
2021-06-05 18:08:36 +00:00
const window = Application.android.startActivity.getWindow()
const decorView = window.getDecorView()
2021-06-18 12:52:03 +00:00
utils.setBarColors(window, decorView, s.state.theme)
Frame.topmost().className = s.state.theme
2021-06-05 18:08:36 +00:00
}
const showOverLockscreen = () => {
2021-05-25 14:32:53 +00:00
let ctx = Utils.ad.getApplicationContext()
const pm = ctx.getSystemService(android.content.Context.POWER_SERVICE)
let isScreenOff = !pm.isInteractive()
if (isScreenOff) {
const window = Application.android.startActivity.getWindow()
const windowMgr = android.view.WindowManager
const flags =
2021-05-25 14:32:53 +00:00
windowMgr.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
windowMgr.LayoutParams.FLAG_TURN_SCREEN_ON |
windowMgr.LayoutParams.FLAG_KEEP_SCREEN_ON
window.addFlags(flags)
2021-06-05 18:08:36 +00:00
}
}
const intentListener = ({ intent, android }: any) => {
let action = ((intent || android).getStringExtra('action') ||
(android && android.getAction())) as string
if (action) {
switch (action) {
case 'new_recipe':
renderView = EditRecipe
break
case 'planner':
renderView = MealPlanner
break
case 'timer':
renderView = CookingTimer
2021-06-18 12:52:03 +00:00
switch (getNumber('isTimer', 0)) {
2021-06-15 11:04:42 +00:00
case 0:
// Closing all modals if available before navigation
2021-06-18 12:52:03 +00:00
if (Frame.topmost()) {
let modals = Frame.topmost()._getRootModalViews()
for (let i = modals.length - 1; i >= 0; i--) {
Frame.topmost()
._getRootModalViews()
[i].closeModal()
}
2021-06-15 11:04:42 +00:00
}
Vue.navigateTo(CookingTimer as any, {
animated: false,
})
2021-06-18 12:52:03 +00:00
setNumber('isTimer', 1)
2021-06-15 11:04:42 +00:00
break
case 2:
Vue.navigateBack()
break
}
2021-06-05 18:08:36 +00:00
break
2021-06-15 11:04:42 +00:00
// case 'grocery':
// renderView = GroceryList
// break
}
2021-05-25 14:32:53 +00:00
}
}
2021-06-05 18:08:36 +00:00
Application.on(Application.launchEvent, (args) => {
2021-06-15 11:04:42 +00:00
utils.updateLocale()
2021-06-05 18:08:36 +00:00
store.commit('setRTL')
2021-06-15 11:04:42 +00:00
intentListener(args)
Application.android.on(
AndroidApplication.activityNewIntentEvent,
intentListener
)
2021-06-05 18:08:36 +00:00
Frame.on(Frame.loadedEvent, initFrame)
})
2021-06-05 18:08:36 +00:00
2021-06-18 12:52:03 +00:00
Application.on(Application.resumeEvent, () => {
showOverLockscreen()
if (utils.sysLocale() !== getString('sysLocale', utils.sysLocale())) {
Frame.reloadPage()
utils.updateLocale()
store.commit('setRTL')
}
})
Application.on(Application.exitEvent, () => {
2021-06-05 18:08:36 +00:00
renderView = EnRecipes
Application.android.off(
AndroidApplication.activityNewIntentEvent,
intentListener
)
})
2021-06-05 18:08:36 +00:00
Vue.config.silent = false
new Vue({
store,
render: (h) => h('Frame', [h(renderView)]),
}).$start()