rustdesk/flutter/lib/models/chat_model.dart

124 lines
2.8 KiB
Dart
Raw Normal View History

import 'dart:convert';
import 'package:dash_chat_2/dash_chat_2.dart';
import 'package:flutter/material.dart';
import '../widgets/overlay.dart';
import 'model.dart';
2022-05-16 00:01:27 +08:00
class MessageBody {
ChatUser chatUser;
List<ChatMessage> chatMessages;
MessageBody(this.chatUser, this.chatMessages);
void insert(ChatMessage cm) {
this.chatMessages.insert(0, cm);
2022-05-16 00:01:27 +08:00
}
void clear() {
this.chatMessages.clear();
}
}
class ChatModel with ChangeNotifier {
static final clientModeID = -1;
final ChatUser me = ChatUser(
id: "",
firstName: "Me",
);
2022-05-16 00:01:27 +08:00
late final Map<int, MessageBody> _messages = Map()
..[clientModeID] = MessageBody(me, []);
var _currentID = clientModeID;
2022-05-16 00:01:27 +08:00
Map<int, MessageBody> get messages => _messages;
2022-03-25 16:34:27 +08:00
int get currentID => _currentID;
2022-05-16 00:01:27 +08:00
ChatUser get currentUser {
final user = messages[currentID]?.chatUser;
if (user == null) {
_currentID = clientModeID;
return me;
} else {
return user;
}
}
2022-04-05 00:51:47 +08:00
changeCurrentID(int id) {
if (_messages.containsKey(id)) {
2022-03-25 16:34:27 +08:00
_currentID = id;
notifyListeners();
} else {
2022-05-16 00:01:27 +08:00
final client = FFI.serverModel.clients[id];
if (client == null) {
2022-04-05 00:51:47 +08:00
return debugPrint(
"Failed to changeCurrentID,remote user doesn't exist");
}
2022-05-16 00:01:27 +08:00
final chatUser = ChatUser(
id: client.peerId,
firstName: client.name,
2022-05-16 00:01:27 +08:00
);
_messages[id] = MessageBody(chatUser, []);
_currentID = id;
2022-05-16 00:01:27 +08:00
notifyListeners();
2022-03-25 16:34:27 +08:00
}
}
receive(int id, String text) {
if (text.isEmpty) return;
// first message show overlay icon
if (chatIconOverlayEntry == null) {
showChatIconOverlay();
}
2022-03-25 16:34:27 +08:00
late final chatUser;
2022-04-05 00:51:47 +08:00
if (id == clientModeID) {
2022-03-25 16:34:27 +08:00
chatUser = ChatUser(
firstName: FFI.ffiModel.pi.username,
id: FFI.getId(),
2022-03-25 16:34:27 +08:00
);
2022-04-05 00:51:47 +08:00
} else {
2022-05-16 00:01:27 +08:00
final client = FFI.serverModel.clients[id];
if (client == null) {
return debugPrint("Failed to receive msg,user doesn't exist");
}
chatUser = ChatUser(id: client.peerId, firstName: client.name);
2022-03-25 16:34:27 +08:00
}
2022-05-16 00:01:27 +08:00
2022-04-05 00:51:47 +08:00
if (!_messages.containsKey(id)) {
2022-05-16 00:01:27 +08:00
_messages[id] = MessageBody(chatUser, []);
}
_messages[id]!.insert(
ChatMessage(text: text, user: chatUser, createdAt: DateTime.now()));
_currentID = id;
notifyListeners();
}
send(ChatMessage message) {
if (message.text.isNotEmpty) {
_messages[_currentID]?.insert(message);
if (_currentID == clientModeID) {
FFI.setByName("chat_client_mode", message.text);
} else {
final msg = Map()
..["id"] = _currentID
..["text"] = message.text;
2022-03-24 17:58:33 +08:00
FFI.setByName("chat_server_mode", jsonEncode(msg));
}
}
notifyListeners();
}
2022-03-25 16:34:27 +08:00
close() {
hideChatIconOverlay();
hideChatWindowOverlay();
notifyListeners();
}
2022-05-16 00:01:27 +08:00
resetClientMode() {
_messages[clientModeID]?.clear();
}
}