Skip to content

Commit 0ca3de3

Browse files
committed
refactor(nutrition): improve localization and date formatting
- Extract date display logic in nutritional plan details - Migrate hardcoded strings to localization files
1 parent 315b1aa commit 0ca3de3

6 files changed

Lines changed: 50 additions & 21 deletions

File tree

lib/l10n/app_en.arb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,25 @@
460460
"@toAddMealsToThePlanGoToNutritionalPlanDetails": {
461461
"description": "Message shown to guide users to the nutritional plan details page to add meals"
462462
},
463+
"planDateRange": "from {startDate} to {endDate}",
464+
"@planDateRange": {
465+
"description": "Label for the start and end date range of a plan",
466+
"placeholders": {
467+
"startDate": { "type": "String" },
468+
"endDate": { "type": "String" }
469+
}
470+
},
471+
"planStartDate": "from {startDate}",
472+
"@planStartDate": {
473+
"description": "Label for the start date of a plan",
474+
"placeholders": {
475+
"startDate": { "type": "String" }
476+
}
477+
},
478+
"otherLogs": "Other logs",
479+
"@otherLogs": {
480+
"description": "Other unassigned logs. Label for the pseudo-meal section that groups all nutritional diary entries not assigned to a specific meal"
481+
},
463482
"goalEnergy": "Energy goal",
464483
"goalProtein": "Protein goal",
465484
"goalCarbohydrates": "Carbohydrates goal",
@@ -675,6 +694,10 @@
675694
"fiber": "Fibers",
676695
"sodium": "Sodium",
677696
"@sodium": {},
697+
"carbohydratesSugar": "Sugar (Carbohydrates)",
698+
"@carbohydratesSugar": {
699+
"description": "Label for the carbohydrates sugar"
700+
},
678701
"amount": "Amount",
679702
"@amount": {
680703
"description": "The amount (e.g. in grams) of an ingredient in a meal"

lib/screens/log_meal_screen.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class _LogMealScreenState extends ConsumerState<LogMealScreen> {
7878
children: [
7979
const DiaryheaderTile(),
8080
...meal.mealItems.map(
81-
(item) => MealItemEditableFullTile(item, viewMode.withAllDetails, false),
81+
(item) => MealItemEditableFullTile(item, ViewMode.withAllDetails, false),
8282
),
8383
const SizedBox(height: 32),
8484
Text(

lib/screens/log_meals_screen.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ class _LogMealsScreenState extends State<LogMealsScreen> {
6161
itemCount: nutritionalPlan.meals.length,
6262
itemBuilder: (context, index) => MealWidget(
6363
nutritionalPlan.meals[index],
64-
nutritionalPlan.dedupMealItems,
6564
true,
6665
true,
6766
),

lib/widgets/nutrition/charts.dart

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ class NutritionalDiaryChartWidgetFlState extends State<NutritionalDiaryChartWidg
337337
final logged7DayAvg = widget._nutritionalPlan.loggedNutritionalValues7DayAvg;
338338

339339
final [colorPlanned, colorLoggedToday, colorLogged7Day] = LIST_OF_COLORS3;
340+
final i18n = AppLocalizations.of(context);
340341

341342
BarChartGroupData barchartGroup(
342343
int x,
@@ -433,18 +434,18 @@ class NutritionalDiaryChartWidgetFlState extends State<NutritionalDiaryChartWidg
433434
borderData: FlBorderData(show: false),
434435
groupsSpace: 30,
435436
barGroups: [
436-
barchartGroup(0, barsSpace, barsWidth, 'protein'),
437-
barchartGroup(1, barsSpace, barsWidth, 'carbohydrates'),
437+
barchartGroup(0, barsSpace, barsWidth, i18n.protein),
438+
barchartGroup(1, barsSpace, barsWidth, i18n.carbohydrates),
438439
barchartGroup(
439440
2,
440441
barsSpace,
441442
barsWidth,
442-
'carbohydratesSugar',
443+
i18n.carbohydratesSugar,
443444
),
444-
barchartGroup(3, barsSpace, barsWidth, 'fat'),
445-
barchartGroup(4, barsSpace, barsWidth, 'fatSaturated'),
445+
barchartGroup(3, barsSpace, barsWidth, i18n.fat),
446+
barchartGroup(4, barsSpace, barsWidth, i18n.saturatedFat),
446447
if (widget._nutritionalPlan.nutritionalGoals.fiber != null)
447-
barchartGroup(5, barsSpace, barsWidth, 'fiber'),
448+
barchartGroup(5, barsSpace, barsWidth, i18n.fiber),
448449
],
449450
),
450451
),

lib/widgets/nutrition/meal.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ class MealHeader extends StatelessWidget {
285285
}
286286
}
287287

288-
class MealIngredientsSection extends StatelessWidget {
288+
class MealIngredientsSection extends ConsumerWidget {
289289
const MealIngredientsSection({
290290
super.key,
291291
required this.meal,
@@ -301,7 +301,7 @@ class MealIngredientsSection extends StatelessWidget {
301301
}
302302

303303
@override
304-
Widget build(BuildContext context) {
304+
Widget build(BuildContext context, WidgetRef ref) {
305305
return Column(
306306
children: [
307307
if (showIngredientsDetails(viewMode)) ...[
@@ -340,9 +340,11 @@ class MealIngredientsSection extends StatelessWidget {
340340
}
341341

342342
Widget _buildTotalRow(BuildContext context) {
343+
final i18n = AppLocalizations.of(context);
344+
343345
return NutritionTile(
344346
vPadding: 0,
345-
leading: const Text('total'),
347+
leading: Text(i18n.total),
346348
title: getNutritionRow(
347349
context,
348350
muted(getNutritionalValues(meal.plannedNutritionalValues, context)),

lib/widgets/nutrition/nutritional_plan_detail.dart

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,26 @@ class NutritionalPlanDetailWidget extends riverpod.ConsumerWidget {
4242
? nutritionalGoals / lastWeightEntry.weight.toDouble()
4343
: null;
4444

45+
final i18n = AppLocalizations.of(context);
46+
final locale = Localizations.localeOf(context).languageCode;
47+
final startDateFormatted = DateFormat.yMd(locale).format(_nutritionalPlan.startDate);
48+
49+
String dateDisplay;
50+
if (_nutritionalPlan.endDate != null) {
51+
final endDateFormatted = DateFormat.yMd(locale).format(_nutritionalPlan.endDate!);
52+
dateDisplay = i18n.planDateRange(startDateFormatted, endDateFormatted);
53+
} else {
54+
dateDisplay = '${i18n.planStartDate(startDateFormatted)} (${i18n.openEnded})';
55+
}
56+
4557
return SliverList(
4658
delegate: SliverChildListDelegate(
4759
[
4860
const SizedBox(height: 16),
4961
Padding(
5062
padding: const EdgeInsets.symmetric(horizontal: 16.0),
5163
child: Text(
52-
_nutritionalPlan.endDate != null
53-
? 'from ${DateFormat.yMd(
54-
Localizations.localeOf(context).languageCode,
55-
).format(_nutritionalPlan.startDate)} to ${DateFormat.yMd(
56-
Localizations.localeOf(context).languageCode,
57-
).format(_nutritionalPlan.endDate!)}'
58-
: 'from ${DateFormat.yMd(
59-
Localizations.localeOf(context).languageCode,
60-
).format(_nutritionalPlan.startDate)} (${AppLocalizations.of(context).openEnded})',
64+
dateDisplay,
6165
style: Theme.of(context).textTheme.titleSmall?.copyWith(
6266
fontStyle: FontStyle.italic,
6367
),
@@ -83,7 +87,7 @@ class NutritionalPlanDetailWidget extends riverpod.ConsumerWidget {
8387
),
8488
),
8589
MealWidget(
86-
_nutritionalPlan.pseudoMealOthers('Other logs'),
90+
_nutritionalPlan.pseudoMealOthers(i18n.otherLogs),
8791
false,
8892
true,
8993
),

0 commit comments

Comments
 (0)