feat: Implement self advertisement functionality and enhance location broadcasting settings

This commit is contained in:
Janez T
2025-10-14 21:36:50 +02:00
parent 2cb6c34167
commit fd54392d3c
7 changed files with 808 additions and 164 deletions

View File

@@ -259,6 +259,13 @@ class _ContactTile extends StatelessWidget {
onPressed: () => _showDirectMessageDialog(context, contact),
tooltip: 'Send direct message',
),
// Login button for rooms (except public channel)
if (contact.type == ContactType.room && contact.advName != 'Public Channel')
IconButton(
icon: const Icon(Icons.login, size: 20),
onPressed: () => _showRoomLoginDialog(context, contact),
tooltip: 'Login to room',
),
// Telemetry refresh button
IconButton(
icon: const Icon(Icons.refresh, size: 20),
@@ -300,6 +307,15 @@ class _ContactTile extends StatelessWidget {
);
}
void _showRoomLoginDialog(BuildContext context, Contact contact) {
showModalBottomSheet(
context: context,
isScrollControlled: true,
backgroundColor: Colors.transparent,
builder: (context) => _RoomLoginSheet(contact: contact),
);
}
void _showContactDetails(BuildContext context, Contact contact) {
showModalBottomSheet(
context: context,
@@ -748,3 +764,249 @@ class _DirectMessageSheetState extends State<_DirectMessageSheet> {
);
}
}
// Room Login Sheet Widget
class _RoomLoginSheet extends StatefulWidget {
final Contact contact;
const _RoomLoginSheet({required this.contact});
@override
State<_RoomLoginSheet> createState() => _RoomLoginSheetState();
}
class _RoomLoginSheetState extends State<_RoomLoginSheet> {
final TextEditingController _passwordController = TextEditingController();
final FocusNode _focusNode = FocusNode();
bool _isLoggingIn = false;
bool _obscurePassword = true;
@override
void dispose() {
_passwordController.dispose();
_focusNode.dispose();
super.dispose();
}
Future<void> _loginToRoom() async {
final password = _passwordController.text.trim();
if (password.isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Please enter a password'),
backgroundColor: Colors.orange,
),
);
return;
}
final connectionProvider = context.read<ConnectionProvider>();
if (!connectionProvider.deviceInfo.isConnected) {
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Not connected to device'),
backgroundColor: Colors.red,
),
);
return;
}
setState(() {
_isLoggingIn = true;
});
try {
// Send login request to room
await connectionProvider.loginToRoom(
roomPublicKey: widget.contact.publicKey,
password: password,
);
_passwordController.clear();
_focusNode.unfocus();
if (!mounted) return;
Navigator.pop(context); // Close the dialog
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Login request sent to ${widget.contact.displayName}'),
backgroundColor: Colors.green,
duration: const Duration(seconds: 2),
),
);
} catch (e) {
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Failed to login: $e'),
backgroundColor: Colors.red,
),
);
} finally {
if (mounted) {
setState(() {
_isLoggingIn = false;
});
}
}
}
@override
Widget build(BuildContext context) {
return Container(
height: MediaQuery.of(context).size.height * 0.6,
decoration: const BoxDecoration(
color: Color(0xFF1E1E1E),
borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
),
child: Column(
children: [
// Header
Container(
padding: const EdgeInsets.all(16),
child: Row(
children: [
IconButton(
icon: const Icon(Icons.arrow_back, color: Colors.white),
onPressed: () => Navigator.pop(context),
),
Expanded(
child: Column(
children: [
const Text(
'Login to Room',
style: TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
Text(
widget.contact.displayName,
style: const TextStyle(
color: Colors.grey,
fontSize: 14,
),
),
],
),
),
const SizedBox(width: 48), // Balance the back button
],
),
),
// Info banner
Container(
margin: const EdgeInsets.symmetric(horizontal: 16),
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.primaryContainer,
borderRadius: BorderRadius.circular(8),
),
child: Row(
children: [
Icon(Icons.info_outline, color: Theme.of(context).colorScheme.onPrimaryContainer),
const SizedBox(width: 12),
Expanded(
child: Text(
'Enter the password to access this room. You will receive a confirmation once logged in.',
style: TextStyle(
color: Theme.of(context).colorScheme.onPrimaryContainer,
fontSize: 13,
),
),
),
],
),
),
const SizedBox(height: 24),
const Spacer(),
// Password input
Container(
padding: EdgeInsets.only(
left: 16,
right: 16,
top: 16,
bottom: 16 + MediaQuery.of(context).viewInsets.bottom,
),
decoration: const BoxDecoration(
color: Color(0xFF2D2D2D),
),
child: Column(
children: [
TextField(
controller: _passwordController,
focusNode: _focusNode,
maxLength: 15, // Max password length from protocol
obscureText: _obscurePassword,
autofocus: true,
maxLengthEnforcement: MaxLengthEnforcement.enforced,
style: const TextStyle(color: Colors.white),
decoration: InputDecoration(
labelText: 'Password',
labelStyle: const TextStyle(color: Colors.grey),
hintText: 'Enter room password',
hintStyle: const TextStyle(color: Colors.grey),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: const BorderSide(color: Colors.grey),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: const BorderSide(color: Colors.grey),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: const BorderSide(color: Colors.white),
),
contentPadding: const EdgeInsets.all(16),
suffixIcon: IconButton(
icon: Icon(
_obscurePassword ? Icons.visibility : Icons.visibility_off,
color: Colors.grey,
),
onPressed: () {
setState(() {
_obscurePassword = !_obscurePassword;
});
},
),
),
textInputAction: TextInputAction.done,
onSubmitted: (_) => _loginToRoom(),
),
const SizedBox(height: 16),
SizedBox(
width: double.infinity,
child: ElevatedButton.icon(
onPressed: _isLoggingIn || _passwordController.text.trim().isEmpty
? null
: _loginToRoom,
icon: _isLoggingIn
? const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Icon(Icons.login),
label: Text(_isLoggingIn ? 'Logging in...' : 'Login'),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 14),
),
),
),
],
),
),
],
),
);
}
}

