From b3a43c9fa5f2c0c29d583fab6e9e32c9d38a75b0 Mon Sep 17 00:00:00 2001 From: whysman Date: Mon, 11 Nov 2024 23:20:39 -0500 Subject: [PATCH] Change default status to none, switch to rest from ws --- lib/shared_preferences_provider.dart | 3 +- lib/status_page.dart | 61 ++++++++++++++++++++++------ pubspec.yaml | 1 + 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/lib/shared_preferences_provider.dart b/lib/shared_preferences_provider.dart index 5837c05..3092a71 100644 --- a/lib/shared_preferences_provider.dart +++ b/lib/shared_preferences_provider.dart @@ -21,6 +21,7 @@ class SharedPreferencesProvider extends ChangeNotifier { List imageBytes = bytes.buffer.asUint8List(); prefs.setString('userLogo', base64Encode(imageBytes)); prefs.setString('id', const Uuid().v4()); + prefs.setString('currentStatus', 'none'); } _initCompleter.complete(); notifyListeners(); @@ -53,7 +54,7 @@ class SharedPreferencesProvider extends ChangeNotifier { // New methods to get and set the current status String getCurrentStatus() { - return prefs.getString('currentStatus') ?? ''; + return prefs.getString('currentStatus') ?? 'none'; } Future setCurrentStatus(String status) async { diff --git a/lib/status_page.dart b/lib/status_page.dart index e329ffe..77d5593 100644 --- a/lib/status_page.dart +++ b/lib/status_page.dart @@ -5,6 +5,7 @@ import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:web_socket_channel/web_socket_channel.dart'; import 'package:fluttertoast/fluttertoast.dart'; +import 'package:http/http.dart' as http; import 'shared_preferences_provider.dart'; @@ -33,8 +34,8 @@ class StatusPageState extends State with WidgetsBindingObserver { void _initializeWebSocket() { channel = WebSocketChannel.connect( - Uri.parse('wss://api.pogdark.com:8889/ws'), - //Uri.parse('ws://localhost:8080/ws'), + //Uri.parse('wss://api.pogdark.com:8889/ws'), + Uri.parse('ws://localhost:8080/ws'), ); controller = StreamController.broadcast(); @@ -75,30 +76,66 @@ class StatusPageState extends State with WidgetsBindingObserver { if (!mounted) return; final isStatusActive = prefsProvider.getCurrentStatus() == status; - final newStatus = isStatusActive ? '' : status; + final newStatus = isStatusActive ? 'none' : status; await prefsProvider.setCurrentStatus(newStatus); - final message = jsonEncode({ + final message = { 'Id': id, 'Name': name, 'Image': image, - 'Status': newStatus.isEmpty ? 'expired' : newStatus, + 'Status': newStatus.isEmpty ? 'none' : newStatus, 'Timestamp': DateTime.now().toIso8601String(), - }); - channel!.sink.add(message); + }; + + final url = Uri.parse('http://localhost:8080/set'); + try { + final response = await http.post( + url, + headers: {'Content-Type': 'application/json'}, + body: jsonEncode(message), + ); + + if (!mounted) return; // Check if widget is still in the tree + + if (response.statusCode == 200) { + Fluttertoast.showToast( + msg: 'Status "${newStatus == 'none' ? 'cleared' : newStatus}" sent!', + toastLength: Toast.LENGTH_SHORT, + gravity: ToastGravity.BOTTOM, + backgroundColor: Colors.blueAccent, + webBgColor: "#0000FF", // Only use hex values for web + textColor: Colors.white, + fontSize: 16.0, + timeInSecForIosWeb: 1, + ); + } else { + Fluttertoast.showToast( + msg: 'Failed to send status. Please try again.', + toastLength: Toast.LENGTH_SHORT, + gravity: ToastGravity.BOTTOM, + backgroundColor: Colors.redAccent, + webBgColor: "#FF0000", // Only use hex values for web + textColor: Colors.white, + fontSize: 16.0, + timeInSecForIosWeb: 1, + ); + } + } catch (e) { + if (!mounted) return; - if (mounted) { Fluttertoast.showToast( - msg: 'Status "${newStatus.isEmpty ? 'cleared' : newStatus}" sent!', + msg: 'Error sending status. Please check your connection.', toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM, - backgroundColor: Colors.blueAccent, - webBgColor: Colors.blueAccent.value.toString(), + backgroundColor: Colors.redAccent, + webBgColor: "#FF0000", textColor: Colors.white, fontSize: 16.0, timeInSecForIosWeb: 1, ); + debugPrint( + "Error in _sendStatus: $e"); // Use debugPrint instead of exposing error } } @@ -113,7 +150,7 @@ class StatusPageState extends State with WidgetsBindingObserver { messages.removeWhere((msg) => msg['Id'] == incomingId); if (status == 'expired' && incomingId == prefsProvider.getUserId()) { - await prefsProvider.setCurrentStatus(''); + await prefsProvider.setCurrentStatus('none'); } else { messages.add(message); _cacheImage(incomingId, image); diff --git a/pubspec.yaml b/pubspec.yaml index cbd8975..b391f9c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -37,6 +37,7 @@ dependencies: image_picker: ^1.1.2 uuid: ^4.4.2 fluttertoast: ^8.2.8 + http: ^1.2.2 # The following adds the Cupertino Icons font to your application.