Adding a lot of debug statements and logic to avoid weird states with ws
This commit is contained in:
parent
df1c98e2f3
commit
3cb6efe5bd
@ -63,10 +63,10 @@ class StatusPageState extends State<StatusPage> with WidgetsBindingObserver {
|
||||
}
|
||||
|
||||
void _reconnectWebSocket() {
|
||||
if (channel != null) {
|
||||
channel!.sink.close();
|
||||
if (channel == null || channel!.closeCode != null) {
|
||||
// Check if channel is null or already closed
|
||||
_initializeWebSocket();
|
||||
}
|
||||
_initializeWebSocket();
|
||||
}
|
||||
|
||||
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 {
|
||||
debugPrint("Entering _sendStatus");
|
||||
final prefsProvider =
|
||||
Provider.of<SharedPreferencesProvider>(context, listen: false);
|
||||
|
||||
debugPrint("Is mounted: $mounted");
|
||||
if (!mounted) return;
|
||||
|
||||
debugPrint("Sending status: $status");
|
||||
final isStatusActive = prefsProvider.getCurrentStatus() == status;
|
||||
debugPrint("Is status active: $isStatusActive");
|
||||
final newStatus = isStatusActive ? 'none' : status;
|
||||
|
||||
debugPrint("New status: $newStatus");
|
||||
// Update local status in SharedPreferences
|
||||
await prefsProvider.setCurrentStatus(newStatus);
|
||||
|
||||
debugPrint(prefsProvider.getCurrentStatus());
|
||||
// Create the status message
|
||||
final message = {
|
||||
'Id': id,
|
||||
@ -99,11 +101,14 @@ class StatusPageState extends State<StatusPage> with WidgetsBindingObserver {
|
||||
// Remove existing message from the same user to avoid duplicates
|
||||
messages.removeWhere((msg) => msg['Id'] == id);
|
||||
// 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
|
||||
channel?.sink.add(jsonEncode(message));
|
||||
//channel?.sink.add(jsonEncode(message));
|
||||
|
||||
// Send the status message to the REST API as well
|
||||
final url = Uri.parse('$restBaseUrl/set');
|
||||
@ -163,12 +168,21 @@ class StatusPageState extends State<StatusPage> with WidgetsBindingObserver {
|
||||
final status = message['Status'];
|
||||
final incomingId = message['Id'];
|
||||
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 == 'expired' && incomingId == prefsProvider.getUserId()) {
|
||||
await prefsProvider.setCurrentStatus('none');
|
||||
} else {
|
||||
if (status == 'removed') {
|
||||
if (incomingId == prefsProvider.getUserId() &&
|
||||
prefsProvider.getCurrentStatus() != 'none') {
|
||||
debugPrint("Clearing local message: $status");
|
||||
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);
|
||||
_cacheImage(incomingId, image);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user