rustdesk/lib/models/chat_model.dart

110 lines
2.8 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:convert';
import 'package:dash_chat/dash_chat.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hbb/pages/chat_page.dart';
import 'model.dart';
class ChatModel with ChangeNotifier {
// -1作为客户端模式的id客户端模式下此id唯一
// 其它正整数的id来自被控服务器模式下的其他客户端的id每个客户端有不同的id
// 注意 此id和peer_id不同服务端模式下的id等同于conn的顺序累加id
static final clientModeID = -1;
final Map<int, List<ChatMessage>> _messages = Map()..[clientModeID] = [];
final ChatUser me = ChatUser(
uid:"",
name: "me",
);
final _scroller = ScrollController();
var _currentID = clientModeID;
ScrollController get scroller => _scroller;
Map<int, List<ChatMessage>> get messages => _messages;
int get currentID => _currentID;
ChatUser get currentUser =>
FFI.serverModel.clients[_currentID]?.chatUser ?? me;
changeCurrentID(int id){
if(_messages.containsKey(id)){
_currentID = id;
notifyListeners();
} else {
final chatUser = FFI.serverModel.clients[id]?.chatUser;
if(chatUser == null){
return debugPrint("Failed to changeCurrentID,remote user doesn't exist");
}
_messages[id] = [];
_currentID = id;
}
}
receive(int id, String text) {
if (text.isEmpty) return;
// first message show overlay icon
if (iconOverlayEntry == null) {
showChatIconOverlay();
}
late final chatUser;
if(id == clientModeID){
chatUser = ChatUser(
name: FFI.ffiModel.pi.username,
uid: FFI.getId(),
);
}else{
chatUser = FFI.serverModel.clients[id]?.chatUser;
}
if(chatUser == null){
return debugPrint("Failed to receive msg,user doesn't exist");
}
if(!_messages.containsKey(id)){
_messages[id] = [];
}
_messages[id]!.add(ChatMessage(
text: text,
user: chatUser));
_currentID = id;
notifyListeners();
scrollToBottom();
}
scrollToBottom(){
Future.delayed(Duration(milliseconds: 500), () {
_scroller.animateTo(
_scroller.position.maxScrollExtent,
duration: Duration(milliseconds: 200),
curve: Curves.fastLinearToSlowEaseIn);
});
}
send(ChatMessage message) {
if (message.text != null && message.text!.isNotEmpty) {
_messages[_currentID]?.add(message);
if (_currentID == clientModeID) {
FFI.setByName("chat_client_mode", message.text!);
} else {
final msg = Map()
..["id"] = _currentID
..["text"] = message.text!;
FFI.setByName("chat_server_mode", jsonEncode(msg));
}
}
notifyListeners();
scrollToBottom();
}
close() {
hideChatIconOverlay();
hideChatWindowOverlay();
notifyListeners();
}
}