1/* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import router from '@ohos.router' 17import { 18 FoodInfo, 19 Category, 20 CategoryId, 21 MealTimeId, 22 OneMealStatisticsInfo, 23 MealFoodInfo, 24 DietRecord 25} from './DataModels' 26import { mockFoods, mockFoodInfo, mockDietRecords } from '../mock/MockData' 27 28const DEBUG_PREVIEW = false 29const MOCK_API = true 30 31export function getFoods(): Array<FoodInfo> { 32 return mockFoods 33} 34 35export function getFoodInfo(): FoodInfo { 36 return DEBUG_PREVIEW ? mockFoodInfo : router.getParams()['foodId'] 37} 38 39// make records ordered by meal time 40export let initDietRecords: Array<DietRecord> = [ 41 { 42 id: -1, 43 foodId: 0, 44 mealTime: { name: $r('app.string.meal_time_breakfast'), id: MealTimeId.Breakfast }, 45 weight: 0 46 }, 47 { 48 id: -1, 49 foodId: 0, 50 mealTime: { name: $r('app.string.meal_time_lunch'), id: MealTimeId.Lunch }, 51 weight: 0 52 }, 53 { 54 id: -1, 55 foodId: 0, 56 mealTime: { name: $r('app.string.meal_time_dinner'), id: MealTimeId.Dinner }, 57 weight: 0 58 }, 59 { 60 id: -1, 61 foodId: 0, 62 mealTime: { name: $r('app.string.meal_time_supper'), id: MealTimeId.Supper }, 63 weight: 0 64 }] 65 66export function getDietRecords(): Array<DietRecord> { 67 return DEBUG_PREVIEW ? initDietRecords.concat(mockDietRecords) : 68 AppStorage.Get<Array<DietRecord>>('dietRecords') 69} 70 71export function getFoodCategories(): Category[] { 72 return [{ id: CategoryId.Vegetable, name: $r('app.string.category_vegetable') }, 73 { id: CategoryId.Fruit, name: $r('app.string.category_fruit') }, 74 { id: CategoryId.Nut, name: $r('app.string.category_nut') }, 75 { id: CategoryId.Seafood, name: $r('app.string.category_seafood') }, 76 { id: CategoryId.Dessert, name: $r('app.string.category_dessert') }] 77} 78 79export function getMileTimes(): string[] { 80 if (MOCK_API) { 81 return ['早餐', '午餐', '晚餐', '夜宵'] 82 } else { 83 let mealTimeResources: Resource[] = [$r("app.string.meal_time_breakfast"), $r('app.string.meal_time_lunch'), $r('app.string.meal_time_dinner'), $r('app.string.meal_time_supper'), $r('app.string.category_dessert')] 84 let mealTimes = [] 85 mealTimeResources.forEach(item => { 86 let mealTime = this.context.resourceManager.getStringSync(item.id) 87 if (mealTime !== '') { 88 mealTimes.push(mealTime) 89 } 90 }) 91 return mealTimes 92 } 93} 94 95export function getSortedFoodData(): Array<FoodInfo> { 96 var letterReg = /^[A-Z]$/ 97 var list = new Array() 98 var foods = getFoods() 99 for (var i = 0;i < foods.length; i++) { 100 list['#'] = new Array() 101 // convert the first letter to uppercase 102 var letter = foods[i].letter.substr(0, 1).toUpperCase() 103 if (!letterReg.test(letter)) { 104 letter = '#' 105 } 106 // create group by letter 107 if (!(letter in list)) { 108 list[letter] = new Array() 109 } 110 list[letter].push(foods[i]) 111 } 112 113 // sort by alphabetical order 114 var letterGroup = new Array() 115 for (var key in list) { 116 letterGroup.push({ 117 letter: key, 118 list: list[key] 119 }) 120 } 121 letterGroup.sort(function (x, y) { 122 return x.letter.charCodeAt(0) - y.letter.charCodeAt(0) 123 }) 124 125 // put '#' to last for others 126 var lastArr = letterGroup[0] 127 letterGroup.splice(0, 1) 128 letterGroup.push(lastArr) 129 130 // create result list 131 var resultList = [] 132 for (var i = 0; i < letterGroup.length; i++) { 133 resultList.push(letterGroup[i].letter) 134 resultList = resultList.concat(letterGroup[i].list) 135 } 136 return resultList 137} 138 139export function statistics(): Array<OneMealStatisticsInfo> { 140 console.info('meal statistics') 141 let dietRecords = getDietRecords() 142 const mealMap = new Map() 143 dietRecords.forEach((item) => { 144 let oneMealStatisticsInfo: OneMealStatisticsInfo = mealMap.get(item.mealTime.id) 145 if (oneMealStatisticsInfo === undefined) { 146 oneMealStatisticsInfo = new OneMealStatisticsInfo(item.mealTime) 147 } 148 var foodInfo = getFoods().find((food) => { 149 return food.id === item.foodId 150 }) 151 var calories = foodInfo.calories * item.weight 152 var protein = foodInfo.protein * item.weight 153 var fat = foodInfo.fat * item.weight 154 var carbohydrates = foodInfo.carbohydrates * item.weight 155 oneMealStatisticsInfo.mealFoods.push(new MealFoodInfo(item.id, foodInfo.name, foodInfo.image, calories, protein, fat, carbohydrates, item.weight)) 156 oneMealStatisticsInfo.totalFat += fat 157 oneMealStatisticsInfo.totalCalories += calories 158 oneMealStatisticsInfo.totalCarbohydrates += carbohydrates 159 oneMealStatisticsInfo.totalProtein += protein 160 mealMap.set(item.mealTime.id, oneMealStatisticsInfo) 161 }) 162 163 return Array.from(mealMap.values()) 164} 165 166export function updateDietWeight(recordId: number, weight: number) { 167 let dietRecords = getDietRecords() 168 let index = dietRecords.findIndex((record) => { 169 return record.id === recordId 170 }) 171 dietRecords[index].weight = weight 172 AppStorage.SetOrCreate<Array<DietRecord>>('dietRecords', dietRecords) 173}