mirror of
https://github.com/zjs81/meshcore-open.git
synced 2026-04-20 22:13:48 +00:00
moved _getRepeaterPrefixMatchNearLocation since I don't need the function anywhere else anymore.
This commit is contained in:
parent
32fa96431e
commit
d1e45fc2ba
2 changed files with 67 additions and 58 deletions
|
|
@ -1,6 +1,3 @@
|
|||
import 'package:latlong2/latlong.dart';
|
||||
|
||||
import '../connector/meshcore_protocol.dart';
|
||||
import '../models/contact.dart';
|
||||
|
||||
export 'contact_filter_types.dart';
|
||||
|
|
@ -46,55 +43,3 @@ String? _extractHexPrefix(String query) {
|
|||
if (!RegExp(r'^[0-9a-f]+$').hasMatch(cleaned)) return null;
|
||||
return cleaned;
|
||||
}
|
||||
|
||||
Contact? getRepeaterPrefixMatchNearLocation(
|
||||
List<Contact> contacts,
|
||||
int pubkeyFirstByte, {
|
||||
LatLng? searchPoint,
|
||||
bool preferFavorites = false,
|
||||
}) {
|
||||
final candidates = contacts
|
||||
.where(
|
||||
(c) =>
|
||||
c.publicKey.isNotEmpty &&
|
||||
c.publicKey.first == pubkeyFirstByte &&
|
||||
(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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue