- Updated LoRa airtime calculations in `lora_airtime_test.dart` to reflect new reference values. - Introduced new tests for LDRO behavior and preamble symbol calculations. - Added `contact_path_width_test.dart` to validate path hash width handling in contacts. - Enhanced `model_changes_test.dart` to ensure legacy data migration works correctly. - Created `remote_node_stats_test.dart` to decode and validate remote node statistics. - Implemented `map_marker_parse_test.dart` for parsing marker text with coordinate validation. - Developed `map_tile_cache_key_test.dart` to ensure API keys are excluded from cache keys. - Added `message_retry_failure_test.dart` to test message retry service behavior on failures. - Extended `path_history_service_test.dart` to preserve learned state across path attempts. - Enhanced `received_image_store_test.dart` to handle concurrent writes and review fixes. - Implemented `repeater_command_service_test.dart` to validate command response handling. - Created `store_tolerant_parsing_test.dart` to ensure stores skip malformed records gracefully. - Cleared untranslated JSON file for localization updates.
75 lines
2.2 KiB
Dart
75 lines
2.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;
|
|
}
|