2026-05-09 15:35:17 +02:00
|
|
|
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) {
|
2026-05-10 10:32:27 +02:00
|
|
|
ref.listen<List<UndoAction>>(undoServiceProvider, (previous, next) {
|
|
|
|
|
if (next.isNotEmpty &&
|
|
|
|
|
(previous == null || previous.length < next.length)) {
|
2026-05-16 09:23:49 +02:00
|
|
|
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);
|
|
|
|
|
}
|
2026-05-09 15:35:17 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return child;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-09 18:59:12 +02:00
|
|
|
void _showUndoSnackbar(
|
|
|
|
|
BuildContext context,
|
|
|
|
|
WidgetRef ref,
|
|
|
|
|
UndoAction action,
|
|
|
|
|
) {
|
2026-05-09 15:35:17 +02:00
|
|
|
final scaffoldMessenger = ScaffoldMessenger.of(context);
|
|
|
|
|
scaffoldMessenger.clearSnackBars();
|
|
|
|
|
scaffoldMessenger.showSnackBar(
|
|
|
|
|
SnackBar(
|
2026-05-14 20:37:06 +02:00
|
|
|
duration: const Duration(seconds: 5),
|
2026-05-09 15:35:17 +02:00
|
|
|
content: Text(
|
|
|
|
|
action.type == UndoType.delete
|
|
|
|
|
? '${action.emailIds.length} email(s) moved to Trash'
|
|
|
|
|
: '${action.emailIds.length} email(s) moved',
|
|
|
|
|
),
|
|
|
|
|
action: SnackBarAction(
|
|
|
|
|
label: 'Undo',
|
2026-05-11 07:33:22 +02:00
|
|
|
textColor: Colors.redAccent,
|
2026-05-09 15:35:17 +02:00
|
|
|
onPressed: () => ref.read(undoServiceProvider.notifier).undo(),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|