Flutter-Ernaehrungsapp/lib/android/config/format_helper.dart

35 lines
1.1 KiB
Dart

import '../models/food.dart';
String getFoodListStringByFood(String foodName, int count, int calories){
int maxWidth = 35;
String limitedText = foodName.length > maxWidth ? "${foodName.substring(0, maxWidth - 3)} ... $count x $calories kcal" : "$foodName $count x $calories kcal";
return limitedText;
}
String getWeeklyRankingString(String foodName){
int maxWidth = 50;
String limitedText = foodName.length > maxWidth ? "${foodName.substring(0, maxWidth - 3)} ... " : foodName;
return limitedText;
}
Map<String,List<int>> getMapOfDistinctElementsWithCounterAndCalories(List<Food> foods){
Map<String,List<int>> resultMap = <String,List<int>>{};
for(int i = 0; i < foods.length;i++){
if(!resultMap.keys.contains(foods[i].name)){
resultMap.putIfAbsent(foods[i].name, () => [1,foods[i].calories]);
}else{
resultMap[foods[i].name]![0] = resultMap[foods[i].name]![0] + 1;
}
}
return resultMap;
}
List<Food> getListOfDistinctElements(List<Food> foods){
List<Food> result = [];
for(int i = 0; i < foods.length;i++){
if(!result.any((element) => element.id == foods[i].id)){
result.add(foods[i]);
}
}
return result;
}