update multi chat,fix provider

This commit is contained in:
csf
2022-03-25 16:34:27 +08:00
parent 4d64fee76d
commit 2ea9d80be6
7 changed files with 190 additions and 119 deletions

View File

@@ -17,14 +17,24 @@ class ChatModel with ChangeNotifier {
final ChatUser me = ChatUser(
uid:"",
name: "me",
customProperties: Map()..["id"] = clientModeID
);
final _scroller = ScrollController();
var _currentID = clientModeID;
get messages => _messages;
ScrollController get scroller => _scroller;
get currentID => _currentID;
Map<int, List<ChatMessage>> get messages => _messages;
int get currentID => _currentID;
changeCurrentID(int id){
if(_messages.containsKey(id)){
_currentID = id;
notifyListeners();
}
}
receive(int id, String text) {
if (text.isEmpty) return;
@@ -32,18 +42,36 @@ class ChatModel with ChangeNotifier {
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] = [];
}
// TODO peer info
_messages[id]?.add(ChatMessage(
_messages[id]!.add(ChatMessage(
text: text,
user: ChatUser(
name: FFI.ffiModel.pi.username,
uid: FFI.getId(),
)));
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) {
@@ -59,13 +87,12 @@ class ChatModel with ChangeNotifier {
}
}
notifyListeners();
scrollToBottom();
}
release() {
close() {
hideChatIconOverlay();
hideChatWindowOverlay();
_messages.forEach((key, value) => value.clear());
_messages.clear();
notifyListeners();
}
}

View File

@@ -722,7 +722,7 @@ class FFI {
}
static void close() {
chatModel.release();
chatModel.close();
if (FFI.imageModel.image != null && !isDesktop) {
savePreference(id, cursorModel.x, cursorModel.y, canvasModel.x,
canvasModel.y, canvasModel.scale, ffiModel.pi.currentDisplay);

View File

@@ -1,5 +1,6 @@
import 'dart:async';
import 'dart:convert';
import 'package:dash_chat/dash_chat.dart';
import 'package:flutter/material.dart';
import '../common.dart';
import '../pages/server_page.dart';
@@ -17,7 +18,7 @@ class ServerModel with ChangeNotifier {
final _serverId = TextEditingController(text: _emptyIdShow);
final _serverPasswd = TextEditingController(text: "");
List<Client> _clients = [];
Map<int,Client> _clients = {};
bool get isStart => _isStart;
@@ -33,7 +34,7 @@ class ServerModel with ChangeNotifier {
TextEditingController get serverPasswd => _serverPasswd;
List<Client> get clients => _clients;
Map<int,Client> get clients => _clients;
ServerModel() {
()async{
@@ -191,9 +192,11 @@ class ServerModel with ChangeNotifier {
var res = FFI.getByName("clients_state");
try {
final List clientsJson = jsonDecode(res);
_clients = clientsJson
.map((clientJson) => Client.fromJson(jsonDecode(res)))
.toList();
for (var clientJson in clientsJson){
final client = Client.fromJson(jsonDecode(clientJson));
_clients[client.id] = client;
}
notifyListeners();
} catch (e) {}
}
@@ -204,7 +207,7 @@ class ServerModel with ChangeNotifier {
final Map<String, dynamic> response = Map();
response["id"] = client.id;
DialogManager.show((setState, close) => CustomAlertDialog(
title: Text(client.isFileTransfer?"File":"Screen" + "Control Request"),
title: Text(translate(client.isFileTransfer?"File Connection":"Screen Connection")),
content: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
@@ -234,7 +237,7 @@ class ServerModel with ChangeNotifier {
"start_capture"); // to Android service
debugPrint("_toAndroidStartCapture:$res");
}
_clients.add(client);
_clients[client.id] = client;
notifyListeners();
close();
}),
@@ -246,7 +249,8 @@ class ServerModel with ChangeNotifier {
void onClientAuthorized(Map<String, dynamic> evt) {
try{
_clients.add(Client.fromJson(jsonDecode(evt['client'])));
final client = Client.fromJson(jsonDecode(evt['client']));
_clients[client.id] = client;
notifyListeners();
}catch(e){
@@ -256,8 +260,7 @@ class ServerModel with ChangeNotifier {
void onClientRemove(Map<String, dynamic> evt) {
try {
final id = int.parse(evt['id'] as String);
Client client = _clients.singleWhere((c) => c.id == id);
_clients.remove(client);
_clients.remove(id);
notifyListeners();
} catch (e) {
// singleWhere fail ,reset the login dialog
@@ -267,10 +270,10 @@ class ServerModel with ChangeNotifier {
}
closeAll() {
_clients.forEach((client) {
FFI.setByName("close_conn", client.id.toString());
_clients.forEach((id,client) {
FFI.setByName("close_conn", id.toString());
});
_clients = [];
_clients.clear();
}
}
@@ -283,6 +286,7 @@ class Client {
bool keyboard = false;
bool clipboard = false;
bool audio = false;
late ChatUser chatUser;
Client(this.authorized, this.isFileTransfer, this.name, this.peerId,this.keyboard,this.clipboard,this.audio);
@@ -295,6 +299,10 @@ class Client {
keyboard= json['keyboard'];
clipboard= json['clipboard'];
audio= json['audio'];
chatUser = ChatUser(
uid:peerId,
name: name,
);
}
Map<String, dynamic> toJson() {