From a3807311cb6873dc6c13effca26afdeccb278400 Mon Sep 17 00:00:00 2001 From: Marcin Szalek Date: Sat, 18 Nov 2017 09:12:56 +0100 Subject: [PATCH] Handled empty dataset scenario --- lib/screens/history_page.dart | 37 +++++++++++++++----------- lib/widgets/progress_chart.dart | 47 +++++++++++++++++++++++++-------- 2 files changed, 57 insertions(+), 27 deletions(-) diff --git a/lib/screens/history_page.dart b/lib/screens/history_page.dart index 92c5d94..e41daef 100644 --- a/lib/screens/history_page.dart +++ b/lib/screens/history_page.dart @@ -37,22 +37,27 @@ class HistoryPage extends StatelessWidget { ); }, builder: (context, viewModel) { - return new ListView.builder( - shrinkWrap: true, - itemCount: viewModel.entries.length, - itemBuilder: (buildContext, index) { - //calculating difference - double difference = index == viewModel.entries.length - 1 - ? 0.0 - : viewModel.entries[index].weight - - viewModel.entries[index + 1].weight; - return new InkWell( - onTap: () => _openEditEntryDialog(viewModel.entries[index], - context, viewModel.editEntryCallback), - child: - new WeightListItem(viewModel.entries[index], difference)); - }, - ); + if (viewModel.entries.isEmpty) { + return new Center(child: new Text("Add your weight to see history"),); + } else { + return new ListView.builder( + shrinkWrap: true, + itemCount: viewModel.entries.length, + itemBuilder: (buildContext, index) { + //calculating difference + double difference = index == viewModel.entries.length - 1 + ? 0.0 + : viewModel.entries[index].weight - + viewModel.entries[index + 1].weight; + return new InkWell( + onTap: () => + _openEditEntryDialog(viewModel.entries[index], + context, viewModel.editEntryCallback), + child: + new WeightListItem(viewModel.entries[index], difference)); + }, + ); + } }, ); } diff --git a/lib/widgets/progress_chart.dart b/lib/widgets/progress_chart.dart index aa38702..7a52e53 100644 --- a/lib/widgets/progress_chart.dart +++ b/lib/widgets/progress_chart.dart @@ -111,15 +111,16 @@ class ChartPainter extends CustomPainter { drawingWidth = size.width * 0.95; drawingHeight = topOffsetEnd; - if (entries.isNotEmpty) { + if (entries.isEmpty) { + _drawParagraphInsteadOfChart( + canvas, size, "Add your weight to see history"); + } else { Tuple2 borderLineValues = _getMinAndMaxValues(entries); _drawHorizontalLinesAndLabels( canvas, size, borderLineValues.item1, borderLineValues.item2); _drawBottomLabels(canvas, size); _drawLines(canvas, borderLineValues.item1, borderLineValues.item2); - } else { - //TODO: I think it should be handled at higher level } } @@ -241,15 +242,22 @@ class ChartPainter extends CustomPainter { double maxWeight = entries.map((entry) => entry.weight).reduce(math.max); double minWeight = entries.map((entry) => entry.weight).reduce(math.min); - int maxLineValue = maxWeight.ceil(); - int difference = maxLineValue - minWeight.floor(); - int toSubtract = (NUMBER_OF_HORIZONTAL_LINES - 1) - - (difference % (NUMBER_OF_HORIZONTAL_LINES - 1)); - if (toSubtract == NUMBER_OF_HORIZONTAL_LINES - 1) { - toSubtract = 0; - } - int minLineValue = minWeight.floor() - toSubtract; + int maxLineValue; + int minLineValue; + if (maxWeight == minWeight) { + maxLineValue = maxWeight.ceil() + 1; + minLineValue = maxLineValue - 4; + } else { + maxLineValue = maxWeight.ceil(); + int difference = maxLineValue - minWeight.floor(); + int toSubtract = (NUMBER_OF_HORIZONTAL_LINES - 1) - + (difference % (NUMBER_OF_HORIZONTAL_LINES - 1)); + if (toSubtract == NUMBER_OF_HORIZONTAL_LINES - 1) { + toSubtract = 0; + } + minLineValue = minWeight.floor() - toSubtract; + } return new Tuple2(minLineValue, maxLineValue); } @@ -266,6 +274,23 @@ class ChartPainter extends CustomPainter { double yOffset = 5 + drawingHeight - relativeYposition * drawingHeight; return new Offset(xOffset, yOffset); } + + _drawParagraphInsteadOfChart(ui.Canvas canvas, ui.Size size, String text) { + double fontSize = 14.0; + ui.ParagraphBuilder builder = new ui.ParagraphBuilder( + new ui.ParagraphStyle( + fontSize: fontSize, + textAlign: TextAlign.center, + ), + ) + ..pushStyle(new ui.TextStyle(color: Colors.black)) + ..addText(text); + final ui.Paragraph paragraph = builder.build() + ..layout(new ui.ParagraphConstraints(width: size.width)); + + canvas.drawParagraph( + paragraph, new Offset(0.0, size.height / 2 - fontSize)); + } } DateTime _getStartDateOfChart() {