Review fixes: - Keep stored message history beyond the 200-message window; stop messages leaking between radios; reset link/session state on disconnect - Accept firmware's 176-byte RX frames over USB/TCP - Hold APP_START during contact sync; contact-sync watchdog; replay DMs from not-yet-known senders; tombstone late command replies; finish channel sync on ERR_NOT_FOUND; fail region-scoped sends cleanly - DM retry watchdog when RESP_CODE_SENT never arrives; late repeater CLI replies; ACK hash collisions; UTF-8-safe name truncation - Image pipeline: delete-during-decode, lifecycle cancels, decoder memory release, entry cap, dead worker recovery, atomic model writes - Storage: deterministic reaction hash across web/native, Smaz double-decode, per-entry parsing, per-radio path history and regions, review-mode data isolation - UI: safe channel slot selection, byte-length name limits, double-send guard, mounted checks, localized hardcoded strings - Skip persisted-store reload on repeat SELF_INFO Features: - Radio default flood scope (firmware ver 11+) and explicit unscoped sends (ver 12+) - Scan for nearby nodes (#570) - Optional distinct first-hop repeater counting for channel resend (#589) - Optional region next to hop count (#580) Issues: - Message text follows system font size (#569) - Repeater/room password field accepts 20 chars (#576) - TCP auto-reconnect and keepalive (#571) - Order feeds by local receive time; flag future timestamps (#584) - Coding rate help text (#568), French "Favoris" (#575) - iOS/macOS app badge now clears Translations for all new strings in every locale.
105 lines
3.2 KiB
Dart
105 lines
3.2 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:characters/characters.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
class Utf8LengthLimitingTextInputFormatter extends TextInputFormatter {
|
|
final int maxBytes;
|
|
final String Function(String)? encoder;
|
|
|
|
const Utf8LengthLimitingTextInputFormatter(this.maxBytes, {this.encoder});
|
|
|
|
int _effectiveByteLength(String text) {
|
|
final effective = encoder != null ? encoder!(text) : text;
|
|
return utf8.encode(effective).length;
|
|
}
|
|
|
|
@override
|
|
TextEditingValue formatEditUpdate(
|
|
TextEditingValue oldValue,
|
|
TextEditingValue newValue,
|
|
) {
|
|
if (maxBytes <= 0) return oldValue;
|
|
final newBytes = _effectiveByteLength(newValue.text);
|
|
if (newBytes <= maxBytes ||
|
|
newBytes <= _effectiveByteLength(oldValue.text)) {
|
|
return newValue;
|
|
}
|
|
|
|
final oldText = oldValue.text;
|
|
final newText = newValue.text;
|
|
var prefix = 0;
|
|
while (prefix < oldText.length &&
|
|
prefix < newText.length &&
|
|
oldText.codeUnitAt(prefix) == newText.codeUnitAt(prefix)) {
|
|
prefix++;
|
|
}
|
|
if (prefix > 0 &&
|
|
prefix < newText.length &&
|
|
_isLowSurrogate(newText.codeUnitAt(prefix))) {
|
|
prefix--;
|
|
}
|
|
var suffix = 0;
|
|
while (suffix < oldText.length - prefix &&
|
|
suffix < newText.length - prefix &&
|
|
oldText.codeUnitAt(oldText.length - 1 - suffix) ==
|
|
newText.codeUnitAt(newText.length - 1 - suffix)) {
|
|
suffix++;
|
|
}
|
|
final suffixStart = newText.length - suffix;
|
|
if (suffix > 0 && _isLowSurrogate(newText.codeUnitAt(suffixStart))) {
|
|
suffix--;
|
|
}
|
|
|
|
final head = newText.substring(0, prefix);
|
|
final tail = newText.substring(newText.length - suffix);
|
|
final inserted = newText.substring(prefix, newText.length - suffix);
|
|
|
|
final kept = StringBuffer();
|
|
for (final char in inserted.characters) {
|
|
if (_effectiveByteLength('$head$kept$char$tail') > maxBytes) break;
|
|
kept.write(char);
|
|
}
|
|
|
|
final cursor = head.length + kept.length;
|
|
return TextEditingValue(
|
|
text: '$head$kept$tail',
|
|
selection: TextSelection.collapsed(offset: cursor),
|
|
composing: TextRange.empty,
|
|
);
|
|
}
|
|
|
|
bool _isLowSurrogate(int codeUnit) =>
|
|
codeUnit >= 0xDC00 && codeUnit <= 0xDFFF;
|
|
}
|
|
|
|
/// Longest name (in UTF-8 bytes) the radio stores for channels and the node
|
|
/// advert name: a 32-byte field that keeps one byte for the NUL terminator.
|
|
const int maxRadioNameBytes = 31;
|
|
|
|
int utf8ByteLength(String text) => utf8.encode(text).length;
|
|
|
|
/// Cuts [text] to at most [maxBytes] UTF-8 bytes without splitting a
|
|
/// character (grapheme cluster).
|
|
String truncateUtf8(String text, int maxBytes) {
|
|
if (maxBytes <= 0) return '';
|
|
if (utf8ByteLength(text) <= maxBytes) return text;
|
|
final kept = StringBuffer();
|
|
var used = 0;
|
|
for (final char in text.characters) {
|
|
final size = utf8ByteLength(char);
|
|
if (used + size > maxBytes) break;
|
|
kept.write(char);
|
|
used += size;
|
|
}
|
|
return kept.toString();
|
|
}
|
|
|
|
/// Builds `"<label> <suffix>"`, shortening [label] so the whole name fits
|
|
/// in [maxRadioNameBytes].
|
|
String fitRadioName(String label, String suffix) {
|
|
final suffixPart = ' $suffix';
|
|
final room = maxRadioNameBytes - utf8ByteLength(suffixPart);
|
|
return '${truncateUtf8(label, room)}$suffixPart';
|
|
}
|