View File

@@ -19,6 +19,7 @@ class _DeviceConfigScreenState extends State<DeviceConfigScreen> {
late TextEditingController _txPowerController;
bool _telemetryEnabled = false;
bool _isBroadcasting = false;
String _selectedBandwidth = '62.5 kHz';
int _selectedSpreadingFactor = 8;
int _selectedCodingRate = 8;
@@ -265,6 +266,51 @@ class _DeviceConfigScreenState extends State<DeviceConfigScreen> {
}
}
Future<void> _broadcastNow() async {
final connectionProvider = context.read<ConnectionProvider>();
if (!connectionProvider.deviceInfo.isConnected) {
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Not connected to device'),
backgroundColor: Colors.orange,
),
);
}
return;
}
setState(() => _isBroadcasting = true);
try {
// Send self advertisement to mesh network
await connectionProvider.sendSelfAdvert(floodMode: true);
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Advertisement broadcast to mesh network'),
backgroundColor: Colors.green,
),
);
}
} catch (e) {
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Failed to broadcast: $e'),
backgroundColor: Colors.red,
),
);
}
} finally {
if (mounted) {
setState(() => _isBroadcasting = false);
}
}
}
@override
Widget build(BuildContext context) {
final deviceInfo = context.watch<ConnectionProvider>().deviceInfo;
@@ -324,10 +370,26 @@ class _DeviceConfigScreenState extends State<DeviceConfigScreen> {
fontWeight: FontWeight.bold,
),
),
ElevatedButton.icon(
onPressed: _savePublicInfo,
icon: const Icon(Icons.save, size: 18),
label: const Text('Save'),
Wrap(
spacing: 8,
children: [
OutlinedButton.icon(
onPressed: _isBroadcasting ? null : _broadcastNow,
icon: _isBroadcasting
? const SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Icon(Icons.sensors, size: 18),
label: const Text('Broadcast'),
),
ElevatedButton.icon(
onPressed: _savePublicInfo,
icon: const Icon(Icons.save, size: 18),
label: const Text('Save'),
),
],
),
],
),

View File

