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>? valueMap = box.get(timestamp); valueMap ??= { "FRÜHSTÜCK": [], "MITTAGESSEN": [], "ABENDESSEN": [] }; List 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>? valueMap = box.get(timestamp); if(valueMap == null){ return 0; } num sum = 0; for(var mealType in valueMap.keys){ if(valueMap.containsKey(mealType)){ List 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>? valueMap = box.get(timestamp); if(valueMap == null){ return 0; } num sum = 0; if(valueMap.containsKey(mealType)){ List values = valueMap[mealType]!; for(var value in values){ sum += value.calories; } } return sum; } showItems(){ print("ITEMS"); print(Hive.box(boxName).keys); } }