mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-03 02:24:30 +01:00
Don't alert again when returning to an expired rest countdown
This commit is contained in:
@@ -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);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -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));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user