Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions apps/client/lib/core/i18n/language_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ class AppStrings {
String get deleteAccountDescription => _ko
? '이 앱에 저장된 계정과 동기화된 메일 목록이 삭제됩니다.'
: 'This removes the account and synced mail list from this app.';
String get signature => _ko ? '서명' : 'Signature';
String get signatureHint => _ko
? '새 메일 끝에 자동으로 붙습니다. 비워 두면 서명을 붙이지 않습니다.'
: 'Added to the end of new messages. Leave empty for no signature.';
String get signatureSaved => _ko ? '서명을 저장했습니다.' : 'Signature saved.';
String get save => _ko ? '저장' : 'Save';
String get cancel => _ko ? '취소' : 'Cancel';
String get statusIdle => _ko ? '대기' : 'Idle';
String get statusValidating => _ko ? '검증 중' : 'Validating';
Expand Down
82 changes: 82 additions & 0 deletions apps/client/lib/features/accounts/accounts_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import '../../core/i18n/language_controller.dart';
import '../../services/browser_launcher.dart';
import '../../services/mail_api_client.dart';
import '../../widgets/app_shell.dart';
import '../message/compose_page.dart' show composeAccountsProvider;
import 'account_providers.dart';

export 'account_providers.dart' show accountsProvider, accountMessagesProvider;
Expand Down Expand Up @@ -472,6 +473,8 @@ class _AccountDetail extends ConsumerWidget {
value: _receiveModeLabel(account, strings),
),
],
const SizedBox(height: 24),
_SignatureEditor(key: ValueKey(account.id), account: account),
if (account.syncError != null) ...[
const SizedBox(height: 12),
Text(
Expand All @@ -484,6 +487,85 @@ class _AccountDetail extends ConsumerWidget {
}
}

/// Saves a signature and refreshes caches; lives in the provider scope so it
/// completes even if the editor widget is disposed mid-request.
final _saveSignatureProvider = Provider<
Future<void> Function(String accountId, String signature)>((ref) {
return (accountId, signature) async {
await ref.read(mailApiClientProvider).updateAccountSignature(
accountId: accountId,
signature: signature,
);
ref.invalidate(accountsProvider);
ref.invalidate(composeAccountsProvider);
};
}, name: 'saveSignature');

class _SignatureEditor extends ConsumerStatefulWidget {
const _SignatureEditor({super.key, required this.account});

final MailAccountSummary account;

@override
ConsumerState<_SignatureEditor> createState() => _SignatureEditorState();
}

class _SignatureEditorState extends ConsumerState<_SignatureEditor> {
late final _controller = TextEditingController(text: widget.account.signature);
bool _saving = false;

@override
void dispose() {
_controller.dispose();
super.dispose();
}

Future<void> _save() async {
final strings = ref.read(appStringsProvider);
final messenger = ScaffoldMessenger.maybeOf(context);
final save = ref.read(_saveSignatureProvider);
setState(() => _saving = true);
try {
await save(widget.account.id, _controller.text);
if (!mounted) return;
messenger?.showSnackBar(SnackBar(content: Text(strings.signatureSaved)));
} catch (error) {
if (!mounted) return;
messenger?.showSnackBar(
SnackBar(content: Text(friendlyError(error, strings))),
);
} finally {
if (mounted) setState(() => _saving = false);
}
}

@override
Widget build(BuildContext context) {
final strings = ref.watch(appStringsProvider);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextField(
controller: _controller,
minLines: 3,
maxLines: 8,
maxLength: 2000,
decoration: InputDecoration(
labelText: strings.signature,
helperText: strings.signatureHint,
border: const OutlineInputBorder(),
),
),
const SizedBox(height: 8),
FilledButton(
onPressed: _saving ? null : _save,
child: Text(strings.save),
),
],
);
}
}

