Continuing to troubleshoot ws
All checks were successful
Build Flutter Web and Docker Image for Local Registry / Build Flutter Web App (push) Successful in 3m22s

This commit is contained in:
whysman 2024-11-23 14:09:45 -05:00
parent 38c3750778
commit 15f780ead6

View File

@ -50,15 +50,76 @@ class StatusPageState extends State<StatusPage> with WidgetsBindingObserver {
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
_reconnectWebSocket();
debugPrint("App resumed");
updateStatus();
updateMessages();
}
}
void _reconnectWebSocket() {
if (channel.closeCode != null) {
debugPrint("Reinitializing WebSocket connection...");
channel.sink.close();
_initializeWebSocket();
void updateStatus() async {
final url = Uri.parse('$restBaseUrl/get');
final prefsProvider =
Provider.of<SharedPreferencesProvider>(context, listen: false);
try {
debugPrint("Updating status...");
final response = await http.post(
url,
headers: {'Content-Type': 'application/json'},
body: jsonEncode({"Id: $prefsProvider.getUserId()"}),
);
debugPrint(response.statusCode.toString());
if (response.statusCode == 200) {
final jsonResponse = jsonDecode(response.body) as Map<String, dynamic>;
// Extract the 'Status' field or other relevant fields from the response
String newStatus = jsonResponse['Status'] ?? 'none';
debugPrint("Starting status update: $newStatus");
// Store the new status in SharedPreferences
await prefsProvider.setCurrentStatus(newStatus);
debugPrint("Status updated: $newStatus");
} else {
debugPrint("Failed to update status: ${response.statusCode}");
}
} catch (e) {
debugPrint("Error in updateMessages: $e");
}
}
void updateMessages() async {
final url = Uri.parse('$restBaseUrl/update');
try {
final response = await http.get(
url,
headers: {'Content-Type': 'application/json'},
);
debugPrint("Response body: ${response.body}\n");
if (response.statusCode == 200) {
final parsed = jsonDecode(response.body);
// Ensure 'parsed' is a List, otherwise handle gracefully
if (parsed is List<dynamic>) {
debugPrint("Messages to update: ${parsed.length}");
// Ensure each item is a Map<String, dynamic>
final List<Map<String, dynamic>> jsonResponse = parsed
.where((item) => item is Map<String, dynamic>)
.map((item) => item as Map<String, dynamic>)
.toList();
setState(() {
messages.clear(); // Clear the existing messages
messages.addAll(jsonResponse); // Add new messages
});
} else {
debugPrint("API returned unexpected format: ${response.body}");
setState(() {
messages.clear(); // Clear messages if format is invalid
});
}
} else {
debugPrint("Failed to fetch messages: ${response.statusCode}");
}
} catch (e) {
debugPrint("Error in updateMessages: $e");
}
}
@ -167,8 +228,7 @@ class StatusPageState extends State<StatusPage> with WidgetsBindingObserver {
"Incoming id $incomingId, status: $status, prefsID: $prefsId, timestamp: $timeStamp");
if (incomingId == prefsProvider.getUserId()) {
if ((status == 'removed' || status == 'none') &&
prefsProvider.getCurrentStatus() != 'none') {
if (status == 'removed' || status == 'none') {
debugPrint("Clearing local message: $status");
await prefsProvider.setCurrentStatus('none');
} else {
@ -177,6 +237,7 @@ class StatusPageState extends State<StatusPage> with WidgetsBindingObserver {
}
} else {
if (status == 'removed' || status == 'none') {
debugPrint("Checking for messages from user: $incomingId");
if (messages.any((msg) => msg['Id'] == incomingId)) {
debugPrint("Removing message from user: $incomingId");
messages.removeWhere((msg) => msg['Id'] == incomingId);