fix chat model

This commit is contained in:
rustdesk 2022-05-16 00:01:27 +08:00
parent 0b869e89a3
commit c3bc539a61
7 changed files with 91 additions and 57 deletions

View File

@ -6,41 +6,68 @@ import 'package:flutter/material.dart';
import '../widgets/overlay.dart'; import '../widgets/overlay.dart';
import 'model.dart'; import 'model.dart';
class MessageBody {
ChatUser chatUser;
List<ChatMessage> chatMessages;
MessageBody(this.chatUser, this.chatMessages);
void add(ChatMessage cm) {
this.chatMessages.add(cm);
}
void clear() {
this.chatMessages.clear();
}
}
class ChatModel with ChangeNotifier { class ChatModel with ChangeNotifier {
static final clientModeID = -1; static final clientModeID = -1;
final Map<int, List<ChatMessage>> _messages = Map()..[clientModeID] = [];
final ChatUser me = ChatUser( final ChatUser me = ChatUser(
uid: "", uid: "",
name: "Me", name: "Me",
); );
late final Map<int, MessageBody> _messages = Map()
..[clientModeID] = MessageBody(me, []);
final _scroller = ScrollController(); final _scroller = ScrollController();
var _currentID = clientModeID; var _currentID = clientModeID;
ScrollController get scroller => _scroller; ScrollController get scroller => _scroller;
Map<int, List<ChatMessage>> get messages => _messages; Map<int, MessageBody> get messages => _messages;
int get currentID => _currentID; int get currentID => _currentID;
ChatUser get currentUser => ChatUser get currentUser {
FFI.serverModel.clients[_currentID]?.chatUser ?? me; final user = messages[currentID]?.chatUser;
if (user == null) {
_currentID = clientModeID;
return me;
} else {
return user;
}
}
changeCurrentID(int id) { changeCurrentID(int id) {
if (_messages.containsKey(id)) { if (_messages.containsKey(id)) {
_currentID = id; _currentID = id;
notifyListeners(); notifyListeners();
} else { } else {
final chatUser = FFI.serverModel.clients[id]?.chatUser; final client = FFI.serverModel.clients[id];
if (chatUser == null) { if (client == null) {
return debugPrint( return debugPrint(
"Failed to changeCurrentID,remote user doesn't exist"); "Failed to changeCurrentID,remote user doesn't exist");
} }
_messages[id] = []; final chatUser = ChatUser(
uid: client.peerId,
name: client.name,
);
_messages[id] = MessageBody(chatUser, []);
_currentID = id; _currentID = id;
notifyListeners();
} }
} }
@ -57,13 +84,15 @@ class ChatModel with ChangeNotifier {
uid: FFI.getId(), uid: FFI.getId(),
); );
} else { } else {
chatUser = FFI.serverModel.clients[id]?.chatUser; final client = FFI.serverModel.clients[id];
} if (client == null) {
if (chatUser == null) {
return debugPrint("Failed to receive msg,user doesn't exist"); return debugPrint("Failed to receive msg,user doesn't exist");
} }
chatUser = ChatUser(uid: client.peerId, name: client.name);
}
if (!_messages.containsKey(id)) { if (!_messages.containsKey(id)) {
_messages[id] = []; _messages[id] = MessageBody(chatUser, []);
} }
_messages[id]!.add(ChatMessage(text: text, user: chatUser)); _messages[id]!.add(ChatMessage(text: text, user: chatUser));
_currentID = id; _currentID = id;
@ -100,4 +129,8 @@ class ChatModel with ChangeNotifier {
hideChatWindowOverlay(); hideChatWindowOverlay();
notifyListeners(); notifyListeners();
} }
resetClientMode() {
_messages[clientModeID]?.clear();
}
} }

View File

