feat(#674): tell the owner when a contact sync comes up short
A short contact sync is now loud (SAFELANE 6). A banner above the whole app says the radio reported N contacts but sent M and that the saved contacts were kept; it stays until dismissed. When the automatic retry is short too, a modal asks: Keep my contacts (the default, focused, so Enter keeps) or Use the radio's list, which states how many contacts it would remove. Owner decisions on #670. The banner sits in MaterialApp.builder next to the storage banner (#385), above the Navigator, so the modal is drawn in place rather than pushed as a route. Strings are English; other locales fall back through untranslated.json, as the storage banner's do. Widget tests cover hidden, shown with counts, the no-total wording, dismiss, the decision's default and both choices; with the Keep default removed, two fail. Epic #668. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>feat/668-contact-sync-guard
parent
9a43fde09f
commit
e5c7a27fdf
@ -0,0 +1,125 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../connector/meshcore_connector.dart';
|
||||
import '../l10n/l10n.dart';
|
||||
|
||||
/// Shown above the whole app when a full contact sync delivered fewer contacts
|
||||
/// than the radio reported (#668). The banner stays until dismissed. When the
|
||||
/// automatic retry was also short, a modal asks whether to keep the saved
|
||||
/// contacts (the default) or accept the radio's shorter list.
|
||||
///
|
||||
/// SAFELANE §6: the short sync that silently replaced 350 saved contacts with
|
||||
/// 106 (#660) must now be loud. Sits in `MaterialApp.builder`, above the
|
||||
/// Navigator, so the modal is drawn here rather than pushed as a route.
|
||||
class ContactSyncShortfallBanner extends StatelessWidget {
|
||||
final ContactSyncShortfall? shortfall;
|
||||
final bool decisionPending;
|
||||
final int undeliveredCount;
|
||||
final VoidCallback onDismiss;
|
||||
final VoidCallback onKeep;
|
||||
final VoidCallback onUseRadio;
|
||||
final Widget child;
|
||||
|
||||
const ContactSyncShortfallBanner({
|
||||
super.key,
|
||||
required this.shortfall,
|
||||
required this.decisionPending,
|
||||
required this.undeliveredCount,
|
||||
required this.onDismiss,
|
||||
required this.onKeep,
|
||||
required this.onUseRadio,
|
||||
required this.child,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final current = shortfall;
|
||||
if (current == null && !decisionPending) return child;
|
||||
|
||||
final content = current == null
|
||||
? child
|
||||
: Column(
|
||||
children: [
|
||||
_banner(context, current),
|
||||
Expanded(child: child),
|
||||
],
|
||||
);
|
||||
if (!decisionPending) return content;
|
||||
|
||||
return Stack(
|
||||
children: [
|
||||
content,
|
||||
const ModalBarrier(dismissible: false, color: Colors.black54),
|
||||
Center(child: _decision(context, current?.received ?? 0)),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _banner(BuildContext context, ContactSyncShortfall current) {
|
||||
final l10n = context.l10n;
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
final declared = current.declared;
|
||||
final body = declared == null
|
||||
? l10n.contactSyncShortfallBodyNoTotal(current.received)
|
||||
: l10n.contactSyncShortfallBody(declared, current.received);
|
||||
return Material(
|
||||
color: scheme.errorContainer,
|
||||
child: SafeArea(
|
||||
bottom: false,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 10, 4, 10),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Icon(Icons.sync_problem, color: scheme.onErrorContainer),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
l10n.contactSyncShortfallTitle,
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: scheme.onErrorContainer,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
body,
|
||||
style: TextStyle(color: scheme.onErrorContainer),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
tooltip: l10n.common_close,
|
||||
icon: Icon(Icons.close, color: scheme.onErrorContainer),
|
||||
onPressed: onDismiss,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _decision(BuildContext context, int received) {
|
||||
final l10n = context.l10n;
|
||||
return AlertDialog(
|
||||
title: Text(l10n.contactSyncDecisionTitle),
|
||||
content: Text(l10n.contactSyncDecisionBody(received, undeliveredCount)),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: onUseRadio,
|
||||
child: Text(l10n.contactSyncUseRadio),
|
||||
),
|
||||
FilledButton(
|
||||
autofocus: true,
|
||||
onPressed: onKeep,
|
||||
child: Text(l10n.contactSyncKeep),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,119 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/connector/meshcore_connector.dart';
|
||||
import 'package:meshcore_open/l10n/app_localizations.dart';
|
||||
import 'package:meshcore_open/widgets/contact_sync_shortfall_banner.dart';
|
||||
|
||||
/// #674: a short contact sync must be loud and stay visible, and the choice
|
||||
/// after a failed retry must default to keeping the saved contacts.
|
||||
void main() {
|
||||
late List<String> calls;
|
||||
|
||||
Widget build({
|
||||
ContactSyncShortfall? shortfall,
|
||||
bool decisionPending = false,
|
||||
int undelivered = 244,
|
||||
}) => MaterialApp(
|
||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
home: Scaffold(
|
||||
body: ContactSyncShortfallBanner(
|
||||
shortfall: shortfall,
|
||||
decisionPending: decisionPending,
|
||||
undeliveredCount: undelivered,
|
||||
onDismiss: () => calls.add('dismiss'),
|
||||
onKeep: () => calls.add('keep'),
|
||||
onUseRadio: () => calls.add('useRadio'),
|
||||
child: const Text('APP'),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
const short = ContactSyncShortfall(
|
||||
declared: 350,
|
||||
received: 106,
|
||||
keptLocally: 244,
|
||||
);
|
||||
|
||||
setUp(() => calls = []);
|
||||
|
||||
testWidgets('no shortfall: only the app renders', (tester) async {
|
||||
await tester.pumpWidget(build());
|
||||
|
||||
expect(find.text('APP'), findsOneWidget);
|
||||
expect(find.text('Contact sync incomplete'), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('shortfall: banner with the counts, above the app', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.pumpWidget(build(shortfall: short));
|
||||
|
||||
expect(find.text('Contact sync incomplete'), findsOneWidget);
|
||||
expect(
|
||||
find.textContaining('reported 350 contacts but sent 106'),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(find.text('APP'), findsOneWidget);
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('no declared total uses its own wording', (tester) async {
|
||||
await tester.pumpWidget(
|
||||
build(
|
||||
shortfall: const ContactSyncShortfall(
|
||||
declared: null,
|
||||
received: 50,
|
||||
keptLocally: 3,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
expect(find.textContaining('did not report how many'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('the close button dismisses', (tester) async {
|
||||
await tester.pumpWidget(build(shortfall: short));
|
||||
await tester.tap(find.byIcon(Icons.close));
|
||||
|
||||
expect(calls, ['dismiss']);
|
||||
});
|
||||
|
||||
group('decision pending', () {
|
||||
testWidgets('asks, says how many would be removed, defaults to Keep', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.pumpWidget(build(shortfall: short, decisionPending: true));
|
||||
await tester.pump();
|
||||
|
||||
expect(find.byType(AlertDialog), findsOneWidget);
|
||||
expect(find.textContaining('removes 244 contacts'), findsOneWidget);
|
||||
final keep = tester.widget<FilledButton>(find.byType(FilledButton));
|
||||
expect(keep.autofocus, isTrue);
|
||||
expect(find.text('Keep my contacts'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('Keep my contacts', (tester) async {
|
||||
await tester.pumpWidget(build(shortfall: short, decisionPending: true));
|
||||
await tester.tap(find.text('Keep my contacts'));
|
||||
|
||||
expect(calls, ['keep']);
|
||||
});
|
||||
|
||||
testWidgets('Use the radio\'s list', (tester) async {
|
||||
await tester.pumpWidget(build(shortfall: short, decisionPending: true));
|
||||
await tester.tap(find.text('Use the radio\'s list'));
|
||||
|
||||
expect(calls, ['useRadio']);
|
||||
});
|
||||
|
||||
testWidgets('Enter on the dialog picks the default, Keep', (tester) async {
|
||||
await tester.pumpWidget(build(shortfall: short, decisionPending: true));
|
||||
await tester.pump();
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
|
||||
|
||||
expect(calls, ['keep']);
|
||||
});
|
||||
});
|
||||
}
|
||||
Loading…
Reference in new issue