mirror of
https://github.com/dz0ny/meshcore-sar.git
synced 2026-08-11 16:30:28 +00:00
feat: Add GPS toggle and node sorting
This commit is contained in:
48
lib/services/relay_candidate_sorter.dart
Normal file
48
lib/services/relay_candidate_sorter.dart
Normal file
@@ -0,0 +1,48 @@
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
import 'package:latlong2/latlong.dart';
|
||||
|
||||
import '../models/contact.dart';
|
||||
|
||||
class RelayCandidateSorter {
|
||||
const RelayCandidateSorter();
|
||||
|
||||
List<Contact> sortByDistanceFromSelf(
|
||||
List<Contact> contacts, {
|
||||
required LatLng? selfPoint,
|
||||
}) {
|
||||
final sorted = List<Contact>.from(contacts);
|
||||
sorted.sort((a, b) {
|
||||
if (selfPoint != null) {
|
||||
final distanceCompare = _distanceFrom(
|
||||
selfPoint,
|
||||
a,
|
||||
).compareTo(_distanceFrom(selfPoint, b));
|
||||
if (distanceCompare != 0) {
|
||||
return distanceCompare;
|
||||
}
|
||||
}
|
||||
|
||||
final nameCompare = a.displayName.compareTo(b.displayName);
|
||||
if (nameCompare != 0) {
|
||||
return nameCompare;
|
||||
}
|
||||
|
||||
return a.publicKeyHex.compareTo(b.publicKeyHex);
|
||||
});
|
||||
return sorted;
|
||||
}
|
||||
|
||||
double _distanceFrom(LatLng selfPoint, Contact contact) {
|
||||
final location = contact.displayLocation;
|
||||
if (location == null) {
|
||||
return double.infinity;
|
||||
}
|
||||
|
||||
return Geolocator.distanceBetween(
|
||||
selfPoint.latitude,
|
||||
selfPoint.longitude,
|
||||
location.latitude,
|
||||
location.longitude,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user