2023-05-14 09:22:26 +02:00
|
|
|
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();
|
2023-05-14 09:27:23 +02:00
|
|
|
int timestamp = dateTime.millisecondsSinceEpoch.toInt() ~/ 1000;
|
|
|
|
print(timestamp);
|
2023-05-14 09:22:26 +02:00
|
|
|
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);
|
2023-05-14 09:27:23 +02:00
|
|
|
int timestamp = date.millisecondsSinceEpoch.toInt() ~/ 1000;
|
2023-05-14 09:22:26 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-05-14 09:27:23 +02:00
|
|
|
num getCaloriesByTimestampAndMealType(DateTime date, String mealType){
|
2023-05-14 09:22:26 +02:00
|
|
|
Box box = Hive.box(boxName);
|
2023-05-14 09:27:23 +02:00
|
|
|
int timestamp = date.millisecondsSinceEpoch.toInt() ~/ 1000;
|
2023-05-14 09:22:26 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|