import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart'; import 'package:diplomaticquarterapp/models/SmartWatch/WeeklyStepsResModel.dart'; import 'package:diplomaticquarterapp/models/SmartWatch/YearlyStepsResModel.dart'; import 'package:diplomaticquarterapp/services/appointment_services/GetDoctorsList.dart'; import 'package:diplomaticquarterapp/uitl/date_uitl.dart'; import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart'; import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart'; import 'package:diplomaticquarterapp/uitl/utils.dart'; import 'package:diplomaticquarterapp/uitl/utils_new.dart'; import 'package:diplomaticquarterapp/widgets/charts/app_time_series_chart.dart'; import 'package:diplomaticquarterapp/widgets/charts/show_chart.dart'; import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; class DistanceTracker extends StatefulWidget { @override _DistanceTrackerState createState() => _DistanceTrackerState(); } class _DistanceTrackerState extends State with SingleTickerProviderStateMixin { late TabController _tabController; late ProjectViewModel projectViewModel; num weeklyStatsAvgValue = 0; num monthlyStatsAvgValue = 0; num yearlyStatsAvgValue = 0; num avgWeeklyStepsValue = 0; num avgMonthlyStepsValue = 0; num avgYearlyStepsValue = 0; int weeklyDataLength = 0; int monthlyDataLength = 0; int yearlyDataLength = 0; List weekyStepsList = []; List monthlyStepsList = []; List yearlyStepsList = []; List weeklyTimeSeriesData = []; List monthlyTimeSeriesData = []; List yearlyTimeSeriesData = []; bool isWeeklyDataLoaded = false; bool isMonthlyDataLoaded = false; bool isYearlyDataLoaded = false; @override void initState() { _tabController = new TabController(length: 3, vsync: this); WidgetsBinding.instance.addPostFrameCallback((_) { getWeeklyDistanceData(); }); super.initState(); } @override Widget build(BuildContext context) { projectViewModel = Provider.of(context); return AppScaffold( isShowAppBar: true, appBarTitle: TranslationBase.of(context).distance, showNewAppBar: true, showNewAppBarTitle: true, isShowDecPage: false, body: Column( mainAxisSize: MainAxisSize.min, children: [ TabBar( controller: _tabController, indicatorWeight: 3.0, indicatorSize: TabBarIndicatorSize.tab, labelColor: Color(0xff2B353E), unselectedLabelColor: Color(0xff575757), labelPadding: EdgeInsets.only(top: 0, bottom: 0, left: 20, right: 20), labelStyle: TextStyle( fontFamily: projectViewModel.isArabic ? 'Cairo' : 'Poppins', fontSize: 16, fontWeight: FontWeight.w600, letterSpacing: -0.48, ), unselectedLabelStyle: TextStyle( fontFamily: projectViewModel.isArabic ? 'Cairo' : 'Poppins', fontSize: 16, fontWeight: FontWeight.w600, letterSpacing: -0.48, ), tabs: [ Tab(text: TranslationBase.of(context).weekly), Tab(text: TranslationBase.of(context).monthlyTab), Tab(text: TranslationBase.of(context).yearly), ], onTap: (value) { print(value); if (value == 0) { getWeeklyDistanceData(); } else if (value == 1) { getMonthlyDistanceData(); } else { getYearlyDistanceData(); } }, ), Expanded( child: new TabBarView( physics: NeverScrollableScrollPhysics(), children: [ isWeeklyDataLoaded ? getWeeklyDistanceDetails() : Container(), isMonthlyDataLoaded ? getMonthlyDistanceDetails() : Container(), isYearlyDataLoaded ? getYearlyDistanceDetails() : Container() ], controller: _tabController, ), ), ], ), ); } getWeeklyDistanceData() { avgWeeklyStepsValue = 0; weeklyDataLength = 0; DoctorsListService service = new DoctorsListService(); GifLoaderDialogUtils.showMyDialog(context); service.getPatientHealthDataStats(7, 1, context).then((res) { GifLoaderDialogUtils.hideDialog(context); weekyStepsList.clear(); res['Med_GetWeekStepsTransactionsStsList'].forEach((element) { weekyStepsList.add(new WeeklyStepsResModel.fromJson(element)); if (element['Value'] != null) { num value = num.tryParse(element['Value']! ?? "0")!; avgWeeklyStepsValue += value; weeklyDataLength++; } }); generateWeekData(); setState(() { if (avgWeeklyStepsValue != 0) { weeklyStatsAvgValue = avgWeeklyStepsValue ~/ weeklyDataLength; weeklyStatsAvgValue = weeklyStatsAvgValue / 1000; } isWeeklyDataLoaded = true; }); }).catchError((err) { GifLoaderDialogUtils.hideDialog(context); print(err); }); } getMonthlyDistanceData() { avgMonthlyStepsValue = 0; monthlyDataLength = 0; DoctorsListService service = new DoctorsListService(); GifLoaderDialogUtils.showMyDialog(context); service.getPatientHealthDataStats(7, 2, context).then((res) { GifLoaderDialogUtils.hideDialog(context); monthlyStepsList.clear(); res['Med_GetMonthStepsTransactionsStsList'].forEach((element) { monthlyStepsList.add(new WeeklyStepsResModel.fromJson(element)); if (element['Value'] != null) { num value = num.tryParse(element['Value'] ?? "0")!; avgMonthlyStepsValue += value; monthlyDataLength++; } }); generateMonthData(); setState(() { if (avgMonthlyStepsValue != 0) { monthlyStatsAvgValue = avgMonthlyStepsValue ~/ monthlyDataLength; monthlyStatsAvgValue = monthlyStatsAvgValue / 1000; } isMonthlyDataLoaded = true; }); }).catchError((err) { GifLoaderDialogUtils.hideDialog(context); print(err); }); } getYearlyDistanceData() { avgYearlyStepsValue = 0; yearlyDataLength = 0; DoctorsListService service = new DoctorsListService(); GifLoaderDialogUtils.showMyDialog(context); service.getPatientHealthDataStats(7, 3, context).then((res) { GifLoaderDialogUtils.hideDialog(context); yearlyStepsList.clear(); res['Med_GetYearStepsTransactionsStsList'].forEach((element) { yearlyStepsList.add(new YearlyStepsResModel.fromJson(element)); if (element['ValueSum'] != null) { num value = element['ValueSum']; avgYearlyStepsValue += value; yearlyDataLength++; } }); generateYearData(); setState(() { if (avgYearlyStepsValue != 0) { yearlyStatsAvgValue = avgYearlyStepsValue ~/ yearlyDataLength; yearlyStatsAvgValue = yearlyStatsAvgValue / 1000; } isYearlyDataLoaded = true; }); }).catchError((err) { GifLoaderDialogUtils.hideDialog(context); print(err); }); } generateWeekData() { if (weekyStepsList.length > 0) { weeklyTimeSeriesData.clear(); weekyStepsList.forEach( (element) { weeklyTimeSeriesData.add( TimeSeriesSales2( DateUtil.convertStringToDate(element.machineDate ?? DateUtil.convertDateToString(DateTime.now())), element.value != null ? (element.value!.toDouble() / 1000) : 0.0, ), ); }, ); } } generateMonthData() { if (monthlyStepsList.length > 0) { monthlyTimeSeriesData.clear(); monthlyStepsList.forEach( (element) { monthlyTimeSeriesData.add( TimeSeriesSales2( DateUtil.convertStringToDate(element.machineDate!), element.value != null ? element.value!.toDouble() / 1000 : 0.0, ), ); }, ); } } generateYearData() { if (yearlyStepsList.length > 0) { yearlyTimeSeriesData.clear(); yearlyStepsList.forEach( (element) { yearlyTimeSeriesData.add( TimeSeriesSales2( new DateTime(element.year!, element.month!, 1), element.valueSum != null ? element.valueSum!.toDouble() / 1000 : 0.0, ), ); }, ); } } getWeeklyDistanceDetails() { return SingleChildScrollView( child: Column( mainAxisSize: MainAxisSize.min, children: [ Container( child: ShowChart( title: "", timeSeries: weeklyTimeSeriesData, indexes: weeklyTimeSeriesData.length ~/ 5.5, horizontalInterval: 8, isWeeklyOrMonthly: true, ), ), Container( child: Container( decoration: cardRadius(12), child: Column( mainAxisSize: MainAxisSize.min, children: [ Container( padding: EdgeInsets.fromLTRB(30.0, 15.0, 30.0, 5.0), child: Text(TranslationBase.of(context).avgDistance, style: TextStyle(fontSize: 18.0)), ), Container( margin: EdgeInsets.only(bottom: 10.0), child: Text(weeklyStatsAvgValue.toString() + " " + TranslationBase.of(context).km_, style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold)), ), ], ), ), ), Container( decoration: cardRadius(12), margin: EdgeInsets.only(left: 16, top: 16, right: 16, bottom: 8), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Padding( padding: const EdgeInsets.all(8.0), child: Text(TranslationBase.of(context).details, style: TextStyle( fontSize: 16, fontWeight: FontWeight.w600, letterSpacing: -0.48, )), ), Container( padding: EdgeInsets.all(10), color: Colors.transparent, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ weekyStepsList.isEmpty ? Container( child: Center( child: Text(TranslationBase.of(context).noDataAvailable), ), ) : Table( columnWidths: { 0: FlexColumnWidth(2.5), }, children: fullDataWeekly(context), ), ], ), ) ], ), ), ], ), ); } getMonthlyDistanceDetails() { return SingleChildScrollView( child: Column( mainAxisSize: MainAxisSize.min, children: [ Container( child: ShowChart( title: "", timeSeries: monthlyTimeSeriesData, indexes: monthlyTimeSeriesData.length ~/ 5.5, horizontalInterval: 8, isWeeklyOrMonthly: true, ), ), Container( child: Container( decoration: cardRadius(12), child: Column( mainAxisSize: MainAxisSize.min, children: [ Container( padding: EdgeInsets.fromLTRB(30.0, 15.0, 30.0, 5.0), child: Text(TranslationBase.of(context).avgDistance, style: TextStyle(fontSize: 18.0)), ), Container( margin: EdgeInsets.only(bottom: 10.0), child: Text(monthlyStatsAvgValue.toString() + " " + TranslationBase.of(context).km_, style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold)), ), ], ), ), ), Container( decoration: cardRadius(12), margin: EdgeInsets.only(left: 16, top: 16, right: 16, bottom: 8), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Padding( padding: const EdgeInsets.all(8.0), child: Text(TranslationBase.of(context).details, style: TextStyle( fontSize: 16, fontWeight: FontWeight.w600, letterSpacing: -0.48, )), ), Container( padding: EdgeInsets.all(10), color: Colors.transparent, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ monthlyStepsList.isEmpty ? Container( child: Center( child: Text(TranslationBase.of(context).noDataAvailable), ), ) : Table( columnWidths: { 0: FlexColumnWidth(2.5), }, children: fullDataMonthly(context), ), ], ), ) ], ), ), ], ), ); } getYearlyDistanceDetails() { return SingleChildScrollView( child: Column( mainAxisSize: MainAxisSize.min, children: [ Container( child: ShowChart( title: "", timeSeries: yearlyTimeSeriesData, indexes: yearlyTimeSeriesData.length ~/ 5.5, horizontalInterval: 8, ), ), Container( child: Container( decoration: cardRadius(12), child: Column( mainAxisSize: MainAxisSize.min, children: [ Container( padding: EdgeInsets.fromLTRB(30.0, 15.0, 30.0, 5.0), child: Text(TranslationBase.of(context).avgDistance, style: TextStyle(fontSize: 18.0)), ), Container( margin: EdgeInsets.only(bottom: 10.0), child: Text(yearlyStatsAvgValue.toString() + " " + TranslationBase.of(context).km_, style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold)), ), ], ), ), ), Container( decoration: cardRadius(12), margin: EdgeInsets.only(left: 16, top: 16, right: 16, bottom: 8), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Padding( padding: const EdgeInsets.all(8.0), child: Text(TranslationBase.of(context).details, style: TextStyle( fontSize: 16, fontWeight: FontWeight.w600, letterSpacing: -0.48, )), ), Container( padding: EdgeInsets.all(10), color: Colors.transparent, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ yearlyStepsList.isEmpty ? Container( child: Center( child: Text(TranslationBase.of(context).noDataAvailable), ), ) : Table( columnWidths: { 0: FlexColumnWidth(2.5), }, children: fullData(context), ), ], ), ) ], ), ), ], ), ); } List fullDataWeekly(BuildContext context) { List tableRow = []; tableRow.add( TableRow( children: [ Utils.tableColumnTitle(TranslationBase.of(context).date), Utils.tableColumnTitle(TranslationBase.of(context).distance), ], ), ); weekyStepsList.forEach( (step) { tableRow.add( TableRow( children: [ Utils.tableColumnValue( '${DateUtil.getDayMonthYearDateFormatted( DateUtil.convertStringToDate(step.machineDate ?? DateUtil.convertDateToString(DateTime.now())), )} ', isCapitable: false, mProjectViewModel: projectViewModel), Utils.tableColumnValue((step.value! / 1000).toString() + " " + TranslationBase.of(context).km_, isCapitable: false, mProjectViewModel: projectViewModel), ], ), ); }, ); return tableRow; } List fullDataMonthly(BuildContext context) { List tableRow = []; tableRow.add( TableRow( children: [ Utils.tableColumnTitle(TranslationBase.of(context).date), Utils.tableColumnTitle(TranslationBase.of(context).distance), ], ), ); monthlyStepsList.forEach( (step) { tableRow.add( TableRow( children: [ Utils.tableColumnValue( '${DateUtil.getDayMonthYearDateFormatted( DateUtil.convertStringToDate(step.machineDate!), )} ', isCapitable: false, mProjectViewModel: projectViewModel), Utils.tableColumnValue((step.value! / 1000).toString() + " " + TranslationBase.of(context).km_, isCapitable: false, mProjectViewModel: projectViewModel), ], ), ); }, ); return tableRow; } List fullData(BuildContext context) { List tableRow = []; tableRow.add( TableRow( children: [ Utils.tableColumnTitle(TranslationBase.of(context).date), Utils.tableColumnTitle(TranslationBase.of(context).distance), ], ), ); yearlyStepsList.forEach( (step) { tableRow.add( TableRow( children: [ Utils.tableColumnValue( '${DateUtil.getDayMonthYearDateFormatted( new DateTime(step.year!, step.month!, 1), )} ', isCapitable: false, mProjectViewModel: projectViewModel), Utils.tableColumnValue(step.valueSum != null ? (step.valueSum! / 1000).toString() + " " + TranslationBase.of(context).km_ : "0.0 " + TranslationBase.of(context).km_, isCapitable: false, mProjectViewModel: projectViewModel), ], ), ); }, ); return tableRow; } }