Skip to content

Offline gym log for Android. No account, no ads, no internet.

Archive foods, plans and supplements and fix the weekly plan refresh (#306)

InlitX6539486c0018
diff --git a/lib/screens/nutrition/cards.dart b/lib/screens/nutrition/cards.dart
index b74571f..40c38f6 100644
--- a/lib/screens/nutrition/cards.dart
+++ b/lib/screens/nutrition/cards.dart
@@ -956,7 +956,8 @@ class _SupplementsCard extends StatelessWidget {
final gc = context.gc;
final day = fit.nutritionDay;
final taken = fit.takenOn(day);
- final list = fit.supplements;
+ final list = fit.activeSupplements;
+ final archived = fit.archivedSuppList;
final due = [for (final s in list) if (s.dueOn(day)) s];
final later = [for (final s in list) if (!s.dueOn(day)) s];
final done = due.where((s) => taken.contains(s.id)).length;
@@ -1011,6 +1012,19 @@ class _SupplementsCard extends StatelessWidget {
const SizedBox(height: 8),
],
],
+ if (archived.isNotEmpty)
+ _ArchivedFold(
+ count: archived.length,
+ open: fit.showArchivedSupps,
+ onToggle: fit.toggleArchivedSupps,
+ child: Column(children: [
+ for (final s in archived)
+ Padding(
+ padding: const EdgeInsets.only(bottom: 8),
+ child: _SupplementRow(supp: s, taken: false, off: true),
+ ),
+ ]),
+ ),
const SizedBox(height: 2),
GestureDetector(
behavior: HitTestBehavior.opaque,
@@ -1150,7 +1164,7 @@ class _SupplementRow extends StatelessWidget {
)
else
Padding(
- padding: const EdgeInsets.only(right: 6),
+ padding: const EdgeInsetsDirectional.only(end: 6),
child: AnimatedContainer(
duration: const Duration(milliseconds: 220),
curve: Curves.easeOutBack,
@@ -1366,6 +1380,21 @@ class _SupplementEditorState extends State<_SupplementEditor> with _StickerDraft
Navigator.of(context).pop();
}
+ void _archive() {
+ final supp = widget.supp!;
+ final on = !fit.archivedSupps.contains(supp.id);
+ fit.setSuppArchived(supp.id, on);
+ Navigator.of(context).pop();
+ if (on) {
+ showNotchToast(context, t.suppArchivedToast,
+ subtitle: supp.name,
+ icon: PhosphorIconsFill.archive,
+ accent: context.gc.textSecondary,
+ action: t.undo,
+ onTap: () => fit.setSuppArchived(supp.id, false));
+ }
+ }
+
Future<void> _remove() async {
final supp = widget.supp!;
final ok = await askConfirm(
@@ -1634,12 +1663,17 @@ class _SupplementEditorState extends State<_SupplementEditor> with _StickerDraft
PrimaryButton(label: t.save, onTap: _save),
if (widget.supp != null) ...[
const SizedBox(height: 8),
- Center(
- child: TextButton(
+ Row(mainAxisAlignment: MainAxisAlignment.center, children: [
+ TextButton(
+ onPressed: _archive,
+ child: Text(fit.archivedSupps.contains(widget.supp!.id) ? t.dishUnarchive : t.dishArchive,
+ style: AppTheme.f(13, weight: FontWeight.w700, color: gc.textSecondary)),
+ ),
+ TextButton(
onPressed: _remove,
child: Text(t.delete, style: AppTheme.f(13, weight: FontWeight.w700, color: gc.danger)),
),
- ),
+ ]),
],
]);
}
diff --git a/lib/screens/nutrition/food.dart b/lib/screens/nutrition/food.dart
index 5ebafc9..7b618d5 100644
--- a/lib/screens/nutrition/food.dart
+++ b/lib/screens/nutrition/food.dart
@@ -76,8 +76,11 @@ class _AddFoodSheetState extends State<_AddFoodSheet> {
if (done == true && mounted) Navigator.of(context).pop();
}
- Future<void> _open(FoodItem f) async =>
- _closeIf(await showLogFoodSheet(context, f, widget.meal, onPick: widget.onPick));
+ Future<void> _open(FoodItem f) async {
+ await FoodSticker.warm(context, f.photo);
+ if (!mounted) return;
+ _closeIf(await showLogFoodSheet(context, f, widget.meal, onPick: widget.onPick));
+ }
Future<void> _create(String name) async {
if (widget.onPick == null) {
@@ -89,8 +92,11 @@ class _AddFoodSheetState extends State<_AddFoodSheet> {
if (f != null && mounted) await _open(f);
}
- Future<void> _info(FoodItem f) async =>
- _closeIf(await showFoodInfoSheet(context, f, widget.meal, onPick: widget.onPick));
+ Future<void> _info(FoodItem f) async {
+ await FoodSticker.warm(context, f.photo);
+ if (!mounted) return;
+ _closeIf(await showFoodInfoSheet(context, f, widget.meal, onPick: widget.onPick));
+ }
Future<void> _openRecipe(Recipe r) async {
final pick = widget.onPick;
@@ -105,6 +111,19 @@ class _AddFoodSheetState extends State<_AddFoodSheet> {
}
}
+ (String, FoodCat?, String, bool)? _libraryKey;
+ List<FoodItem> _libraryHit = const [];
+
+ List<FoodItem> _library(String q) {
+ final key = (q, _cat, appLanguage, FoodLibrary.ready);
+ if (key == _libraryKey) return _libraryHit;
+ _libraryKey = key;
+ return _libraryHit = [
+ for (final f in FoodLibrary.search(q, appLanguage, cat: _cat, limit: _cat != null && q.isEmpty ? 200 : 60))
+ f.item(appLanguage),
+ ];
+ }
+
Widget _section(GymColors gc, String title, {String? trailing}) => Padding(
padding: const EdgeInsets.only(top: 20, bottom: 10, left: 2),
child: Row(children: [
@@ -304,11 +323,7 @@ class _AddFoodSheetState extends State<_AddFoodSheet> {
final recipes = _cat != null || grouped ? const <Recipe>[] : fit.searchRecipes(q);
final dishes = _cat != null || grouped || (q.isEmpty && !_dishes) ? const <Recipe>[] : fit.searchDishes(q);
final mine = _dishes ? const <FoodItem>[] : fit.searchFoods(q, cat: _cat, group: _group);
- final library = [
- if (!_dishes && !grouped)
- for (final f in FoodLibrary.search(q, appLanguage, cat: _cat, limit: _cat != null && q.isEmpty ? 200 : 60))
- f.item(appLanguage),
- ];
+ final library = _dishes || grouped ? const <FoodItem>[] : _library(q);
final recents = browsing ? const <FoodItem>[] : fit.recentLibraryFoods;
final nothing = browsing && recipes.isEmpty && dishes.isEmpty && mine.isEmpty && library.isEmpty;
return _sheetSlivers(heightFactor: 0.92, context, [
@@ -361,6 +376,13 @@ class _AddFoodSheetState extends State<_AddFoodSheet> {
trailing: grouped ? '${mine.length}' : null),
_grid(mine),
],
+ if (q.isEmpty && _cat == null && !grouped && !_dishes && fit.archivedFoods.isNotEmpty)
+ _ArchivedFold(
+ count: fit.archivedFoodList.length,
+ open: fit.showArchivedFoods,
+ onToggle: fit.toggleArchivedFoods,
+ child: _grid(fit.archivedFoodList),
+ ),
if (recents.isNotEmpty) ...[_section(gc, t.recentFoods), _grid(recents, quick: true)],
if (library.isNotEmpty) ...[
_section(gc, _cat == null ? t.foodDatabase : catName(_cat!), trailing: '${library.length}'),
@@ -646,6 +668,28 @@ class _FoodInfoSheet extends StatelessWidget {
if (done == true && context.mounted) Navigator.of(context).pop(true);
},
),
+ if (!picking && !f.isLibrary && fit.foods.any((x) => x.id == f.id)) ...[
+ const SizedBox(height: 6),
+ Center(
+ child: TextButton(
+ onPressed: () {
+ final on = !fit.archivedFoods.contains(f.id);
+ fit.setFoodArchived(f.id, on);
+ Navigator.of(context).pop();
+ if (on) {
+ showNotchToast(context, t.foodArchivedToast,
+ subtitle: f.name,
+ icon: PhosphorIconsFill.archive,
+ accent: gc.textSecondary,
+ action: t.undo,
+ onTap: () => fit.setFoodArchived(f.id, false));
+ }
+ },
+ child: Text(fit.archivedFoods.contains(f.id) ? t.dishUnarchive : t.dishArchive,
+ style: AppTheme.f(13, weight: FontWeight.w700, color: gc.textSecondary)),
+ ),
+ ),
+ ],
]);
}
}
@@ -2039,6 +2083,21 @@ class _FoodEditorState extends State<_FoodEditor> with _StickerDrafts {
);
}
+ void _archive() {
+ final food = widget.food!;
+ final on = !fit.archivedFoods.contains(food.id);
+ fit.setFoodArchived(food.id, on);
+ Navigator.of(context).pop();
+ if (on) {
+ showNotchToast(context, t.foodArchivedToast,
+ subtitle: food.name,
+ icon: PhosphorIconsFill.archive,
+ accent: context.gc.textSecondary,
+ action: t.undo,
+ onTap: () => fit.setFoodArchived(food.id, false));
+ }
+ }
+
Future<void> _delete() async {
final food = widget.food!;
final ok = await askConfirm(
@@ -2207,12 +2266,17 @@ class _FoodEditorState extends State<_FoodEditor> with _StickerDrafts {
PrimaryButton(label: t.save, onTap: _working ? () {} : _save),
if (widget.food != null) ...[
const SizedBox(height: 8),
- Center(
- child: TextButton(
+ Row(mainAxisAlignment: MainAxisAlignment.center, children: [
+ TextButton(
+ onPressed: _archive,
+ child: Text(fit.archivedFoods.contains(widget.food!.id) ? t.dishUnarchive : t.dishArchive,
+ style: AppTheme.f(13, weight: FontWeight.w700, color: gc.textSecondary)),
+ ),
+ TextButton(
onPressed: _delete,
child: Text(t.delete, style: AppTheme.f(13, weight: FontWeight.w700, color: gc.danger)),
),
- ),
+ ]),
],
]);
}
diff --git a/lib/screens/nutrition/meals.dart b/lib/screens/nutrition/meals.dart
index e6a01fa..d22d73e 100644
--- a/lib/screens/nutrition/meals.dart
+++ b/lib/screens/nutrition/meals.dart
@@ -219,38 +219,13 @@ Future<void> showSavedMealsSheet(BuildContext context) => _sheet<void>(context,
},
),
for (final r in fit.activeRecipes) recipeRow(r),
- if (fit.archivedRecipes.isNotEmpty) ...[
- GestureDetector(
- behavior: HitTestBehavior.opaque,
- onTap: () {
- HapticFeedback.selectionClick();
- fit.toggleArchivedRecipes();
- },
- child: Padding(
- padding: const EdgeInsets.fromLTRB(4, 14, 4, 10),
- child: Row(children: [
- Expanded(child: SectionLabel('${t.archivedFilter} · ${fit.archivedRecipes.length}')),
- AnimatedRotation(
- turns: fit.showArchivedRecipes ? 0.5 : 0,
- duration: const Duration(milliseconds: 220),
- curve: Curves.easeOutCubic,
- child: Icon(PhosphorIconsBold.caretDown, size: 13, color: gc.textTertiary),
- ),
- ]),
- ),
+ if (fit.archivedRecipes.isNotEmpty)
+ _ArchivedFold(
+ count: fit.archivedRecipes.length,
+ open: fit.showArchivedRecipes,
+ onToggle: fit.toggleArchivedRecipes,
+ child: Column(children: [for (final r in fit.archivedRecipeList) recipeRow(r)]),
),
- AnimatedSize(
- duration: const Duration(milliseconds: 220),
- curve: Curves.easeOutCubic,
- alignment: Alignment.topCenter,
- child: fit.showArchivedRecipes
- ? Opacity(
- opacity: 0.6,
- child: Column(children: [for (final r in fit.archivedRecipeList) recipeRow(r)]),
- )
- : const SizedBox(width: double.infinity),
- ),
- ],
if (fit.recipes.isEmpty) ...[
const SizedBox(height: 10),
Text(t.savedMealsEmpty,
@@ -369,7 +344,7 @@ class _MealTabsState extends State<_MealTabs> {
),
if (count(m) > 0 && m != meal)
Padding(
- padding: const EdgeInsets.only(left: 3),
+ padding: const EdgeInsetsDirectional.only(start: 3),
child: Container(
width: 6,
height: 6,
@@ -843,7 +818,7 @@ class _EntryRow extends StatelessWidget {
direction: DismissDirection.endToStart,
background: Container(
alignment: Alignment.centerRight,
- padding: const EdgeInsets.only(right: 22),
+ padding: const EdgeInsetsDirectional.only(end: 22),
decoration: BoxDecoration(color: gc.danger.withValues(alpha: 0.18), borderRadius: BorderRadius.circular(20)),
child: Icon(PhosphorIconsBold.trash, size: 18, color: gc.danger),
),
@@ -1184,3 +1159,44 @@ class _DishEditorState extends State<_DishEditor> {
);
}
}
+
+class _ArchivedFold extends StatelessWidget {
+ const _ArchivedFold({required this.count, required this.open, required this.onToggle, required this.child});
+
+ final int count;
+ final bool open;
+ final VoidCallback onToggle;
+ final Widget child;
+
+ @override
+ Widget build(BuildContext context) {
+ final gc = context.gc;
+ return Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
+ GestureDetector(
+ behavior: HitTestBehavior.opaque,
+ onTap: () {
+ HapticFeedback.selectionClick();
+ onToggle();
+ },
+ child: Padding(
+ padding: const EdgeInsets.fromLTRB(4, 14, 4, 10),
+ child: Row(children: [
+ Expanded(child: SectionLabel('${t.archivedFilter} · $count')),
+ AnimatedRotation(
+ turns: open ? 0.5 : 0,
+ duration: const Duration(milliseconds: 220),
+ curve: Curves.easeOutCubic,
+ child: Icon(PhosphorIconsBold.caretDown, size: 13, color: gc.textTertiary),
+ ),
+ ]),
+ ),
+ ),
+ AnimatedSize(
+ duration: const Duration(milliseconds: 220),
+ curve: Curves.easeOutCubic,
+ alignment: Alignment.topCenter,
+ child: open ? Opacity(opacity: 0.6, child: child) : const SizedBox(width: double.infinity),
+ ),
+ ]);
+ }
+}
diff --git a/lib/screens/nutrition/plans.dart b/lib/screens/nutrition/plans.dart
index 7543e0f..94d5bef 100644
--- a/lib/screens/nutrition/plans.dart
+++ b/lib/screens/nutrition/plans.dart
@@ -51,7 +51,8 @@ class _PlansSection extends StatelessWidget {
@override
Widget build(BuildContext context) {
final gc = context.gc;
- final plans = fit.mealPlans;
+ final plans = fit.activeMealPlans;
+ final archived = fit.archivedPlanList;
return Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
_PlanWeek(),
const SizedBox(height: 12),
@@ -85,6 +86,22 @@ class _PlansSection extends StatelessWidget {
Expanded(child: i + 1 < plans.length ? _planFolder(context, plans[i + 1]) : const SizedBox.shrink()),
]),
],
+ if (archived.isNotEmpty)
+ _ArchivedFold(
+ count: archived.length,
+ open: fit.showArchivedPlans,
+ onToggle: fit.toggleArchivedPlans,
+ child: Column(children: [
+ for (var i = 0; i < archived.length; i += 2) ...[
+ if (i > 0) const SizedBox(height: 12),
+ Row(crossAxisAlignment: CrossAxisAlignment.end, children: [
+ Expanded(child: _planFolder(context, archived[i])),
+ const SizedBox(width: 12),
+ Expanded(child: i + 1 < archived.length ? _planFolder(context, archived[i + 1]) : const SizedBox.shrink()),
+ ]),
+ ],
+ ]),
+ ),
]);
}
@@ -205,14 +222,14 @@ class _PlanWeek extends StatelessWidget {
}
Future<void> _pickPlanForDay(BuildContext context, int weekday) {
- if (fit.mealPlans.isEmpty) return showMealPlanEditor(context, days: {weekday});
+ if (fit.activeMealPlans.isEmpty) return showMealPlanEditor(context, days: {weekday});
return _sheet<void>(context, (sheet) {
final current = fit.mealWeek[weekday];
return _sheetFrame(sheet, [
SheetTitle(t.weekday(weekday), subtitle: t.planPickHint),
const SizedBox(height: 16),
OptionGroup([
- for (final p in fit.mealPlans)
+ for (final p in fit.activeMealPlans)
OptionItem(
p.name,
leading: FoodPlate(items: _plateOf(p.items), size: 34),
@@ -222,7 +239,7 @@ Future<void> _pickPlanForDay(BuildContext context, int weekday) {
HapticFeedback.selectionClick();
fit.setPlanDay(weekday, p.id);
Navigator.of(sheet).pop();
- showNotchToast(context, t.planSetForDay(t.weekday(weekday).toLowerCase()),
+ showNotchToast(context, t.planSetForDay(t.weekdayInline(weekday)),
subtitle: p.name, icon: PhosphorIconsFill.calendarCheck, accent: context.gc.sage);
},
),
@@ -378,6 +395,23 @@ class _PlanEditorState extends State<_PlanEditor> {
fit.moveMealPlan(plan.id, 1);
Navigator.of(sheet).pop();
}),
+ OptionItem(fit.archivedPlans.contains(plan.id) ? t.dishUnarchive : t.dishArchive,
+ icon: fit.archivedPlans.contains(plan.id) ? PhosphorIconsRegular.arrowUUpLeft : PhosphorIconsRegular.archive,
+ onTap: () {
+ final on = !fit.archivedPlans.contains(plan.id);
+ final days = fit.setPlanArchived(plan.id, on);
+ _dirty = false;
+ Navigator.of(sheet).pop();
+ if (mounted) Navigator.of(context).pop();
+ if (on) {
+ showNotchToast(context, t.planArchivedToast,
+ subtitle: plan.name,
+ icon: PhosphorIconsFill.archive,
+ accent: context.gc.textSecondary,
+ action: t.undo,
+ onTap: () => fit.setPlanArchived(plan.id, false, restore: days));
+ }
+ }),
OptionItem(t.delete, icon: PhosphorIconsRegular.trash, danger: true, onTap: () async {
final ok = await askConfirm(
sheet,
@@ -676,7 +710,7 @@ class _TodayPlanCard extends StatelessWidget {
Container(
width: 14,
height: 4,
- margin: const EdgeInsets.only(right: 3),
+ margin: const EdgeInsetsDirectional.only(end: 3),
decoration: BoxDecoration(
color: i < p.done ? gc.sage : gc.border,
borderRadius: BorderRadius.circular(2),
diff --git a/lib/screens/nutrition_screen.dart b/lib/screens/nutrition_screen.dart
index a04df54..8339441 100644
--- a/lib/screens/nutrition_screen.dart
+++ b/lib/screens/nutrition_screen.dart
@@ -234,7 +234,7 @@ class _NutritionScreenState extends State<NutritionScreen> {
const SizedBox(height: 26),
_Heading(t.mealPlansTitle, action: t.mealPlanNew, onAction: () => showNewPlanSheet(context)),
const SizedBox(height: 12),
- const _PlansSection(),
+ _PlansSection(),
],
if (detail.isNotEmpty) ...[
const SizedBox(height: 26),
@@ -281,7 +281,7 @@ class _Heading extends StatelessWidget {
Widget build(BuildContext context) {
final gc = context.gc;
return Padding(
- padding: const EdgeInsets.only(left: 4),
+ padding: const EdgeInsetsDirectional.only(start: 4),
child: Row(
children: [
Expanded(
@@ -445,9 +445,43 @@ Future<T?> _sheet<T>(BuildContext context, WidgetBuilder builder) => showAppShee
context: context,
isScrollControlled: true,
backgroundColor: Colors.transparent,
- builder: (sheet) => AnimatedBuilder(animation: fit, builder: (c, _) => builder(c)),
+ builder: (sheet) => _LiveSheet(builder: builder),
);
+class _LiveSheet extends StatefulWidget {
+ const _LiveSheet({required this.builder});
+
+ final WidgetBuilder builder;
+
+ @override
+ State<_LiveSheet> createState() => _LiveSheetState();
+}
+
+class _LiveSheetState extends State<_LiveSheet> {
+ @override
+ void initState() {
+ super.initState();
+ fit.addListener(_changed);
+ }
+
+ @override
+ void dispose() {
+ fit.removeListener(_changed);
+ super.dispose();
+ }
+
+ void _changed() {
+ if (!mounted) return;
+ if (ModalRoute.isCurrentOf(context) ?? true) setState(() {});
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ ModalRoute.isCurrentOf(context);
+ return widget.builder(context);
+ }
+}
+
Widget _macroSummary(BuildContext context, Macros m) {
final gc = context.gc;
Widget cell(String label, String value, Color color) => Expanded(
diff --git a/lib/state/nutrition_state.dart b/lib/state/nutrition_state.dart
index 1794163..52602e5 100644
--- a/lib/state/nutrition_state.dart
+++ b/lib/state/nutrition_state.dart
@@ -7,6 +7,12 @@ mixin NutritionState on FitCore, ToolsState, StatsState {
final List<Supplement> supplements = [];
final List<Recipe> recipes = [];
final Set<String> archivedRecipes = {};
+ final Set<String> archivedFoods = {};
+ final Set<String> archivedPlans = {};
+ final Set<String> archivedSupps = {};
+ bool showArchivedFoods = false;
+ bool showArchivedPlans = false;
+ bool showArchivedSupps = false;
bool showArchivedRecipes = false;
final List<Batch> batches = [];
final List<String> favoriteFoods = [];
@@ -733,7 +739,7 @@ mixin NutritionState on FitCore, ToolsState, StatsState {
final q = foldText(query.trim());
return [
for (final f in foodsByUse)
- if ((q.isEmpty || foldText(f.name).contains(q)) &&
+ if ((q.isEmpty ? !archivedFoods.contains(f.id) : foldText(f.name).contains(q)) &&
(cat == null || f.cat == cat) &&
(group == null || f.group == group))
f,
@@ -835,6 +841,64 @@ mixin NutritionState on FitCore, ToolsState, StatsState {
notifyListeners();
}
+ List<FoodItem> get archivedFoodList => [for (final f in foodsByUse) if (archivedFoods.contains(f.id)) f];
+
+ List<MealPlan> get activeMealPlans => [for (final p in mealPlans) if (!archivedPlans.contains(p.id)) p];
+
+ List<MealPlan> get archivedPlanList => [for (final p in mealPlans) if (archivedPlans.contains(p.id)) p];
+
+ List<Supplement> get activeSupplements => [for (final s in supplements) if (!archivedSupps.contains(s.id)) s];
+
+ List<Supplement> get archivedSuppList => [for (final s in supplements) if (archivedSupps.contains(s.id)) s];
+
+ void setFoodArchived(String id, bool on) {
+ if (on ? !archivedFoods.add(id) : !archivedFoods.remove(id)) return;
+ if (on) favoriteFoods.remove(id);
+ if (archivedFoods.isEmpty) showArchivedFoods = false;
+ _dropCaches();
+ _persist();
+ notifyListeners();
+ }
+
+ List<int> setPlanArchived(String id, bool on, {List<int> restore = const []}) {
+ if (on ? !archivedPlans.add(id) : !archivedPlans.remove(id)) return const [];
+ final days = [for (final e in mealWeek.entries) if (e.value == id) e.key];
+ if (on) {
+ mealWeek.removeWhere((_, v) => v == id);
+ } else {
+ for (final d in restore) {
+ mealWeek.putIfAbsent(d, () => id);
+ }
+ }
+ if (archivedPlans.isEmpty) showArchivedPlans = false;
+ _persist();
+ notifyListeners();
+ return on ? days : const [];
+ }
+
+ void setSuppArchived(String id, bool on) {
+ if (on ? !archivedSupps.add(id) : !archivedSupps.remove(id)) return;
+ if (archivedSupps.isEmpty) showArchivedSupps = false;
+ syncSupplementReminder();
+ _persist();
+ notifyListeners();
+ }
+
+ void toggleArchivedFoods() {
+ showArchivedFoods = !showArchivedFoods;
+ notifyListeners();
+ }
+
+ void toggleArchivedPlans() {
+ showArchivedPlans = !showArchivedPlans;
+ notifyListeners();
+ }
+
+ void toggleArchivedSupps() {
+ showArchivedSupps = !showArchivedSupps;
+ notifyListeners();
+ }
+
void toggleArchivedRecipes() {
showArchivedRecipes = !showArchivedRecipes;
notifyListeners();
@@ -976,6 +1040,7 @@ mixin NutritionState on FitCore, ToolsState, StatsState {
void deleteMealPlan(String id) {
mealPlans.removeWhere((p) => p.id == id);
+ archivedPlans.remove(id);
mealWeek.removeWhere((_, v) => v == id);
_persist();
notifyListeners();
@@ -1301,6 +1366,7 @@ mixin NutritionState on FitCore, ToolsState, StatsState {
if (shakeFoodId == id) shakeFoodId = '';
shakeFoods.remove(id);
favoriteFoods.remove(id);
+ archivedFoods.remove(id);
final gone = foods.removeAt(i);
if (gone.photo.isNotEmpty && !isAssetPhoto(gone.photo)) MediaStore.delete(gone.photo);
_persist();
@@ -1375,12 +1441,13 @@ mixin NutritionState on FitCore, ToolsState, StatsState {
int? supplementTime(Supplement s) => s.at ?? supplementReminderMin;
- List<Supplement> supplementsDueOn(DateTime day) => [for (final s in supplements) if (s.dueOn(day)) s];
+ List<Supplement> supplementsDueOn(DateTime day) => [for (final s in activeSupplements) if (s.dueOn(day)) s];
void removeSupplement(String id) {
final gone = supplements.where((s) => s.id == id).firstOrNull;
if (gone != null && gone.photo.isNotEmpty && !isAssetPhoto(gone.photo)) MediaStore.delete(gone.photo);
supplements.removeWhere((s) => s.id == id);
+ archivedSupps.remove(id);
for (final taken in supplementsTaken.values) {
taken.remove(id);
}
@@ -1419,7 +1486,7 @@ mixin NutritionState on FitCore, ToolsState, StatsState {
for (var i = 0; i < 14; i++) {
final day = shiftDays(today, i);
final byTime = <int, List<Supplement>>{};
- for (final s in supplements) {
+ for (final s in activeSupplements) {
final at = supplementTime(s);
if (at == null || !s.dueOn(day) || (i == 0 && taken.contains(s.id))) continue;
byTime.putIfAbsent(at, () => []).add(s);
@@ -1617,6 +1684,9 @@ mixin NutritionState on FitCore, ToolsState, StatsState {
'suppAt': ?supplementReminderMin,
if (recipes.isNotEmpty) 'recipes': recipes.map((r) => r.toJson()).toList(),
if (archivedRecipes.isNotEmpty) 'recArch': archivedRecipes.toList(),
+ if (archivedFoods.isNotEmpty) 'foodArch': archivedFoods.toList(),
+ if (archivedPlans.isNotEmpty) 'planArch': archivedPlans.toList(),
+ if (archivedSupps.isNotEmpty) 'suppArch': archivedSupps.toList(),
if (favoriteFoods.isNotEmpty) 'fav': favoriteFoods,
if (batches.isNotEmpty) 'batches': batches.map((b) => b.toJson()).toList(),
'meals': mealSlots.map((m) => m.toJson()).toList(),
@@ -1693,6 +1763,14 @@ mixin NutritionState on FitCore, ToolsState, StatsState {
..clear()
..addAll([for (final id in (j?['recArch'] as List?) ?? const []) if (id is String && recipes.any((r) => r.id == id)) id]);
showArchivedRecipes = false;
+ for (final (set, key) in [(archivedFoods, 'foodArch'), (archivedPlans, 'planArch'), (archivedSupps, 'suppArch')]) {
+ set
+ ..clear()
+ ..addAll([for (final id in (j?[key] as List?) ?? const []) if (id is String) id]);
+ }
+ showArchivedFoods = false;
+ showArchivedPlans = false;
+ showArchivedSupps = false;
batches
..clear()
..addAll(list('batches').map(Batch.fromJson).nonNulls);
diff --git a/lib/widgets/nutrition_art.dart b/lib/widgets/nutrition_art.dart
index d26edfd..abd4a21 100644
--- a/lib/widgets/nutrition_art.dart
+++ b/lib/widgets/nutrition_art.dart
@@ -186,6 +186,13 @@ class FoodSticker extends StatelessWidget {
static const _faded = AlwaysStoppedAnimation(0.45);
+ static Future<void> warm(BuildContext context, String photo) async {
+ if (!isAssetPhoto(photo)) return;
+ try {
+ await precacheImage(AssetImage(assetOfPhoto(photo)), context);
+ } catch (_) {}
+ }
+
@override
Widget build(BuildContext context) {
final gc = context.gc;
@@ -193,7 +200,8 @@ class FoodSticker extends StatelessWidget {
final path = photo.isEmpty || asset ? null : MediaStore.pathFor(photo);
final file = path == null ? null : File(path);
final angle = tilt ? ((name.hashCode % 9) - 4) * 0.012 : 0.0;
- final cache = (size * MediaQuery.devicePixelRatioOf(context)).round();
+ final px = (size * MediaQuery.devicePixelRatioOf(context)).round();
+ final cache = asset && px >= 300 ? null : px;
Widget art;
if (asset || file != null) {
final image = asset
@@ -850,10 +858,11 @@ class _VesselPainter extends CustomPainter {
}
class FoodPlate extends StatelessWidget {
- const FoodPlate({super.key, required this.items, this.size = 150});
+ const FoodPlate({super.key, required this.items, this.size = 150, this.wrap});
final List<(String photo, String name)> items;
final double size;
+ final Widget Function(int i, Widget child)? wrap;
static const _layouts = <List<(double, double, double)>>[
[],
@@ -896,7 +905,9 @@ class FoodPlate extends StatelessWidget {
Positioned(
left: size / 2 + layout[i].$1 * size - layout[i].$3 * size / 2,
top: size / 2 + layout[i].$2 * size - layout[i].$3 * size / 2,
- child: FoodSticker(photo: shown[i].$1, name: shown[i].$2, size: layout[i].$3 * size),
+ child: wrap == null
+ ? FoodSticker(photo: shown[i].$1, name: shown[i].$2, size: layout[i].$3 * size)
+ : wrap!(i, FoodSticker(photo: shown[i].$1, name: shown[i].$2, size: layout[i].$3 * size)),
),
]),
);
diff --git a/test/nutrition_test.dart b/test/nutrition_test.dart
index 1b2115e..761e2d3 100644
--- a/test/nutrition_test.dart
+++ b/test/nutrition_test.dart
@@ -990,4 +990,39 @@ void main() {
expect(fit.fasts, isEmpty);
});
});
+
+ group('archivar comida', () {
+ test('un alimento propio archivado sale de Mis alimentos pero se encuentra al buscarlo', () {
+ final f = fit.saveFood(name: 'Tarta de la abuela', grams: 100, macros: const Macros(kcal: 300, protein: 5, carbs: 40, fat: 12));
+ fit.setFoodArchived(f.id, true);
+ expect(fit.searchFoods('').map((x) => x.id), isNot(contains(f.id)));
+ expect(fit.searchFoods('abuela').map((x) => x.id), contains(f.id));
+ expect(fit.archivedFoodList.map((x) => x.id), [f.id]);
+ fit.setFoodArchived(f.id, false);
+ expect(fit.searchFoods('').map((x) => x.id), contains(f.id));
+ });
+
+ test('un plan archivado deja sus días libres y al recuperarlo vuelven', () {
+ final p = fit.createMealPlan(name: 'Volumen');
+ fit.setPlanDay(1, p.id);
+ fit.setPlanDay(3, p.id);
+ final days = fit.setPlanArchived(p.id, true);
+ expect(days, unorderedEquals([1, 3]));
+ expect(fit.activeMealPlans, isEmpty);
+ expect(fit.mealWeek, isEmpty);
+ fit.setPlanArchived(p.id, false, restore: days);
+ expect(fit.mealWeek, {1: p.id, 3: p.id});
+ });
+
+ test('un suplemento archivado no toca hoy y se guarda en la copia', () {
+ final s = fit.addSupplement('Creatina')!;
+ fit.setSuppArchived(s.id, true);
+ expect(fit.supplementsDueOn(DateTime.now()), isEmpty);
+ final json = fit.toJson();
+ fit.resetAllData();
+ fit.applyBackup(json);
+ expect(fit.archivedSupps, {s.id});
+ expect(fit.activeSupplements, isEmpty);
+ });
+ });
}