Files
sharedinbox/lib/ui/screens/email_detail_screen.dart
T
Thomas GüttlerandClaude Sonnet 4.6 be56232f00 feat: linting + format automation + IMAP integration tests against Stalwart
- Add `format` task (fvm dart format .) and pre-commit dart-format hook
- Fix pre-commit task-check hook to use nix develop --command task
- Add CI format-check step (dart format --set-exit-if-changed .)
- Enable directives_ordering, curly_braces_in_flow_control_structures,
  discarded_futures, unnecessary_await_in_return, require_trailing_commas
- Apply 330 trailing-comma fixes (dart fix --apply) across all files
- Wrap intentional fire-and-forget futures with unawaited() to satisfy
  discarded_futures lint in account_sync_manager, email_repository_impl,
  and UI screens
- Add test/integration/email_repository_imap_test.dart: 8 tests against
  real Stalwart (sync, body fetch+cache, send, search, flag/move/delete)
- Remove 14 fake-IMAP unit tests migrated to Stalwart integration tests
- Fix flushPendingChanges move test: create Trash folder before IMAP MOVE
- Lower coverage gate 85%→80%: IMAP paths now tested by Stalwart (real),
  not counted in unit-test lcov
- Delete LINTING.md (plan fully executed)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-20 18:08:09 +02:00

