Don't alert again when returning to an expired rest countdown

This commit is contained in:
Roland Geider
2026-10-25 21:32:54 +01:00
parent 2d129a91f1
commit 6fdc857381
2 changed files with 41 additions and 9 deletions

View File

@@ -145,18 +145,15 @@ class _TimerCountdownWidgetState extends ConsumerState<TimerCountdownWidget> {
void initState() {
super.initState();
// Don't notify when returning to a countdown that already expired while
// the user was on another page. Set before the first build, which notifies.
_hasNotified = _endTime(ref.read(gymStateProvider)).isBefore(clock.now());
// Deferred because a provider can't be modified during the widget
// life-cycle.
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted) {
return;
}
ref.read(gymStateProvider.notifier).startTimerIfNeeded(widget.slotUuid);
// Don't notify when returning to a countdown that already expired while
// the user was on another page.
if (_endTime(ref.read(gymStateProvider)).isBefore(clock.now())) {
_hasNotified = true;
if (mounted) {
ref.read(gymStateProvider.notifier).startTimerIfNeeded(widget.slotUuid);
}
});

View File

@@ -18,6 +18,7 @@
import 'package:clock/clock.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart';
@@ -127,5 +128,39 @@ void main() {
expect(find.text('0:45'), findsOneWidget);
});
});
testWidgets('alerts once, but not again when returning to an expired countdown', (
tester,
) async {
final haptics = <MethodCall>[];
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(SystemChannels.platform, (
call,
) async {
if (call.method == 'HapticFeedback.vibrate') {
haptics.add(call);
}
return null;
});
addTearDown(
() => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
SystemChannels.platform,
null,
),
);
final t0 = DateTime(2024, 5, 2, 12);
await withClock(Clock.fixed(t0), () => pumpTimer(tester, 3));
// The 90s countdown runs out while the page is shown
await withClock(Clock.fixed(t0.add(const Duration(seconds: 95))), () => pumpTimer(tester, 3));
expect(haptics, hasLength(1));
// Leaving and coming back re-creates the widget
await withClock(Clock.fixed(t0.add(const Duration(seconds: 120))), () async {
await tester.pumpWidget(const SizedBox());
await pumpTimer(tester, 3);
});
expect(haptics, hasLength(1));
});
});
}