Compare commits

...
Author SHA1 Message Date
Thomas SharedInboxandClaude Sonnet 4.6 0e3816a22f refactor(A1): extract EmailDetailNotifier, drop initState DB coupling
EmailDetailScreen no longer calls the repository directly in initState.
A new EmailDetailNotifier (AsyncNotifierProvider.autoDispose.family) owns
the load + setFlag(seen) side effect; the screen watches the provider and
renders via detail.when(loading/error/data).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-14 09:17:14 +02:00
2 changed files with 133 additions and 125 deletions
+23 -1
View File
@@ -3,6 +3,7 @@ import 'dart:async';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'package:sharedinbox/core/models/account.dart' as model; import 'package:sharedinbox/core/models/account.dart' as model;
import 'package:sharedinbox/core/models/email.dart';
import 'package:sharedinbox/core/models/undo_action.dart'; import 'package:sharedinbox/core/models/undo_action.dart';
import 'package:sharedinbox/core/repositories/account_repository.dart'; import 'package:sharedinbox/core/repositories/account_repository.dart';
import 'package:sharedinbox/core/repositories/draft_repository.dart'; import 'package:sharedinbox/core/repositories/draft_repository.dart';
@@ -17,7 +18,7 @@ import 'package:sharedinbox/core/services/undo_service.dart';
import 'package:sharedinbox/core/storage/secure_storage.dart'; import 'package:sharedinbox/core/storage/secure_storage.dart';
import 'package:sharedinbox/core/sync/account_sync_manager.dart'; import 'package:sharedinbox/core/sync/account_sync_manager.dart';
import 'package:sharedinbox/core/sync/reliability_runner.dart'; import 'package:sharedinbox/core/sync/reliability_runner.dart';
import 'package:sharedinbox/data/db/database.dart'; import 'package:sharedinbox/data/db/database.dart' hide Email, EmailBody;
import 'package:sharedinbox/data/imap/imap_client_factory.dart'; import 'package:sharedinbox/data/imap/imap_client_factory.dart';
import 'package:sharedinbox/data/jmap/sieve_repository.dart'; import 'package:sharedinbox/data/jmap/sieve_repository.dart';
import 'package:sharedinbox/data/repositories/account_repository_impl.dart'; import 'package:sharedinbox/data/repositories/account_repository_impl.dart';
@@ -168,6 +169,27 @@ final undoServiceProvider =
return service; return service;
}); });
/// Loads email header + body and marks the email as seen.
/// Owned by [EmailDetailScreen]; decouples data loading from the widget tree.
final emailDetailProvider = AsyncNotifierProvider.autoDispose
.family<EmailDetailNotifier, (Email?, EmailBody), String>(
EmailDetailNotifier.new,
);
class EmailDetailNotifier
extends AutoDisposeFamilyAsyncNotifier<(Email?, EmailBody), String> {
@override
Future<(Email?, EmailBody)> build(String emailId) async {
final repo = ref.read(emailRepositoryProvider);
final results = await Future.wait([
repo.getEmail(emailId),
repo.getEmailBody(emailId),
]);
unawaited(repo.setFlag(emailId, seen: true));
return (results[0] as Email?, results[1] as EmailBody);
}
}
final accountByIdProvider = final accountByIdProvider =
StreamProvider.autoDispose.family<model.Account?, String>((ref, accountId) { StreamProvider.autoDispose.family<model.Account?, String>((ref, accountId) {
return ref.watch(accountRepositoryProvider).observeAccounts().map( return ref.watch(accountRepositoryProvider).observeAccounts().map(
+110 -124
View File
@@ -26,144 +26,130 @@ class EmailDetailScreen extends ConsumerStatefulWidget {
} }
class _EmailDetailScreenState extends ConsumerState<EmailDetailScreen> { class _EmailDetailScreenState extends ConsumerState<EmailDetailScreen> {
late final Future<(Email?, EmailBody)> _dataFuture;
bool _isFlagged = false; bool _isFlagged = false;
bool _loadRemoteImages = false; bool _loadRemoteImages = false;
final Set<String> _downloading = {}; 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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final repo = ref.watch(emailRepositoryProvider); final repo = ref.watch(emailRepositoryProvider);
return FutureBuilder<(Email?, EmailBody)>( final detail = ref.watch(emailDetailProvider(widget.emailId));
future: _dataFuture,
builder: (ctx, snap) {
final header = snap.data?.$1;
final body = snap.data?.$2;
return Scaffold( ref.listen<AsyncValue<(Email?, EmailBody)>>(
appBar: AppBar( emailDetailProvider(widget.emailId),
title: Text( (_, next) {
header?.subject ?? '(loading…)', final email = next.valueOrNull?.$1;
overflow: TextOverflow.ellipsis, if (email != null && mounted) {
setState(() => _isFlagged = email.isFlagged);
}
},
);
final header = detail.valueOrNull?.$1;
final body = detail.valueOrNull?.$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, body, replyAll: false),
),
IconButton(
icon: const Icon(Icons.reply_all),
tooltip: 'Reply all',
onPressed: header == null
? null
: () => _reply(context, header, body, replyAll: true),
),
IconButton(
icon: const Icon(Icons.forward),
tooltip: 'Forward',
onPressed:
header == null ? null : () => _forward(context, header, body),
),
IconButton(
icon: const Icon(Icons.mark_email_unread_outlined),
tooltip: 'Mark as unread',
onPressed: () async {
await repo.setFlag(widget.emailId, seen: false);
if (context.mounted) context.pop();
},
),
IconButton(
icon: Icon(
_isFlagged ? Icons.star : Icons.star_border,
color: _isFlagged ? Colors.amber : null,
), ),
actions: [ tooltip: _isFlagged ? 'Unflag' : 'Flag',
IconButton( onPressed: () async {
icon: const Icon(Icons.reply), final next = !_isFlagged;
tooltip: 'Reply', await repo.setFlag(widget.emailId, flagged: next);
onPressed: header == null if (mounted) setState(() => _isFlagged = next);
? null },
: () => _reply(context, header, body, replyAll: false), ),
), IconButton(
IconButton( icon: const Icon(Icons.drive_file_move_outline),
icon: const Icon(Icons.reply_all), tooltip: 'Move to folder',
tooltip: 'Reply all', onPressed: header == null ? null : () => _moveTo(context, header),
onPressed: header == null ),
? null IconButton(
: () => _reply(context, header, body, replyAll: true), icon: const Icon(Icons.access_time),
), tooltip: 'Snooze',
IconButton( onPressed: header == null ? null : () => _snooze(context, header),
icon: const Icon(Icons.forward), ),
tooltip: 'Forward', IconButton(
onPressed: header == null icon: const Icon(Icons.delete),
? null tooltip: 'Delete',
: () => _forward(context, header, body), onPressed: () async {
), final destPath = await repo.deleteEmail(widget.emailId);
IconButton(
icon: const Icon(Icons.mark_email_unread_outlined),
tooltip: 'Mark as unread',
onPressed: () async {
await repo.setFlag(widget.emailId, seen: false);
if (context.mounted) context.pop();
},
),
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.access_time),
tooltip: 'Snooze',
onPressed:
header == null ? null : () => _snooze(context, header),
),
IconButton(
icon: const Icon(Icons.delete),
tooltip: 'Delete',
onPressed: () async {
final destPath = await repo.deleteEmail(widget.emailId);
if (header != null) { if (header != null) {
unawaited( unawaited(
ref.read(undoServiceProvider.notifier).pushAction( ref.read(undoServiceProvider.notifier).pushAction(
UndoAction( UndoAction(
id: DateTime.now().toIso8601String(), id: DateTime.now().toIso8601String(),
accountId: header.accountId, accountId: header.accountId,
type: UndoType.delete, type: UndoType.delete,
emailIds: [widget.emailId], emailIds: [widget.emailId],
sourceMailboxPath: header.mailboxPath, sourceMailboxPath: header.mailboxPath,
destinationMailboxPath: destPath, destinationMailboxPath: destPath,
originalEmails: [header], originalEmails: [header],
), ),
), ),
); );
} }
if (context.mounted) context.pop(); if (context.mounted) context.pop();
}, },
), ),
PopupMenuButton<String>( PopupMenuButton<String>(
itemBuilder: (ctx) => [ itemBuilder: (ctx) => [
const PopupMenuItem( const PopupMenuItem(
value: 'headers', value: 'headers',
child: Text('Show Mail Headers'), child: Text('Show Mail Headers'),
),
],
onSelected: (value) {
if (value == 'headers' && body != null) {
_showHeaders(context, body);
}
},
), ),
], ],
onSelected: (value) {
if (value == 'headers' && body != null) {
_showHeaders(context, body);
}
},
), ),
body: snap.connectionState == ConnectionState.waiting ],
? const Center(child: CircularProgressIndicator()) ),
: snap.hasError body: detail.when(
? Center(child: Text('Error: ${snap.error}')) loading: () => const Center(child: CircularProgressIndicator()),
: _buildBody(ctx, header, body!), error: (e, _) => Center(child: Text('Error: $e')),
); data: (d) => _buildBody(context, d.$1, d.$2),
}, ),
); );
} }