Preview image versions before sending, and repair unheard chunks

Send sheet: opens on what recipients will see (the encoded photo decoded
on this device) instead of the original. "Try another version" encodes
the whole photo again with a near-invisible zoom/shift; the generative
decoder reconstructs each version differently, and on Kodak the best of
three raised CLIP agreement from 0.828 to 0.853. The user picks the
version to send; packet count and airtime follow the selection. Version 1
is byte-identical to the previous encode, so the wire format is
unchanged. The sender's bubble shows the chosen version and carries the
AI-reconstructed label.

Repeater echoes: the raw RX log handler now decrypts GRP_DATA as well as
GRP_TXT and counts repeaters re-flooding each chunk of our own images.
The bubble shows the repeat count like channel text, and Packet info
lists each packet. About 15 s after sending, chunks no repeater echoed
are resent once, but only when another chunk of the same image was
echoed, so senders with no repeater in earshot do not double their
airtime. Outgoing entries now record their payload size.
This commit is contained in:
zjs81 committed 2026-11-14 20:58:43 -07:00
1 parent aafd541b0f
commit 53e4e457ca
36 files changed
+2366 -150

No files matched your search

@@ -0,0 +1,57 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/widgets/image_send_codec_binding.dart';
void main() {
group('rerollSourceRect', () {
const sizes = [
(4000, 3000),
(3000, 4000),
(1920, 1080),
(512, 512),
(7, 3),
];
test('variant 0 is the whole photo', () {
for (final (w, h) in sizes) {
final r = rerollSourceRect(w, h, 0);
expect(r.left, 0);
expect(r.top, 0);
expect(r.width, w);
expect(r.height, h);
}
});
test('every variant stays inside the photo and keeps its shape', () {
for (final (w, h) in sizes) {
for (var v = 0; v < kImageSendMaxVersions; v++) {
final r = rerollSourceRect(w, h, v);
expect(r.left, greaterThanOrEqualTo(0), reason: '$w x $h v$v');
expect(r.top, greaterThanOrEqualTo(0), reason: '$w x $h v$v');
expect(r.left + r.width, lessThanOrEqualTo(w + 1e-9));
expect(r.top + r.height, lessThanOrEqualTo(h + 1e-9));
// Same aspect, so the version keeps the photo's aspect code.
expect(r.width / r.height, closeTo(w / h, (w / h) * 0.01));
// "The whole photo": at most a few percent trimmed.
expect(r.width, greaterThanOrEqualTo(w * 0.95));
}
}
});
test('variants differ from each other and are deterministic', () {
final rects = [
for (var v = 0; v < kImageSendMaxVersions; v++)
rerollSourceRect(4000, 3000, v),
];
expect(rects.toSet().length, kImageSendMaxVersions);
expect(rerollSourceRect(4000, 3000, 3), rects[3]);
});
test('rejects a variant outside the table', () {
expect(() => rerollSourceRect(10, 10, -1), throwsRangeError);
expect(
() => rerollSourceRect(10, 10, kImageSendMaxVersions),
throwsRangeError,
);
});
});
}
@@ -336,4 +336,145 @@ void main() {
final l10n = await AppLocalizations.delegate.load(const Locale('en'));
expect(_texts(tester), contains(l10n.imageSend_codecUnavailable));
});
group('versions', () {
final previewCodec = FakeImageSendCodec(
latency: Duration.zero,
previewPng: _onePixelPng,
);
Future<void> tapText(WidgetTester tester, String text) async {
final finder = find.text(text);
await tester.tap(finder);
await tester.pumpAndSettle();
}
Future<ImageSendPreviewResult?> send(
WidgetTester tester,
ImageSendPreviewResult? Function() result,
) async {
await tester.tap(find.byType(FilledButton));
await tester.pumpAndSettle();
return result();
}
testWidgets('opens on what recipients see, as Version 1', (tester) async {
_tallScreen(tester);
await _openSheet(tester, radio: _knownRadio, codec: previewCodec);
final l10n = await AppLocalizations.delegate.load(const Locale('en'));
final texts = _texts(tester);
expect(texts, contains(l10n.imageSend_versionLabel(1)));
expect(texts, contains(l10n.imageSend_tryAnother));
expect(texts, isNot(contains(l10n.imageSend_reconstructionUnavailable)));
final segments = tester.widget<SegmentedButton<bool>>(
find.byType(SegmentedButton<bool>),
);
expect(segments.selected, {
false,
}, reason: 'the reconstruction, not the original, is the default view');
});
testWidgets('trying another version selects it and sends its payload and '
'reconstruction', (tester) async {
_tallScreen(tester);
ImageSendPreviewResult? result;
await tester.pumpWidget(_host(previewCodec, (r) => result = r));
await tester.tap(find.text('open'));
await tester.pumpAndSettle();
final l10n = await AppLocalizations.delegate.load(const Locale('en'));
await tapText(tester, l10n.imageSend_tryAnother);
expect(_texts(tester), contains(l10n.imageSend_versionLabel(2)));
final sent = await send(tester, () => result);
expect(
sent!.payload.length,
FakeImageSendCodec.payloadBytesFor(kImageSendRatePoint, 1),
);
expect(sent.previewPng, isNotNull);
});
testWidgets('going back to Version 1 sends Version 1', (tester) async {
_tallScreen(tester);
ImageSendPreviewResult? result;
await tester.pumpWidget(_host(previewCodec, (r) => result = r));
await tester.tap(find.text('open'));
await tester.pumpAndSettle();
final l10n = await AppLocalizations.delegate.load(const Locale('en'));
await tapText(tester, l10n.imageSend_tryAnother);
await tapText(tester, l10n.imageSend_versionLabel(1));
final sent = await send(tester, () => result);
expect(
sent!.payload.length,
FakeImageSendCodec.payloadBytesFor(kImageSendRatePoint, 0),
);
});
testWidgets('stops offering versions at the maximum', (tester) async {
_tallScreen(tester);
await _openSheet(tester, radio: _knownRadio, codec: previewCodec);
final l10n = await AppLocalizations.delegate.load(const Locale('en'));
for (var i = 1; i < kImageSendMaxVersions; i++) {
await tapText(tester, l10n.imageSend_tryAnother);
}
final texts = _texts(tester);
expect(
texts,
contains(l10n.imageSend_versionLabel(kImageSendMaxVersions)),
);
expect(texts, isNot(contains(l10n.imageSend_tryAnother)));
});
testWidgets('a device that cannot decode shows the original and no '
'versions', (tester) async {
_tallScreen(tester);
await _openSheet(tester, radio: _knownRadio);
final l10n = await AppLocalizations.delegate.load(const Locale('en'));
final texts = _texts(tester);
expect(texts, contains(l10n.imageSend_reconstructionUnavailable));
expect(texts, isNot(contains(l10n.imageSend_versionLabel(1))));
expect(texts, isNot(contains(l10n.imageSend_tryAnother)));
expect(find.byType(SegmentedButton<bool>), findsNothing);
});
});
}
/// Room for the whole sheet, so version tiles and "Try another version" are
/// on screen without scrolling the draggable sheet.
void _tallScreen(WidgetTester tester) {
tester.view.physicalSize = const Size(1000, 2400);
tester.view.devicePixelRatio = 1.0;
addTearDown(tester.view.reset);
}
/// A host app whose button opens the sheet and reports what it returned.
Widget _host(
ImageSendCodec codec,
void Function(ImageSendPreviewResult?) onResult,
) => MaterialApp(
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: Builder(
builder: (context) => Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () async => onResult(
await showImageSendPreviewSheet(
context: context,
imageBytes: _onePixelPng,
originalFileBytes: 1024,
codec: codec,
radio: _knownRadio,
),
),
child: const Text('open'),
),
),
),
),
);