From cc1ea62d2d30f62720a20d3a7a7421271dd91c6a Mon Sep 17 00:00:00 2001 From: 98spag Date: Tue, 30 May 2023 21:03:46 +0200 Subject: [PATCH] feat: add observable values for charts --- .flutter-plugins-dependencies | 2 +- .../circular/circular_component.dart | 42 +++++++++++++------ .../line_circular_with_text_component.dart | 1 + .../statistics_text_component.dart | 4 +- .../statistics_today_component.dart | 38 +++++++++-------- ...atistics_circular_indicator_component.dart | 2 +- lib/android/config/statistics.dart | 32 ++++++++++---- lib/android/pages/nav_pages/main_page.dart | 5 +++ 8 files changed, 86 insertions(+), 40 deletions(-) diff --git a/.flutter-plugins-dependencies b/.flutter-plugins-dependencies index 16ccc09..e6a219b 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 16:44:16.405074","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 20:39:45.634550","version":"3.10.2"} \ No newline at end of file diff --git a/lib/android/components/circular/circular_component.dart b/lib/android/components/circular/circular_component.dart index 5106623..568a333 100644 --- a/lib/android/components/circular/circular_component.dart +++ b/lib/android/components/circular/circular_component.dart @@ -1,21 +1,39 @@ +import 'package:ernaehrung/android/config/statistics.dart'; import 'package:flutter/material.dart'; import 'package:percent_indicator/circular_percent_indicator.dart'; class CircularLoadingComponent extends StatelessWidget { - final int eatenCalories; - const CircularLoadingComponent(this.eatenCalories, {Key? key}) : super(key: key); + const CircularLoadingComponent({Key? key}) : super(key: key); final int targetCaolries = 3500; // TODO get from user data @override - Widget build(BuildContext context) { - double progress = double.parse((eatenCalories/targetCaolries).toStringAsFixed(1)); - return CircularPercentIndicator( - animation: true, - radius: 60.0, - lineWidth: 5.0, - percent: progress, - center: Text("${progress*100} %"), - progressColor: Colors.lightGreen, - ); + Widget build(BuildContext context) {; + + return ValueListenableBuilder( + valueListenable: StatisticsService.instance.eatenCalories, + builder: (context, value, child) { + double progress = double.parse((value / targetCaolries).toStringAsFixed(1)); + progress = progress > 1.0 ? 1.0 : progress; + + return Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + CircularPercentIndicator( + animation: true, + radius: 60.0, + lineWidth: 5.0, + percent: progress, + center: Text( + "$value/$targetCaolries kcal", + textAlign: TextAlign.center, + ), + progressColor: Colors.lightGreen, + ), + ], + ); + }, + ); + + } } \ No newline at end of file diff --git a/lib/android/components/circular/line_circular_with_text_component.dart b/lib/android/components/circular/line_circular_with_text_component.dart index ab41c94..1cd91c4 100644 --- a/lib/android/components/circular/line_circular_with_text_component.dart +++ b/lib/android/components/circular/line_circular_with_text_component.dart @@ -16,6 +16,7 @@ class LineCircularWiTextComponent extends StatelessWidget { @override Widget build(BuildContext context) { double progress = double.parse((value/total).toStringAsFixed(1)); + progress = progress > 1.0 ? 1.0 : progress; return Padding( padding: const EdgeInsets.symmetric(vertical: 0, horizontal: 8), child: Column( diff --git a/lib/android/components/meal_page_text/statistics_text_component.dart b/lib/android/components/meal_page_text/statistics_text_component.dart index 365e324..098fe8a 100644 --- a/lib/android/components/meal_page_text/statistics_text_component.dart +++ b/lib/android/components/meal_page_text/statistics_text_component.dart @@ -11,8 +11,8 @@ class StaticsTextComponent extends StatelessWidget { return Text(name, textAlign: TextAlign.left, style: TextStyle( - color: const Color.fromARGB(255, 211, 211, 211), - fontSize: titleOrSecondary ? 14 : 10, + color: const Color(0xff000000), + fontSize: titleOrSecondary ? 15 : 13, fontWeight: titleOrSecondary ? FontWeight.w600 : FontWeight.w400 )); } 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 7d6e06f..6916795 100644 --- a/lib/android/components/meal_page_text/statistics_today_component.dart +++ b/lib/android/components/meal_page_text/statistics_today_component.dart @@ -11,9 +11,7 @@ class StatisticsPercentage extends StatelessWidget { const StatisticsPercentage({Key? key}) : super(key: key); @override - Widget build(BuildContext context) { - StatisticsService statisticsService = StatisticsService(); - List ingredients = statisticsService.getAllEatenIngredientsForTodayStatistics(); + Widget build(BuildContext context) {; return Container( decoration: BoxDecoration(border: Border.all(width: 2.0, color: Colors.black)), @@ -22,28 +20,34 @@ class StatisticsPercentage extends StatelessWidget { child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - Expanded( + const Expanded( child: SizedBox( child: Padding( - padding: const EdgeInsets.symmetric(vertical: 0, horizontal: 8), + padding: EdgeInsets.symmetric(vertical: 0, horizontal: 8), child: FittedBox( - child: CircularLoadingComponent(statisticsService.getAllEatenCaloriesForTodayStatistics()), + child: CircularLoadingComponent(), ), ), ), ), Expanded( - child: Column( - mainAxisAlignment: MainAxisAlignment.spaceEvenly, - children: [ - // TODO adjust total values (200) to user inputs - LineCircularWiTextComponent( - Colors.green.shade400, ingredients[0], 200, "Fat (g)"), - LineCircularWiTextComponent( - Colors.green.shade400, ingredients[1], 200, "Protein (g)"), - LineCircularWiTextComponent( - Colors.green.shade400, ingredients[2], 200, "Carbohydrate (g)"), - ], + child: ValueListenableBuilder>( + valueListenable: StatisticsService.instance.ingredients, + builder: (context, value, child) { + print(value); + return Column( + mainAxisAlignment: MainAxisAlignment.spaceEvenly, + children: [ + // TODO adjust 200 to values from user box + LineCircularWiTextComponent( + Colors.lightGreen, value[0], 200, "Fat (g)"), + LineCircularWiTextComponent( + Colors.lightGreen, value[1], 200, "Protein (g)"), + LineCircularWiTextComponent( + Colors.lightGreen, value[2], 200, "Carbohydrate (g)"), + ], + ); + }, ), ) ], diff --git a/lib/android/components/statistics_circular_indicator_component.dart b/lib/android/components/statistics_circular_indicator_component.dart index c853d7b..1051537 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"), - CircularLoadingComponent(eaten), + const CircularLoadingComponent(), Text("$calorienBurned verbrannt"), ], ); diff --git a/lib/android/config/statistics.dart b/lib/android/config/statistics.dart index ba306da..2d17e42 100644 --- a/lib/android/config/statistics.dart +++ b/lib/android/config/statistics.dart @@ -1,17 +1,28 @@ 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'; class StatisticsService { - final String reducedStatisticsBoxName = 'STATISTICS_REDUCED'; - final String mainStatisticsBoxName = 'STATISTICS_MAIN'; - 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'; + ValueNotifier eatenCalories = ValueNotifier(0); + ValueNotifier> ingredients = ValueNotifier>([0,0,0]); + + + initBoxes()async{ Box reducedBox = Hive.box(reducedStatisticsBoxName); putIfKeyNotExists(reducedBox, 'FRÜHSTÜCK', []); @@ -20,6 +31,7 @@ class StatisticsService { updateReducedBoxByTimespan(TimeSpan.day); } + void putIfKeyNotExists(Box box, String key, dynamic value) { if (!box.containsKey(key)) { box.put(key, value); @@ -47,8 +59,15 @@ class StatisticsService { } break; } + updateCalculationsAndNotfiyListeners(); } + void updateCalculationsAndNotfiyListeners(){ + eatenCalories.value = getAllEatenCaloriesForTodayStatistics(); + eatenCalories.notifyListeners(); + ingredients.value = getAllEatenIngredientsForTodayStatistics(); + ingredients.notifyListeners(); + } void getNewFoodAndUpdateReducedBoxByTimestamp(int timestamp){ Map> newFood = getFoodMapForGivenTimestampFromMainBox(timestamp); if(newFood.keys.isNotEmpty){ @@ -78,6 +97,7 @@ class StatisticsService { box.put(box.keys.elementAt(i), []); } } + setElementsOfReducedBox(Map> newFood){ Box box = Hive.box(reducedStatisticsBoxName); Iterable keys = newFood.keys; @@ -186,8 +206,8 @@ class StatisticsService { 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; + protein += food.proteing; + carbs += food.carbohydrateg; } } return [fat as double,protein as double,carbs as double]; @@ -220,8 +240,6 @@ class StatisticsService { return sum; } - - showItems(){ print("Statistics.dart - showItems() - ITEMS"); //Hive.box(boxName).clear(); diff --git a/lib/android/pages/nav_pages/main_page.dart b/lib/android/pages/nav_pages/main_page.dart index a0e228e..641d58e 100644 --- a/lib/android/pages/nav_pages/main_page.dart +++ b/lib/android/pages/nav_pages/main_page.dart @@ -1,3 +1,5 @@ +import 'package:ernaehrung/android/components/meal_page_text/days_component.dart'; +import 'package:ernaehrung/android/config/statistics.dart'; import 'package:ernaehrung/android/pages/nav_pages/progress_page.dart'; import 'package:ernaehrung/android/pages/nav_pages/today_page.dart'; import 'package:flutter/material.dart'; @@ -22,6 +24,9 @@ class MainPageState extends State { void onTap(int index) { setState(() { currentIndex = index; + if(currentIndex == 1){ + StatisticsService.instance.updateReducedBoxByTimespan(TimeSpan.day); + } pages[currentIndex]; }); }