276 lines
9.7 KiB
Dart
276 lines
9.7 KiB
Dart
import 'dart:math';
|
|
import 'package:ernaehrung/android/components/meal_page_text/days_component.dart';
|
|
import 'package:ernaehrung/android/config/cast_helper.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:hive/hive.dart';
|
|
import '../models/food.dart';
|
|
import 'format_helper.dart';
|
|
|
|
class StatisticsService {
|
|
|
|
static final StatisticsService _instance = StatisticsService._internal();
|
|
factory StatisticsService() => _instance;
|
|
|
|
static StatisticsService get instance => _instance;
|
|
|
|
StatisticsService._internal() {
|
|
initBoxes();
|
|
}
|
|
|
|
final String reducedStatisticsBoxName = 'STATISTICS_REDUCED';
|
|
final String mainStatisticsBoxName = 'STATISTICS_MAIN';
|
|
final String progressStatisticsBoxName = 'STATISTICS_PROGRESS';
|
|
ValueNotifier<int> eatenCalories = ValueNotifier<int>(0);
|
|
ValueNotifier<List<double>> ingredients = ValueNotifier<List<double>>([0,0,0]);
|
|
ValueNotifier<int> dailyAverageForCurrentWeek = ValueNotifier<int>(0);
|
|
ValueNotifier<List<Food>> weeklyCaloryRanking = ValueNotifier<List<Food>>([]);
|
|
|
|
|
|
|
|
initBoxes()async{
|
|
Box reducedBox = Hive.box(reducedStatisticsBoxName);
|
|
Box progressBox = Hive.box(progressStatisticsBoxName);
|
|
|
|
putIfKeyNotExists([reducedBox,progressBox], 'FRÜHSTÜCK', []);
|
|
putIfKeyNotExists([reducedBox,progressBox], 'MITTAGESSEN', []);
|
|
putIfKeyNotExists([reducedBox,progressBox], 'ABENDESSEN', []);
|
|
updateReducedBoxByTimespan(TimeSpan.day);
|
|
}
|
|
|
|
void putIfKeyNotExists(List<Box> boxes, String key, dynamic value) {
|
|
for(int i = 0; i < boxes.length;i++){
|
|
if (!boxes[i].containsKey(key)) {
|
|
boxes[i].put(key, value);
|
|
}
|
|
}
|
|
}
|
|
|
|
updateReducedBoxByTimespan(TimeSpan timeSpan){
|
|
clearReducedBoxBeforeUpdate();
|
|
int timestamp = getTimestampFromNow();
|
|
switch(timeSpan){
|
|
case TimeSpan.day:
|
|
getNewFoodAndUpdateBoxByTimestampAndBox(timestamp, Hive.box(reducedStatisticsBoxName));
|
|
break;
|
|
case TimeSpan.week:
|
|
List<int> currentWeek = getTimestampsByTimestampAndTimespan(TimeSpan.week,timestamp);
|
|
for(int i = 0;i < currentWeek.length;i++){
|
|
getNewFoodAndUpdateBoxByTimestampAndBox(currentWeek[i],Hive.box(reducedStatisticsBoxName));
|
|
}
|
|
break;
|
|
case TimeSpan.month:
|
|
List<int> currentMonth = getTimestampsByTimestampAndTimespan(TimeSpan.month,timestamp);
|
|
for(int i = 0;i < currentMonth.length;i++){
|
|
getNewFoodAndUpdateBoxByTimestampAndBox(currentMonth[i],Hive.box(reducedStatisticsBoxName));
|
|
}
|
|
break;
|
|
}
|
|
updateCalculationsAndNotfiyListenersForPorgressStatistics();
|
|
}
|
|
|
|
void updateCalculationsAndNotfiyListenersForPorgressStatistics(){
|
|
eatenCalories.value = getAllEatenCaloriesByBox(Hive.box(reducedStatisticsBoxName));
|
|
eatenCalories.notifyListeners();
|
|
ingredients.value = getAllEatenIngredientsForTodayStatistics();
|
|
ingredients.notifyListeners();
|
|
}
|
|
|
|
void getNewFoodAndUpdateBoxByTimestampAndBox(int timestamp, Box box){
|
|
Map<String,List<Food>> newFood = getFoodMapForGivenTimestampFromMainBox(timestamp);
|
|
if(newFood.keys.isNotEmpty){
|
|
setElementsOfBoxByBox(newFood, box);
|
|
}
|
|
}
|
|
|
|
List<int> getTimestampsByTimestampAndTimespan(TimeSpan timespan, int timestamp) {
|
|
int range = timespan == TimeSpan.week ? 7 : 31;
|
|
int targetWeekday = DateTime.monday; // Example target weekday (Monday)
|
|
DateTime currentDateTime = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
|
|
int currentWeekday = currentDateTime.weekday;
|
|
int daysToAdd = targetWeekday - currentWeekday;
|
|
DateTime targetDateTime = currentDateTime.add(Duration(days: daysToAdd));
|
|
|
|
List<int> timestampsForWeekdays = [];
|
|
for (int i = 0; i < range; i++) {
|
|
timestampsForWeekdays.add(targetDateTime.millisecondsSinceEpoch ~/ 1000);
|
|
targetDateTime = targetDateTime.add(const Duration(days: 1));
|
|
}
|
|
return timestampsForWeekdays;
|
|
}
|
|
|
|
clearReducedBoxBeforeUpdate(){
|
|
Box box = Hive.box(reducedStatisticsBoxName);
|
|
for(int i = 0; i < box.keys.length;i++){
|
|
box.put(box.keys.elementAt(i), []);
|
|
}
|
|
}
|
|
|
|
setElementsOfBoxByBox(Map<String,List<Food>> newFood,Box box){
|
|
Iterable<String> keys = newFood.keys;
|
|
for(int i = 0; i < keys.length;i++){
|
|
List<Food> alreadyExisting = castDynamicToListFood(box.get(keys.elementAt(i)));
|
|
alreadyExisting.addAll(newFood[keys.elementAt(i)] as Iterable<Food>);
|
|
box.put(keys.elementAt(i), alreadyExisting);
|
|
}
|
|
}
|
|
|
|
getDayAsIntFromTimestamp(int timestamp){
|
|
DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
|
|
return dateTime.day;
|
|
}
|
|
|
|
Map<String,List<Food>> getFoodMapForGivenTimestampFromMainBox(int timestamp){
|
|
Box box = Hive.box(mainStatisticsBoxName);
|
|
dynamic matchingTimestamp = getMatchingTimeStamp(box, timestamp);
|
|
if(matchingTimestamp != null){
|
|
return castDynamicMap(box.get(matchingTimestamp));
|
|
}
|
|
return <String,List<Food>>{};
|
|
}
|
|
|
|
getMatchingTimeStamp(Box box,int newTimestamp){
|
|
if(box.keys.isNotEmpty){
|
|
for(int i = 0; i < box.keys.length;i++){
|
|
int timestamp = box.keys.elementAt(i);
|
|
if(isDateEqual(newTimestamp, timestamp)){
|
|
return timestamp;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
getMonthAsIntFromTimestamp(int timestamp){
|
|
DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
|
|
return dateTime.month;
|
|
}
|
|
|
|
getYearAsIntFromTimestamp(int timestamp){
|
|
DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
|
|
return dateTime.year;
|
|
}
|
|
|
|
isDateEqual(int timestamp1, int timestamp2){
|
|
return
|
|
getDayAsIntFromTimestamp(timestamp1) == getDayAsIntFromTimestamp(timestamp2) &&
|
|
getMonthAsIntFromTimestamp(timestamp1) == getMonthAsIntFromTimestamp(timestamp2) &&
|
|
getYearAsIntFromTimestamp(timestamp1) == getYearAsIntFromTimestamp(timestamp2);
|
|
}
|
|
|
|
addItemToMainBox(Food value, String mealType){
|
|
// Hive.deleteFromDisk();
|
|
Box box = Hive.box(mainStatisticsBoxName);
|
|
DateTime dateTime = DateTime.now();
|
|
|
|
// DEBUG
|
|
//DateTime dateTime = getRandomTimestampForTesting();
|
|
|
|
int newTimestamp = dateTime.millisecondsSinceEpoch.toInt() ~/ 1000;
|
|
|
|
dynamic matchingTimestamp = getMatchingTimeStamp(box, newTimestamp);
|
|
if(matchingTimestamp != null){
|
|
newTimestamp = matchingTimestamp;
|
|
}
|
|
Map<String, List<Food>> valueMap = castDynamicMap(box.get(newTimestamp));
|
|
List<Food> values = [];
|
|
if(valueMap.containsKey(mealType)){
|
|
values = valueMap[mealType]!;
|
|
}
|
|
values.add(value);
|
|
valueMap[mealType] = values;
|
|
box.put(newTimestamp, valueMap);
|
|
}
|
|
|
|
getRandomTimestampForTesting(){
|
|
DateTime now = DateTime.now();
|
|
DateTime startOfWeek = now.subtract(Duration(days: now.weekday - 1));
|
|
DateTime endOfWeek = startOfWeek.add(const Duration(days: 6));
|
|
|
|
Random random = Random();
|
|
int randomMilliseconds = random.nextInt(endOfWeek.millisecondsSinceEpoch - startOfWeek.millisecondsSinceEpoch);
|
|
DateTime randomTimestamp = startOfWeek.add(Duration(milliseconds: randomMilliseconds));
|
|
|
|
return randomTimestamp;
|
|
}
|
|
|
|
int getAllEatenCaloriesByBox(Box box){
|
|
num sum = 0;
|
|
for(int i = 0; i < box.keys.length;i++){
|
|
for(Food food in box.get(box.keys.elementAt(i))){
|
|
sum += food.calories;
|
|
}
|
|
}
|
|
return sum as int;
|
|
}
|
|
|
|
List<double> getAllEatenIngredientsForTodayStatistics(){
|
|
Box box = Hive.box(reducedStatisticsBoxName);
|
|
num fat = 0;
|
|
num protein = 0;
|
|
num carbs = 0;
|
|
for(int i = 0; i < box.keys.length;i++){
|
|
for(Food food in box.get(box.keys.elementAt(i))){
|
|
fat += food.fatg;
|
|
protein += food.proteing;
|
|
carbs += food.carbohydrateg;
|
|
}
|
|
}
|
|
return [fat as double,protein as double,carbs as double];
|
|
}
|
|
|
|
void updateCalculationsAndNotfiyListenersForTodayStatistics(){
|
|
dailyAverageForCurrentWeek.value = getAverageCaloriesForCurrentWeekOnDailyBasis();
|
|
dailyAverageForCurrentWeek.notifyListeners();
|
|
weeklyCaloryRanking.value = getWeeklyCaloryRanking();
|
|
weeklyCaloryRanking.notifyListeners();
|
|
}
|
|
|
|
void updateProgressBoxValues(){
|
|
Box box = Hive.box(progressStatisticsBoxName);
|
|
box.clear();
|
|
int timestamp = getTimestampFromNow();
|
|
List<int> currentWeek = getTimestampsByTimestampAndTimespan(TimeSpan.week,timestamp);
|
|
for(int i = 0;i < currentWeek.length;i++){
|
|
getNewFoodAndUpdateBoxByTimestampAndBox(currentWeek[i],box);
|
|
}
|
|
updateCalculationsAndNotfiyListenersForTodayStatistics();
|
|
}
|
|
|
|
int getAverageCaloriesForCurrentWeekOnDailyBasis(){
|
|
Box box = Hive.box(progressStatisticsBoxName);
|
|
return getAllEatenCaloriesByBox(box)~/7;
|
|
}
|
|
|
|
List<Food> getWeeklyCaloryRanking(){
|
|
int timestamp = getTimestampFromNow();
|
|
List<int> currentWeek = getTimestampsByTimestampAndTimespan(TimeSpan.week,timestamp);
|
|
List<Food> allFoodsOfWeek = [];
|
|
for(int i = 0;i < currentWeek.length;i++){
|
|
Map<String,List<Food>> foodFromMainBox = getFoodMapForGivenTimestampFromMainBox(currentWeek[i]);
|
|
Iterable<String> keys = foodFromMainBox.keys;
|
|
if(keys.isNotEmpty){
|
|
for(int i = 0; i < keys.length;i++ ){
|
|
allFoodsOfWeek.addAll(foodFromMainBox[keys.elementAt(i)] as Iterable<Food>);
|
|
}
|
|
}
|
|
}
|
|
allFoodsOfWeek.sort((a, b) => b.calories - a.calories);
|
|
return getListOfDistinctElements(allFoodsOfWeek);
|
|
}
|
|
|
|
getTimestampFromNow(){
|
|
DateTime now = DateTime.now();
|
|
return now.millisecondsSinceEpoch.toInt() ~/ 1000;
|
|
}
|
|
showItems(){
|
|
print("Statistics.dart - showItems() - ITEMS");
|
|
//Hive.box(boxName).clear();
|
|
print(Hive.box(mainStatisticsBoxName).keys.length);
|
|
for(int i = 0; i < Hive.box(mainStatisticsBoxName).keys.length; i++){
|
|
print(Hive.box(mainStatisticsBoxName).keys.elementAt(i));
|
|
print(Hive.box(mainStatisticsBoxName).values.elementAt(i));
|
|
//print(Hive.box(boxName).keys.elementAt(i) + " " + Hive.box(boxName).values.elementAt(i));
|
|
}
|
|
}
|
|
|
|
} |