@@ -30,7 +30,11 @@ class _SettingsScreenState extends State<SettingsScreen> {
PackageInfo? _packageInfo;
bool _isLoadingSampleData = false;
double _gpsUpdateDistance = 10.0;
double _gpsMinDistance = 5.0;
double _gpsMaxDistance = 100.0;
int _minTimeIntervalSeconds = 30;
bool _backgroundTrackingEnabled = false;
bool _isSendingLocationUpdate = false;
final BackgroundLocationService _backgroundLocationService =
BackgroundLocationService();
@@ -56,6 +60,9 @@ class _SettingsScreenState extends State<SettingsScreen> {
if (mounted) {
setState(() {
_gpsUpdateDistance = prefs.getDouble('map_gps_update_distance') ?? 10.0;
_gpsMinDistance = prefs.getDouble('map_gps_min_distance') ?? 5.0;
_gpsMaxDistance = prefs.getDouble('map_gps_max_distance') ?? 100.0;
_minTimeIntervalSeconds = prefs.getInt('map_gps_min_time_interval') ?? 30;
_backgroundTrackingEnabled =
prefs.getBool('background_tracking_enabled') ?? false;
});
@@ -80,6 +87,9 @@ class _SettingsScreenState extends State<SettingsScreen> {
Future<void> _saveLocationSettings() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setDouble('map_gps_update_distance', _gpsUpdateDistance);
await prefs.setDouble('map_gps_min_distance', _gpsMinDistance);
await prefs.setDouble('map_gps_max_distance', _gpsMaxDistance);
await prefs.setInt('map_gps_min_time_interval', _minTimeIntervalSeconds);
await prefs.setBool(
'background_tracking_enabled',
_backgroundTrackingEnabled,
@@ -215,6 +225,69 @@ class _SettingsScreenState extends State<SettingsScreen> {
await _backgroundLocationService.stopTracking();
}
Future<void> _sendLocationUpdateNow() async {
setState(() => _isSendingLocationUpdate = true);
try {
// Get current location
Position position = await Geolocator.getCurrentPosition(
locationSettings: const LocationSettings(
accuracy: LocationAccuracy.best,
),
);
if (!mounted) return;
// Get connection provider
final appProvider = context.read<AppProvider>();
final connectionProvider = appProvider.connectionProvider;
if (!connectionProvider.deviceInfo.isConnected) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Not connected to device'),
backgroundColor: Colors.orange,
),
);
}
return;
}
// Update device location
await connectionProvider.setAdvertLatLon(
latitude: position.latitude,
longitude: position.longitude,
);
// Send advertisement
await connectionProvider.sendSelfAdvert(floodMode: true);
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'Location broadcast: ${position.latitude.toStringAsFixed(5)}, ${position.longitude.toStringAsFixed(5)}',
),
backgroundColor: Colors.green,
),
);
} catch (e) {
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Failed to send location: $e'),
backgroundColor: Colors.red,
),
);
} finally {
if (mounted) {
setState(() => _isSendingLocationUpdate = false);
}
}
}
Future<void> _clearSampleData() async {
final confirmed = await showDialog<bool>(
context: context,
@@ -279,18 +352,37 @@ class _SettingsScreenState extends State<SettingsScreen> {
const Divider(),
// Location Settings Section
_buildSectionHeader('Location'),
ListTile(
leading: const Icon(Icons.gps_fixed),
title: const Text('GPS Update Distance'),
subtitle: Text('${_gpsUpdateDistance.toStringAsFixed(0)} meters'),
trailing: const Icon(Icons.chevron_right),
onTap: () => _showGpsDistanceDialog(),
_buildSectionHeader('Location Broadcasting'),
// Manual location update button
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: SizedBox(
width: double.infinity,
child: ElevatedButton.icon(
onPressed: _isSendingLocationUpdate ? null : _sendLocationUpdateNow,
icon: _isSendingLocationUpdate
? const SizedBox(
width: 18,
height: 18,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Icon(Icons.my_location),
label: const Text('Broadcast Location Now'),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 12),
),
),
),
),
const Divider(),
// Automatic tracking settings
SwitchListTile(
secondary: const Icon(Icons.location_on),
title: const Text('Background Location Tracking'),
subtitle: const Text('Send position updates to mesh network'),
title: const Text('Auto Location Tracking'),
subtitle: const Text('Automatically broadcast position updates'),
value: _backgroundTrackingEnabled,
onChanged: (value) {
setState(() {
@@ -304,6 +396,17 @@ class _SettingsScreenState extends State<SettingsScreen> {
_saveLocationSettings();
},
),
if (_backgroundTrackingEnabled) ...[
ListTile(
leading: const Icon(Icons.tune),
title: const Text('Configure Tracking'),
subtitle: const Text('Distance and time thresholds'),
trailing: const Icon(Icons.chevron_right),
onTap: () => _showTrackingConfigDialog(),
),
],
const Divider(),
// About Section
@@ -405,44 +508,161 @@ class _SettingsScreenState extends State<SettingsScreen> {
);
}
void _showGpsDistanceDialog() {
double tempDistance = _gpsUpdateDistance;
void _showTrackingConfigDialog() {
double tempMinDistance = _gpsMinDistance;
double tempMaxDistance = _gpsMaxDistance;
int tempTimeInterval = _minTimeIntervalSeconds;
showDialog(
context: context,
builder: (context) => StatefulBuilder(
builder: (context, setDialogState) => AlertDialog(
title: const Text('GPS Update Distance'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
'Position updates sent every ${tempDistance.toStringAsFixed(0)} meters',
style: Theme.of(context).textTheme.bodyMedium,
),
const SizedBox(height: 16),
Slider(
value: tempDistance,
min: 1,
max: 100,
divisions: 99,
label: '${tempDistance.toStringAsFixed(0)}m',
onChanged: (value) {
setDialogState(() {
tempDistance = value;
});
},
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text('1m', style: Theme.of(context).textTheme.bodySmall),
Text('100m', style: Theme.of(context).textTheme.bodySmall),
],
title: const Text('Location Tracking Configuration'),
content: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Description
Text(
'Configure when location broadcasts are sent to the mesh network',
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: Colors.grey,
),
),
),
],
const SizedBox(height: 24),
// Minimum Distance
Text(
'Minimum Distance',
style: Theme.of(context).textTheme.titleSmall?.copyWith(
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
Text(
'Broadcast only after moving ${tempMinDistance.toStringAsFixed(0)} meters',
style: Theme.of(context).textTheme.bodyMedium,
),
const SizedBox(height: 8),
SliderTheme(
data: SliderTheme.of(context).copyWith(
showValueIndicator: ShowValueIndicator.always,
),
child: Slider(
value: tempMinDistance,
min: 1,
max: 50,
divisions: 49,
label: '${tempMinDistance.toStringAsFixed(0)}m',
onChanged: (value) {
setDialogState(() {
tempMinDistance = value;
// Ensure max is always >= min
if (tempMaxDistance < value) {
tempMaxDistance = value;
}
});
},
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text('1m', style: Theme.of(context).textTheme.bodySmall),
Text('50m', style: Theme.of(context).textTheme.bodySmall),
],
),
),
const SizedBox(height: 24),
// Maximum Distance
Text(
'Maximum Distance',
style: Theme.of(context).textTheme.titleSmall?.copyWith(
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
Text(
'Always broadcast after moving ${tempMaxDistance.toStringAsFixed(0)} meters',
style: Theme.of(context).textTheme.bodyMedium,
),
const SizedBox(height: 8),
SliderTheme(
data: SliderTheme.of(context).copyWith(
showValueIndicator: ShowValueIndicator.always,
),
child: Slider(
value: tempMaxDistance,
min: tempMinDistance,
max: 500,
divisions: (500 - tempMinDistance).toInt(),
label: '${tempMaxDistance.toStringAsFixed(0)}m',
onChanged: (value) {
setDialogState(() {
tempMaxDistance = value;
});
},
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text('${tempMinDistance.toStringAsFixed(0)}m',
style: Theme.of(context).textTheme.bodySmall),
Text('500m', style: Theme.of(context).textTheme.bodySmall),
],
),
),
const SizedBox(height: 24),
// Minimum Time Interval
Text(
'Minimum Time Interval',
style: Theme.of(context).textTheme.titleSmall?.copyWith(
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
Text(
'Always broadcast every ${_formatDuration(tempTimeInterval)}',
style: Theme.of(context).textTheme.bodyMedium,
),
const SizedBox(height: 8),
SliderTheme(
data: SliderTheme.of(context).copyWith(
showValueIndicator: ShowValueIndicator.always,
),
child: Slider(
value: tempTimeInterval.toDouble(),
min: 10,
max: 600, // 10 minutes
divisions: 59,
label: _formatDuration(tempTimeInterval),
onChanged: (value) {
setDialogState(() {
tempTimeInterval = value.toInt();
});
},
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text('10s', style: Theme.of(context).textTheme.bodySmall),
Text('10min', style: Theme.of(context).textTheme.bodySmall),
],
),
),
],
),
),
actions: [
TextButton(
@@ -452,14 +672,17 @@ class _SettingsScreenState extends State<SettingsScreen> {
TextButton(
onPressed: () {
setState(() {
_gpsUpdateDistance = tempDistance;
_gpsMinDistance = tempMinDistance;
_gpsMaxDistance = tempMaxDistance;
_minTimeIntervalSeconds = tempTimeInterval;
_gpsUpdateDistance = tempMinDistance; // Use min as the primary threshold
});
_saveLocationSettings();
// Update background tracking if active
if (_backgroundTrackingEnabled) {
_backgroundLocationService.updateDistanceThreshold(
tempDistance,
tempMinDistance,
);
}
@@ -473,6 +696,20 @@ class _SettingsScreenState extends State<SettingsScreen> {
);
}
String _formatDuration(int seconds) {
if (seconds < 60) {
return '${seconds}s';
} else {
final minutes = seconds ~/ 60;
final remainingSeconds = seconds % 60;
if (remainingSeconds == 0) {
return '${minutes}min';
} else {
return '${minutes}min ${remainingSeconds}s';
}
}
}
void _showThemeDialog() {
showDialog(
context: context,