Files
sharedinbox/lib/ui/widgets/undo_shell.dart
Thomas SharedInboxandClaude Sonnet 4.6 651110b389 fix: do not show snackbar for stale undo actions on startup (#113)
Actions persisted to the database triggered a snackbar when the app
restarted. Added a 30-second recency check so only actions created in
the current session show the snackbar; added widget tests covering both
cases.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-16 09:23:49 +02:00

52 lines
1.6 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:sharedinbox/core/models/undo_action.dart';
import 'package:sharedinbox/di.dart';
class UndoShell extends ConsumerWidget {
const UndoShell({super.key, required this.child});
final Widget child;
@override
Widget build(BuildContext context, WidgetRef ref) {
ref.listen<List<UndoAction>>(undoServiceProvider, (previous, next) {
if (next.isNotEmpty &&
(previous == null || previous.length < next.length)) {
final action = next.last;
// Don't show a snackbar for actions loaded from persistence on app
// startup — only for actions pushed in this session.
if (DateTime.now().difference(action.timestamp).inSeconds < 30) {
_showUndoSnackbar(context, ref, action);
}
}
});
return child;
}
void _showUndoSnackbar(
BuildContext context,
WidgetRef ref,
UndoAction action,
) {
final scaffoldMessenger = ScaffoldMessenger.of(context);
scaffoldMessenger.clearSnackBars();
scaffoldMessenger.showSnackBar(
SnackBar(
duration: const Duration(seconds: 5),
content: Text(
action.type == UndoType.delete
? '${action.emailIds.length} email(s) moved to Trash'
: '${action.emailIds.length} email(s) moved',
),
action: SnackBarAction(
label: 'Undo',
textColor: Colors.redAccent,
onPressed: () => ref.read(undoServiceProvider.notifier).undo(),
),
),
);
}
}