Set my location by long-pressing the path maps
Move the map's confirm-and-set-location flow into a shared helper and call it on long-press and right-click in the path trace and channel message path maps, so the self position can be set while viewing a path.
This commit is contained in:
1 parent
c296597a2d
commit
00756ef05e
4 files changed
+99
-67
No files matched your search
@@ -24,6 +24,7 @@ import '../widgets/adaptive_app_bar_title.dart';
|
|||||||
import '../widgets/mesh_ui.dart';
|
import '../widgets/mesh_ui.dart';
|
||||||
import '../widgets/path_map_scaffold.dart';
|
import '../widgets/path_map_scaffold.dart';
|
||||||
import '../widgets/path_map_ui.dart';
|
import '../widgets/path_map_ui.dart';
|
||||||
|
import '../widgets/set_my_location_dialog.dart';
|
||||||
|
|
||||||
class ChannelMessagePathScreen extends StatelessWidget {
|
class ChannelMessagePathScreen extends StatelessWidget {
|
||||||
final ChannelMessage message;
|
final ChannelMessage message;
|
||||||
@@ -911,6 +912,10 @@ class _ChannelMessagePathMapScreenState
|
|||||||
initialCenter: model.initialCenter,
|
initialCenter: model.initialCenter,
|
||||||
initialZoom: model.initialZoom,
|
initialZoom: model.initialZoom,
|
||||||
bounds: model.bounds,
|
bounds: model.bounds,
|
||||||
|
onLongPress: (_, point) =>
|
||||||
|
confirmAndSetMyLocation(context, connector, point),
|
||||||
|
onSecondaryTap: (_, point) =>
|
||||||
|
confirmAndSetMyLocation(context, connector, point),
|
||||||
onPositionChanged: (camera, hasGesture) {
|
onPositionChanged: (camera, hasGesture) {
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
// A manual pan/zoom releases the follow lock.
|
// A manual pan/zoom releases the follow lock.
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ import '../theme/mesh_theme.dart';
|
|||||||
import '../widgets/mesh_ui.dart';
|
import '../widgets/mesh_ui.dart';
|
||||||
import '../widgets/repeater_login_dialog.dart';
|
import '../widgets/repeater_login_dialog.dart';
|
||||||
import '../widgets/room_login_dialog.dart';
|
import '../widgets/room_login_dialog.dart';
|
||||||
|
import '../widgets/set_my_location_dialog.dart';
|
||||||
import '../helpers/guessed_location_estimator.dart';
|
import '../helpers/guessed_location_estimator.dart';
|
||||||
import '../helpers/snack_bar_builder.dart';
|
import '../helpers/snack_bar_builder.dart';
|
||||||
import '../helpers/utf8_length_limiter.dart';
|
import '../helpers/utf8_length_limiter.dart';
|
||||||
@@ -3498,7 +3499,7 @@ class _MapScreenState extends State<MapScreen> with DisconnectNavigationMixin {
|
|||||||
title: Text(context.l10n.map_setAsMyLocation),
|
title: Text(context.l10n.map_setAsMyLocation),
|
||||||
onTap: () {
|
onTap: () {
|
||||||
Navigator.pop(sheetContext);
|
Navigator.pop(sheetContext);
|
||||||
_confirmAndSetMyLocation(connector, position);
|
confirmAndSetMyLocation(this.context, connector, position);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
ListTile(
|
ListTile(
|
||||||
@@ -3512,72 +3513,6 @@ class _MapScreenState extends State<MapScreen> with DisconnectNavigationMixin {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _confirmAndSetMyLocation(
|
|
||||||
MeshCoreConnector connector,
|
|
||||||
LatLng position,
|
|
||||||
) async {
|
|
||||||
if (!connector.isConnected) return;
|
|
||||||
final l10n = context.l10n;
|
|
||||||
final latText = position.latitude.toStringAsFixed(6);
|
|
||||||
final lonText = position.longitude.toStringAsFixed(6);
|
|
||||||
final confirmed = await showDialog<bool>(
|
|
||||||
context: context,
|
|
||||||
builder: (dialogContext) => AlertDialog(
|
|
||||||
title: Text(l10n.map_setLocationConfirmTitle),
|
|
||||||
content: Text(l10n.map_setLocationConfirmBody(latText, lonText)),
|
|
||||||
actions: [
|
|
||||||
TextButton(
|
|
||||||
onPressed: () => Navigator.pop(dialogContext, false),
|
|
||||||
child: Text(l10n.common_cancel),
|
|
||||||
),
|
|
||||||
FilledButton(
|
|
||||||
onPressed: () => Navigator.pop(dialogContext, true),
|
|
||||||
child: Text(l10n.map_setLocationAction),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
if (confirmed != true || !mounted || !connector.isConnected) return;
|
|
||||||
|
|
||||||
// The firmware stores coordinates as int32 micro-degrees.
|
|
||||||
bool matches() {
|
|
||||||
final lat = connector.selfLatitude;
|
|
||||||
final lon = connector.selfLongitude;
|
|
||||||
return lat != null &&
|
|
||||||
lon != null &&
|
|
||||||
(lat - position.latitude).abs() < 2e-6 &&
|
|
||||||
(lon - position.longitude).abs() < 2e-6;
|
|
||||||
}
|
|
||||||
|
|
||||||
var ok = false;
|
|
||||||
try {
|
|
||||||
await connector.setNodeLocation(
|
|
||||||
lat: position.latitude,
|
|
||||||
lon: position.longitude,
|
|
||||||
);
|
|
||||||
await connector.refreshDeviceInfo();
|
|
||||||
// Self info arrives asynchronously after the query; wait for it.
|
|
||||||
final deadline = DateTime.now().add(const Duration(seconds: 6));
|
|
||||||
while (mounted && connector.isConnected) {
|
|
||||||
if (matches()) {
|
|
||||||
ok = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (DateTime.now().isAfter(deadline)) break;
|
|
||||||
await Future<void>.delayed(const Duration(milliseconds: 250));
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
debugPrint('Set node location failed: $e');
|
|
||||||
}
|
|
||||||
if (!mounted) return;
|
|
||||||
showDismissibleSnackBar(
|
|
||||||
context,
|
|
||||||
content: Text(
|
|
||||||
ok ? l10n.map_locationSetSuccess : l10n.map_locationSetFailed,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _shareMarker({
|
Future<void> _shareMarker({
|
||||||
required BuildContext context,
|
required BuildContext context,
|
||||||
required MeshCoreConnector connector,
|
required MeshCoreConnector connector,
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import 'package:meshcore_open/helpers/snack_bar_builder.dart';
|
|||||||
import 'package:meshcore_open/widgets/path_map_scaffold.dart';
|
import 'package:meshcore_open/widgets/path_map_scaffold.dart';
|
||||||
import 'package:meshcore_open/widgets/path_map_ui.dart';
|
import 'package:meshcore_open/widgets/path_map_ui.dart';
|
||||||
import 'package:meshcore_open/widgets/snr_indicator.dart';
|
import 'package:meshcore_open/widgets/snr_indicator.dart';
|
||||||
|
import 'package:meshcore_open/widgets/set_my_location_dialog.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
import '../theme/mesh_theme.dart';
|
import '../theme/mesh_theme.dart';
|
||||||
|
|
||||||
@@ -239,6 +240,16 @@ class _PathTraceMapScreenState extends State<PathTraceMapScreen>
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _setMyLocation(LatLng point) async {
|
||||||
|
final connector = context.read<MeshCoreConnector>();
|
||||||
|
final ok = await confirmAndSetMyLocation(context, connector, point);
|
||||||
|
if (!ok || !mounted || !_hasData) return;
|
||||||
|
// Display paths start at the self position, so redraw them from it.
|
||||||
|
setState(() {
|
||||||
|
_rebuildDisplayPaths(connector);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/// Keeps the camera centered on the packet while the follow lock is on.
|
/// Keeps the camera centered on the packet while the follow lock is on.
|
||||||
void _followPacketCamera() {
|
void _followPacketCamera() {
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
@@ -1497,6 +1508,8 @@ class _PathTraceMapScreenState extends State<PathTraceMapScreen>
|
|||||||
initialCenter: _initialCenter!,
|
initialCenter: _initialCenter!,
|
||||||
initialZoom: _initialZoom,
|
initialZoom: _initialZoom,
|
||||||
bounds: _bounds,
|
bounds: _bounds,
|
||||||
|
onLongPress: (_, point) => _setMyLocation(point),
|
||||||
|
onSecondaryTap: (_, point) => _setMyLocation(point),
|
||||||
onPositionChanged: (camera, hasGesture) {
|
onPositionChanged: (camera, hasGesture) {
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
// A manual pan/zoom releases the follow lock.
|
// A manual pan/zoom releases the follow lock.
|
||||||
|
|||||||
@@ -0,0 +1,79 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:latlong2/latlong.dart';
|
||||||
|
|
||||||
|
import '../connector/meshcore_connector.dart';
|
||||||
|
import '../helpers/snack_bar_builder.dart';
|
||||||
|
import '../l10n/l10n.dart';
|
||||||
|
|
||||||
|
/// Asks the user to confirm, then sets the radio's advertised location to
|
||||||
|
/// [position] and waits for SELF_INFO to report it. Shows a success or
|
||||||
|
/// failure snackbar and returns whether the radio confirmed the new location.
|
||||||
|
Future<bool> confirmAndSetMyLocation(
|
||||||
|
BuildContext context,
|
||||||
|
MeshCoreConnector connector,
|
||||||
|
LatLng position,
|
||||||
|
) async {
|
||||||
|
if (!connector.isConnected) return false;
|
||||||
|
final l10n = context.l10n;
|
||||||
|
final latText = position.latitude.toStringAsFixed(6);
|
||||||
|
final lonText = position.longitude.toStringAsFixed(6);
|
||||||
|
final confirmed = await showDialog<bool>(
|
||||||
|
context: context,
|
||||||
|
builder: (dialogContext) => AlertDialog(
|
||||||
|
title: Text(l10n.map_setLocationConfirmTitle),
|
||||||
|
content: Text(l10n.map_setLocationConfirmBody(latText, lonText)),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.pop(dialogContext, false),
|
||||||
|
child: Text(l10n.common_cancel),
|
||||||
|
),
|
||||||
|
FilledButton(
|
||||||
|
onPressed: () => Navigator.pop(dialogContext, true),
|
||||||
|
child: Text(l10n.map_setLocationAction),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
if (confirmed != true || !context.mounted || !connector.isConnected) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The firmware stores coordinates as int32 micro-degrees.
|
||||||
|
bool matches() {
|
||||||
|
final lat = connector.selfLatitude;
|
||||||
|
final lon = connector.selfLongitude;
|
||||||
|
return lat != null &&
|
||||||
|
lon != null &&
|
||||||
|
(lat - position.latitude).abs() < 2e-6 &&
|
||||||
|
(lon - position.longitude).abs() < 2e-6;
|
||||||
|
}
|
||||||
|
|
||||||
|
var ok = false;
|
||||||
|
try {
|
||||||
|
await connector.setNodeLocation(
|
||||||
|
lat: position.latitude,
|
||||||
|
lon: position.longitude,
|
||||||
|
);
|
||||||
|
await connector.refreshDeviceInfo();
|
||||||
|
// Self info arrives asynchronously after the query; wait for it.
|
||||||
|
final deadline = DateTime.now().add(const Duration(seconds: 6));
|
||||||
|
while (context.mounted && connector.isConnected) {
|
||||||
|
if (matches()) {
|
||||||
|
ok = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (DateTime.now().isAfter(deadline)) break;
|
||||||
|
await Future<void>.delayed(const Duration(milliseconds: 250));
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
debugPrint('Set node location failed: $e');
|
||||||
|
}
|
||||||
|
if (!context.mounted) return ok;
|
||||||
|
showDismissibleSnackBar(
|
||||||
|
context,
|
||||||
|
content: Text(
|
||||||
|
ok ? l10n.map_locationSetSuccess : l10n.map_locationSetFailed,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user