diff --git a/lib/shared_preferences_provider.dart b/lib/shared_preferences_provider.dart index 6a2ee19..5837c05 100644 --- a/lib/shared_preferences_provider.dart +++ b/lib/shared_preferences_provider.dart @@ -50,4 +50,14 @@ class SharedPreferencesProvider extends ChangeNotifier { await prefs.setString('userLogo', image!); notifyListeners(); } + + // New methods to get and set the current status + String getCurrentStatus() { + return prefs.getString('currentStatus') ?? ''; + } + + Future setCurrentStatus(String status) async { + await prefs.setString('currentStatus', status); + notifyListeners(); + } } diff --git a/lib/status_page.dart b/lib/status_page.dart index 914ca9f..e2b1e2b 100644 --- a/lib/status_page.dart +++ b/lib/status_page.dart @@ -38,18 +38,53 @@ class StatusPageState extends State { return messages.where((message) => message['Status'] == status).toList(); } - void _sendStatus(String id, String name, String? image, String status) { + void _sendStatus(String id, String name, String? image, String status) async { + final prefsProvider = + Provider.of(context, listen: false); + + if (!mounted) return; + + // Check if the status is already active; if so, clear it, otherwise set it + final isStatusActive = prefsProvider.getCurrentStatus() == status; + final newStatus = isStatusActive ? '' : status; + + // Update the status in SharedPreferences + await prefsProvider.setCurrentStatus(newStatus); + + // Send the message to the WebSocket final message = jsonEncode({ 'Id': id, 'Name': name, 'Image': image, - 'Status': status, + 'Status': newStatus.isEmpty ? 'expired' : newStatus, 'Timestamp': DateTime.now().toIso8601String(), }); channel.sink.add(message); - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('Status "$status" sent!')), - ); + + if (mounted) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + 'Status "${newStatus.isEmpty ? 'cleared' : newStatus}" sent!')), + ); + } + } + + void _handleIncomingMessage(Map message) async { + final prefsProvider = + Provider.of(context, listen: false); + + final status = message['Status']; + final incomingId = message['Id']; + + messages.removeWhere((msg) => msg['Id'] == incomingId); + + // Check if the message status is "expired" and update SharedPreferences + if (status == 'expired') { + await prefsProvider.setCurrentStatus(''); + } else { + messages.add(message); + } } Widget _buildMessageItem(Map message) { @@ -94,6 +129,20 @@ class StatusPageState extends State { ); } + // Function to get the background color based on the current status + Color getButtonColor(String buttonStatus) { + final prefsProvider = Provider.of(context); + final currentStatus = prefsProvider.getCurrentStatus(); + return currentStatus == buttonStatus ? Colors.blueAccent : Colors.grey; + } + + // Function to get the text color based on the current status + Color getButtonTextColor(String buttonStatus) { + final prefsProvider = Provider.of(context); + final currentStatus = prefsProvider.getCurrentStatus(); + return currentStatus == buttonStatus ? Colors.white : Colors.black; + } + @override Widget build(BuildContext context) { final prefsProvider = Provider.of(context); @@ -115,13 +164,7 @@ class StatusPageState extends State { if (snapshot.hasData) { final newMessage = jsonDecode(snapshot.data as String) as Map; - - final incomingId = newMessage['Id']; - messages.removeWhere((msg) => msg['Id'] == incomingId); - if (newMessage['Status'] != 'expired' && - newMessage['Status'] != "is leaving") { - messages.add(newMessage); - } + _handleIncomingMessage(newMessage); } final onTheWayMessages = _getMessagesByStatus('is on the way'); @@ -181,19 +224,28 @@ class StatusPageState extends State { mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ ElevatedButton( + style: ElevatedButton.styleFrom( + backgroundColor: getButtonColor('is on the way'), + ), onPressed: () => _sendStatus( userId, userName, userLogo, 'is on the way'), - child: const Text('On the Way'), + child: Text( + 'On the Way', + style: TextStyle( + color: getButtonTextColor('is on the way')), + ), ), ElevatedButton( + style: ElevatedButton.styleFrom( + backgroundColor: getButtonColor('has arrived'), + ), onPressed: () => _sendStatus( userId, userName, userLogo, 'has arrived'), - child: const Text('Arrived'), - ), - ElevatedButton( - onPressed: () => - _sendStatus(userId, userName, userLogo, 'is leaving'), - child: const Text('Leaving'), + child: Text( + 'Arrived', + style: + TextStyle(color: getButtonTextColor('has arrived')), + ), ), IconButton( icon: const Icon(Icons.edit, color: Colors.blueAccent),