Skip to content

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

Edit past fasts and add the ones you forgot

InlitX7577f2d8dec8
diff --git a/lib/screens/nutrition/fasting.dart b/lib/screens/nutrition/fasting.dart
index 20d52ee..cef7820 100644
--- a/lib/screens/nutrition/fasting.dart
+++ b/lib/screens/nutrition/fasting.dart
@@ -186,7 +186,20 @@ class _FastingCardState extends State<_FastingCard> {
fg: reached ? null : gc.text,
onTap: () {
HapticFeedback.mediumImpact();
- fit.endFast();
+ final saved = fit.endFast();
+ if (saved == null) return;
+ showNotchToast(
+ context,
+ t.fastSaved,
+ subtitle: _span(saved.length),
+ icon: PhosphorIconsFill.hourglassMedium,
+ accent: saved.reached ? gc.sage : gc.brass,
+ action: t.fastAdjust,
+ onTap: () {
+ final kept = fit.fasts.where((r) => identical(r, saved));
+ if (kept.isNotEmpty) showFastEditor(context, record: kept.first);
+ },
+ );
},
),
),
@@ -260,7 +273,13 @@ Future<void> showFastingSheet(BuildContext context) => _sheet<void>(context, (co
for (final r in recent)
Padding(
padding: const EdgeInsets.only(bottom: 6),
- child: Container(
+ child: Pressable(
+ scale: 0.98,
+ onTap: () {
+ HapticFeedback.selectionClick();
+ showFastEditor(context, record: r);
+ },
+ child: Container(
padding: const EdgeInsets.fromLTRB(14, 10, 6, 10),
decoration: BoxDecoration(color: gc.bgRaised2, borderRadius: BorderRadius.circular(16)),
child: Row(children: [
@@ -280,11 +299,18 @@ Future<void> showFastingSheet(BuildContext context) => _sheet<void>(context, (co
child: Icon(PhosphorIconsRegular.trash, size: 15, color: gc.textTertiary),
),
]),
+ ),
),
),
],
+ const SizedBox(height: 14),
+ GhostButton(
+ label: t.fastAdd,
+ icon: PhosphorIconsRegular.plus,
+ onTap: () => showFastEditor(context),
+ ),
if (fit.fasting) ...[
- const SizedBox(height: 14),
+ const SizedBox(height: 8),
GhostButton(
label: t.fastCancel,
icon: PhosphorIconsRegular.x,
@@ -309,3 +335,164 @@ Widget _fastStat(GymColors gc, String label, String value) => Container(
FitText(value, style: AppTheme.f(16, weight: FontWeight.w800, color: gc.text)),
]),
);
+
+Future<void> showFastEditor(BuildContext context, {FastRecord? record}) =>
+ _sheet<void>(context, (_) => _FastEditSheet(record: record));
+
+class _FastEditSheet extends StatefulWidget {
+ const _FastEditSheet({this.record});
+
+ final FastRecord? record;
+
+ @override
+ State<_FastEditSheet> createState() => _FastEditSheetState();
+}
+
+class _FastEditSheetState extends State<_FastEditSheet> {
+ late DateTime _end = widget.record?.end ?? _now();
+ late DateTime _start = widget.record?.start ?? _end.subtract(Duration(hours: fit.fastGoalHours));
+
+ static DateTime _now() {
+ final n = DateTime.now();
+ return DateTime(n.year, n.month, n.day, n.hour, n.minute);
+ }
+
+ String _dayName(DateTime d) {
+ final now = DateTime.now();
+ final today = DateTime(now.year, now.month, now.day);
+ final day = DateTime(d.year, d.month, d.day);
+ if (day == today) return t.nutritionToday;
+ if (day == shiftDays(today, -1)) return t.nutritionYesterday;
+ return t.shortDate(d);
+ }
+
+ Future<DateTime?> _pickDay(DateTime at) async {
+ final now = DateTime.now();
+ final floor = DateTime(now.year, now.month, now.day).subtract(const Duration(days: 90));
+ final picked = await showDatePicker(
+ context: context,
+ initialDate: at,
+ firstDate: at.isBefore(floor) ? at : floor,
+ lastDate: now,
+ );
+ if (picked == null) return null;
+ return DateTime(picked.year, picked.month, picked.day, at.hour, at.minute);
+ }
+
+ Future<DateTime?> _pickTime(DateTime at, String help) async {
+ final picked = await showTimePicker(
+ context: context,
+ initialTime: TimeOfDay.fromDateTime(at),
+ initialEntryMode: TimePickerEntryMode.inputOnly,
+ helpText: help,
+ );
+ if (picked == null) return null;
+ return DateTime(at.year, at.month, at.day, picked.hour, picked.minute);
+ }
+
+ Widget _when(GymColors gc, DateTime at, String help, ValueChanged<DateTime> set) {
+ Widget part(String text, IconData icon, Future<DateTime?> Function() pick) => GestureDetector(
+ behavior: HitTestBehavior.opaque,
+ onTap: () async {
+ HapticFeedback.selectionClick();
+ final v = await pick();
+ if (v != null && mounted) setState(() => set(v));
+ },
+ child: Padding(
+ padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 12),
+ child: Row(mainAxisSize: MainAxisSize.min, children: [
+ Text(text, style: AppTheme.f(14, weight: FontWeight.w800, color: gc.accent)),
+ const SizedBox(width: 6),
+ Icon(icon, size: 15, color: gc.accent),
+ ]),
+ ),
+ );
+ return Row(mainAxisSize: MainAxisSize.min, children: [
+ part(_dayName(at), PhosphorIconsBold.calendarBlank, () => _pickDay(at)),
+ Ltr(child: part(t.clockTime(at), PhosphorIconsBold.clock, () => _pickTime(at, help))),
+ ]);
+ }
+
+ Widget _row(GymColors gc, String label, Widget control) => Container(
+ constraints: const BoxConstraints(minHeight: 56),
+ padding: const EdgeInsetsDirectional.only(start: 16, end: 8),
+ decoration: BoxDecoration(color: gc.bgRaised2, borderRadius: BorderRadius.circular(22)),
+ child: Row(children: [
+ Expanded(
+ child: Text(label,
+ maxLines: 1,
+ overflow: TextOverflow.ellipsis,
+ style: AppTheme.f(14.5, weight: FontWeight.w600, color: gc.text)),
+ ),
+ const SizedBox(width: 8),
+ control,
+ ]),
+ );
+
+ String? _problemText(String? p) => switch (p) {
+ 'future' => t.fastErrFuture,
+ 'order' => t.fastErrOrder,
+ 'short' => t.fastErrShort,
+ 'overlap' => t.fastErrOverlap,
+ _ => null,
+ };
+
+ @override
+ Widget build(BuildContext context) {
+ final gc = context.gc;
+ final problem = fit.fastProblem(_start, _end, editing: widget.record);
+ final goal = widget.record?.goalHours ?? fit.fastGoalHours;
+ final length = _end.difference(_start);
+ final reached = problem == null && length.inMinutes >= goal * 60;
+ return _sheetFrame(context, [
+ SheetTitle(widget.record == null ? t.fastAddTitle : t.fastEditTitle),
+ const SizedBox(height: 16),
+ _row(gc, t.fastStartRow, _when(gc, _start, t.fastStartedAt, (v) => _start = v)),
+ const SizedBox(height: 8),
+ _row(gc, t.fastEndRow, _when(gc, _end, t.fastEndedAt, (v) => _end = v)),
+ const SizedBox(height: 14),
+ AnimatedSwitcher(
+ duration: const Duration(milliseconds: 180),
+ child: Row(
+ key: ValueKey(problem ?? '${length.inMinutes}'),
+ mainAxisAlignment: MainAxisAlignment.center,
+ children: [
+ Icon(
+ problem != null
+ ? PhosphorIconsRegular.warningCircle
+ : reached
+ ? PhosphorIconsBold.check
+ : PhosphorIconsRegular.hourglassMedium,
+ size: 14,
+ color: problem != null ? gc.danger : (reached ? gc.sage : gc.textTertiary),
+ ),
+ const SizedBox(width: 6),
+ Flexible(
+ child: Text(
+ _problemText(problem) ?? t.fastLasted(_span(length)),
+ maxLines: 2,
+ overflow: TextOverflow.ellipsis,
+ textAlign: TextAlign.center,
+ style: AppTheme.f(12.5, weight: FontWeight.w600, color: problem != null ? gc.danger : gc.textSecondary),
+ ),
+ ),
+ ],
+ ),
+ ),
+ const SizedBox(height: 16),
+ PrimaryButton(
+ label: t.save,
+ onTap: () {
+ if (problem != null) {
+ HapticFeedback.heavyImpact();
+ return;
+ }
+ HapticFeedback.mediumImpact();
+ fit.saveFast(_start, _end, replacing: widget.record);
+ Navigator.of(context).pop();
+ },
+ ),
+ const SizedBox(height: 6),
+ ]);
+ }
+}
diff --git a/lib/state/nutrition_state.dart b/lib/state/nutrition_state.dart
index e346433..ab84056 100644
--- a/lib/state/nutrition_state.dart
+++ b/lib/state/nutrition_state.dart
@@ -1760,19 +1760,49 @@ mixin NutritionState on FitCore, ToolsState, StatsState {
startFast(at: at);
}
- void endFast() {
+ FastRecord? endFast() {
final start = fastStart;
- if (start == null) return;
+ if (start == null) return null;
final end = DateTime.now();
+ FastRecord? saved;
if (end.difference(start).inMinutes >= 10) {
- fasts.add(FastRecord(start, end, fastGoalHours));
- fasts.sort((a, b) => a.start.compareTo(b.start));
- if (fasts.length > 200) fasts.removeRange(0, fasts.length - 200);
+ saved = FastRecord(start, end, fastGoalHours);
+ _keepFast(saved);
}
fastStart = null;
_syncFast();
_persist();
notifyListeners();
+ return saved;
+ }
+
+ void _keepFast(FastRecord r) {
+ fasts.add(r);
+ fasts.sort((a, b) => a.start.compareTo(b.start));
+ if (fasts.length > 200) fasts.removeRange(0, fasts.length - 200);
+ }
+
+ String? fastProblem(DateTime start, DateTime end, {FastRecord? editing}) {
+ if (end.isAfter(DateTime.now().add(const Duration(minutes: 1)))) return 'future';
+ if (!end.isAfter(start)) return 'order';
+ if (end.difference(start).inMinutes < 10) return 'short';
+ final live = fastStart;
+ if (live != null && end.isAfter(live)) return 'overlap';
+ for (final r in fasts) {
+ if (identical(r, editing)) continue;
+ if (start.isBefore(r.end) && end.isAfter(r.start)) return 'overlap';
+ }
+ return null;
+ }
+
+ FastRecord? saveFast(DateTime start, DateTime end, {FastRecord? replacing}) {
+ if (fastProblem(start, end, editing: replacing) != null) return null;
+ if (replacing != null && !fasts.remove(replacing)) return null;
+ final r = FastRecord(start, end, replacing?.goalHours ?? fastGoalHours);
+ _keepFast(r);
+ _persist();
+ notifyListeners();
+ return r;
}
void cancelFast() {
diff --git a/test/nutrition_test.dart b/test/nutrition_test.dart
index f1967bf..57fe971 100644
--- a/test/nutrition_test.dart
+++ b/test/nutrition_test.dart
@@ -1059,6 +1059,47 @@ void main() {
expect(fit.fastGoalHours, 18);
});
+ test('un ayuno terminado tarde se ajusta a la hora real', () {
+ fit.setFastGoal(16);
+ fit.startFast(at: DateTime.now().subtract(const Duration(hours: 20)));
+ final saved = fit.endFast()!;
+ final realEnd = saved.start.add(const Duration(hours: 16, minutes: 30));
+ expect(fit.saveFast(saved.start, realEnd, replacing: saved), isNotNull);
+ expect(fit.fasts.single.end, realEnd);
+ expect(fit.fasts.single.reached, true);
+ expect(fit.fasts.single.goalHours, 16);
+ });
+
+ test('se puede añadir un ayuno olvidado y se guarda en su sitio', () {
+ final now = DateTime.now();
+ fit.startFast(at: now.subtract(const Duration(hours: 2)));
+ fit.endFast();
+ final start = now.subtract(const Duration(days: 2, hours: 18));
+ final end = now.subtract(const Duration(days: 2));
+ expect(fit.saveFast(start, end), isNotNull);
+ expect(fit.fasts.first.start, start, reason: 'ordenado por inicio');
+ final saved = fit.nutritionJson();
+ fit.fasts.clear();
+ fit.loadNutrition(saved);
+ expect(fit.fasts.length, 2);
+ });
+
+ test('no se guardan ayunos imposibles', () {
+ final now = DateTime.now();
+ final a = fit.saveFast(now.subtract(const Duration(hours: 30)), now.subtract(const Duration(hours: 14)))!;
+ expect(fit.fastProblem(now.subtract(const Duration(hours: 2)), now.add(const Duration(hours: 3))), 'future');
+ expect(fit.fastProblem(now.subtract(const Duration(hours: 2)), now.subtract(const Duration(hours: 3))), 'order');
+ expect(fit.fastProblem(now.subtract(const Duration(minutes: 5)), now), 'short');
+ expect(fit.fastProblem(now.subtract(const Duration(hours: 20)), now.subtract(const Duration(hours: 10))), 'overlap');
+ expect(fit.fastProblem(a.start, a.end.add(const Duration(hours: 1)), editing: a), isNull,
+ reason: 'al editar no choca consigo mismo');
+ fit.startFast(at: now.subtract(const Duration(hours: 4)));
+ expect(fit.fastProblem(now.subtract(const Duration(hours: 8)), now.subtract(const Duration(hours: 3))), 'overlap',
+ reason: 'tampoco con el ayuno en curso');
+ expect(fit.saveFast(now.subtract(const Duration(hours: 8)), now.subtract(const Duration(hours: 3))), isNull);
+ expect(fit.fasts.length, 1);
+ });
+
test('cancelar no deja rastro en el historial', () {
fit.startFast(at: DateTime.now().subtract(const Duration(hours: 5)));
fit.cancelFast();