@ -221,8 +221,8 @@ class FfiModel with ChangeNotifier {
void showMsgBox(String type, String title, String text, bool hasRetry) { void showMsgBox(String type, String title, String text, bool hasRetry) {
msgBox(type, title, text); msgBox(type, title, text);
if (hasRetry) {
_timer?.cancel(); _timer?.cancel();
if (hasRetry) {
_timer = Timer(Duration(seconds: _reconnects), () { _timer = Timer(Duration(seconds: _reconnects), () {
FFI.reconnect(); FFI.reconnect();
showLoading(translate('Connecting...')); showLoading(translate('Connecting...'));
@ -245,7 +245,7 @@ class FfiModel with ChangeNotifier {
if (isPeerAndroid) { if (isPeerAndroid) {
_touchMode = true; _touchMode = true;
if (FFI.ffiModel.permissions['keyboard'] != false) { if (FFI.ffiModel.permissions['keyboard'] != false) {
showMobileActionsOverlay(); Timer(Duration(milliseconds: 100), showMobileActionsOverlay);
} }
} else { } else {
_touchMode = FFI.getByName('peer_option', "touch-mode") != ''; _touchMode = FFI.getByName('peer_option', "touch-mode") != '';
@ -757,6 +757,7 @@ class FFI {
if (isFileTransfer) { if (isFileTransfer) {
setByName('connect_file_transfer', id); setByName('connect_file_transfer', id);
} else { } else {
FFI.chatModel.resetClientMode();
setByName('connect', id); setByName('connect', id);
} }
FFI.id = id; FFI.id = id;

View File

@ -448,7 +448,6 @@ class Client {
bool keyboard = false; bool keyboard = false;
bool clipboard = false; bool clipboard = false;
bool audio = false; bool audio = false;
late ChatUser chatUser;
Client(this.authorized, this.isFileTransfer, this.name, this.peerId, Client(this.authorized, this.isFileTransfer, this.name, this.peerId,
this.keyboard, this.clipboard, this.audio); this.keyboard, this.clipboard, this.audio);
@ -462,10 +461,6 @@ class Client {
keyboard = json['keyboard']; keyboard = json['keyboard'];
clipboard = json['clipboard']; clipboard = json['clipboard'];
audio = json['audio']; audio = json['audio'];
chatUser = ChatUser(
uid: peerId,
name: name,
);
} }
Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {

View File

@ -21,10 +21,9 @@ class ChatPage extends StatelessWidget implements PageShape {
icon: Icon(Icons.group), icon: Icon(Icons.group),
itemBuilder: (context) { itemBuilder: (context) {
final chatModel = FFI.chatModel; final chatModel = FFI.chatModel;
final serverModel = FFI.serverModel;
return chatModel.messages.entries.map((entry) { return chatModel.messages.entries.map((entry) {
final id = entry.key; final id = entry.key;
final user = serverModel.clients[id]?.chatUser ?? chatModel.me; final user = entry.value.chatUser;
return PopupMenuItem<int>( return PopupMenuItem<int>(
child: Text("${user.name} ${user.uid}"), child: Text("${user.name} ${user.uid}"),
value: id, value: id,
@ -54,7 +53,9 @@ class ChatPage extends StatelessWidget implements PageShape {
chatModel.send(chatMsg); chatModel.send(chatMsg);
}, },
user: chatModel.me, user: chatModel.me,
messages: chatModel.messages[chatModel.currentID] ?? [], messages:
chatModel.messages[chatModel.currentID]?.chatMessages ??
[],
// default scrollToBottom has bug https://github.com/fayeed/dash_chat/issues/53 // default scrollToBottom has bug https://github.com/fayeed/dash_chat/issues/53
scrollToBottom: false, scrollToBottom: false,
scrollController: chatModel.scroller, scrollController: chatModel.scroller,

View File

@ -91,7 +91,9 @@ class _RemotePageState extends State<RemotePage> {
if (v < 100) { if (v < 100) {
SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
overlays: []); overlays: []);
if (chatWindowOverlayEntry == null) { // [pi.version.isNotEmpty] -> check ready or not,avoid login without soft-keyboard
if (chatWindowOverlayEntry == null &&
FFI.ffiModel.pi.version.isNotEmpty) {
FFI.invokeMethod("enable_soft_keyboard", false); FFI.invokeMethod("enable_soft_keyboard", false);
} }
} }
@ -397,14 +399,9 @@ class _RemotePageState extends State<RemotePage> {
] + ] +
(isDesktop (isDesktop
? [] ? []
: [ : FFI.ffiModel.isPeerAndroid
? [
IconButton( IconButton(
color: Colors.white,
icon: Icon(Icons.keyboard),
onPressed: openKeyboard),
FFI.ffiModel.isPeerAndroid
? (keyboard
? IconButton(
color: Colors.white, color: Colors.white,
icon: Icon(Icons.build), icon: Icon(Icons.build),
onPressed: () { onPressed: () {
@ -415,14 +412,19 @@ class _RemotePageState extends State<RemotePage> {
} }
}, },
) )
: SizedBox.shrink()) ]
: IconButton( : [
IconButton(
color: Colors.white,
icon: Icon(Icons.keyboard),
onPressed: openKeyboard),
IconButton(
color: Colors.white, color: Colors.white,
icon: Icon(FFI.ffiModel.touchMode icon: Icon(FFI.ffiModel.touchMode
? Icons.touch_app ? Icons.touch_app
: Icons.mouse), : Icons.mouse),
onPressed: changeTouchMode, onPressed: changeTouchMode,
) ),
]) + ]) +
(isWeb (isWeb
? [] ? []

View File

@ -97,6 +97,12 @@ showChatIconOverlay({Offset offset = const Offset(200, 50)}) {
} }
if (globalKey.currentState == null || globalKey.currentState!.overlay == null) if (globalKey.currentState == null || globalKey.currentState!.overlay == null)
return; return;
final bar = navigationBarKey.currentWidget;
if (bar != null) {
if ((bar as BottomNavigationBar).currentIndex == 1) {
return;
}
}
final globalOverlayState = globalKey.currentState!.overlay!; final globalOverlayState = globalKey.currentState!.overlay!;
final overlay = OverlayEntry(builder: (context) { final overlay = OverlayEntry(builder: (context) {
@ -118,7 +124,6 @@ showChatIconOverlay({Offset offset = const Offset(200, 50)}) {
}); });
globalOverlayState.insert(overlay); globalOverlayState.insert(overlay);
chatIconOverlayEntry = overlay; chatIconOverlayEntry = overlay;
debugPrint("created");
} }
hideChatIconOverlay() { hideChatIconOverlay() {
@ -140,7 +145,6 @@ showChatWindowOverlay() {
}); });
globalOverlayState.insert(overlay); globalOverlayState.insert(overlay);
chatWindowOverlayEntry = overlay; chatWindowOverlayEntry = overlay;
debugPrint("chatEntry created");
} }
hideChatWindowOverlay() { hideChatWindowOverlay() {
@ -237,8 +241,7 @@ showMobileActionsOverlay() {
final double overlayW = 200; final double overlayW = 200;
final double overlayH = 45; final double overlayH = 45;
final left = (screenW - overlayW) / 2; final left = (screenW - overlayW) / 2;
final top = screenH - overlayH - 60; final top = screenH - overlayH - 80;
debugPrint("left top : $left,$top");
final overlay = OverlayEntry(builder: (context) { final overlay = OverlayEntry(builder: (context) {
return DraggableMobileActions( return DraggableMobileActions(
@ -256,7 +259,6 @@ showMobileActionsOverlay() {
}); });
globalOverlayState.insert(overlay); globalOverlayState.insert(overlay);
mobileActionsOverlayEntry = overlay; mobileActionsOverlayEntry = overlay;
debugPrint("mobileActionsOverlay created");
} }
hideMobileActionsOverlay() { hideMobileActionsOverlay() {

View File

@ -15,8 +15,8 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion. # In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at # Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# 1.1.9-1 works for android, but for ios it becomes 1.1.91, need to set it to 1.1.9-a.1 for iOS, will get 1.1.9.1 # 1.1.9-1 works for android, but for ios it becomes 1.1.91, need to set it to 1.1.9-a.1 for iOS, will get 1.1.9.1, but iOS store not allow 4 numbers
version: 1.1.9-a.1+26 version: 1.1.10+27
environment: environment:
sdk: ">=2.12.0 <3.0.0" sdk: ">=2.12.0 <3.0.0"