Files
meshcore-open/lib/widgets/snr_indicator.dart
T
zjs81 c131f56dae Open repeater details from the nearby repeaters list
Each nearby repeater is now a tappable row that opens the same node
details sheet as the map, or a short sheet with the key prefix and
signal when the repeater isn't a known contact. The map's node info
sheet and login dialogs move to widgets/node_info_sheet.dart so both
places share them.
2026-10-24 21:01:47 -07:00

431 lines
14 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:latlong2/latlong.dart';
import '../connector/meshcore_connector.dart';
import '../connector/meshcore_protocol.dart';
import '../helpers/path_helper.dart';
import '../l10n/l10n.dart';
import '../models/contact.dart';
import '../theme/mesh_theme.dart';
import 'mesh_ui.dart';
import 'node_info_sheet.dart';
import 'signal_ui.dart';
Contact? _getRepeaterPrefixMatchNearLocation(
List<Contact> contacts,
List<int> pubkeyPrefix, {
String? contactKeyHex,
LatLng? searchPoint,
bool preferFavorites = false,
}) {
if (contactKeyHex != null) {
for (final c in contacts) {
if (c.publicKeyHex == contactKeyHex) {
return c;
}
}
}
final candidates = contacts
.where(
(c) =>
c.publicKey.length >= pubkeyPrefix.length &&
listEquals(
c.publicKey.sublist(0, pubkeyPrefix.length),
pubkeyPrefix,
) &&
(c.type == advTypeRepeater || c.type == advTypeRoom),
)
.toList();
if (candidates.isEmpty) return null;
candidates.sort((a, b) {
if (preferFavorites) {
final favA = a.isFavorite ? 1 : 0;
final favB = b.isFavorite ? 1 : 0;
final favCompare = favB.compareTo(favA);
if (favCompare != 0) return favCompare;
}
final seenCompare = b.lastSeen.compareTo(a.lastSeen);
if (seenCompare != 0) return seenCompare;
return a.publicKeyHex.compareTo(b.publicKeyHex);
});
if (searchPoint == null) {
return candidates.first;
}
final distance = Distance();
Contact best = candidates.first;
var bestDistance = double.infinity;
for (final c in candidates) {
if (c.hasLocation && c.latitude != null && c.longitude != null) {
final d = distance(searchPoint, LatLng(c.latitude!, c.longitude!));
if (d < bestDistance) {
bestDistance = d;
best = c;
}
}
}
return best;
}
class SNRUi {
final IconData icon;
final Color color;
final String text;
const SNRUi(this.icon, this.color, this.text);
}
List<double> getSNRfromSF(int spreadingFactor) {
switch (spreadingFactor) {
case 7:
return [4.0, -2.0, -4.0, -6.0];
case 8:
return [4.0, -4.0, -6.0, -8.0];
case 9:
return [4.0, -6.0, -8.0, -10.0];
case 10:
return [4.0, -8.0, -10.0, -13.0];
case 11:
return [4.0, -10.0, -12.5, -15.0];
case 12:
return [4.0, -12.5, -15.0, -18.0];
default:
return []; // Or throw Exception('Invalid SF: $spreadingFactor');
}
}
SNRUi snrUiFromSNR(double? snr, int? spreadingFactor) {
if (snr == null ||
spreadingFactor == null ||
spreadingFactor < 7 ||
spreadingFactor > 12) {
return const SNRUi(Icons.signal_cellular_off, Colors.grey, '—');
}
final snrLevels = getSNRfromSF(spreadingFactor);
String text = '${snr.toStringAsFixed(1)} dB';
final tier = snr >= snrLevels[0]
? 0
: snr >= snrLevels[1]
? 1
: snr >= snrLevels[2]
? 2
: snr >= snrLevels[3]
? 3
: 4;
final signalUi = signalUiForStrengthTier(tier);
return SNRUi(signalUi.icon, signalUi.color, text);
}
class SNRIndicator extends StatefulWidget {
final MeshCoreConnector connector;
const SNRIndicator({super.key, required this.connector});
@override
State<SNRIndicator> createState() => _SNRIndicatorState();
}
class _SNRIndicatorState extends State<SNRIndicator> {
bool _isValidSelfLocation(double lat, double lon) {
const double epsilon = 1e-6;
return (lat.abs() > epsilon || lon.abs() > epsilon) &&
lat >= -90.0 &&
lat <= 90.0 &&
lon >= -180.0 &&
lon <= 180.0;
}
@override
Widget build(BuildContext context) {
final directRepeaters = widget.connector.directRepeaters;
final directBestRepeaters = List.of(directRepeaters)
..sort((a, b) => (b.ranking).compareTo(a.ranking));
final directRepeater = directBestRepeaters.isEmpty
? null
: directBestRepeaters.first;
final snrUi = snrUiFromSNR(
directBestRepeaters.isNotEmpty ? directRepeater!.snr : null,
widget.connector.currentSf,
);
return ConstrainedBox(
constraints: const BoxConstraints(minWidth: 40, minHeight: 40),
child: InkWell(
onTap: directRepeater != null
? () => _showFullPathDialog(context, directBestRepeaters)
: null,
borderRadius: BorderRadius.circular(8),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 8),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(snrUi.icon, size: 18, color: snrUi.color),
Text(
snrUi.text,
style: TextStyle(fontSize: 12, color: snrUi.color),
),
if (directRepeater != null)
Text(
'${directRepeaters.length}: ${directRepeater.pubkeyPrefixHex}: ${_formatLastUpdated(directRepeater.lastUpdated)}',
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w500,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
],
),
),
),
);
}
String _formatLastUpdated(DateTime lastSeen) {
final now = DateTime.now();
final diff = now.difference(lastSeen);
if (diff.isNegative) {
return "0s";
}
if (diff.inMinutes < 1) {
return "${diff.inSeconds}s";
}
if (diff.inMinutes < 60) {
return "${diff.inMinutes}m";
}
if (diff.inHours < 24) {
final hours = diff.inHours;
return "${hours}h";
}
final days = diff.inDays;
return "${days}d";
}
void _showFullPathDialog(
BuildContext context,
List<DirectRepeater> directBestRepeaters,
) {
final l10n = context.l10n;
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text(l10n.snrIndicator_nearByRepeaters),
content: SizedBox(
width: double.maxFinite,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
l10n.snrIndicator_nearByRepeatersDescription,
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
const SizedBox(height: 8),
Flexible(
child: Scrollbar(
child: ListView.separated(
shrinkWrap: true,
padding: const EdgeInsets.symmetric(vertical: 4),
itemCount: directBestRepeaters.length,
separatorBuilder: (_, _) => const Divider(height: 1),
itemBuilder: (context, index) {
final repeater = directBestRepeaters[index];
final allContacts = widget.connector.allContacts;
final selfLat = widget.connector.selfLatitude;
final selfLon = widget.connector.selfLongitude;
LatLng? selfPoint;
if (selfLat != null &&
selfLon != null &&
_isValidSelfLocation(selfLat, selfLon)) {
selfPoint = LatLng(selfLat, selfLon);
}
final contact = _getRepeaterPrefixMatchNearLocation(
allContacts,
repeater.pubkeyPrefix,
contactKeyHex: repeater.contactKeyHex,
searchPoint: selfPoint,
preferFavorites: true,
);
final name = contact?.name;
final prefixLabel = PathHelper.formatHopHex(
repeater.pubkeyPrefix,
);
final snrColor = MeshTheme.snrColor(
repeater.snr,
blocked: false,
);
final colorScheme = Theme.of(context).colorScheme;
return Semantics(
button: true,
hint: l10n.map_details,
child: InkWell(
onTap: () => contact != null
? showNodeInfoSheet(context, contact)
: _showUnknownRepeaterSheet(
context,
repeater,
prefixLabel,
),
child: ConstrainedBox(
constraints: const BoxConstraints(minHeight: 56),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 10,
),
child: Row(
children: [
AvatarCircle(
name: name ?? prefixLabel,
size: 36,
color: colorScheme.onSurfaceVariant,
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: [
Text(
name ?? prefixLabel,
style: Theme.of(context)
.textTheme
.bodyMedium
?.copyWith(
color: colorScheme.onSurface,
),
),
Row(
children: [
Icon(
Icons.circle,
size: 8,
color: snrColor,
),
const SizedBox(width: 6),
Text(
'${repeater.snr.toStringAsFixed(1)} dB • ${_formatLastUpdated(repeater.lastUpdated)}',
style: MeshTheme.mono(
fontSize: 12,
color: colorScheme
.onSurfaceVariant,
),
),
],
),
],
),
),
const SizedBox(width: 8),
Icon(
Icons.chevron_right,
size: 24,
color: colorScheme.onSurfaceVariant,
),
],
),
),
),
),
);
},
),
),
),
],
),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: Text(l10n.common_close),
),
],
),
);
}
void _showUnknownRepeaterSheet(
BuildContext context,
DirectRepeater repeater,
String prefixLabel,
) {
showMeshSheet<void>(
context,
builder: (sheetContext) {
final l10n = sheetContext.l10n;
final scheme = Theme.of(sheetContext).colorScheme;
return SafeArea(
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
BottomSheetHeader(
title: prefixLabel,
subtitle: l10n.snrIndicator_unknownRepeater,
trailing: Icon(
mapNodeTypeIcon(advTypeRepeater),
color: mapNodeTypeColor(advTypeRepeater),
size: 20,
),
),
Padding(
padding: const EdgeInsets.fromLTRB(16, 0, 16, 16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
NodeInfoRow(
label: l10n.map_publicKeyPrefix,
value: repeater.pubkeyPrefixHex,
),
NodeInfoRow(
label: l10n.repeater_lastSnr,
value: '${repeater.snr.toStringAsFixed(1)} dB',
),
NodeInfoRow(
label: l10n.map_lastSeen,
value: formatNodeLastSeen(l10n, repeater.lastUpdated),
),
const SizedBox(height: 12),
Text(
l10n.snrIndicator_unknownRepeaterHint,
style: TextStyle(
fontSize: 15,
color: scheme.onSurfaceVariant,
),
),
],
),
),
],
),
),
);
},
);
}
}