Adding a lot of debug statements and logic to avoid weird states with ws

This commit is contained in:
whysman 2024-11-15 11:05:33 -05:00
parent df1c98e2f3
commit 3cb6efe5bd

View File

@ -63,10 +63,10 @@ class StatusPageState extends State<StatusPage> with WidgetsBindingObserver {
} }
void _reconnectWebSocket() { void _reconnectWebSocket() {
if (channel != null) { if (channel == null || channel!.closeCode != null) {
channel!.sink.close(); // Check if channel is null or already closed
_initializeWebSocket();
} }
_initializeWebSocket();
} }
List<Map<String, dynamic>> _getMessagesByStatus(String status) { List<Map<String, dynamic>> _getMessagesByStatus(String status) {
@ -74,17 +74,19 @@ class StatusPageState extends State<StatusPage> with WidgetsBindingObserver {
} }
void _sendStatus(String id, String name, String? image, String status) async { void _sendStatus(String id, String name, String? image, String status) async {
debugPrint("Entering _sendStatus");
final prefsProvider = final prefsProvider =
Provider.of<SharedPreferencesProvider>(context, listen: false); Provider.of<SharedPreferencesProvider>(context, listen: false);
debugPrint("Is mounted: $mounted");
if (!mounted) return; if (!mounted) return;
debugPrint("Sending status: $status");
final isStatusActive = prefsProvider.getCurrentStatus() == status; final isStatusActive = prefsProvider.getCurrentStatus() == status;
debugPrint("Is status active: $isStatusActive");
final newStatus = isStatusActive ? 'none' : status; final newStatus = isStatusActive ? 'none' : status;
debugPrint("New status: $newStatus");
// Update local status in SharedPreferences // Update local status in SharedPreferences
await prefsProvider.setCurrentStatus(newStatus); await prefsProvider.setCurrentStatus(newStatus);
debugPrint(prefsProvider.getCurrentStatus());
// Create the status message // Create the status message
final message = { final message = {
'Id': id, 'Id': id,
@ -99,11 +101,14 @@ class StatusPageState extends State<StatusPage> with WidgetsBindingObserver {
// Remove existing message from the same user to avoid duplicates // Remove existing message from the same user to avoid duplicates
messages.removeWhere((msg) => msg['Id'] == id); messages.removeWhere((msg) => msg['Id'] == id);
// Add the updated message to the local messages list // Add the updated message to the local messages list
messages.add(message); if (newStatus != 'none') {
messages.add(message);
debugPrint("Adding local message: $newStatus");
}
}); });
// Send the updated status message to the WebSocket for other users // Send the updated status message to the WebSocket for other users
channel?.sink.add(jsonEncode(message)); //channel?.sink.add(jsonEncode(message));
// Send the status message to the REST API as well // Send the status message to the REST API as well
final url = Uri.parse('$restBaseUrl/set'); final url = Uri.parse('$restBaseUrl/set');
@ -163,12 +168,21 @@ class StatusPageState extends State<StatusPage> with WidgetsBindingObserver {
final status = message['Status']; final status = message['Status'];
final incomingId = message['Id']; final incomingId = message['Id'];
final image = message['Image']; final image = message['Image'];
final timeStamp = message['Timestamp'];
final prefsId = prefsProvider.getUserId();
debugPrint(
"Incoming id $incomingId, status: $status, prefsID: $prefsId, timestamp: $timeStamp");
messages.removeWhere((msg) => msg['Id'] == incomingId); if (status == 'removed') {
if (incomingId == prefsProvider.getUserId() &&
if (status == 'expired' && incomingId == prefsProvider.getUserId()) { prefsProvider.getCurrentStatus() != 'none') {
await prefsProvider.setCurrentStatus('none'); debugPrint("Clearing local message: $status");
} else { await prefsProvider.setCurrentStatus('none');
} else if (messages.any((msg) => msg['Id'] == incomingId)) {
messages.removeWhere((msg) => msg['Id'] == incomingId);
}
} else if (incomingId != prefsId) {
debugPrint("Adding incoming message: $status");
messages.add(message); messages.add(message);
_cacheImage(incomingId, image); _cacheImage(incomingId, image);
} }