Change default status to none, switch to rest from ws
All checks were successful
Build Flutter Web and Docker Image for Local Registry / Build Flutter Web App (push) Successful in 3m13s

This commit is contained in:
whysman 2024-11-11 23:20:39 -05:00
parent 1413ba7263
commit b3a43c9fa5
3 changed files with 52 additions and 13 deletions

View File

@ -21,6 +21,7 @@ class SharedPreferencesProvider extends ChangeNotifier {
List<int> 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<void> setCurrentStatus(String status) async {

View File

@ -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<StatusPage> 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<dynamic>.broadcast();
@ -75,30 +76,66 @@ class StatusPageState extends State<StatusPage> 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<StatusPage> 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);

View File

@ -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.