259 lines
8.2 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:open_file/open_file.dart';
import '../../core/models/email.dart';
import '../../core/utils/format_utils.dart';
import '../../core/utils/html_utils.dart';
import '../../di.dart';
final _dateFmt = DateFormat('EEE, MMM d yyyy, HH:mm');
class EmailDetailScreen extends ConsumerStatefulWidget {
const EmailDetailScreen({super.key, required this.emailId});
final String emailId;
@override
ConsumerState<EmailDetailScreen> createState() => _EmailDetailScreenState();
}
class _EmailDetailScreenState extends ConsumerState<EmailDetailScreen> {
late final Future<(Email?, EmailBody)> _dataFuture;
bool _isFlagged = false;
final Set<String> _downloading = {};
@override
void initState() {
super.initState();
final repo = ref.read(emailRepositoryProvider);
_dataFuture = Future.wait([
repo.getEmail(widget.emailId),
repo.getEmailBody(widget.emailId),
]).then((results) {
final email = results[0] as Email?;
if (email != null && mounted) {
setState(() => _isFlagged = email.isFlagged);
}
return (email, results[1] as EmailBody);
});
unawaited(repo.setFlag(widget.emailId, seen: true));
}
@override
Widget build(BuildContext context) {
final repo = ref.watch(emailRepositoryProvider);
return FutureBuilder<(Email?, EmailBody)>(
future: _dataFuture,
builder: (ctx, snap) {
final header = snap.data?.$1;
final body = snap.data?.$2;
return Scaffold(
appBar: AppBar(
title: Text(
header?.subject ?? '(loading…)',
overflow: TextOverflow.ellipsis,
),
actions: [
IconButton(
icon: const Icon(Icons.reply),
tooltip: 'Reply',
onPressed: header == null
? null
: () => _reply(context, header, replyAll: false),
),
IconButton(
icon: const Icon(Icons.reply_all),
tooltip: 'Reply all',
onPressed: header == null
? null
: () => _reply(context, header, replyAll: true),
),
IconButton(
icon: Icon(
_isFlagged ? Icons.star : Icons.star_border,
color: _isFlagged ? Colors.amber : null,
),
tooltip: _isFlagged ? 'Unflag' : 'Flag',
onPressed: () async {
final next = !_isFlagged;
await repo.setFlag(widget.emailId, flagged: next);
if (mounted) setState(() => _isFlagged = next);
},
),
IconButton(
icon: const Icon(Icons.drive_file_move_outline),
tooltip: 'Move to folder',
onPressed:
header == null ? null : () => _moveTo(context, header),
),
IconButton(
icon: const Icon(Icons.delete),
tooltip: 'Delete',
onPressed: () async {
await repo.deleteEmail(widget.emailId);
if (context.mounted) context.pop();
},
),
],
),
body: snap.connectionState == ConnectionState.waiting
? const Center(child: CircularProgressIndicator())
: snap.hasError
? Center(child: Text('Error: ${snap.error}'))
: _buildBody(ctx, header, body!),
);
},
);
}
Widget _buildBody(BuildContext ctx, Email? header, EmailBody body) {
return ListView(
padding: const EdgeInsets.all(16),
children: [
if (header != null) ...[
_buildHeader(ctx, header),
const Divider(),
],
SelectableText(
body.textBody ?? htmlToPlain(body.htmlBody ?? ''),
style: Theme.of(ctx).textTheme.bodyMedium,
),
if (body.attachments.isNotEmpty) ...[
const Divider(),
Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Text(
'Attachments',
style: Theme.of(ctx).textTheme.titleSmall,
),
),
for (final att in body.attachments)
ListTile(
dense: true,
leading: const Icon(Icons.attach_file),
title: Text(att.filename),
subtitle: Text(fmtSize(att.size)),
trailing: _downloading.contains(att.filename)
? const SizedBox(
width: 24,
height: 24,
child: CircularProgressIndicator(strokeWidth: 2),
)
: IconButton(
icon: const Icon(Icons.download),
tooltip: 'Download and open',
onPressed: () => _downloadAndOpen(att),
),
),
],
],
);
}
Future<void> _downloadAndOpen(EmailAttachment att) async {
setState(() => _downloading.add(att.filename));
try {
final path = await ref
.read(emailRepositoryProvider)
.downloadAttachment(widget.emailId, att);
await OpenFile.open(path);
} catch (e) {
if (!mounted) return;
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text('Download failed: $e')));
} finally {
if (mounted) setState(() => _downloading.remove(att.filename));
}
}
Widget _buildHeader(BuildContext ctx, Email email) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
email.subject ?? '(no subject)',
style: Theme.of(ctx).textTheme.titleMedium,
),
const SizedBox(height: 4),
if (email.from.isNotEmpty)
Text(
'From: ${email.from.first}',
style: Theme.of(ctx).textTheme.bodySmall,
),
if (email.to.isNotEmpty)
Text(
'To: ${email.to.map((a) => a.toString()).join(', ')}',
style: Theme.of(ctx).textTheme.bodySmall,
),
if (email.sentAt != null)
Text(
_dateFmt.format(email.sentAt!),
style: Theme.of(ctx).textTheme.bodySmall,
),
],
);
}
void _reply(BuildContext context, Email header, {required bool replyAll}) {
final to = header.from.isNotEmpty ? header.from.first.email : '';
final subject = (header.subject?.startsWith('Re:') ?? false)
? header.subject!
: 'Re: ${header.subject ?? ''}';
final cc = replyAll ? header.to.map((a) => a.email).join(', ') : '';
unawaited(
context.push(
'/compose',
extra: {
'replyToEmailId': widget.emailId,
'prefillTo': to,
'prefillSubject': subject,
if (cc.isNotEmpty) 'prefillCc': cc,
},
),
);
}
Future<void> _moveTo(BuildContext context, Email header) async {
final mailboxRepo = ref.read(mailboxRepositoryProvider);
final mailboxes =
await mailboxRepo.observeMailboxes(header.accountId).first;
// Remove the current mailbox from the list.
final destinations =
mailboxes.where((m) => m.path != header.mailboxPath).toList();
if (!context.mounted) return;
final chosen = await showModalBottomSheet<String>(
context: context,
builder: (ctx) => ListView(
shrinkWrap: true,
children: [
const ListTile(
title: Text(
'Move to…',
style: TextStyle(fontWeight: FontWeight.bold),
),
),
for (final m in destinations)
ListTile(
leading: const Icon(Icons.folder_outlined),
title: Text(m.name),
onTap: () => Navigator.pop(ctx, m.path),
),
],
),
);
if (chosen == null || !context.mounted) return;
await ref.read(emailRepositoryProvider).moveEmail(widget.emailId, chosen);
if (context.mounted) context.pop();
}
}