Files
meshcore-open/lib/widgets/chat_send_pipeline.dart
T
zjs81 32977bdd50 Improve bookmarks navigation, scoping, search, and bulk actions
Add Bookmarks to the Contacts overflow menu, open the dedicated BookmarksScreen from direct and channel chat menus with a split scope pill (All vs current chat), add in-screen bookmark search with scroll-and-highlight jump, support bulk bookmarking from channel selection mode and bulk removal in BookmarksScreen, and prompt with a confirmation dialog before removing single or selected bookmarks.
2026-11-23 18:20:19 -07:00

261 lines
8.0 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import '../connector/meshcore_connector.dart';
import '../helpers/snack_bar_builder.dart';
import '../l10n/l10n.dart';
import '../models/translation_support.dart';
import '../services/app_settings_service.dart';
import '../services/translation_service.dart';
import '../utils/app_logger.dart';
import '../utils/platform_info.dart';
import 'marker_label_dialog.dart';
/// Send guard and one-second cooldown shared by the chat composers.
///
/// The cooldown is only stamped by [markDispatched], i.e. once a send has
/// actually gone to the connector, so a rejected attempt (empty, too long,
/// translation aborted) never locks the user out.
class ChatSendGuard {
static const cooldown = Duration(seconds: 1);
DateTime? _lastDispatchAt;
bool _busy = false;
/// True while a send is awaiting outgoing translation, so a second tap
/// can't send the same text again.
bool get isBusy => _busy;
/// Runs [action] unless another send is already in flight.
Future<void> runExclusive(Future<void> Function() action) async {
if (_busy) return;
_busy = true;
try {
await action();
} finally {
_busy = false;
}
}
/// Returns false, and tells the user, if the last dispatch was under a
/// second ago.
bool checkCooldown(BuildContext context) {
final last = _lastDispatchAt;
if (last != null && DateTime.now().difference(last) < cooldown) {
showDismissibleSnackBar(
context,
content: Text(context.l10n.chat_sendCooldown),
);
return false;
}
return true;
}
void markDispatched() => _lastDispatchAt = DateTime.now();
}
/// Outcome of the optional outgoing translation step.
@immutable
class OutgoingTranslation {
final String text;
final String? originalText;
final String? languageCode;
final String? modelId;
const OutgoingTranslation(
this.text, {
this.originalText,
this.languageCode,
this.modelId,
});
}
/// Translates [text] when composer translation is on and applies, returning
/// the text to send plus translation metadata. Returns null when [context]
/// was unmounted while the translation ran.
Future<OutgoingTranslation?> translateOutgoingText(
BuildContext context,
String text,
) async {
final settings = context.read<AppSettingsService>().settings;
if (!settings.translationEnabled) return OutgoingTranslation(text);
final translationService = context.read<TranslationService>();
final targetLanguageCode = translationService.resolvedTargetLanguageCode(
Localizations.localeOf(context).languageCode,
);
if (!translationService.shouldTranslateOutgoing(
text: text,
targetLanguageCode: targetLanguageCode,
)) {
return OutgoingTranslation(text);
}
final result = await translationService.translateOutgoingText(
text: text,
targetLanguageCode: targetLanguageCode,
);
if (!context.mounted) return null;
if (result != null &&
result.status == MessageTranslationStatus.completed &&
result.translatedText.isNotEmpty) {
return OutgoingTranslation(
result.translatedText,
originalText: text,
languageCode: result.targetLanguageCode,
modelId: result.modelId,
);
}
return OutgoingTranslation(text);
}
/// Shares the radio's own position as a map marker through [send]. Uses the
/// same [guard] as typed messages so it can't race or flood alongside them.
Future<void> shareMyLocationMarker({
required BuildContext context,
required MeshCoreConnector connector,
required ChatSendGuard guard,
required Future<void> Function(String markerText) send,
String logTag = 'Chat',
}) async {
final lat = connector.selfLatitude;
final lon = connector.selfLongitude;
if (!hasShareableLocation(lat, lon)) {
showDismissibleSnackBar(
context,
content: Text(context.l10n.chat_noRadioLocation),
);
return;
}
final selfName = connector.selfName;
final label = await showMarkerLabelDialog(
context,
lat: lat!,
lon: lon!,
defaultLabel: selfName != null && selfName.isNotEmpty
? selfName
: context.l10n.map_sharedPin,
flags: 'poi',
confirmText: context.l10n.common_share,
);
if (label == null || label.isEmpty || !context.mounted) return;
await guard.runExclusive(() async {
if (!guard.checkCooldown(context)) return;
final pending = send(formatMarkerText(lat, lon, label, 'poi'));
guard.markDispatched();
try {
await pending;
} catch (e) {
// A failed send is already marked failed on the message itself.
appLogger.warn('Sharing location failed: $e', tag: logTag);
}
});
}
/// Copies a message to the clipboard and confirms it.
void copyChatMessageText(BuildContext context, String text) {
Clipboard.setData(ClipboardData(text: text));
// Android shows its own clipboard chip; the snackbar is redundant there
// and lingers over the input field after the chip dismisses.
if (PlatformInfo.isAndroid) return;
showDismissibleSnackBar(
context,
content: Text(context.l10n.chat_messageCopied),
);
}
/// Asks before deleting [count] selected messages from this device.
Future<bool> confirmBulkDelete(BuildContext context, int count) async {
final result = await showDialog<bool>(
context: context,
builder: (dialogContext) {
final l10n = dialogContext.l10n;
return AlertDialog(
title: Text(l10n.chat_deleteSelectedTitle),
content: Text(l10n.chat_deleteSelectedConfirm(count)),
actions: [
TextButton(
onPressed: () => Navigator.pop(dialogContext, false),
child: Text(l10n.common_cancel),
),
FilledButton(
style: FilledButton.styleFrom(
backgroundColor: Theme.of(dialogContext).colorScheme.error,
foregroundColor: Theme.of(dialogContext).colorScheme.onError,
),
onPressed: () => Navigator.pop(dialogContext, true),
child: Text(l10n.common_delete),
),
],
);
},
);
return result ?? false;
}
/// Asks before clearing a transcript. [message] says what will be erased.
Future<bool> confirmClearChat(
BuildContext context, {
required String message,
}) async {
final confirmed = await showDialog<bool>(
context: context,
builder: (dialogContext) => AlertDialog(
title: Text(dialogContext.l10n.contact_clearChat),
content: Text(message),
actions: [
TextButton(
onPressed: () => Navigator.pop(dialogContext, false),
child: Text(dialogContext.l10n.common_cancel),
),
TextButton(
onPressed: () => Navigator.pop(dialogContext, true),
style: TextButton.styleFrom(
foregroundColor: Theme.of(dialogContext).colorScheme.error,
),
child: Text(dialogContext.l10n.common_delete),
),
],
),
);
return confirmed == true;
}
/// Asks before removing [count] bookmarked messages.
Future<bool> confirmRemoveBookmarks(
BuildContext context, {
int count = 1,
}) async {
final confirmed = await showDialog<bool>(
context: context,
builder: (dialogContext) {
final l10n = dialogContext.l10n;
return AlertDialog(
title: Text(
count == 1
? l10n.bookmarks_removeConfirmTitle
: l10n.bookmarks_removeSelectedConfirmTitle,
),
content: Text(
count == 1
? l10n.bookmarks_removeConfirmMessage
: l10n.bookmarks_removeSelectedConfirmMessage(count),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(dialogContext, false),
child: Text(l10n.common_cancel),
),
TextButton(
onPressed: () => Navigator.pop(dialogContext, true),
style: TextButton.styleFrom(
foregroundColor: Theme.of(dialogContext).colorScheme.error,
),
child: Text(l10n.common_remove),
),
],
);
},
);
return confirmed == true;
}