limit length of label in shareLocation.

This commit is contained in:
ericz 2026-03-10 22:30:55 +01:00
parent f057d6b370
commit db993761a5
2 changed files with 68 additions and 2 deletions

View file

@ -866,9 +866,17 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
return;
}
final maxBytes = maxChannelMessageBytes(connector.selfName);
final prefix = 'm:${lat.toStringAsFixed(6)},${lon.toStringAsFixed(6)}|';
const suffix = '|loc';
final maxLabelBytes =
maxBytes - utf8.encode(prefix).length - utf8.encode(suffix).length;
final defaultLabel =
'${connector.deviceDisplayName} ${DateTime.now().toUtc().toIso8601String()}';
final controller = TextEditingController(text: defaultLabel);
final controller = TextEditingController(
text: _truncateToUtf8Bytes(defaultLabel, maxLabelBytes),
);
final label = await showDialog<String>(
context: context,
@ -878,6 +886,9 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
controller: controller,
decoration: InputDecoration(labelText: context.l10n.chat_location),
autofocus: true,
inputFormatters: [
Utf8LengthLimitingTextInputFormatter(maxLabelBytes),
],
),
actions: [
TextButton(
@ -894,11 +905,33 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
if (label == null || label.isEmpty) return;
if (!mounted) return;
final markerText =
'm:${lat.toStringAsFixed(6)},${lon.toStringAsFixed(6)}|$label|loc';
if (utf8.encode(markerText).length > maxBytes) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(context.l10n.chat_messageTooLong(maxBytes))),
);
return;
}
connector.sendChannelMessage(widget.channel, markerText);
}
String _truncateToUtf8Bytes(String text, int maxBytes) {
if (maxBytes <= 0) return '';
final codeUnits = text.codeUnits;
var end = codeUnits.length;
while (end > 0 &&
utf8.encode(String.fromCharCodes(codeUnits.take(end))).length >
maxBytes) {
end--;
}
return String.fromCharCodes(codeUnits.take(end));
}
Widget _buildAvatar(String senderName) {
final initial = _getFirstCharacterOrEmoji(senderName);
final color = _getColorForName(senderName);

View file

@ -515,9 +515,17 @@ class _ChatScreenState extends State<ChatScreen> {
return;
}
final maxBytes = maxContactMessageBytes();
final prefix = 'm:${lat.toStringAsFixed(6)},${lon.toStringAsFixed(6)}|';
const suffix = '|loc';
final maxLabelBytes =
maxBytes - utf8.encode(prefix).length - utf8.encode(suffix).length;
final defaultLabel =
'${connector.deviceDisplayName} ${DateTime.now().toUtc().toIso8601String()}';
final controller = TextEditingController(text: defaultLabel);
final controller = TextEditingController(
text: _truncateToUtf8Bytes(defaultLabel, maxLabelBytes),
);
final label = await showDialog<String>(
context: context,
@ -527,6 +535,9 @@ class _ChatScreenState extends State<ChatScreen> {
controller: controller,
decoration: InputDecoration(labelText: context.l10n.chat_location),
autofocus: true,
inputFormatters: [
Utf8LengthLimitingTextInputFormatter(maxLabelBytes),
],
),
actions: [
TextButton(
@ -543,11 +554,33 @@ class _ChatScreenState extends State<ChatScreen> {
if (label == null || label.isEmpty) return;
if (!mounted) return;
final markerText =
'm:${lat.toStringAsFixed(6)},${lon.toStringAsFixed(6)}|$label|loc';
if (utf8.encode(markerText).length > maxBytes) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(context.l10n.chat_messageTooLong(maxBytes))),
);
return;
}
connector.sendMessage(widget.contact, markerText);
}
String _truncateToUtf8Bytes(String text, int maxBytes) {
if (maxBytes <= 0) return '';
final codeUnits = text.codeUnits;
var end = codeUnits.length;
while (end > 0 &&
utf8.encode(String.fromCharCodes(codeUnits.take(end))).length >
maxBytes) {
end--;
}
return String.fromCharCodes(codeUnits.take(end));
}
void _showGifPicker(BuildContext context) {
showModalBottomSheet(
context: context,