From df1c98e2f37c318154a20c75d6771eb5187e0e3f Mon Sep 17 00:00:00 2001 From: whysman Date: Wed, 13 Nov 2024 23:29:30 -0500 Subject: [PATCH] Added defaulting to local changes and fixed local notify --- lib/shared_preferences_provider.dart | 8 +++++++- lib/status_page.dart | 15 ++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/shared_preferences_provider.dart b/lib/shared_preferences_provider.dart index 3092a71..6fa348d 100644 --- a/lib/shared_preferences_provider.dart +++ b/lib/shared_preferences_provider.dart @@ -59,6 +59,12 @@ class SharedPreferencesProvider extends ChangeNotifier { Future setCurrentStatus(String status) async { await prefs.setString('currentStatus', status); - notifyListeners(); + // Reload and verify the latest status from SharedPreferences + final reloadedStatus = prefs.getString('currentStatus') ?? 'none'; + + // Notify listeners only if the status has been confirmed to update + if (reloadedStatus == status) { + notifyListeners(); + } } } diff --git a/lib/status_page.dart b/lib/status_page.dart index 000951e..c97c216 100644 --- a/lib/status_page.dart +++ b/lib/status_page.dart @@ -82,8 +82,10 @@ class StatusPageState extends State with WidgetsBindingObserver { final isStatusActive = prefsProvider.getCurrentStatus() == status; final newStatus = isStatusActive ? 'none' : status; + // Update local status in SharedPreferences await prefsProvider.setCurrentStatus(newStatus); + // Create the status message final message = { 'Id': id, 'Name': name, @@ -92,7 +94,18 @@ class StatusPageState extends State with WidgetsBindingObserver { 'Timestamp': DateTime.now().toIso8601String(), }; - //final url = Uri.parse('http://localhost:8080/set'); + // Update the local messages list with the new status message + setState(() { + // 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); + }); + + // Send the updated status message to the WebSocket for other users + channel?.sink.add(jsonEncode(message)); + + // Send the status message to the REST API as well final url = Uri.parse('$restBaseUrl/set'); try { final response = await http.post(