A firmware-gated repeater-hub tile opens a Discover Regions screen that runs RegionDiscoveryService against the selected repeater and renders each state distinctly (loading / region list / no-regions / timeout / error, with retry). Shows region name + source repeater; the wire reply has no per-region SNR, so none is shown (the B2 spec's SNR is not available). Widget tests cover all four states via an injected fake discoverer. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>pull/852/head
parent
fbfc419a7c
commit
187708ab27
@ -0,0 +1,184 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../connector/meshcore_connector.dart';
|
||||
import '../l10n/app_localizations.dart';
|
||||
import '../l10n/l10n.dart';
|
||||
import '../models/contact.dart';
|
||||
import '../services/region_discovery_service.dart';
|
||||
|
||||
/// Discover the flood regions a repeater advertises (Feature #812, Epic #814).
|
||||
///
|
||||
/// Launched from the repeater hub for a specific repeater, so the source node
|
||||
/// is [repeater]; the reply carries only region names and the repeater clock
|
||||
/// (there is no per-region SNR on the wire). Each outcome renders distinctly:
|
||||
/// loading, the region list, no-regions, timeout, or error.
|
||||
class DiscoverRegionsScreen extends StatefulWidget {
|
||||
const DiscoverRegionsScreen({
|
||||
super.key,
|
||||
required this.repeater,
|
||||
this.service,
|
||||
});
|
||||
|
||||
final Contact repeater;
|
||||
|
||||
/// Test seam: inject a service with a fake discoverer. In production the
|
||||
/// service is built from the connector.
|
||||
final RegionDiscoveryService? service;
|
||||
|
||||
@override
|
||||
State<DiscoverRegionsScreen> createState() => _DiscoverRegionsScreenState();
|
||||
}
|
||||
|
||||
class _DiscoverRegionsScreenState extends State<DiscoverRegionsScreen> {
|
||||
late final RegionDiscoveryService _service;
|
||||
late final bool _ownsService;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
final injected = widget.service;
|
||||
_ownsService = injected == null;
|
||||
if (injected != null) {
|
||||
_service = injected;
|
||||
} else {
|
||||
final connector = context.read<MeshCoreConnector>();
|
||||
_service = RegionDiscoveryService(
|
||||
(pub, timeout) =>
|
||||
connector.discoverRegions(repeaterPubKey: pub, timeout: timeout),
|
||||
);
|
||||
}
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (mounted) _start();
|
||||
});
|
||||
}
|
||||
|
||||
void _start() => _service.discover(widget.repeater.publicKey);
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
if (_ownsService) _service.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = context.l10n;
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(l10n.discoverRegions_title),
|
||||
centerTitle: true,
|
||||
),
|
||||
body: ListenableBuilder(
|
||||
listenable: _service,
|
||||
builder: (context, _) => _body(l10n),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _body(AppLocalizations l10n) {
|
||||
final name = widget.repeater.name;
|
||||
switch (_service.status) {
|
||||
case RegionDiscoveryStatus.idle:
|
||||
case RegionDiscoveryStatus.loading:
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const CircularProgressIndicator(),
|
||||
const SizedBox(height: 16),
|
||||
Text(l10n.discoverRegions_loading(name)),
|
||||
],
|
||||
),
|
||||
);
|
||||
case RegionDiscoveryStatus.success:
|
||||
return _regionList(l10n);
|
||||
case RegionDiscoveryStatus.empty:
|
||||
return _message(
|
||||
stateKey: 'discoverRegionsEmpty',
|
||||
icon: Icons.location_off,
|
||||
text: l10n.discoverRegions_empty(name),
|
||||
l10n: l10n,
|
||||
);
|
||||
case RegionDiscoveryStatus.timeout:
|
||||
return _message(
|
||||
stateKey: 'discoverRegionsTimeout',
|
||||
icon: Icons.timer_off,
|
||||
text: l10n.discoverRegions_timeout(name),
|
||||
l10n: l10n,
|
||||
);
|
||||
case RegionDiscoveryStatus.error:
|
||||
return _message(
|
||||
stateKey: 'discoverRegionsError',
|
||||
icon: Icons.error_outline,
|
||||
text: _service.errorMessage ?? l10n.discoverRegions_error,
|
||||
l10n: l10n,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Widget _regionList(AppLocalizations l10n) {
|
||||
return Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(Icons.cell_tower),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
l10n.discoverRegions_source(widget.repeater.name),
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const Divider(height: 1),
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
itemCount: _service.regions.length,
|
||||
itemBuilder: (context, i) {
|
||||
final region = _service.regions[i];
|
||||
return ListTile(
|
||||
leading: Icon(region.isWildcard ? Icons.public : Icons.tag),
|
||||
title: Text(region.name),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _message({
|
||||
required String stateKey,
|
||||
required IconData icon,
|
||||
required String text,
|
||||
required AppLocalizations l10n,
|
||||
}) {
|
||||
return Center(
|
||||
key: Key(stateKey),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(icon, size: 48),
|
||||
const SizedBox(height: 12),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 32),
|
||||
child: Text(text, textAlign: TextAlign.center),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 16),
|
||||
child: ElevatedButton(
|
||||
key: const Key('discoverRegionsRetry'),
|
||||
onPressed: _start,
|
||||
child: Text(l10n.discoverRegions_retry),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,65 @@
|
||||
// Epic #814 T B2: Discover Regions screen state rendering.
|
||||
// Injects a fake discoverer service so each state renders without a radio.
|
||||
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/connector/meshcore_protocol.dart';
|
||||
import 'package:meshcore_open/l10n/app_localizations.dart';
|
||||
import 'package:meshcore_open/models/contact.dart';
|
||||
import 'package:meshcore_open/screens/discover_regions_screen.dart';
|
||||
import 'package:meshcore_open/services/region_discovery_service.dart';
|
||||
|
||||
Contact _repeater() => Contact(
|
||||
publicKey: Uint8List(32),
|
||||
name: 'rpt-01',
|
||||
type: advTypeRepeater,
|
||||
pathLength: 0,
|
||||
path: Uint8List(0),
|
||||
lastSeen: DateTime(2026),
|
||||
);
|
||||
|
||||
Future<void> _pump(WidgetTester tester, RegionDiscoveryService svc) async {
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
home: DiscoverRegionsScreen(repeater: _repeater(), service: svc),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
void main() {
|
||||
RegionsReply reply(List<String> names) =>
|
||||
RegionsReply(tag: 1, clock: 1, regionNames: names);
|
||||
|
||||
testWidgets('success lists region names', (tester) async {
|
||||
await _pump(
|
||||
tester,
|
||||
RegionDiscoveryService((_, _) async => reply(['oki', 'test'])),
|
||||
);
|
||||
expect(find.text('oki'), findsOneWidget);
|
||||
expect(find.text('test'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('empty reply shows the empty state', (tester) async {
|
||||
await _pump(tester, RegionDiscoveryService((_, _) async => reply([])));
|
||||
expect(find.byKey(const Key('discoverRegionsEmpty')), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('timeout shows the timeout state with a retry', (tester) async {
|
||||
await _pump(tester, RegionDiscoveryService((_, _) async => null));
|
||||
expect(find.byKey(const Key('discoverRegionsTimeout')), findsOneWidget);
|
||||
expect(find.byKey(const Key('discoverRegionsRetry')), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('a thrown error shows the error state', (tester) async {
|
||||
await _pump(
|
||||
tester,
|
||||
RegionDiscoveryService((_, _) async => throw StateError('boom')),
|
||||
);
|
||||
expect(find.byKey(const Key('discoverRegionsError')), findsOneWidget);
|
||||
});
|
||||
}
|
||||
Loading…
Reference in new issue