Skip to content

Commit

Permalink
Improved average count
Browse files Browse the repository at this point in the history
  • Loading branch information
prijindal committed May 1, 2024
1 parent 8d6b262 commit 15387f6
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
7 changes: 5 additions & 2 deletions lib/components/statistics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ class _StatisticsSubPageState extends State<StatisticsSubPage> {
return startDate;
}

DateTime? _getEndDate() {
return DateTime.now();
}

List<Widget> _buildStats() {
final habbit = widget.habbit;
final list = <ListTile>[];
Expand All @@ -144,12 +148,11 @@ class _StatisticsSubPageState extends State<StatisticsSubPage> {
}
final config = HabbitConfig.getConfig(habbit.config);
for (var element in config.statistics) {
final endDate = getEntries().lastOrNull?.creationTime;
list.add(
ListTile(
title: Text(element.name),
subtitle: Text(
element.transform(getEntries(), _getStartDate(), endDate),
element.transform(getEntries(), _getStartDate(), _getEndDate()),
),
),
);
Expand Down
4 changes: 2 additions & 2 deletions lib/helpers/stats.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ List<DateTime> getDaysInBetween(DateTime startDate, DateTime endDate) {
}

List<CountsDayData> countPerDaysData(List<HabbitEntry>? entries,
[bool includeEmptyDates = true, DateTime? startDate]) {
[bool includeEmptyDates = true, DateTime? startDate, DateTime? endDate]) {
final counts = <DateTime, int>{};
if (entries == null) {
return [];
Expand All @@ -114,7 +114,7 @@ List<CountsDayData> countPerDaysData(List<HabbitEntry>? entries,
if (includeEmptyDates && entries.isNotEmpty) {
final daysInBetween = getDaysInBetween(
startDate?.toLocal() ?? entries.last.creationTime.toLocal(),
entries.first.creationTime.toLocal(),
endDate?.toLocal() ?? entries.first.creationTime.toLocal(),
);
for (var day in daysInBetween) {
final date = DateTime(
Expand Down
2 changes: 1 addition & 1 deletion lib/models/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class HabbitStatistic {
static final averageCounts = HabbitStatistic(
name: "Average counts",
transform: (entries, startDate, endDate) {
final counts = stats.countPerDaysData(entries, true, startDate);
final counts = stats.countPerDaysData(entries, true, startDate, endDate);
var sum = 0;
for (var element in counts) {
// print(element.date);
Expand Down

0 comments on commit 15387f6

Please sign in to comment.