String _receiveModeLabel(MailAccountSummary account, AppStrings strings) {
final idle = account.idleSupported;
if (idle == null) return strings.receiveUnknown;
Expand Down
61 changes: 60 additions & 1 deletion apps/client/lib/features/message/compose_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ final composeAccountsProvider = FutureProvider<List<MailAccountSummary>>((ref) {
return ref.watch(mailApiClientProvider).fetchAccounts();
});

const _signatureSeparator = '\n\n-- \n';

/// Appends [signature] to [body], or swaps it for [previous] when the user
/// has not edited the old signature block.
String swapSignature(String body, String previous, String signature) {
final oldBlock = previous.isEmpty ? '' : _signatureSeparator + previous;
final newBlock = signature.isEmpty ? '' : _signatureSeparator + signature;
if (oldBlock.isNotEmpty) {
// Edited or deleted by the user: leave the body alone.
if (!body.endsWith(oldBlock)) return body;
return body.substring(0, body.length - oldBlock.length) + newBlock;
}
return body + newBlock;
}

class ComposePage extends ConsumerStatefulWidget {
const ComposePage({
super.key,
Expand All @@ -40,6 +55,8 @@ class _ComposePageState extends ConsumerState<ComposePage> {
final _subjectController = TextEditingController();
final _bodyController = TextEditingController();
String? _accountId;
String _signature = '';
bool _signatureInitialized = false;
bool _sending = false;

@override
Expand Down Expand Up @@ -109,6 +126,15 @@ class _ComposePageState extends ConsumerState<ComposePage> {
!items.any((account) => account.id == _accountId)) {
_accountId = items.firstOrNull?.id;
}
if (!_signatureInitialized && items.isNotEmpty) {
_signatureInitialized = true;
if (widget.initialDraft != null) {
// Recovered draft already carries its signature: adopt, don't re-insert.
_signature = _signatureOf(items, _accountId);
} else {
_useSignatureOf(items, _accountId);
}
}

return ListView(
padding: const EdgeInsets.all(24),
Expand All @@ -123,7 +149,10 @@ class _ComposePageState extends ConsumerState<ComposePage> {
child: Text(account.emailAddress),
),
],
onChanged: (value) => setState(() => _accountId = value),
onChanged: (value) => setState(() {
_accountId = value;
_useSignatureOf(items, value);
}),
),
const SizedBox(height: 12),
TextField(
Expand Down Expand Up @@ -170,6 +199,36 @@ class _ComposePageState extends ConsumerState<ComposePage> {
);
}

String _signatureOf(List<MailAccountSummary> items, String? accountId) =>
items.where((a) => a.id == accountId).firstOrNull?.signature ?? '';

void _useSignatureOf(List<MailAccountSummary> items, String? accountId) {
final next = _signatureOf(items, accountId);
// Reply quote stays below the signature (signature sits above the quote).
final quote = widget.initialDraft == null ? widget.reply?.body ?? '' : '';
final full = _bodyController.text;
if (!full.endsWith(quote)) return;
final body = full.substring(0, full.length - quote.length);
// User edited the managed block: keep owning the old signature so later
// switches neither overwrite the edit nor append a duplicate.
if (_signature.isNotEmpty &&
!body.endsWith(_signatureSeparator + _signature)) {
return;
}
final text = swapSignature(body, _signature, next);
final bodyEnd = text.length -
(next.isEmpty ? 0 : _signatureSeparator.length + next.length);
final selection = _bodyController.selection;
// An invalid selection jumps to the end on focus, i.e. after the signature.
_bodyController.value = TextEditingValue(
text: text + quote,
selection: selection.isValid && selection.end <= bodyEnd
? selection
: TextSelection.collapsed(offset: bodyEnd),
);
_signature = next;
}

Future<void> _send() async {
final accountId = _accountId;
if (accountId == null || _sending) return;
Expand Down
12 changes: 12 additions & 0 deletions apps/client/lib/services/mail_api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,15 @@ class MailApiClient {
});
}

Future<void> updateAccountSignature({
required String accountId,
required String signature,
}) async {
await _dio.put('/accounts/$accountId/signature', data: {
'signature': signature,
});
}

Future<void> validateStoredAccount(String accountId) async {
await _dio.post('/accounts/$accountId/validate');
}
Expand Down Expand Up @@ -588,6 +597,7 @@ class MailAccountSummary {
this.syncErrorCode,
required this.initialSync,
required this.lastSyncedAtLabel,
this.signature = '',
this.threadBackfilling = false,
this.idleSupported,
this.capabilitiesCheckedAtLabel = '',
Expand All @@ -597,6 +607,7 @@ class MailAccountSummary {
final String id;
final String providerId;
final String emailAddress;
final String signature;
final String incomingProtocol;
final String authMode;
final String syncStatus;
Expand Down Expand Up @@ -630,6 +641,7 @@ class MailAccountSummary {
json['initialSync'] as Map<String, dynamic>)
: null,
lastSyncedAtLabel: _formatReceivedAt(json['lastSyncedAt'] as String?),
signature: json['signature'] as String? ?? '',
threadBackfilling: json['threadBackfill'] == 'running',
idleSupported: capabilities?['idle'] as bool?,
capabilitiesCheckedAtLabel:
Expand Down
Loading
Loading