From 7a97f994969edf4c90750d4c6364954c1332d14f Mon Sep 17 00:00:00 2001 From: 98spag Date: Wed, 31 May 2023 13:36:44 +0200 Subject: [PATCH 1/2] add first draft of progress page chart --- .flutter-plugins-dependencies | 2 +- lib/android/config/statistics.dart | 170 +++++++++++++++++- .../pages/nav_pages/progress_page.dart | 112 ++++-------- 3 files changed, 199 insertions(+), 85 deletions(-) diff --git a/.flutter-plugins-dependencies b/.flutter-plugins-dependencies index e6a219b..78a6273 100644 --- a/.flutter-plugins-dependencies +++ b/.flutter-plugins-dependencies @@ -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\\\\FUCHSLAU\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\path_provider_foundation-2.2.2\\\\","shared_darwin_source":true,"native_build":true,"dependencies":[]}],"android":[{"name":"path_provider_android","path":"C:\\\\Users\\\\FUCHSLAU\\\\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\\\\FUCHSLAU\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\path_provider_foundation-2.2.2\\\\","shared_darwin_source":true,"native_build":true,"dependencies":[]}],"linux":[{"name":"path_provider_linux","path":"C:\\\\Users\\\\FUCHSLAU\\\\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\\\\FUCHSLAU\\\\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-30 20:39:45.634550","version":"3.10.2"} \ No newline at end of file +{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"path_provider_foundation","path":"C:\\\\Users\\\\FUCHSLAU\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\path_provider_foundation-2.2.2\\\\","shared_darwin_source":true,"native_build":true,"dependencies":[]}],"android":[{"name":"path_provider_android","path":"C:\\\\Users\\\\FUCHSLAU\\\\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\\\\FUCHSLAU\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\path_provider_foundation-2.2.2\\\\","shared_darwin_source":true,"native_build":true,"dependencies":[]}],"linux":[{"name":"path_provider_linux","path":"C:\\\\Users\\\\FUCHSLAU\\\\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\\\\FUCHSLAU\\\\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-30 22:47:26.759130","version":"3.10.2"} \ No newline at end of file diff --git a/lib/android/config/statistics.dart b/lib/android/config/statistics.dart index 87291c3..7b46f72 100644 --- a/lib/android/config/statistics.dart +++ b/lib/android/config/statistics.dart @@ -1,7 +1,9 @@ import 'dart:math'; import 'package:ernaehrung/android/components/meal_page_text/days_component.dart'; import 'package:ernaehrung/android/config/cast_helper.dart'; +import 'package:fl_chart/fl_chart.dart'; import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; import 'package:hive/hive.dart'; import '../models/food.dart'; import 'format_helper.dart'; @@ -16,7 +18,6 @@ class StatisticsService { StatisticsService._internal() { initBoxes(); } - final String reducedStatisticsBoxName = 'STATISTICS_REDUCED'; final String mainStatisticsBoxName = 'STATISTICS_MAIN'; final String progressStatisticsBoxName = 'STATISTICS_PROGRESS'; @@ -24,8 +25,7 @@ class StatisticsService { ValueNotifier> ingredients = ValueNotifier>([0,0,0]); ValueNotifier dailyAverageForCurrentWeek = ValueNotifier(0); ValueNotifier> weeklyCaloryRanking = ValueNotifier>([]); - - + ValueNotifier> barChartData = ValueNotifier>([]); initBoxes()async{ Box reducedBox = Hive.box(reducedStatisticsBoxName); @@ -223,6 +223,8 @@ class StatisticsService { dailyAverageForCurrentWeek.notifyListeners(); weeklyCaloryRanking.value = getWeeklyCaloryRanking(); weeklyCaloryRanking.notifyListeners(); + barChartData.value = getBarChartData(); + barChartData.notifyListeners(); } void updateProgressBoxValues(){ @@ -258,10 +260,170 @@ class StatisticsService { return getListOfDistinctElements(allFoodsOfWeek); } + int getSumOfCaloriesByFoodList(List foods){ + int sum = 0; + for(int i = 0; i < foods.length;i++){ + sum += foods[i].calories as int; + } + return sum; + } + + List getBarChartData(){ + int timestamp = getTimestampFromNow(); + List currentWeek = getTimestampsByTimestampAndTimespan(TimeSpan.week,timestamp); + int maxValue = 0; + List> result = + [ + [0,0,0], + [0,0,0], + [0,0,0], + [0,0,0], + [0,0,0], + [0,0,0], + [0,0,0], + ]; //[[breakfast,lunch,dinner],[breakfast,lunch,dinner] ...] + for(int i = 0;i < currentWeek.length;i++){ + Map> foodMapFromMainBoxByTimestamp = getFoodMapForGivenTimestampFromMainBox(currentWeek[i]); + Iterable keys = foodMapFromMainBoxByTimestamp.keys; + if(keys.isNotEmpty){ + for(int j = 0; j < keys.length;j++){ + int value = getSumOfCaloriesByFoodList(foodMapFromMainBoxByTimestamp[keys.elementAt(j)] ?? []); + if(value > maxValue){ + maxValue = value; + } + + result[i][j] = (value * 175) ~/ 100; + } + } + } + return [ + BarChartGroupData( + x: 0, + barRods: [BarChartRodData( + width: 7.5, // Adjust the width of the bar if needed + color: Colors.yellow, + toY: result[0][0].toDouble(), // First segment color + ), BarChartRodData( + width: 7.5, // Adjust the width of the bar if needed + color: Colors.red, + toY: result[0][1].toDouble(), // Second segment color + ), BarChartRodData( + width: 7.5, // Adjust the width of the bar if needed + color: Colors.green, + toY: result[0][2].toDouble(), // Second segment color + ),], + showingTooltipIndicators: [0], + groupVertically: true + ), + BarChartGroupData( + x: 1, + barRods: [BarChartRodData( + width: 7.5, // Adjust the width of the bar if needed + color: Colors.yellow, + toY: result[1][0].toDouble(), + // First segment color + ), BarChartRodData( + width: 7.5, // Adjust the width of the bar if needed + color: Colors.red, + toY: result[1][1].toDouble(), // Second segment color + ), BarChartRodData( + width: 7.5, // Adjust the width of the bar if needed + color: Colors.green, + toY: result[1][2].toDouble(), // Second segment color + ),], + showingTooltipIndicators: [0], + ), + BarChartGroupData( + x: 2, + barRods: [BarChartRodData( + width: 7.5, // Adjust the width of the bar if needed + color: Colors.yellow, + toY: result[2][0].toDouble(), // First segment color + ), BarChartRodData( + width: 7.5, // Adjust the width of the bar if needed + color: Colors.red, + toY: result[2][1].toDouble(), // Second segment color + ), BarChartRodData( + width: 7.5, // Adjust the width of the bar if needed + color: Colors.green, + toY: result[2][2].toDouble(), // Second segment color + ),], + showingTooltipIndicators: [0], + ), + BarChartGroupData( + x: 3, + barRods: [BarChartRodData( + width: 7.5, // Adjust the width of the bar if needed + color: Colors.yellow, + toY: result[3][0].toDouble(), // First segment color + ), BarChartRodData( + width: 7.5, // Adjust the width of the bar if needed + color: Colors.red, + toY: result[3][1].toDouble(), // Second segment color + ), BarChartRodData( + width: 7.5, // Adjust the width of the bar if needed + color: Colors.green, + toY: result[3][2].toDouble(), // Second segment color + ),], + showingTooltipIndicators: [0], + ), + BarChartGroupData( + x: 4, + barRods: [BarChartRodData( + width: 7.5, // Adjust the width of the bar if needed + color: Colors.yellow, + toY: result[4][0].toDouble(), // First segment color + ), BarChartRodData( + width: 7.5, // Adjust the width of the bar if needed + color: Colors.red, + toY: result[4][1].toDouble(), // Second segment color + ), BarChartRodData( + width: 7.5, // Adjust the width of the bar if needed + color: Colors.green, + toY: result[4][2].toDouble(), // Second segment color + ),], + showingTooltipIndicators: [0], + ), + BarChartGroupData( + x: 5, + barRods: [BarChartRodData( + width: 7.5, // Adjust the width of the bar if needed + color: Colors.yellow, + toY: result[5][0].toDouble(), // First segment color + ), BarChartRodData( + width: 7.5, // Adjust the width of the bar if needed + color: Colors.red, + toY: result[5][1].toDouble(), // Second segment color + ), BarChartRodData( + width: 7.5, // Adjust the width of the bar if needed + color: Colors.green, + toY: result[5][2].toDouble(), // Second segment color + ),], showingTooltipIndicators: [0], + ), + BarChartGroupData( + x: 6, + barRods: [BarChartRodData( + width: 7.5, // Adjust the width of the bar if needed + color: Colors.yellow, + toY: result[6][0].toDouble(), // First segment color + ), BarChartRodData( + width: 7.5, // Adjust the width of the bar if needed + color: Colors.red, + toY: result[6][1].toDouble(), // Second segment color + ), BarChartRodData( + width: 7.5, // Adjust the width of the bar if needed + color: Colors.green, + toY: result[6][2].toDouble(), // Second segment color + ),], showingTooltipIndicators: [0], + ), + ]; + } + getTimestampFromNow(){ DateTime now = DateTime.now(); return now.millisecondsSinceEpoch.toInt() ~/ 1000; } + showItems(){ print("Statistics.dart - showItems() - ITEMS"); //Hive.box(boxName).clear(); @@ -273,4 +435,6 @@ class StatisticsService { } } + + } \ No newline at end of file diff --git a/lib/android/pages/nav_pages/progress_page.dart b/lib/android/pages/nav_pages/progress_page.dart index 287626b..b84b491 100644 --- a/lib/android/pages/nav_pages/progress_page.dart +++ b/lib/android/pages/nav_pages/progress_page.dart @@ -35,7 +35,8 @@ class ProgressPage extends StatelessWidget { valueListenable: StatisticsService.instance.dailyAverageForCurrentWeek, builder: (context, value, child) { return Container( - height: 284, + height: 100, + width: 400, decoration: BoxDecoration( border: Border.all(), ), @@ -48,6 +49,19 @@ class ProgressPage extends StatelessWidget { ), const SecondaryTextComponent("Durchschnittlich"), SecondaryBigTextComponent(value.toString()), + ], + ), + ); + }, + ), + ValueListenableBuilder( + valueListenable: StatisticsService.instance.barChartData, + builder: (context, value, child) { + return SizedBox( + height: 205, + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ SizedBox( height: 200, child: BarChart( @@ -55,7 +69,7 @@ class ProgressPage extends StatelessWidget { barTouchData: barTouchData, titlesData: titlesData, borderData: borderData, - barGroups: barGroups, + barGroups: value, gridData: FlGridData(show: false), alignment: BarChartAlignment.spaceAround, maxY: 200, @@ -127,25 +141,25 @@ class ProgressPage extends StatelessWidget { String text; switch (value.toInt()) { case 0: - text = 'Mn'; + text = 'M'; break; case 1: - text = 'Te'; + text = 'T'; break; case 2: - text = 'Wd'; + text = 'W'; break; case 3: - text = 'Tu'; + text = 'T'; break; case 4: - text = 'Fr'; + text = 'F'; break; case 5: - text = 'St'; + text = 'S'; break; case 6: - text = 'Sn'; + text = 'S'; break; default: text = ''; @@ -190,77 +204,13 @@ class ProgressPage extends StatelessWidget { begin: Alignment.bottomCenter, end: Alignment.topCenter, ); + LinearGradient get _breakfastGradient => const LinearGradient( + colors: [ + Colors.green, + Colors.yellow, + ], + begin: Alignment.bottomCenter, + end: Alignment.topCenter, + ); - List get barGroups => [ - BarChartGroupData( - x: 0, - barRods: [ - BarChartRodData( - toY: 8, - gradient: _barsGradient, - ) - ], - showingTooltipIndicators: [0], - ), - BarChartGroupData( - x: 1, - barRods: [ - BarChartRodData( - toY: 10, - gradient: _barsGradient, - ) - ], - showingTooltipIndicators: [0], - ), - BarChartGroupData( - x: 2, - barRods: [ - BarChartRodData( - toY: 14, - gradient: _barsGradient, - ) - ], - showingTooltipIndicators: [0], - ), - BarChartGroupData( - x: 3, - barRods: [ - BarChartRodData( - toY: 150, - gradient: _barsGradient, - ) - ], - showingTooltipIndicators: [0], - ), - BarChartGroupData( - x: 4, - barRods: [ - BarChartRodData( - toY: 13, - gradient: _barsGradient, - ) - ], - showingTooltipIndicators: [0], - ), - BarChartGroupData( - x: 5, - barRods: [ - BarChartRodData( - toY: 10, - gradient: _barsGradient, - ) - ], - showingTooltipIndicators: [0], - ), - BarChartGroupData( - x: 6, - barRods: [ - BarChartRodData( - toY: 16, - gradient: _barsGradient, - ) - ], - showingTooltipIndicators: [0], - ), - ]; } From b93bf41961abc584836818282099f47ff3fca5cd Mon Sep 17 00:00:00 2001 From: bogdan <1926167@stud.hs-mannheim.de> Date: Thu, 1 Jun 2023 11:46:53 +0200 Subject: [PATCH 2/2] bug fixing --- .../components/card/card_data_food_component.dart | 15 ++++++++++----- .../components/circular/circular_component.dart | 10 ++++++---- .../statistics_today_component.dart | 8 ++++---- .../statistics_circular_indicator_component.dart | 2 +- lib/android/config/statistics.dart | 3 +-- 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/lib/android/components/card/card_data_food_component.dart b/lib/android/components/card/card_data_food_component.dart index efa3e47..b757c87 100644 --- a/lib/android/components/card/card_data_food_component.dart +++ b/lib/android/components/card/card_data_food_component.dart @@ -9,11 +9,8 @@ class CardDataFoodComponent extends StatelessWidget { @override Widget build(BuildContext context) { - return Container( + return SizedBox( height: 180, - decoration: const BoxDecoration( - color: Colors.pink, - borderRadius: BorderRadius.all(Radius.circular(16))), child: Padding( padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 8), child: SingleChildScrollView( @@ -25,7 +22,15 @@ class CardDataFoodComponent extends StatelessWidget { shrinkWrap: true, itemCount: foods.length, itemBuilder: (context, i) { - return CardFoodItemComponent(foods[i]); + return Column( + children: [ + CardFoodItemComponent(foods[i]), + Divider( + color: Colors.grey.shade300, + thickness: 1.2, + ) + ], + ); }) ], ), diff --git a/lib/android/components/circular/circular_component.dart b/lib/android/components/circular/circular_component.dart index 568a333..a0fe388 100644 --- a/lib/android/components/circular/circular_component.dart +++ b/lib/android/components/circular/circular_component.dart @@ -1,10 +1,12 @@ import 'package:ernaehrung/android/config/statistics.dart'; +import 'package:ernaehrung/android/models/user.dart'; import 'package:flutter/material.dart'; +import 'package:hive/hive.dart'; import 'package:percent_indicator/circular_percent_indicator.dart'; class CircularLoadingComponent extends StatelessWidget { - const CircularLoadingComponent({Key? key}) : super(key: key); - final int targetCaolries = 3500; // TODO get from user data + CircularLoadingComponent({Key? key}) : super(key: key); + final int? targetCaolries = Hive.box("USER_BOX").get("USER")?.kalorien; @override Widget build(BuildContext context) {; @@ -12,7 +14,7 @@ class CircularLoadingComponent extends StatelessWidget { return ValueListenableBuilder( valueListenable: StatisticsService.instance.eatenCalories, builder: (context, value, child) { - double progress = double.parse((value / targetCaolries).toStringAsFixed(1)); + double progress = double.parse((value / targetCaolries!).toStringAsFixed(1)); progress = progress > 1.0 ? 1.0 : progress; return Column( @@ -24,7 +26,7 @@ class CircularLoadingComponent extends StatelessWidget { lineWidth: 5.0, percent: progress, center: Text( - "$value/$targetCaolries kcal", + "$value/$targetCaolries Kcal", textAlign: TextAlign.center, ), progressColor: Colors.lightGreen, diff --git a/lib/android/components/meal_page_text/statistics_today_component.dart b/lib/android/components/meal_page_text/statistics_today_component.dart index 1808eaf..9fed7c2 100644 --- a/lib/android/components/meal_page_text/statistics_today_component.dart +++ b/lib/android/components/meal_page_text/statistics_today_component.dart @@ -1,7 +1,6 @@ import 'package:ernaehrung/android/components/circular/circular_component.dart'; import 'package:ernaehrung/android/components/circular/line_circular_with_text_component.dart'; import 'package:ernaehrung/android/config/statistics.dart'; -import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; class StatisticsPercentage extends StatelessWidget { @@ -11,7 +10,7 @@ class StatisticsPercentage extends StatelessWidget { const StatisticsPercentage({Key? key}) : super(key: key); @override - Widget build(BuildContext context) {; + Widget build(BuildContext context) { return Container( decoration: BoxDecoration(border: Border.all(width: 2.0, color: Colors.black)), @@ -20,10 +19,10 @@ class StatisticsPercentage extends StatelessWidget { child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - const Expanded( + Expanded( child: SizedBox( child: Padding( - padding: EdgeInsets.symmetric(vertical: 0, horizontal: 8), + padding: const EdgeInsets.symmetric(vertical: 0, horizontal: 8), child: FittedBox( child: CircularLoadingComponent(), ), @@ -34,6 +33,7 @@ class StatisticsPercentage extends StatelessWidget { child: ValueListenableBuilder>( valueListenable: StatisticsService.instance.ingredients, builder: (context, value, child) { + print("value ${value}"); return Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ diff --git a/lib/android/components/statistics_circular_indicator_component.dart b/lib/android/components/statistics_circular_indicator_component.dart index 1051537..fcd648a 100644 --- a/lib/android/components/statistics_circular_indicator_component.dart +++ b/lib/android/components/statistics_circular_indicator_component.dart @@ -18,7 +18,7 @@ class StatisticsPercentComponent extends StatelessWidget { mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text("$eaten gegessen"), - const CircularLoadingComponent(), + CircularLoadingComponent(), Text("$calorienBurned verbrannt"), ], ); diff --git a/lib/android/config/statistics.dart b/lib/android/config/statistics.dart index 7b46f72..4cfc158 100644 --- a/lib/android/config/statistics.dart +++ b/lib/android/config/statistics.dart @@ -2,7 +2,6 @@ import 'dart:math'; import 'package:ernaehrung/android/components/meal_page_text/days_component.dart'; import 'package:ernaehrung/android/config/cast_helper.dart'; import 'package:fl_chart/fl_chart.dart'; -import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:hive/hive.dart'; import '../models/food.dart'; @@ -215,7 +214,7 @@ class StatisticsService { carbs += food.carbohydrateg; } } - return [fat as double,protein as double,carbs as double]; + return [fat.roundToDouble(),protein.roundToDouble(),carbs.roundToDouble()]; } void updateCalculationsAndNotfiyListenersForTodayStatistics(){