Flutter-Ernaehrungsapp/lib/android/config/statistics.dart

81 lines
1.9 KiB
Dart

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() ~/ 1000;
print(timestamp);
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() ~/ 1000;
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 date, String mealType){
Box box = Hive.box(boxName);
int timestamp = date.millisecondsSinceEpoch.toInt() ~/ 1000;
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);
}
}