-
-
Notifications
You must be signed in to change notification settings - Fork 292
Ingredient ordering #805 #814
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
4ec3611
f2d4b27
e9f7438
33166d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import 'dart:collection'; | ||
import 'package:collection/collection.dart'; | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:json_annotation/json_annotation.dart'; | ||
|
@@ -226,15 +227,25 @@ class NutritionalPlan { | |
/// returns diary entries | ||
/// deduped by the combination of amount and ingredient ID | ||
List<Log> get dedupDiaryEntries { | ||
final out = <Log>[]; | ||
//Get unique and sort by frequency | ||
const Duration recentWindow = Duration(days: -90); | ||
var uniqueLogs = <String, Log>{}; | ||
for (final log in diaryEntries) { | ||
final found = out.firstWhereOrNull( | ||
(e) => e.amount == log.amount && e.ingredientId == log.ingredientId, | ||
); | ||
if (found == null) { | ||
out.add(log); | ||
} | ||
final int howMany = diaryEntries | ||
.where((diaryEntries) => | ||
diaryEntries.ingredientId == log.ingredientId && | ||
diaryEntries.amount == log.amount && | ||
diaryEntries.datetime.isAfter(DateTime.now().add(recentWindow))) | ||
.length; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm we do (diaryEntries.length)^2 iterations here. could be a performance issue. i have a lot of diary entries. not sure what's the best way to compare the performance though, does dart come with benchmark tools? |
||
//Padding allows for sorting string as if number | ||
final String howManyPadded = howMany.toString().padLeft(10, '0'); | ||
final uniqueAndCountString = '${howManyPadded}_${log.ingredientId}_${log.amount}'; | ||
uniqueLogs[uniqueAndCountString] = log; | ||
} | ||
|
||
final sortedByFrequency = SplayTreeMap<String, Log>.from(uniqueLogs, (b, a) => a.compareTo(b)); | ||
final out = sortedByFrequency.values.toList(); | ||
|
||
return out; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this function should be renamed to describe its actual usage.
like maybe "ingredientSuggestions" or something