enrecipes/app/store.ts

1049 lines
29 KiB
TypeScript
Raw Normal View History

2021-04-14 13:38:22 +00:00
import Vue from 'nativescript-vue'
2021-03-21 17:02:04 +00:00
import Vuex from 'vuex'
Vue.use(Vuex)
2021-05-22 08:56:31 +00:00
import { openOrCreate } from '@akylas/nativescript-sqlite'
import {
getFileAccess,
File,
ApplicationSettings,
Application,
knownFolders,
path,
} from '@nativescript/core'
// OPEN DATABASE FILE
const db = openOrCreate(
path.join(knownFolders.documents().path, 'EnRecipes.db')
)
// CREATE recipes TABLE
db.execute(
'CREATE TABLE IF NOT EXISTS recipes (id TEXT PRIMARY KEY, image TEXT, title TEXT, cuisine TEXT, category TEXT, tags TEXT, prepTime TEXT, cookTime TEXT, yieldQuantity TEXT, yieldUnit TEXT, difficulty TEXT, rating INT, ingredients TEXT, instructions TEXT, combinations TEXT, notes TEXT, favorite INT, tried INT, lastTried INT, lastModified INT, created INT)'
)
// CREATE lists TABLE
db.execute(
'CREATE TABLE IF NOT EXISTS lists (cuisines TEXT, categories TEXT, yieldUnits TEXT, units TEXT)'
)
// CREATE mealPlans TABLE
db.execute(
'CREATE TABLE IF NOT EXISTS mealPlans (date INT, type TEXT, title TEXT)'
)
2020-12-29 10:35:19 +00:00
const defaultCuisines = [
2021-03-21 17:02:04 +00:00
'American',
'Brazilian',
'British',
'Chinese',
'Danish',
'Egyptian',
'Filipino',
'French',
'German',
'Greek',
'Indian',
'Irish',
'Italian',
'Jamaican',
'Japanese',
'Jewish',
'Kenyan',
'Korean',
'Mexican',
'Nigerian',
'Portuguese',
'Russian',
'Scottish',
'Spanish',
'Sri Lankan',
'Swedish',
'Thai',
'Turkish',
'Vietnamese',
]
2020-12-29 10:35:19 +00:00
const defaultCategories = [
2021-03-21 17:02:04 +00:00
'Appetizers',
'Barbecue',
'Beverages',
'Breads',
2021-04-15 16:44:21 +00:00
'breakfast',
2021-03-21 17:02:04 +00:00
'Desserts',
2021-04-15 16:44:21 +00:00
'dinner',
2021-03-21 17:02:04 +00:00
'Healthy',
2021-04-15 16:44:21 +00:00
'lunch',
2021-03-21 17:02:04 +00:00
'Main dishes',
'Meat',
'Noodles',
'Pasta',
'Poultry',
'Rice',
'Salads',
'Sauces',
'Seafood',
'Side dishes',
2021-04-15 16:44:21 +00:00
'snacks',
2021-03-21 17:02:04 +00:00
'Soups',
'Undefined',
'Vegan',
'Vegetarian',
]
2020-12-29 10:35:19 +00:00
const defaultYieldUnits = [
2021-03-21 17:02:04 +00:00
'Serving',
'Piece',
'Teaspoon',
'Tablespoon',
'Fluid Ounce',
'Ounce',
'Pound',
'Gram',
'Kilogram',
'Cup',
'Gallon',
'Millilitre',
'Litre',
'Roll',
'Patty',
'Loaf',
]
2020-12-29 10:35:19 +00:00
const defaultUnits = [
2021-03-21 17:02:04 +00:00
'unit',
'tsp',
'dsp',
'tbsp',
'fl oz',
'cup',
'pt',
'qt',
'gal',
'ml',
'l',
'oz',
'lb',
'mg',
'g',
'kg',
'cm',
'in',
'leaf',
'clove',
'piece',
'pinch',
'drop',
'dozen',
'stick',
'small',
'medium',
'large',
]
2020-12-29 10:35:19 +00:00
const listItems = {
cuisines: {
sort: true,
2021-03-21 17:02:04 +00:00
defaultItems: defaultCuisines,
2020-12-29 10:35:19 +00:00
},
categories: {
sort: true,
2021-03-21 17:02:04 +00:00
defaultItems: defaultCategories,
2020-12-29 10:35:19 +00:00
},
yieldUnits: {
sort: false,
2021-03-21 17:02:04 +00:00
defaultItems: defaultYieldUnits,
2020-12-29 10:35:19 +00:00
},
units: {
sort: false,
2021-03-21 17:02:04 +00:00
defaultItems: defaultUnits,
},
}
2021-04-01 10:55:35 +00:00
2020-09-15 11:10:16 +00:00
export default new Vuex.Store({
state: {
2020-11-29 12:54:21 +00:00
recipes: [],
2020-12-29 10:35:19 +00:00
cuisines: [],
2020-10-26 20:49:54 +00:00
categories: [],
2020-11-02 11:36:53 +00:00
yieldUnits: [],
2020-12-29 10:35:19 +00:00
units: [],
2020-11-23 09:49:58 +00:00
mealPlans: [],
2020-10-14 19:32:32 +00:00
icon: {
2021-05-22 08:56:31 +00:00
cog: '\ue900',
home: '\ue901',
try: '\ue902',
tried: '\ue903',
fav: '\ue904',
faved: '\ue905',
menu: '\ue906',
2021-04-12 18:09:48 +00:00
cuisine: '\ue907',
2021-05-22 08:56:31 +00:00
category: '\ue908',
tag: '\ue909',
star: '\ue90a',
starred: '\ue90b',
time: '\ue90c',
diff: '\ue90d',
bag: '\ue90e',
bagged: '\ue90f',
price: '\ue910',
sear: '\ue911',
sort: '\ue912',
filter: '\ue913',
plus: '\ue914',
done: '\ue915',
back: '\ue916',
x: '\ue917',
del: '\ue918',
save: '\ue919',
undo: '\ue91a',
edit: '\ue91b',
cal: '\ue91c',
tod: '\ue91d',
left: '\ue91e',
right: '\ue91f',
img: '\ue920',
uncheck: '\ue921',
check: '\ue922',
share: '\ue923',
timer: '\ue924',
start: '\ue925',
pause: '\ue926',
addTo: '\ue927',
sound: '\ue928',
vibrate: '\ue929',
interface: '\ue92a',
opts: '\ue92b',
db: '\ue92c',
reset: '\ue92d',
info: '\ue92e',
lang: '\ue92f',
theme: '\ue930',
layout: '\ue931',
shuf: '\ue932',
week: '\ue933',
folder: '\ue934',
exp: '\ue935',
imp: '\ue936',
gh: '\ue937',
tg: '\ue938',
help: '\ue939',
priv: '\ue93a',
don: '\ue93b',
trans: '\ue93c',
delay: '\ue93d',
ring: '\ue93e',
2021-03-21 17:02:04 +00:00
},
currentComponent: 'EnRecipes',
sortType: 'Oldest first',
2020-12-29 10:35:19 +00:00
language: [
2021-04-23 14:30:50 +00:00
{
locale: 'da',
title: 'Dansk',
},
2021-03-21 17:02:04 +00:00
{
locale: 'de',
title: 'Deutsch',
},
{
locale: 'en-GB',
title: 'English (UK)',
},
{
locale: 'es',
title: 'Español',
},
{
locale: 'fr',
title: 'Français',
},
{
locale: 'fr-BE',
title: 'Français (BE)',
},
{
locale: 'fr-CA',
title: 'Français (CA)',
},
{
locale: 'fr-CH',
title: 'Français (CH)',
},
2021-04-17 16:24:47 +00:00
{
locale: 'hi',
title: 'हिंदी',
},
{
locale: 'ml',
title: 'മലയാളം',
},
{
locale: 'id',
title: 'Indonesia',
},
2021-03-21 17:02:04 +00:00
{
locale: 'it',
title: 'Italiano',
},
{
locale: 'nb-NO',
title: 'Norsk bokmål',
},
{
locale: 'nl',
title: 'Nederlands',
},
2021-04-17 16:24:47 +00:00
// {
// locale: 'pt',
// title: 'Português',
// },
2021-04-23 14:30:50 +00:00
{
locale: 'pt-BR',
title: 'Português (BR)',
},
2021-03-21 17:02:04 +00:00
{
locale: 'ru',
title: 'Русский',
},
{
locale: 'ta',
title: 'தமிழ்',
},
2021-04-17 16:24:47 +00:00
{
locale: 'te',
title: 'తెలుగు',
},
2020-12-29 10:35:19 +00:00
],
2021-05-22 08:56:31 +00:00
shakeEnabled: ApplicationSettings.getBoolean('shakeEnabled', true),
2020-12-29 10:35:19 +00:00
importSummary: {
found: 0,
imported: 0,
2021-03-21 17:02:04 +00:00
updated: 0,
},
2021-05-22 08:56:31 +00:00
layout: ApplicationSettings.getString('layout', 'detailed'),
2021-04-01 10:55:35 +00:00
selectedCuisine: null,
selectedCategory: null,
selectedTag: null,
2021-04-23 19:50:32 +00:00
appTheme: 'sysDef',
2021-05-22 08:56:31 +00:00
mondayFirst: ApplicationSettings.getBoolean('mondayFirst', false),
timerDelay: ApplicationSettings.getString('timerDelay', '1 minute'),
timerSound: {},
timerVibrate: ApplicationSettings.getBoolean('timerVibrate', false),
timerPresets: [
{
id: 534534563,
label: 'Soft Eggs',
recipeID: null,
time: '00:06:00',
timerInterval: null,
isPaused: false,
preset: 1,
},
{
id: 564646,
label: 'Medium Eggs',
recipeID: null,
time: '00:08:00',
timerInterval: null,
isPaused: false,
preset: 1,
},
{
id: 43276767,
label: 'Hard Eggs',
recipeID: null,
time: '00:10:00',
timerInterval: null,
isPaused: false,
preset: 1,
},
],
activeTimers: [],
timerIntervals: [],
2020-09-15 11:10:16 +00:00
},
mutations: {
2021-05-22 08:56:31 +00:00
addTimerPreset(state, timer) {
state.timerPresets.push(timer)
},
removeTimerPreset(state, index) {
state.timerPresets.splice(index, 1)
},
clearTimerInterval(state) {
state.activeTimers.forEach((e) => {
clearInterval(e.timerInterval)
e.timerInterval = null
})
},
addActiveTimer(state, { timer, index }) {
state.activeTimers.splice(index, 0, timer)
},
updateActiveTimer(state, timer) {
let index = state.activeTimers.findIndex((e) => e.id == timer.id)
state.activeTimers.splice(index, 1, timer)
},
removeActiveTimer(state, index) {
state.activeTimers.splice(index, 1)
},
setTimerDelay(state, delay) {
state.timerDelay = delay
ApplicationSettings.setString('timerDelay', delay)
},
setTimerSound(state, sound) {
state.timerSound = {
title: sound.title,
uri: sound.uri,
}
2021-05-25 14:32:53 +00:00
ApplicationSettings.setString('timerSound', JSON.stringify(sound))
2021-05-22 08:56:31 +00:00
},
setTimerVibrate(state, bool) {
state.timerVibrate = bool
ApplicationSettings.setBoolean('timerVibrate', bool)
},
2021-04-12 18:09:48 +00:00
clearImportSummary(state) {
for (const key in state.importSummary) state.importSummary[key] = 0
},
setFirstDay(state, bool) {
state.mondayFirst = bool
ApplicationSettings.setBoolean('mondayFirst', bool)
},
2021-04-01 10:55:35 +00:00
setTheme(state, theme) {
2021-04-23 19:50:32 +00:00
switch (theme) {
case 'sysDef':
2021-05-22 08:56:31 +00:00
state.appTheme =
Application.systemAppearance() == 'dark' ? 'Dark' : 'Light'
break
2021-04-23 19:50:32 +00:00
case 'sysDefB':
2021-05-22 08:56:31 +00:00
state.appTheme =
Application.systemAppearance() == 'dark' ? 'Black' : 'Light'
break
2021-04-23 19:50:32 +00:00
default:
state.appTheme = theme
2021-05-22 08:56:31 +00:00
break
2021-04-23 19:50:32 +00:00
}
2021-04-12 18:09:48 +00:00
ApplicationSettings.setString('appTheme', theme)
2021-04-01 10:55:35 +00:00
},
clearFilter(state) {
state.selectedCuisine = state.selectedCategory = state.selectedTag = null
},
2021-03-21 17:02:04 +00:00
setLayout(state, type) {
state.layout = type
},
2020-12-29 10:35:19 +00:00
setSortType(state, sortType) {
state.sortType = sortType
2020-10-26 20:49:54 +00:00
},
2021-04-01 10:55:35 +00:00
initRecipes(state) {
2021-05-22 08:56:31 +00:00
db.select('SELECT * FROM recipes').then((res) => {
res.forEach((e) => {
Object.keys(e).forEach(
(f) =>
f.match(/tags|ingredients|instructions|combinations|notes/) &&
(e[f] = JSON.parse(e[f]))
)
state.recipes.push(e)
})
2021-03-21 17:02:04 +00:00
})
state.shakeEnabled = ApplicationSettings.getBoolean('shakeEnabled', true)
state.sortType = ApplicationSettings.getString('sortType', 'Oldest first')
2020-11-06 09:07:41 +00:00
},
2021-05-22 08:56:31 +00:00
importRecipesFromJSON(state, recipes) {
2021-03-21 17:02:04 +00:00
let localRecipesIDs, partition
2020-12-29 10:35:19 +00:00
let imported = 0
let updated = 0
function getUpdatedData(data) {
2021-03-21 17:02:04 +00:00
return data.map((recipe) => {
let r = Object.assign({}, recipe)
if (r.timeRequired) {
2021-03-21 17:02:04 +00:00
r.prepTime = '00:00'
r.cookTime = r.timeRequired
delete r.timeRequired
}
2021-05-22 08:56:31 +00:00
if (r.imageSrc) {
r.image = r.imageSrc.replace('enrecipes', 'EnRecipes')
delete r.imageSrc
}
2021-03-21 17:02:04 +00:00
if (!r.hasOwnProperty('cuisine')) r.cuisine = 'Undefined'
if (!r.hasOwnProperty('tags')) r.tags = []
if (!r.hasOwnProperty('difficulty')) r.difficulty = 'Easy'
if (!r.hasOwnProperty('rating')) r.rating = 0
if (!r.hasOwnProperty('created')) r.created = r.lastModified
2021-05-22 08:56:31 +00:00
r.yieldQuantity = r.yield.quantity
r.yieldUnit = r.yield.unit
delete r.yield
2021-03-21 17:02:04 +00:00
return r
})
}
2020-11-06 09:07:41 +00:00
function createDocuments(data) {
2021-03-21 17:02:04 +00:00
data = getUpdatedData(data)
state.recipes = [...state.recipes, ...data]
2021-05-22 08:56:31 +00:00
data.forEach((r) => {
2020-12-29 10:35:19 +00:00
imported++
2021-05-22 08:56:31 +00:00
db.execute(
`INSERT INTO recipes (id, image, title, cuisine, category, tags, prepTime, cookTime, yieldQuantity, yieldUnit, difficulty, rating, ingredients, instructions, combinations, notes, favorite, tried, lastTried, lastModified, created) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[
r.id,
r.image,
r.title,
r.cuisine,
r.category,
JSON.stringify(r.tags),
r.prepTime,
r.cookTime,
r.yieldQuantity,
r.yieldUnit,
r.difficulty,
r.rating,
JSON.stringify(r.ingredients),
JSON.stringify(r.instructions),
JSON.stringify(r.combinations),
JSON.stringify(r.notes),
r.favorite ? 1 : 0,
r.tried ? 1 : 0,
r.lastTried ? new Date(r.lastTried).getTime() : null,
r.lastModified ? new Date(r.lastModified).getTime() : null,
r.created ? new Date(r.created).getTime() : null,
]
)
2021-03-21 17:02:04 +00:00
})
2020-11-06 09:07:41 +00:00
}
function updateDocuments(data) {
2021-03-21 17:02:04 +00:00
data = getUpdatedData(data)
2021-05-22 08:56:31 +00:00
data.forEach((r) => {
2021-03-21 17:02:04 +00:00
let recipeIndex = state.recipes
.map((e, i) => {
let d1 = new Date(e.lastModified).getTime()
2021-05-22 08:56:31 +00:00
let d2 = new Date(r.lastModified).getTime()
return e.id === r.id && d1 < d2 ? i : -1
2021-03-21 17:02:04 +00:00
})
.filter((e) => e >= 0)[0]
2020-11-23 09:49:58 +00:00
if (recipeIndex >= 0) {
2020-12-29 10:35:19 +00:00
updated++
2021-05-22 08:56:31 +00:00
Object.assign(state.recipes[recipeIndex], r)
db.execute(
`REPLACE INTO recipes (id, image, title, cuisine, category, tags, prepTime, cookTime, yieldQuantity, yieldUnit, difficulty, rating, ingredients, instructions, combinations, notes, favorite, tried, lastTried, lastModified, created) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[
r.id,
r.image,
r.title,
r.cuisine,
r.category,
JSON.stringify(r.tags),
r.prepTime,
r.cookTime,
r.yieldQuantity,
r.yieldUnit,
r.difficulty,
r.rating,
JSON.stringify(r.ingredients),
JSON.stringify(r.instructions),
JSON.stringify(r.combinations),
JSON.stringify(r.notes),
r.favorite ? 1 : 0,
r.tried ? 1 : 0,
r.lastTried ? new Date(r.lastTried).getTime() : null,
r.lastModified ? new Date(r.lastModified).getTime() : null,
r.created ? new Date(r.created).getTime() : null,
]
)
2020-11-23 09:49:58 +00:00
}
2021-03-21 17:02:04 +00:00
})
2020-11-02 11:36:53 +00:00
}
2020-12-29 10:35:19 +00:00
if (state.recipes.length) {
2021-03-21 17:02:04 +00:00
localRecipesIDs = state.recipes.map((e) => e.id)
partition = recipes.reduce(
2021-05-22 08:56:31 +00:00
(result, recipe) => {
2021-03-21 17:02:04 +00:00
localRecipesIDs.indexOf(recipe.id) < 0
? result[0].push(recipe) // create candidates
: result[1].push(recipe) // update candidates
return result
},
[[], []]
)
if (partition[0].length) createDocuments(partition[0])
if (partition[1].length) updateDocuments(partition[1])
} else {
createDocuments(recipes)
2020-10-26 20:49:54 +00:00
}
2020-12-29 10:35:19 +00:00
state.importSummary.found = recipes.length
state.importSummary.imported = imported
state.importSummary.updated = updated
},
2021-05-22 08:56:31 +00:00
importRecipesFromDB(state, recipes) {
let localRecipesIDs: string[], partition: any[]
let imported = 0
let updated = 0
function createDocuments(data: any[]) {
data.forEach((r) => {
const cols = Object.keys(r).join(', ')
const placeholder = Object.keys(r)
.fill('?')
.join(', ')
imported++
db.execute(
`REPLACE INTO recipes (${cols}) VALUES (${placeholder})`,
Object.values(r)
)
Object.keys(r).forEach(
(f) =>
f.match(/tags|ingredients|instructions|combinations|notes/) &&
(r[f] = JSON.parse(r[f]))
)
state.recipes.push(r)
})
}
function updateDocuments(data: any[]) {
data.forEach((r) => {
let recipeIndex = state.recipes
.map((e, i) => {
let d1 = new Date(e.lastModified).getTime()
let d2 = new Date(r.lastModified).getTime()
return e.id === r.id && d1 < d2 ? i : -1
})
.filter((e) => e >= 0)[0]
if (recipeIndex >= 0) {
updated++
const cols = Object.keys(r).join(', ')
const placeholder = Object.keys(r)
.fill('?')
.join(', ')
db.execute(
`REPLACE INTO recipes (${cols}) VALUES (${placeholder})`,
Object.values(r)
)
Object.keys(r).forEach(
(f) =>
f.match(/tags|ingredients|instructions|combinations|notes/) &&
(r[f] = JSON.parse(r[f]))
)
Object.assign(state.recipes[recipeIndex], r)
}
})
}
if (state.recipes.length) {
localRecipesIDs = state.recipes.map((e) => e.id)
partition = recipes.reduce(
(result, recipe) => {
localRecipesIDs.indexOf(recipe.id) < 0
? result[0].push(recipe) // create candidates
: result[1].push(recipe) // update candidates
return result
},
[[], []]
)
if (partition[0].length) createDocuments(partition[0])
if (partition[1].length) updateDocuments(partition[1])
2021-05-25 14:32:53 +00:00
} else createDocuments(recipes)
2021-05-22 08:56:31 +00:00
state.importSummary.found = recipes.length
state.importSummary.imported = imported
state.importSummary.updated = updated
2021-03-21 17:02:04 +00:00
},
2021-05-22 08:56:31 +00:00
addRecipe(state, recipe) {
let r = JSON.parse(JSON.stringify(recipe))
Object.keys(recipe).forEach((e) => {
if (e.match(/tags|ingredients|instructions|combinations|notes/))
r[e] = JSON.stringify(r[e])
if (e.match(/favorite|tried/)) r[e] = r[e] ? 1 : 0
})
const cols = Object.keys(recipe).join(', ')
const placeholder = Object.keys(recipe)
.fill('?')
.join(', ')
db.execute(
`REPLACE INTO recipes (${cols}) VALUES (${placeholder})`,
Object.values(r)
2021-03-21 17:02:04 +00:00
)
2021-05-22 08:56:31 +00:00
let i: number
function exist({ id }, index: number) {
if (id === recipe.id) {
i = index
return true
2020-11-15 10:51:10 +00:00
}
2021-05-22 08:56:31 +00:00
return false
}
state.recipes.some(exist)
? Object.assign(state.recipes[i], recipe)
: state.recipes.push(recipe)
2020-12-29 10:35:19 +00:00
},
2021-01-23 17:20:15 +00:00
deleteRecipes(state, ids) {
2021-05-22 08:56:31 +00:00
ids.forEach((id: string) => {
2021-03-21 17:02:04 +00:00
let index = state.recipes.findIndex((e) => e.id === id)
2021-05-22 08:56:31 +00:00
getFileAccess().deleteFile(state.recipes[index].image)
2021-03-21 17:02:04 +00:00
state.recipes.splice(index, 1)
2021-05-22 08:56:31 +00:00
db.execute(`DELETE FROM recipes WHERE id = '${id}'`)
2021-01-23 17:20:15 +00:00
state.recipes.forEach((e, i) => {
if (e.combinations.includes(id)) {
2021-03-21 17:02:04 +00:00
state.recipes[i].combinations.splice(e.combinations.indexOf(id), 1)
2021-05-22 08:56:31 +00:00
db.execute(
`UPDATE recipes SET combinations = '${JSON.stringify(
state.recipes[i].combinations
)}' WHERE id = '${id}'`
)
2021-01-23 17:20:15 +00:00
}
2021-03-21 17:02:04 +00:00
})
})
2021-01-23 17:20:15 +00:00
},
2021-04-01 10:55:35 +00:00
initListItems(state) {
2021-05-22 08:56:31 +00:00
if (!state.cuisines.length) {
db.select(`SELECT * FROM lists`).then((res) => {
if (!res.length) {
db.execute(
`INSERT INTO lists (cuisines, categories, yieldUnits, units) VALUES (?, ?, ?, ?)`,
[null, null, null, null]
2021-03-21 17:02:04 +00:00
)
2020-12-29 10:35:19 +00:00
}
2021-05-22 08:56:31 +00:00
db.select(`SELECT * FROM lists`).then((res) => {
Object.keys(res[0]).forEach((list) => {
let userItems: string[]
let defaultItems = listItems[list].defaultItems
if (res[0][list]) {
userItems = JSON.parse(res[0][list])
state[list] = userItems.some((e: string) =>
defaultItems.includes(e)
)
? userItems
: [...defaultItems, ...userItems]
} else {
state[list] = defaultItems
listItems[list].sort && state[list].sort()
}
})
})
})
2021-03-21 17:02:04 +00:00
}
},
importListItems(state, { data, listName }) {
2021-05-22 08:56:31 +00:00
state[listName] = [...new Set([...state[listName], ...data])]
if (listItems[listName].sort) state[listName].sort()
db.execute(
`UPDATE lists SET ${listName} = '${JSON.stringify(state[listName])}'`
)
2021-03-21 17:02:04 +00:00
},
addListItem(state, { item, listName }) {
2021-05-22 08:56:31 +00:00
let lowercase = state[listName].map((e: string) => e.toLowerCase())
2020-12-29 10:35:19 +00:00
if (lowercase.indexOf(item.toLowerCase()) == -1) {
2021-05-22 08:56:31 +00:00
state[listName].push(item)
2020-12-29 10:35:19 +00:00
if (listItems[listName].sort)
2021-05-22 08:56:31 +00:00
state[listName].sort((a: string, b: string) =>
2021-03-21 17:02:04 +00:00
a.toLowerCase().localeCompare(b.toLowerCase())
)
2021-05-22 08:56:31 +00:00
db.execute(
`UPDATE lists SET ${listName} = '${JSON.stringify(state[listName])}'`
)
2021-01-23 17:40:30 +00:00
}
},
2021-03-21 17:02:04 +00:00
removeListItem(state, { item, listName }) {
2021-05-22 08:56:31 +00:00
state[listName].splice(state[listName].indexOf(item), 1)
db.execute(
`UPDATE lists SET ${listName} = '${JSON.stringify(state[listName])}'`
)
2021-03-21 17:02:04 +00:00
},
2020-12-29 10:35:19 +00:00
resetListItems(state, listName) {
2021-03-21 17:02:04 +00:00
let defaultItems = listItems[listName].defaultItems
state[listName] = [...defaultItems]
2021-05-22 08:56:31 +00:00
if (listItems[listName].sort)
state[listName].sort((a: string, b: string) =>
a.toLowerCase().localeCompare(b.toLowerCase())
)
db.execute(
`UPDATE lists SET ${listName} = '${JSON.stringify(state[listName])}'`
)
2020-10-26 20:49:54 +00:00
},
2021-04-01 10:55:35 +00:00
initMealPlans(state) {
2021-05-22 08:56:31 +00:00
if (!state.mealPlans.length)
db.select(`SELECT * FROM mealPlans`).then((res) =>
res.forEach((m: {}) => state.mealPlans.push(m))
2021-03-21 17:02:04 +00:00
)
2020-11-23 09:49:58 +00:00
},
2021-05-22 08:56:31 +00:00
importMealPlansFromJSON(state, mealPlans) {
2021-03-21 17:02:04 +00:00
let newMealPlans = mealPlans.filter((e) => {
if (e.hasOwnProperty('eventColor')) {
return !state.mealPlans.some((f) => {
2021-01-23 17:20:15 +00:00
let d = new Date(e.startDate)
2021-03-21 17:02:04 +00:00
let date = new Date(
d.getFullYear(),
d.getMonth(),
d.getDate(),
0
).getTime()
2021-01-23 17:20:15 +00:00
let type
switch (d.getHours()) {
case 0:
2021-03-21 17:02:04 +00:00
type = 'breakfast'
break
2021-01-23 17:20:15 +00:00
case 5:
2021-03-21 17:02:04 +00:00
type = 'lunch'
break
2021-01-23 17:20:15 +00:00
case 10:
2021-03-21 17:02:04 +00:00
type = 'dinner'
break
2021-01-23 17:20:15 +00:00
case 15:
2021-03-21 17:02:04 +00:00
type = 'snacks'
break
2021-01-23 17:20:15 +00:00
}
return f.title === e.title && f.date === date && f.type === type
})
} else {
2021-03-21 17:02:04 +00:00
return !state.mealPlans.some(
(f) => f.title === e.title && f.date === e.date && f.type === e.type
)
2021-01-23 17:20:15 +00:00
}
})
let updatedMealPlans = []
2021-03-21 17:02:04 +00:00
if (newMealPlans[0].hasOwnProperty('eventColor')) {
newMealPlans.forEach((p) => {
2021-01-23 17:20:15 +00:00
let d = new Date(p.startDate)
2021-03-21 17:02:04 +00:00
p.date = new Date(
d.getFullYear(),
d.getMonth(),
d.getDate(),
0
).getTime()
2021-01-23 17:20:15 +00:00
switch (d.getHours()) {
case 0:
2021-03-21 17:02:04 +00:00
p.type = 'breakfast'
break
2021-01-23 17:20:15 +00:00
case 5:
2021-03-21 17:02:04 +00:00
p.type = 'lunch'
break
2021-01-23 17:20:15 +00:00
case 10:
2021-03-21 17:02:04 +00:00
p.type = 'dinner'
break
2021-01-23 17:20:15 +00:00
case 15:
2021-03-21 17:02:04 +00:00
p.type = 'snacks'
break
2021-01-23 17:20:15 +00:00
}
delete p.startDate
delete p.endDate
delete p.eventColor
updatedMealPlans.push(p)
2021-03-21 17:02:04 +00:00
})
2021-01-23 17:20:15 +00:00
}
2021-03-21 17:02:04 +00:00
state.mealPlans = [...state.mealPlans, ...updatedMealPlans]
2021-05-22 08:56:31 +00:00
updatedMealPlans.forEach((m) => {
db.execute(
`INSERT INTO mealPlans (date, type, title) VALUES (?, ?, ?)`,
[m.date, m.type, m.title]
)
})
2020-12-29 10:35:19 +00:00
},
2021-05-22 08:56:31 +00:00
importMealPlansFromDB(state, mealPlans) {
let newMealPlans = mealPlans.filter(
(e) =>
!state.mealPlans.some((f) =>
Object.keys(f).every((key) => f[key] == e[key])
)
)
state.mealPlans = [...state.mealPlans, ...newMealPlans]
newMealPlans.forEach((m) => {
db.execute(
`INSERT INTO mealPlans (date, type, title) VALUES (?, ?, ?)`,
[m.date, m.type, m.title]
)
})
},
addMealPlan(state, { date, type, title }) {
2021-01-13 05:02:48 +00:00
let mealPlan = {
2021-01-23 17:20:15 +00:00
date,
2021-03-21 17:02:04 +00:00
type,
2021-05-22 08:56:31 +00:00
title,
2021-01-13 05:02:48 +00:00
}
2021-05-22 08:56:31 +00:00
state.mealPlans.push(mealPlan)
const cols = Object.keys(mealPlan).join(', ')
const placeholder = Object.keys(mealPlan)
.fill('?')
.join(', ')
db.execute(
`INSERT INTO mealPlans (${cols}) VALUES (${placeholder})`,
Object.values(mealPlan)
)
2021-03-21 17:02:04 +00:00
},
2021-05-22 08:56:31 +00:00
deleteMealPlan(state, { date, type, title }) {
let index = state.mealPlans.findIndex(
(e) => e.title === title && e.type === type && e.date === date
)
2021-03-21 17:02:04 +00:00
state.mealPlans.splice(index, 1)
state.mealPlans = [...state.mealPlans]
2021-05-22 08:56:31 +00:00
db.execute(
`DELETE FROM mealPlans WHERE date = ${date} AND type = '${type}' AND title = '${title}'`
2021-03-21 17:02:04 +00:00
)
},
2021-05-22 08:56:31 +00:00
toggleState(state, { id, key, setDate }) {
let index = state.recipes.findIndex((e) => e.id == id)
state.recipes[index][key] = state.recipes[index][key] ? 0 : 1
db.execute(
`UPDATE recipes SET ${key} = ${state.recipes[index][key]} WHERE id = '${id}'`
2021-03-21 17:02:04 +00:00
)
2021-05-22 08:56:31 +00:00
if (setDate) {
state.recipes[index].lastTried = new Date().getTime()
db.execute(
`UPDATE recipes SET lastTried = ${state.recipes[index].lastTried} WHERE id = '${id}'`
)
}
2020-10-26 20:49:54 +00:00
},
2021-04-01 10:55:35 +00:00
setComponent(state, comp) {
2021-03-21 17:02:04 +00:00
state.currentComponent = comp
2020-11-23 09:49:58 +00:00
},
2021-03-21 17:02:04 +00:00
unSyncCombinations(state, { id, combinations }) {
2020-11-15 10:51:10 +00:00
state.recipes.forEach((e, i) => {
if (combinations.includes(e.id)) {
2021-03-21 17:02:04 +00:00
state.recipes[i].combinations.splice(e.combinations.indexOf(id), 1)
2021-05-22 08:56:31 +00:00
db.execute(
`UPDATE recipes SET combinations = '${JSON.stringify(
state.recipes[i].combinations
)}' WHERE id = '${id}'`
)
2020-12-29 10:35:19 +00:00
}
2021-03-21 17:02:04 +00:00
})
2020-12-29 10:35:19 +00:00
},
setShake(state, shake) {
2021-03-21 17:02:04 +00:00
state.shakeEnabled = shake
2021-04-12 18:09:48 +00:00
ApplicationSettings.setBoolean('shakeEnabled', shake)
2020-12-29 10:35:19 +00:00
},
2021-05-22 08:56:31 +00:00
setRating(state, { id, rating }) {
let index = state.recipes.findIndex((e) => e.id == id)
2020-12-29 10:35:19 +00:00
state.recipes[index].rating = rating
2021-05-22 08:56:31 +00:00
db.execute(`UPDATE recipes SET rating = ${rating} WHERE id = '${id}'`)
2020-12-29 10:35:19 +00:00
},
2021-05-22 08:56:31 +00:00
toggleCart(state, { id }) {
2021-03-21 17:02:04 +00:00
let index = state.recipes.indexOf(
state.recipes.filter((e) => e.id === id)[0]
)
2021-01-23 17:20:15 +00:00
state.recipes[index].inBag = !state.recipes[index].inBag
2021-01-13 05:02:48 +00:00
},
2020-12-29 10:35:19 +00:00
unlinkBrokenImages(state) {
state.recipes.forEach((r, i) => {
2021-05-22 08:56:31 +00:00
if (r.image && !File.exists(r.image)) {
r.image = null
2021-03-21 17:02:04 +00:00
Object.assign(state.recipes[i], r)
2021-05-22 08:56:31 +00:00
db.execute(`UPDATE recipes SET image = null WHERE id = '${r.id}'`)
2020-11-15 10:51:10 +00:00
}
})
2021-03-21 17:02:04 +00:00
},
2021-04-01 10:55:35 +00:00
setCuisine(state, value) {
state.selectedCuisine = value
},
setCategory(state, value) {
state.selectedCategory = value
},
setTag(state, value) {
state.selectedTag = value
},
2020-09-15 11:10:16 +00:00
},
actions: {
2021-05-22 08:56:31 +00:00
addTimerPreset({ commit }, timer) {
commit('addTimerPreset', timer)
},
removeTimerPreset({ commit }, timer) {
commit('removeTimerPreset', timer)
},
clearTimerInterval({ commit }) {
commit('clearTimerInterval')
},
addActiveTimer({ commit }, timer) {
commit('addActiveTimer', timer)
},
updateActiveTimer({ commit }, timer) {
commit('updateActiveTimer', timer)
},
removeActiveTimer({ commit }, index) {
commit('removeActiveTimer', index)
},
setTimerDelay({ commit }, delay) {
commit('setTimerDelay', delay)
},
setTimerSound({ commit }, sound) {
commit('setTimerSound', sound)
},
setTimerVibrate({ commit }, bool) {
commit('setTimerVibrate', bool)
},
2021-04-12 18:09:48 +00:00
clearImportSummary({ commit }) {
commit('clearImportSummary')
},
setFirstDay({ commit }, bool) {
commit('setFirstDay', bool)
},
2021-04-01 10:55:35 +00:00
setTheme({ commit }, theme) {
commit('setTheme', theme)
},
clearFilter({ commit }) {
commit('clearFilter')
},
2021-03-21 17:02:04 +00:00
setLayout({ commit }, type) {
commit('setLayout', type)
},
2021-04-12 18:09:48 +00:00
setSortType({ commit }, sortType) {
2021-03-21 17:02:04 +00:00
commit('setSortType', sortType)
},
2021-04-01 10:55:35 +00:00
initRecipes({ commit }) {
commit('initRecipes')
2021-03-21 17:02:04 +00:00
},
2021-05-22 08:56:31 +00:00
importRecipesFromJSON({ commit }, recipes) {
commit('importRecipesFromJSON', recipes)
},
importRecipesFromDB({ commit }, recipes) {
commit('importRecipesFromDB', recipes)
2021-03-21 17:02:04 +00:00
},
addRecipeAction({ commit }, recipe) {
commit('addRecipe', recipe)
},
2021-05-22 08:56:31 +00:00
deleteRecipes({ commit }, ids) {
2021-03-21 17:02:04 +00:00
commit('deleteRecipes', ids)
},
2021-04-01 10:55:35 +00:00
initListItems({ commit }) {
commit('initListItems')
2021-03-21 17:02:04 +00:00
},
2021-05-22 08:56:31 +00:00
importListItems({ commit }, data) {
2021-03-21 17:02:04 +00:00
commit('importListItems', data)
},
addListItemAction({ commit }, item) {
commit('addListItem', item)
},
removeListItemAction({ commit }, item) {
commit('removeListItem', item)
},
resetListItemsAction({ commit }, listName) {
commit('resetListItems', listName)
},
2021-04-01 10:55:35 +00:00
initMealPlans({ commit }) {
commit('initMealPlans')
2021-03-21 17:02:04 +00:00
},
2021-05-22 08:56:31 +00:00
importMealPlansFromJSON({ commit }, mealPlans) {
commit('importMealPlansFromJSON', mealPlans)
},
importMealPlansFromDB({ commit }, mealPlans) {
commit('importMealPlansFromDB', mealPlans)
2021-03-21 17:02:04 +00:00
},
addMealPlanAction({ commit }, mealPlan) {
commit('addMealPlan', mealPlan)
},
deleteMealPlanAction({ commit }, mealPlan) {
commit('deleteMealPlan', mealPlan)
},
toggleStateAction({ commit }, toggledRecipe) {
commit('toggleState', toggledRecipe)
},
2021-04-01 10:55:35 +00:00
setComponent({ commit }, comp) {
commit('setComponent', comp)
2021-03-21 17:02:04 +00:00
},
unSyncCombinationsAction({ commit }, combinations) {
commit('unSyncCombinations', combinations)
},
2021-04-12 18:09:48 +00:00
setShake({ commit }, shake) {
2021-03-21 17:02:04 +00:00
commit('setShake', shake)
},
setRatingAction({ commit }, rating) {
commit('setRating', rating)
},
toggleCartAction({ commit }, recipe) {
commit('toggleCart', recipe)
},
unlinkBrokenImages({ commit }) {
commit('unlinkBrokenImages')
},
2021-04-01 10:55:35 +00:00
setCuisine({ commit }, value) {
commit('setCuisine', value)
},
setCategory({ commit }, value) {
commit('setCategory', value)
},
setTag({ commit }, value) {
commit('setTag', value)
},
2021-03-21 17:02:04 +00:00
},
})