2026-04-16 07:35:56 +02:00
|
|
|
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 '../../core/models/email.dart';
|
2026-04-16 08:11:29 +02:00
|
|
|
import '../../core/utils/format_utils.dart';
|
|
|
|
|
import '../../core/utils/html_utils.dart';
|
2026-04-16 07:35:56 +02:00
|
|
|
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> {
|
2026-04-16 07:51:52 +02:00
|
|
|
late final Future<(Email?, EmailBody)> _dataFuture;
|
2026-04-16 07:35:56 +02:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
2026-04-16 07:51:52 +02:00
|
|
|
final repo = ref.read(emailRepositoryProvider);
|
|
|
|
|
_dataFuture = Future.wait([
|
|
|
|
|
repo.getEmail(widget.emailId),
|
|
|
|
|
repo.getEmailBody(widget.emailId),
|
|
|
|
|
]).then((results) => (results[0] as Email?, results[1] as EmailBody));
|
|
|
|
|
repo.setFlag(widget.emailId, seen: true);
|
2026-04-16 07:35:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
final repo = ref.watch(emailRepositoryProvider);
|
2026-04-16 07:51:52 +02:00
|
|
|
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: const Icon(Icons.delete),
|
|
|
|
|
onPressed: () async {
|
|
|
|
|
await repo.deleteEmail(widget.emailId);
|
|
|
|
|
if (context.mounted) context.pop();
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
],
|
2026-04-16 07:35:56 +02:00
|
|
|
),
|
2026-04-16 07:51:52 +02:00
|
|
|
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(
|
2026-04-16 08:11:29 +02:00
|
|
|
body.textBody ?? htmlToPlain(body.htmlBody ?? ''),
|
2026-04-16 07:51:52 +02:00
|
|
|
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,
|
|
|
|
|
),
|
2026-04-16 07:35:56 +02:00
|
|
|
),
|
2026-04-16 07:51:52 +02:00
|
|
|
for (final att in body.attachments)
|
|
|
|
|
ListTile(
|
|
|
|
|
dense: true,
|
|
|
|
|
leading: const Icon(Icons.attach_file),
|
|
|
|
|
title: Text(att.filename),
|
2026-04-16 08:11:29 +02:00
|
|
|
subtitle: Text(fmtSize(att.size)),
|
2026-04-16 07:51:52 +02:00
|
|
|
),
|
2026-04-16 07:35:56 +02:00
|
|
|
],
|
2026-04-16 07:51:52 +02:00
|
|
|
],
|
2026-04-16 07:35:56 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-16 07:51:52 +02:00
|
|
|
Widget _buildHeader(BuildContext ctx, Email email) {
|
2026-04-16 07:35:56 +02:00
|
|
|
return Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
Text(
|
2026-04-16 07:51:52 +02:00
|
|
|
email.subject ?? '(no subject)',
|
2026-04-16 07:35:56 +02:00
|
|
|
style: Theme.of(ctx).textTheme.titleMedium,
|
|
|
|
|
),
|
2026-04-16 07:51:52 +02:00
|
|
|
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,
|
|
|
|
|
),
|
2026-04-16 07:35:56 +02:00
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-16 07:51:52 +02:00
|
|
|
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(', ')
|
|
|
|
|
: '';
|
2026-04-16 07:35:56 +02:00
|
|
|
context.push('/compose', extra: {
|
|
|
|
|
'replyToEmailId': widget.emailId,
|
2026-04-16 07:51:52 +02:00
|
|
|
'prefillTo': to,
|
|
|
|
|
'prefillSubject': subject,
|
|
|
|
|
if (cc.isNotEmpty) 'prefillCc': cc,
|
2026-04-16 07:35:56 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|