Files
sharedinbox/lib/ui/screens/thread_detail_screen.dart
T
8ea5237991 fix(detail): auto-dismiss "Load remote images" snack bar (#548)
## Summary

- The "Load remote images" snack bar in single-mail view (and the analogous thread view) never disappeared on its own — the user had to interact with it.
- Flutter's `SnackBar` defaults to `persist: true` whenever an `action` is provided (see `flutter/lib/src/material/snack_bar.dart`: `persist = persist ?? action != null`), which short-circuits the duration-based dismiss timer in `ScaffoldMessengerState.build`:

  ```dart
  _snackBarTimer = Timer(snackBar.duration, () {
    if (snackBar.persist) return;          // <-- here
    hideCurrentSnackBar(reason: SnackBarClosedReason.timeout);
  });
  ```

  So the explicit `duration: 3s` was set, but the "View" action made the snack bar persistent and the timer's callback returned early.
- Pass `persist: false` explicitly on both snack bars so the 3-second timer fires and the snack bar slides away on its own, while the "View" action button still works to navigate to the trusted-senders settings.

## Test plan

- [x] Added widget regression test in `test/widget/email_detail_screen_test.dart` (`Load remote images snack bar auto-dismisses after 3 seconds`).
- [x] Added analogous test in `test/widget/thread_detail_screen_test.dart`.
- [x] `task test-widget` — all 174 widget tests pass.
- [x] `scripts/run_unit_tests.sh` — all 552 unit tests pass.
- [x] `fvm dart analyze --fatal-infos` on changed files — no issues.
- [x] `fvm dart format` — no diffs.
- [ ] Manual: open a single mail with HTML body from an untrusted sender; tap "Load remote images"; verify the snack bar appears, images load, and the snack bar disappears after ~3 seconds while the "View" action button still navigates to `/accounts/trusted-senders` when tapped.

Closes #484

Co-authored-by: Agentloop Bot <agentloop-bot@noreply.codeberg.org>
Reviewed-on: https://codeberg.org/guettli/sharedinbox/pulls/548
2026-06-08 21:59:49 +02:00

332 lines
11 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:intl/intl.dart';
import 'package:sharedinbox/core/models/email.dart';
import 'package:sharedinbox/core/models/undo_action.dart';
import 'package:sharedinbox/core/models/user_preferences.dart';
import 'package:sharedinbox/core/utils/glob_match.dart';
import 'package:sharedinbox/core/utils/html_utils.dart';
import 'package:sharedinbox/di.dart';
import 'package:sharedinbox/ui/widgets/secure_email_webview.dart';
final _dateFmt = DateFormat('EEE, MMM d, HH:mm');
class ThreadDetailScreen extends ConsumerWidget {
const ThreadDetailScreen({
super.key,
required this.accountId,
required this.mailboxPath,
required this.threadId,
});
final String accountId;
final String mailboxPath;
final String threadId;
@override
Widget build(BuildContext context, WidgetRef ref) {
final repo = ref.watch(emailRepositoryProvider);
final prefs =
ref.watch(userPreferencesProvider).value ?? const UserPreferences();
final buttonAtBottom = prefs.mailViewButtonPosition == MenuPosition.bottom;
return Scaffold(
appBar: AppBar(
title: const Text('Thread'),
automaticallyImplyLeading: !buttonAtBottom,
),
bottomNavigationBar: buttonAtBottom ? _buildBackButtonBar(context) : null,
body: StreamBuilder<List<Email>>(
stream: repo.observeEmailsInThread(accountId, mailboxPath, threadId),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
}
if (snapshot.hasError) {
return Center(child: Text('Error: ${snapshot.error}'));
}
final emails = snapshot.data ?? [];
if (emails.isEmpty) {
return const Center(child: Text('Thread not found or empty'));
}
return ListView.builder(
padding: const EdgeInsets.all(8),
itemCount: emails.length,
itemBuilder: (context, index) {
final email = emails[index];
return _EmailMessageCard(
email: email,
isLatest: index == emails.length - 1,
);
},
);
},
),
);
}
Widget _buildBackButtonBar(BuildContext context) {
return BottomAppBar(
child: Row(
children: [
IconButton(
icon: const Icon(Icons.arrow_back),
tooltip: 'Back',
onPressed: () => context.pop(),
),
],
),
);
}
}
class _EmailMessageCard extends ConsumerStatefulWidget {
const _EmailMessageCard({required this.email, required this.isLatest});
final Email email;
final bool isLatest;
@override
ConsumerState<_EmailMessageCard> createState() => _EmailMessageCardState();
}
class _EmailMessageCardState extends ConsumerState<_EmailMessageCard> {
late Future<EmailBody> _bodyFuture;
bool _expanded = false;
bool _loadRemoteImages = false;
@override
void initState() {
super.initState();
_bodyFuture =
ref.read(emailRepositoryProvider).getEmailBody(widget.email.id);
_expanded = widget.isLatest;
if (widget.email.isSeen == false) {
unawaited(
ref.read(emailRepositoryProvider).setFlag(widget.email.id, seen: true),
);
}
}
@override
Widget build(BuildContext context) {
final trustedSenders =
ref.watch(trustedImageSendersProvider).value ?? const <String>[];
final senderEmail = widget.email.from.isNotEmpty
? widget.email.from.first.email.toLowerCase()
: null;
final isTrusted = senderEmail != null &&
trustedSenders.any((p) => globMatch(senderEmail, p));
return Card(
margin: const EdgeInsets.symmetric(vertical: 4),
child: Column(
children: [
ListTile(
onTap: () => setState(() => _expanded = !_expanded),
leading: CircleAvatar(
child: Text(
widget.email.from.isNotEmpty
? widget.email.from.first.email[0].toUpperCase()
: '?',
),
),
title: Text(
widget.email.from.isNotEmpty
? widget.email.from.first.toString()
: '(unknown)',
style: const TextStyle(fontWeight: FontWeight.bold),
),
subtitle: Text(
widget.email.sentAt != null
? _dateFmt.format(widget.email.sentAt!)
: '',
style: Theme.of(context).textTheme.bodySmall,
),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
if (widget.email.isFlagged)
const Icon(Icons.star, color: Colors.amber, size: 20),
Icon(_expanded ? Icons.expand_less : Icons.expand_more),
],
),
),
if (_expanded) _buildExpandedBody(isTrusted, senderEmail),
],
),
);
}
Widget _buildExpandedBody(bool isTrusted, String? senderEmail) {
return Padding(
padding: const EdgeInsets.fromLTRB(16, 0, 16, 16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Divider(),
FutureBuilder<EmailBody>(
future: _bodyFuture,
builder: (context, snapshot) {
if (snapshot.hasError) {
return Padding(
padding: const EdgeInsets.all(16),
child: Text(
'Failed to load email: ${snapshot.error}',
style: TextStyle(
color: Theme.of(context).colorScheme.error,
),
),
);
}
if (!snapshot.hasData) {
return const Center(
child: Padding(
padding: EdgeInsets.all(16),
child: CircularProgressIndicator(strokeWidth: 2),
),
);
}
final body = snapshot.data!;
final hasHtml = (body.htmlBody ?? '').trim().isNotEmpty;
final effectiveLoadImages = _loadRemoteImages || isTrusted;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (hasHtml) ...[
if (!effectiveLoadImages)
TextButton.icon(
icon: const Icon(Icons.image_outlined, size: 16),
label: const Text('Load remote images'),
onPressed: () {
setState(() => _loadRemoteImages = true);
if (senderEmail != null) {
unawaited(
ref
.read(userPreferencesRepositoryProvider)
.addTrustedImageSender(senderEmail),
);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
duration: const Duration(seconds: 3),
// SnackBar defaults to persist=true when an
// action is set, which disables auto-dismiss.
// Explicitly opt into duration-based dismiss.
persist: false,
content: const Text(
'Images will be loaded automatically for this sender.',
),
action: SnackBarAction(
label: 'View',
onPressed: () {
if (mounted) {
unawaited(
context.push(
'/accounts/trusted-senders',
extra: senderEmail,
),
);
}
},
),
),
);
}
},
),
SecureEmailWebView(
htmlBody: body.htmlBody!,
loadRemoteImages: effectiveLoadImages,
),
] else
SelectableText(
body.textBody ?? '(no body text)',
style: Theme.of(context).textTheme.bodyMedium,
),
const SizedBox(height: 16),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
IconButton(
icon: const Icon(Icons.reply),
onPressed: () => _reply(context, body, replyAll: false),
),
IconButton(
icon: const Icon(Icons.delete_outline),
onPressed: _delete,
),
],
),
],
);
},
),
],
),
);
}
void _reply(BuildContext context, EmailBody body, {required bool replyAll}) {
final to =
widget.email.from.isNotEmpty ? widget.email.from.first.email : '';
final subject = (widget.email.subject?.startsWith('Re:') ?? false)
? widget.email.subject!
: 'Re: ${widget.email.subject ?? ''}';
unawaited(
context.push(
'/compose',
extra: {
'accountId': widget.email.accountId,
'replyToEmailId': widget.email.id,
'prefillTo': to,
'prefillSubject': subject,
'prefillBody': _quotedBody(body),
},
),
);
}
String _quotedBody(EmailBody body) {
final date = widget.email.sentAt != null
? _dateFmt.format(widget.email.sentAt!)
: '';
final from = widget.email.from.isNotEmpty
? widget.email.from.first.toString()
: '(unknown)';
final text = body.textBody ?? htmlToPlain(body.htmlBody ?? '');
final quoted = text.trim().split('\n').map((l) => '> $l').join('\n');
return '\n\n— On $date, $from wrote:\n$quoted';
}
Future<void> _delete() async {
final repo = ref.read(emailRepositoryProvider);
// Fetch data first for IMAP undo support
final original = await repo.getEmail(widget.email.id);
final destPath = await repo.deleteEmail(widget.email.id);
if (!mounted) return;
if (original != null) {
unawaited(
ref.read(undoServiceProvider.notifier).pushAction(
UndoAction(
id: DateTime.now().toIso8601String(),
accountId: widget.email.accountId,
type: UndoType.delete,
emailIds: [widget.email.id],
sourceMailboxPath: widget.email.mailboxPath,
destinationMailboxPath: destPath,
originalEmails: [original],
),
),
);
}
}
}