Skip to content

Commit

Permalink
Handled empty dataset scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinusX committed Nov 18, 2017
1 parent d201210 commit a380731
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 27 deletions.
37 changes: 21 additions & 16 deletions lib/screens/history_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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));
},
);
}
},
);
}
Expand Down
47 changes: 36 additions & 11 deletions lib/widgets/progress_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, int> 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
}
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -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() {
Expand Down

0 comments on commit a380731

Please sign in to comment.