Advanced chat management via UI
This commit is contained in:
1 parent
fbee31841d
commit
5351144edf
9 files changed
+396
-59
No files matched your search
@@ -3,6 +3,13 @@ import 'dart:html';
|
||||
import 'dart:convert';
|
||||
import 'dart:async'; // Import for handling Completers
|
||||
|
||||
class ChatInfo {
|
||||
int id;
|
||||
String name;
|
||||
|
||||
ChatInfo({required this.id, required this.name});
|
||||
}
|
||||
|
||||
class Backend {
|
||||
static WebSocket? _socket;
|
||||
static String? _url;
|
||||
@@ -10,6 +17,9 @@ class Backend {
|
||||
static Map<int, StreamController<Map<String, dynamic>>> _chatControllers = {};
|
||||
static int _nextNonce = 0; // Tracking nonce for each action
|
||||
static StreamController<Map<String, dynamic>> _packetStreamController = StreamController.broadcast();
|
||||
|
||||
// Cached Data
|
||||
static List<ChatInfo>? _chats;
|
||||
|
||||
// Authentication
|
||||
static ({String username, String password})? _credentials;
|
||||
@@ -36,6 +46,8 @@ class Backend {
|
||||
_socket = WebSocket(_url!);
|
||||
_socket!.onOpen.listen((event) {
|
||||
print('Connected to the WebSocket server');
|
||||
Backend.invalidateCache(chat: true);
|
||||
|
||||
print('Logging in...');
|
||||
if (_credentials != null) {
|
||||
_socket!.send(json.encode({'username': _credentials!.username, 'password': _credentials!.password}));
|
||||
@@ -128,6 +140,33 @@ class Backend {
|
||||
}
|
||||
}
|
||||
|
||||
static Future<List<ChatInfo>> getChats () async {
|
||||
if (_chats == null) {
|
||||
final response = await Backend.sendAction("get-chats", {});
|
||||
List<dynamic> chatObjects = response['chats'];
|
||||
_chats = chatObjects
|
||||
.map((msg) => ChatInfo(
|
||||
id: msg['id'],
|
||||
name: msg['name']?.isNotEmpty == true ? msg['name'] : '[Chat ${msg['id']}]',
|
||||
))
|
||||
.toList();
|
||||
}
|
||||
return _chats!;
|
||||
}
|
||||
|
||||
static Future<ChatInfo?> getChat (int chatId) async {
|
||||
final chats = await getChats();
|
||||
try {
|
||||
return chats.firstWhere((chat) => chat.id == chatId);
|
||||
} on StateError {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static void invalidateCache ({bool chat = false}) {
|
||||
if (chat) { _chats = null; }
|
||||
}
|
||||
|
||||
static void ping() {
|
||||
if (_socket!= null && _socket!.readyState == WebSocket.OPEN) {
|
||||
final data = json.encode({'path': 'ping'});
|
||||
|
||||
Reference in new issue
Block a user