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
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:
parent
38c3750778
commit
15f780ead6
@ -50,15 +50,76 @@ class StatusPageState extends State<StatusPage> with WidgetsBindingObserver {
|
|||||||
@override
|
@override
|
||||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||||
if (state == AppLifecycleState.resumed) {
|
if (state == AppLifecycleState.resumed) {
|
||||||
_reconnectWebSocket();
|
debugPrint("App resumed");
|
||||||
|
updateStatus();
|
||||||
|
updateMessages();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _reconnectWebSocket() {
|
void updateStatus() async {
|
||||||
if (channel.closeCode != null) {
|
final url = Uri.parse('$restBaseUrl/get');
|
||||||
debugPrint("Reinitializing WebSocket connection...");
|
final prefsProvider =
|
||||||
channel.sink.close();
|
Provider.of<SharedPreferencesProvider>(context, listen: false);
|
||||||
_initializeWebSocket();
|
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");
|
"Incoming id $incomingId, status: $status, prefsID: $prefsId, timestamp: $timeStamp");
|
||||||
|
|
||||||
if (incomingId == prefsProvider.getUserId()) {
|
if (incomingId == prefsProvider.getUserId()) {
|
||||||
if ((status == 'removed' || status == 'none') &&
|
if (status == 'removed' || status == 'none') {
|
||||||
prefsProvider.getCurrentStatus() != 'none') {
|
|
||||||
debugPrint("Clearing local message: $status");
|
debugPrint("Clearing local message: $status");
|
||||||
await prefsProvider.setCurrentStatus('none');
|
await prefsProvider.setCurrentStatus('none');
|
||||||
} else {
|
} else {
|
||||||
@ -177,6 +237,7 @@ class StatusPageState extends State<StatusPage> with WidgetsBindingObserver {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (status == 'removed' || status == 'none') {
|
if (status == 'removed' || status == 'none') {
|
||||||
|
debugPrint("Checking for messages from user: $incomingId");
|
||||||
if (messages.any((msg) => msg['Id'] == incomingId)) {
|
if (messages.any((msg) => msg['Id'] == incomingId)) {
|
||||||
debugPrint("Removing message from user: $incomingId");
|
debugPrint("Removing message from user: $incomingId");
|
||||||
messages.removeWhere((msg) => msg['Id'] == incomingId);
|
messages.removeWhere((msg) => msg['Id'] == incomingId);
|
||||||
|
Loading…
Reference in New Issue
Block a user