added status to sharedprefs, refactored buttons and added button functionality
All checks were successful
Build Flutter Web and Docker Image for Local Registry / Build Flutter Web App (push) Successful in 3m6s

This commit is contained in:
whysman 2024-11-10 00:40:21 -05:00
parent 89c1134a85
commit ed345b1fa9
2 changed files with 81 additions and 19 deletions

View File

@ -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<void> setCurrentStatus(String status) async {
await prefs.setString('currentStatus', status);
notifyListeners();
}
}

View File

@ -38,18 +38,53 @@ class StatusPageState extends State<StatusPage> {
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<SharedPreferencesProvider>(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<String, dynamic> message) async {
final prefsProvider =
Provider.of<SharedPreferencesProvider>(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<String, dynamic> message) {
@ -94,6 +129,20 @@ class StatusPageState extends State<StatusPage> {
);
}
// Function to get the background color based on the current status
Color getButtonColor(String buttonStatus) {
final prefsProvider = Provider.of<SharedPreferencesProvider>(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<SharedPreferencesProvider>(context);
final currentStatus = prefsProvider.getCurrentStatus();
return currentStatus == buttonStatus ? Colors.white : Colors.black;
}
@override
Widget build(BuildContext context) {
final prefsProvider = Provider.of<SharedPreferencesProvider>(context);
@ -115,13 +164,7 @@ class StatusPageState extends State<StatusPage> {
if (snapshot.hasData) {
final newMessage =
jsonDecode(snapshot.data as String) as Map<String, dynamic>;
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<StatusPage> {
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),