feat: add statistics service

welcome
98spag 2023-05-14 09:22:26 +02:00
parent 0fd124bf5c
commit 5d3f12c9a1
6 changed files with 86 additions and 11 deletions

3
.env
View File

@ -4,10 +4,7 @@ SECOND_NAME_FIELD=SECOND_NAME
WEIGHT_FIELD=WEIGHT
HEIGHT_FIELD=HEIGHT
CALORIES_FIELD=CALORIES
FIRST_NAME_FIELD=FIRST_NAME
FIRST_NAME_FIELD=FIRST_NAME
TODAY_BOX=TODAY
MEALPLAN_BOX=MEALPLAN
DATE_FIELD=DATE # datum
BREAKFAST_FIELD=FRÜHSTÜCK # frühstück
LUNCH_FIELD=MITTAGESSEN # mittagessen

View File

@ -1 +1 @@
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"path_provider_foundation","path":"C:\\\\Users\\\\Nutzer\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\path_provider_foundation-2.2.2\\\\","native_build":true,"dependencies":[]}],"android":[{"name":"path_provider_android","path":"C:\\\\Users\\\\Nutzer\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\path_provider_android-2.0.27\\\\","native_build":true,"dependencies":[]}],"macos":[{"name":"path_provider_foundation","path":"C:\\\\Users\\\\Nutzer\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\path_provider_foundation-2.2.2\\\\","native_build":true,"dependencies":[]}],"linux":[{"name":"path_provider_linux","path":"C:\\\\Users\\\\Nutzer\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\path_provider_linux-2.1.10\\\\","native_build":false,"dependencies":[]}],"windows":[{"name":"path_provider_windows","path":"C:\\\\Users\\\\Nutzer\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\path_provider_windows-2.1.6\\\\","native_build":false,"dependencies":[]}],"web":[]},"dependencyGraph":[{"name":"path_provider","dependencies":["path_provider_android","path_provider_foundation","path_provider_linux","path_provider_windows"]},{"name":"path_provider_android","dependencies":[]},{"name":"path_provider_foundation","dependencies":[]},{"name":"path_provider_linux","dependencies":[]},{"name":"path_provider_windows","dependencies":[]}],"date_created":"2023-05-13 11:33:23.416757","version":"3.7.11"}
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"path_provider_foundation","path":"C:\\\\Users\\\\Nutzer\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\path_provider_foundation-2.2.2\\\\","native_build":true,"dependencies":[]}],"android":[{"name":"path_provider_android","path":"C:\\\\Users\\\\Nutzer\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\path_provider_android-2.0.27\\\\","native_build":true,"dependencies":[]}],"macos":[{"name":"path_provider_foundation","path":"C:\\\\Users\\\\Nutzer\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\path_provider_foundation-2.2.2\\\\","native_build":true,"dependencies":[]}],"linux":[{"name":"path_provider_linux","path":"C:\\\\Users\\\\Nutzer\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\path_provider_linux-2.1.10\\\\","native_build":false,"dependencies":[]}],"windows":[{"name":"path_provider_windows","path":"C:\\\\Users\\\\Nutzer\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\path_provider_windows-2.1.6\\\\","native_build":false,"dependencies":[]}],"web":[]},"dependencyGraph":[{"name":"path_provider","dependencies":["path_provider_android","path_provider_foundation","path_provider_linux","path_provider_windows"]},{"name":"path_provider_android","dependencies":[]},{"name":"path_provider_foundation","dependencies":[]},{"name":"path_provider_linux","dependencies":[]},{"name":"path_provider_windows","dependencies":[]}],"date_created":"2023-05-14 09:21:07.613440","version":"3.7.11"}

View File

@ -1,4 +1,5 @@
import 'package:assorted_layout_widgets/assorted_layout_widgets.dart';
import 'package:ernaehrung/android/config/statistics.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:hive/hive.dart';
@ -15,8 +16,10 @@ class SearchedFoodComponent extends StatefulWidget {
}
class _SearchFoodComponentState extends State<SearchedFoodComponent> {
StatisticsService statisticsService = StatisticsService();
void storeFood() async {
statisticsService.addItemToStatisticsBox(widget.food, widget.cardName);
final todayBox = Hive.box(dotenv.env['TODAY_BOX']!);
if (!todayBox.isOpen){
Hive.openBox(dotenv.env['TODAY_BOX']!);

View File

@ -0,0 +1,80 @@
import 'package:hive/hive.dart';
import '../models/food.dart';
class StatisticsService {
final String boxName = 'STATISTICS';
StatisticsService() {
initBox();
}
initBox()async{
await Hive.openBox(boxName);
}
addItemToStatisticsBox(Food value, String mealType){
// Hive.deleteFromDisk();
Box box = Hive.box(boxName);
DateTime dateTime = DateTime.now();
int timestamp = dateTime.millisecondsSinceEpoch.toInt();
Map<String, List<Food>>? valueMap = box.get(timestamp);
valueMap ??= {
"FRÜHSTÜCK": [],
"MITTAGESSEN": [],
"ABENDESSEN": []
};
List<Food> values = [];
if(valueMap.containsKey(mealType)){
values = valueMap[mealType]!;
}
values.add(value);
valueMap[mealType] = values;
box.put(timestamp, valueMap);
showItems();
}
num getAllCaloriesByTimestamp(DateTime date){
Box box = Hive.box(boxName);
int timestamp = date.millisecondsSinceEpoch.toInt();
Map<String, List<Food>>? valueMap = box.get(timestamp);
if(valueMap == null){
return 0;
}
num sum = 0;
for(var mealType in valueMap.keys){
if(valueMap.containsKey(mealType)){
List<Food> values = valueMap[mealType]!;
for(var value in values){
sum += value.calories;
}
}
}
return sum;
}
num getCaloriesByTimestampAndMealType(DateTime timestamp, String mealType){
Box box = Hive.box(boxName);
Map<String, List<Food>>? valueMap = box.get(timestamp);
if(valueMap == null){
return 0;
}
num sum = 0;
if(valueMap.containsKey(mealType)){
List<Food> values = valueMap[mealType]!;
for(var value in values){
sum += value.calories;
}
}
return sum;
}
showItems(){
print("ITEMS");
print(Hive.box(boxName).keys);
}
}

View File

@ -1,10 +1,6 @@
import 'dart:math';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:hive_flutter/adapters.dart';
import '../../components/card_component.dart';
import '../../models/food.dart';
@ -41,7 +37,6 @@ class _MealPlanState extends State<MealPlanPage> {
for (Food element in dynamicList) {
foodList.add(element);
}
print(foodList.length);
return foodList;
}

View File

@ -16,8 +16,8 @@ void main() async {
Hive.registerAdapter(FoodAdapter());
}
await Hive.openBox(dotenv.env['TODAY_BOX']!);
await Hive.openBox(dotenv.env['MEALPLAN_BOX']!);
await setupTodayBox();
if(defaultTargetPlatform == TargetPlatform.android){