Commit 72e11618 authored by Nilupul Jayasekara's avatar Nilupul Jayasekara

implement time frame to record screen

parent e98ff601
...@@ -127,7 +127,7 @@ class _DashboardScreenState extends State<DashboardScreen> { ...@@ -127,7 +127,7 @@ class _DashboardScreenState extends State<DashboardScreen> {
builder: (context, AsyncSnapshot snapshot) { builder: (context, AsyncSnapshot snapshot) {
if (snapshot.hasData) { if (snapshot.hasData) {
int length = snapshot.data!.length; int length = snapshot.data!.length;
return ListView( return length >0 ? ListView(
shrinkWrap: true, shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(), physics: const NeverScrollableScrollPhysics(),
children: List.generate( children: List.generate(
...@@ -152,6 +152,17 @@ class _DashboardScreenState extends State<DashboardScreen> { ...@@ -152,6 +152,17 @@ class _DashboardScreenState extends State<DashboardScreen> {
); );
}, },
), ),
): Padding(
padding: const EdgeInsets.only(top: 50.0),
child: Column(
children: [
Container(child: Center(child: Text(" No records, yet.")),),
SizedBox(
height: 8,
),
Container(child: Center(child: Text(" Add your first record.")),),
],
),
); );
} else { } else {
return const Center( return const Center(
......
...@@ -23,7 +23,7 @@ class _LogbookScreenState extends State<LogbookScreen> { ...@@ -23,7 +23,7 @@ class _LogbookScreenState extends State<LogbookScreen> {
builder: (context, AsyncSnapshot snapshot) { builder: (context, AsyncSnapshot snapshot) {
if (snapshot.hasData) { if (snapshot.hasData) {
int length = snapshot.data!.length; int length = snapshot.data!.length;
return ListView( return length>0 ? ListView(
shrinkWrap: true, shrinkWrap: true,
children: List.generate(length, (index) { children: List.generate(length, (index) {
return Center( return Center(
...@@ -44,7 +44,7 @@ class _LogbookScreenState extends State<LogbookScreen> { ...@@ -44,7 +44,7 @@ class _LogbookScreenState extends State<LogbookScreen> {
), ),
); );
}), }),
); ): Container(child: Center(child: Text("No data to display."),),);
} else { } else {
return const Center( return const Center(
child: CircularProgressIndicator(), child: CircularProgressIndicator(),
......
...@@ -33,14 +33,23 @@ class _ReportScreenState extends State<ReportScreen> { ...@@ -33,14 +33,23 @@ class _ReportScreenState extends State<ReportScreen> {
List<SplineAreaWeightData> _chartGlucoseData = <SplineAreaWeightData>[]; List<SplineAreaWeightData> _chartGlucoseData = <SplineAreaWeightData>[];
List<SplineAreaWeightData> _chartCarbData = <SplineAreaWeightData>[]; List<SplineAreaWeightData> _chartCarbData = <SplineAreaWeightData>[];
final _currentUser = FirebaseAuth.instance.currentUser; final _currentUser = FirebaseAuth.instance.currentUser;
int _gselectedIndex =0; int _gselectedIndex = 0;
int _cselectedIndex =0; int _cselectedIndex = 0;
void getChartGlucoseData() async { void getChartGlucoseDataByTimeFrame() async {
List<SplineAreaWeightData> _data = <SplineAreaWeightData>[]; List<SplineAreaWeightData> _data = <SplineAreaWeightData>[];
List data = await GlucoseLogService.getAllRecords(_currentUser!.uid, false); String period = "This week";
if (_gselectedIndex == 1) {
period = "This month";
} else if (_gselectedIndex == 2) {
period = "This year";
}
List data = await GlucoseLogService.getRecordsByTimeFrame(
_currentUser!.uid, period,
isDecending: false);
data.forEach((element) { data.forEach((element) {
if(element.glucoseLevel != null){ if (element.glucoseLevel != null) {
_data.add(SplineAreaWeightData( _data.add(SplineAreaWeightData(
DateFormat('MMM d').format(element.dateTime).toString(), DateFormat('MMM d').format(element.dateTime).toString(),
element.glucoseLevel)); element.glucoseLevel));
...@@ -51,12 +60,18 @@ class _ReportScreenState extends State<ReportScreen> { ...@@ -51,12 +60,18 @@ class _ReportScreenState extends State<ReportScreen> {
}); });
} }
void getChartCarbsData() async { void getChartCarbsDataByTimeFrame() async {
List<SplineAreaWeightData> _data = <SplineAreaWeightData>[]; List<SplineAreaWeightData> _data = <SplineAreaWeightData>[];
List data = await GlucoseLogService.getAllRecords(_currentUser!.uid, false); String period = "This week";
if (_cselectedIndex == 1) {
period = "This month";
} else if (_cselectedIndex == 2) {
period = "This year";
}
List data = await GlucoseLogService.getRecordsByTimeFrame(_currentUser!.uid, period, isDecending: false);
data.forEach((element) { data.forEach((element) {
if(element.carbs != null){ if (element.carbs != null) {
_data.add(SplineAreaWeightData( _data.add(SplineAreaWeightData(
DateFormat('MMM d').format(element.dateTime).toString(), DateFormat('MMM d').format(element.dateTime).toString(),
element.carbs)); element.carbs));
...@@ -67,10 +82,11 @@ class _ReportScreenState extends State<ReportScreen> { ...@@ -67,10 +82,11 @@ class _ReportScreenState extends State<ReportScreen> {
_chartCarbData = _data; _chartCarbData = _data;
}); });
} }
@override @override
void initState() { void initState() {
getChartGlucoseData(); getChartGlucoseDataByTimeFrame();
getChartCarbsData(); getChartCarbsDataByTimeFrame();
} }
@override @override
...@@ -93,16 +109,24 @@ class _ReportScreenState extends State<ReportScreen> { ...@@ -93,16 +109,24 @@ class _ReportScreenState extends State<ReportScreen> {
onTap: (() { onTap: (() {
setState(() { setState(() {
_gselectedIndex = 0; _gselectedIndex = 0;
getChartGlucoseDataByTimeFrame();
}); });
}), }),
child: Container( child: Container(
width: (MediaQuery.of(context).size.width-16)/3, width: (MediaQuery.of(context).size.width - 16) / 3,
decoration: BoxDecoration( decoration: BoxDecoration(
color: _gselectedIndex != 0 ? kPrimaryColor : Colors.teal[300] color: _gselectedIndex != 0
), ? kPrimaryColor
: Colors.teal[300]),
child: Padding( child: Padding(
padding: const EdgeInsets.all(8.0), padding: const EdgeInsets.all(8.0),
child: Text("WEEK",textAlign: TextAlign.center, style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),), child: Text(
"WEEK",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold),
),
), ),
), ),
), ),
...@@ -110,16 +134,24 @@ class _ReportScreenState extends State<ReportScreen> { ...@@ -110,16 +134,24 @@ class _ReportScreenState extends State<ReportScreen> {
onTap: (() { onTap: (() {
setState(() { setState(() {
_gselectedIndex = 1; _gselectedIndex = 1;
getChartGlucoseDataByTimeFrame();
}); });
}), }),
child: Container( child: Container(
width: (MediaQuery.of(context).size.width-16)/3, width: (MediaQuery.of(context).size.width - 16) / 3,
decoration: BoxDecoration( decoration: BoxDecoration(
color: _gselectedIndex != 1 ? kPrimaryColor : Colors.teal[300] color: _gselectedIndex != 1
), ? kPrimaryColor
: Colors.teal[300]),
child: Padding( child: Padding(
padding: const EdgeInsets.all(8.0), padding: const EdgeInsets.all(8.0),
child: Text("MONTH",textAlign: TextAlign.center, style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),), child: Text(
"MONTH",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold),
),
), ),
), ),
), ),
...@@ -127,24 +159,31 @@ class _ReportScreenState extends State<ReportScreen> { ...@@ -127,24 +159,31 @@ class _ReportScreenState extends State<ReportScreen> {
onTap: (() { onTap: (() {
setState(() { setState(() {
_gselectedIndex = 2; _gselectedIndex = 2;
print(_gselectedIndex); getChartGlucoseDataByTimeFrame();
}); });
}), }),
child: Container( child: Container(
width: (MediaQuery.of(context).size.width-16)/3, width: (MediaQuery.of(context).size.width - 16) / 3,
decoration: BoxDecoration( decoration: BoxDecoration(
color: _gselectedIndex != 2 ? kPrimaryColor : Colors.teal[300] color: _gselectedIndex != 2
), ? kPrimaryColor
: Colors.teal[300]),
child: Padding( child: Padding(
padding: const EdgeInsets.all(8.0), padding: const EdgeInsets.all(8.0),
child: Text("YEAR",textAlign: TextAlign.center, style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),), child: Text(
"YEAR",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold),
),
), ),
), ),
) )
], ],
), ),
), ),
ChartCard(width, "Glucose","mg/dL", _chartGlucoseData), ChartCard(width, "Glucose", "mg/dL", _chartGlucoseData),
SizedBox( SizedBox(
height: 30, height: 30,
), ),
...@@ -156,16 +195,24 @@ class _ReportScreenState extends State<ReportScreen> { ...@@ -156,16 +195,24 @@ class _ReportScreenState extends State<ReportScreen> {
onTap: (() { onTap: (() {
setState(() { setState(() {
_cselectedIndex = 0; _cselectedIndex = 0;
getChartCarbsDataByTimeFrame();
}); });
}), }),
child: Container( child: Container(
width: (MediaQuery.of(context).size.width-16)/3, width: (MediaQuery.of(context).size.width - 16) / 3,
decoration: BoxDecoration( decoration: BoxDecoration(
color: _cselectedIndex != 0 ? kPrimaryColor : Colors.teal[300] color: _cselectedIndex != 0
), ? kPrimaryColor
: Colors.teal[300]),
child: Padding( child: Padding(
padding: const EdgeInsets.all(8.0), padding: const EdgeInsets.all(8.0),
child: Text("WEEK",textAlign: TextAlign.center, style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),), child: Text(
"WEEK",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold),
),
), ),
), ),
), ),
...@@ -173,16 +220,24 @@ class _ReportScreenState extends State<ReportScreen> { ...@@ -173,16 +220,24 @@ class _ReportScreenState extends State<ReportScreen> {
onTap: (() { onTap: (() {
setState(() { setState(() {
_cselectedIndex = 1; _cselectedIndex = 1;
getChartCarbsDataByTimeFrame();
}); });
}), }),
child: Container( child: Container(
width: (MediaQuery.of(context).size.width-16)/3, width: (MediaQuery.of(context).size.width - 16) / 3,
decoration: BoxDecoration( decoration: BoxDecoration(
color: _cselectedIndex != 1 ? kPrimaryColor : Colors.teal[300] color: _cselectedIndex != 1
), ? kPrimaryColor
: Colors.teal[300]),
child: Padding( child: Padding(
padding: const EdgeInsets.all(8.0), padding: const EdgeInsets.all(8.0),
child: Text("MONTH",textAlign: TextAlign.center, style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),), child: Text(
"MONTH",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold),
),
), ),
), ),
), ),
...@@ -190,23 +245,31 @@ class _ReportScreenState extends State<ReportScreen> { ...@@ -190,23 +245,31 @@ class _ReportScreenState extends State<ReportScreen> {
onTap: (() { onTap: (() {
setState(() { setState(() {
_cselectedIndex = 2; _cselectedIndex = 2;
getChartCarbsDataByTimeFrame();
}); });
}), }),
child: Container( child: Container(
width: (MediaQuery.of(context).size.width-16)/3, width: (MediaQuery.of(context).size.width - 16) / 3,
decoration: BoxDecoration( decoration: BoxDecoration(
color: _cselectedIndex != 2 ? kPrimaryColor : Colors.teal[300] color: _cselectedIndex != 2
), ? kPrimaryColor
: Colors.teal[300]),
child: Padding( child: Padding(
padding: const EdgeInsets.all(8.0), padding: const EdgeInsets.all(8.0),
child: Text("YEAR",textAlign: TextAlign.center, style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),), child: Text(
"YEAR",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold),
),
), ),
), ),
) )
], ],
), ),
), ),
ChartCard(width, "Carbohydrates","grams", _chartCarbData), ChartCard(width, "Carbohydrates", "grams", _chartCarbData),
//ChartCard(width, "","mg/dL", _chartCarbData), //ChartCard(width, "","mg/dL", _chartCarbData),
], ],
), ),
...@@ -215,7 +278,8 @@ class _ReportScreenState extends State<ReportScreen> { ...@@ -215,7 +278,8 @@ class _ReportScreenState extends State<ReportScreen> {
); );
} }
Container ChartCard(double width, String title, String unit,List<SplineAreaWeightData> chartData ) { Container ChartCard(double width, String title, String unit,
List<SplineAreaWeightData> chartData) {
return Container( return Container(
height: 380, height: 380,
width: width - 20, width: width - 20,
...@@ -263,7 +327,13 @@ class _ReportScreenState extends State<ReportScreen> { ...@@ -263,7 +327,13 @@ class _ReportScreenState extends State<ReportScreen> {
), ),
Container( Container(
height: 320, height: 320,
child: chartData.isEmpty ? const Center(child: Text("No data to display.", style: TextStyle(color: Colors.white),)):SfCartesianChart( child: chartData.isEmpty
? const Center(
child: Text(
"No data to display.",
style: TextStyle(color: Colors.white),
))
: SfCartesianChart(
zoomPanBehavior: ZoomPanBehavior( zoomPanBehavior: ZoomPanBehavior(
enablePinching: true, enablePinching: true,
enableDoubleTapZooming: true, enableDoubleTapZooming: true,
...@@ -311,7 +381,8 @@ class _ReportScreenState extends State<ReportScreen> { ...@@ -311,7 +381,8 @@ class _ReportScreenState extends State<ReportScreen> {
// begin: Alignment.topCenter, // begin: Alignment.topCenter,
// end: Alignment.bottomCenter, // end: Alignment.bottomCenter,
// ), // ),
onRendererCreated: (ChartSeriesController controller) { onRendererCreated:
(ChartSeriesController controller) {
_chartSeriesController = controller; _chartSeriesController = controller;
// setState(() { // setState(() {
...@@ -325,8 +396,10 @@ class _ReportScreenState extends State<ReportScreen> { ...@@ -325,8 +396,10 @@ class _ReportScreenState extends State<ReportScreen> {
color: Colors.transparent, color: Colors.transparent,
borderWidth: 3, borderWidth: 3,
name: unit, name: unit,
xValueMapper: (SplineAreaWeightData data, _) => data.title, xValueMapper: (SplineAreaWeightData data, _) =>
yValueMapper: (SplineAreaWeightData data, _) => data.value, data.title,
yValueMapper: (SplineAreaWeightData data, _) =>
data.value,
) )
], ],
tooltipBehavior: TooltipBehavior(enable: true), tooltipBehavior: TooltipBehavior(enable: true),
......
...@@ -206,7 +206,7 @@ class GlucoseLogService { ...@@ -206,7 +206,7 @@ class GlucoseLogService {
} }
static Future<List<LogEntry>> getRecordsByTimeFrame( static Future<List<LogEntry>> getRecordsByTimeFrame(
String accountId, String period) async { String accountId, String period, {bool isDecending = true}) async {
List<LogEntry> recordList = []; List<LogEntry> recordList = [];
DateTime date = DateTime.now(); DateTime date = DateTime.now();
...@@ -218,7 +218,7 @@ class GlucoseLogService { ...@@ -218,7 +218,7 @@ class GlucoseLogService {
.where("userId", isEqualTo: accountId) .where("userId", isEqualTo: accountId)
.where("dateTime", .where("dateTime",
isGreaterThanOrEqualTo: Timestamp.fromDate(firstDayOfWeek)) isGreaterThanOrEqualTo: Timestamp.fromDate(firstDayOfWeek))
.orderBy("dateTime", descending: true) .orderBy("dateTime", descending: isDecending)
.get() .get()
.then((QuerySnapshot querySnapshot) { .then((QuerySnapshot querySnapshot) {
querySnapshot.docs.forEach((doc) { querySnapshot.docs.forEach((doc) {
...@@ -245,7 +245,7 @@ class GlucoseLogService { ...@@ -245,7 +245,7 @@ class GlucoseLogService {
.where("dateTime", .where("dateTime",
isGreaterThanOrEqualTo: isGreaterThanOrEqualTo:
Timestamp.fromDate(DateTime(now.year, now.month, 1))) Timestamp.fromDate(DateTime(now.year, now.month, 1)))
.orderBy("dateTime", descending: true) .orderBy("dateTime", descending: isDecending)
.get() .get()
.then((QuerySnapshot querySnapshot) { .then((QuerySnapshot querySnapshot) {
querySnapshot.docs.forEach((doc) { querySnapshot.docs.forEach((doc) {
...@@ -272,7 +272,7 @@ class GlucoseLogService { ...@@ -272,7 +272,7 @@ class GlucoseLogService {
.where("dateTime", .where("dateTime",
isGreaterThanOrEqualTo: isGreaterThanOrEqualTo:
Timestamp.fromDate(DateTime(now.year, 1, 1))) Timestamp.fromDate(DateTime(now.year, 1, 1)))
.orderBy("dateTime", descending: true) .orderBy("dateTime", descending: isDecending)
.get() .get()
.then((QuerySnapshot querySnapshot) { .then((QuerySnapshot querySnapshot) {
querySnapshot.docs.forEach((doc) { querySnapshot.docs.forEach((doc) {
...@@ -295,7 +295,7 @@ class GlucoseLogService { ...@@ -295,7 +295,7 @@ class GlucoseLogService {
} else if (period == "All records") { } else if (period == "All records") {
await _records await _records
.where("userId", isEqualTo: accountId) .where("userId", isEqualTo: accountId)
.orderBy("dateTime", descending: true) .orderBy("dateTime", descending: isDecending)
.get() .get()
.then((QuerySnapshot querySnapshot) { .then((QuerySnapshot querySnapshot) {
querySnapshot.docs.forEach((doc) { querySnapshot.docs.forEach((doc) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment