diff --git a/lib/screens/channel_message_path_screen.dart b/lib/screens/channel_message_path_screen.dart index 092e296a..218fe78f 100644 --- a/lib/screens/channel_message_path_screen.dart +++ b/lib/screens/channel_message_path_screen.dart @@ -24,6 +24,7 @@ import '../widgets/adaptive_app_bar_title.dart'; import '../widgets/mesh_ui.dart'; import '../widgets/path_map_scaffold.dart'; import '../widgets/path_map_ui.dart'; +import '../widgets/set_my_location_dialog.dart'; class ChannelMessagePathScreen extends StatelessWidget { final ChannelMessage message; @@ -911,6 +912,10 @@ class _ChannelMessagePathMapScreenState initialCenter: model.initialCenter, initialZoom: model.initialZoom, bounds: model.bounds, + onLongPress: (_, point) => + confirmAndSetMyLocation(context, connector, point), + onSecondaryTap: (_, point) => + confirmAndSetMyLocation(context, connector, point), onPositionChanged: (camera, hasGesture) { if (!mounted) return; // A manual pan/zoom releases the follow lock. diff --git a/lib/screens/map_screen.dart b/lib/screens/map_screen.dart index d2d74bb9..5dbd4f7a 100644 --- a/lib/screens/map_screen.dart +++ b/lib/screens/map_screen.dart @@ -43,6 +43,7 @@ import '../theme/mesh_theme.dart'; import '../widgets/mesh_ui.dart'; import '../widgets/repeater_login_dialog.dart'; import '../widgets/room_login_dialog.dart'; +import '../widgets/set_my_location_dialog.dart'; import '../helpers/guessed_location_estimator.dart'; import '../helpers/snack_bar_builder.dart'; import '../helpers/utf8_length_limiter.dart'; @@ -3498,7 +3499,7 @@ class _MapScreenState extends State with DisconnectNavigationMixin { title: Text(context.l10n.map_setAsMyLocation), onTap: () { Navigator.pop(sheetContext); - _confirmAndSetMyLocation(connector, position); + confirmAndSetMyLocation(this.context, connector, position); }, ), ListTile( @@ -3512,72 +3513,6 @@ class _MapScreenState extends State with DisconnectNavigationMixin { ); } - Future _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( - 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.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 _shareMarker({ required BuildContext context, required MeshCoreConnector connector, diff --git a/lib/screens/path_trace_map.dart b/lib/screens/path_trace_map.dart index f0bee80e..43d21d70 100644 --- a/lib/screens/path_trace_map.dart +++ b/lib/screens/path_trace_map.dart @@ -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_ui.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 '../theme/mesh_theme.dart'; @@ -239,6 +240,16 @@ class _PathTraceMapScreenState extends State }); } + Future _setMyLocation(LatLng point) async { + final connector = context.read(); + 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. void _followPacketCamera() { if (!mounted) return; @@ -1497,6 +1508,8 @@ class _PathTraceMapScreenState extends State initialCenter: _initialCenter!, initialZoom: _initialZoom, bounds: _bounds, + onLongPress: (_, point) => _setMyLocation(point), + onSecondaryTap: (_, point) => _setMyLocation(point), onPositionChanged: (camera, hasGesture) { if (!mounted) return; // A manual pan/zoom releases the follow lock. diff --git a/lib/widgets/set_my_location_dialog.dart b/lib/widgets/set_my_location_dialog.dart new file mode 100644 index 00000000..5c611ff0 --- /dev/null +++ b/lib/widgets/set_my_location_dialog.dart @@ -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 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( + 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.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; +}