rustdesk/flutter/lib/models/chat_model.dart

269 lines
7.1 KiB
Dart
Raw Normal View History

2022-08-04 17:24:02 +08:00
import 'package:dash_chat_2/dash_chat_2.dart';
2022-08-11 10:19:12 +08:00
import 'package:draggable_float_widget/draggable_float_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hbb/models/platform_model.dart';
2022-08-22 20:12:58 +08:00
import 'package:window_manager/window_manager.dart';
2022-08-11 10:19:12 +08:00
import '../common.dart';
2022-09-13 09:03:34 +08:00
import '../common/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);
2022-08-04 17:24:02 +08:00
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;
2022-08-11 10:19:12 +08:00
/// _overlayState:
/// Desktop: store session overlay by using [setOverlayState].
/// Mobile: always null, use global overlay.
/// see [_getOverlayState] in [showChatIconOverlay] or [showChatWindowOverlay]
OverlayState? _overlayState;
OverlayEntry? chatIconOverlayEntry;
OverlayEntry? chatWindowOverlayEntry;
final ChatUser me = ChatUser(
2022-08-04 17:24:02 +08:00
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-08-22 20:12:58 +08:00
late bool _isShowChatPage = false;
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-08-22 20:12:58 +08:00
bool get isShowChatPage => _isShowChatPage;
2022-09-27 20:35:02 +08:00
final WeakReference<FFI> parent;
2022-09-27 20:35:02 +08:00
ChatModel(this.parent);
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-08-11 10:19:12 +08:00
setOverlayState(OverlayState? os) {
_overlayState = os;
}
OverlayState? _getOverlayState() {
if (_overlayState == null) {
if (globalKey.currentState == null ||
globalKey.currentState!.overlay == null) return null;
return globalKey.currentState!.overlay;
} else {
return _overlayState;
}
}
showChatIconOverlay({Offset offset = const Offset(200, 50)}) {
if (chatIconOverlayEntry != null) {
chatIconOverlayEntry!.remove();
}
// mobile check navigationBar
final bar = navigationBarKey.currentWidget;
if (bar != null) {
if ((bar as BottomNavigationBar).currentIndex == 1) {
return;
}
}
final overlayState = _getOverlayState();
if (overlayState == null) return;
final overlay = OverlayEntry(builder: (context) {
return DraggableFloatWidget(
config: DraggableFloatWidgetBaseConfig(
initPositionYInTop: false,
initPositionYMarginBorder: 100,
borderTopContainTopBar: true,
),
child: FloatingActionButton(
onPressed: () {
if (chatWindowOverlayEntry == null) {
showChatWindowOverlay();
} else {
hideChatWindowOverlay();
}
},
child: Icon(Icons.message)));
});
overlayState.insert(overlay);
chatIconOverlayEntry = overlay;
}
hideChatIconOverlay() {
if (chatIconOverlayEntry != null) {
chatIconOverlayEntry!.remove();
chatIconOverlayEntry = null;
}
}
showChatWindowOverlay() {
if (chatWindowOverlayEntry != null) return;
final overlayState = _getOverlayState();
if (overlayState == null) return;
final overlay = OverlayEntry(builder: (context) {
return DraggableChatWindow(
position: const Offset(20, 80),
width: 250,
height: 350,
chatModel: this);
2022-08-11 10:19:12 +08:00
});
overlayState.insert(overlay);
chatWindowOverlayEntry = overlay;
}
hideChatWindowOverlay() {
if (chatWindowOverlayEntry != null) {
chatWindowOverlayEntry!.remove();
chatWindowOverlayEntry = null;
return;
}
}
toggleChatOverlay() {
if ((!isDesktop && chatIconOverlayEntry == null) ||
chatWindowOverlayEntry == null) {
2022-08-11 10:19:12 +08:00
gFFI.invokeMethod("enable_soft_keyboard", true);
if (!isDesktop) {
showChatIconOverlay();
}
2022-08-11 10:19:12 +08:00
showChatWindowOverlay();
} else {
hideChatIconOverlay();
hideChatWindowOverlay();
}
}
2022-08-22 20:12:58 +08:00
toggleCMChatPage(int id) async {
if (gFFI.chatModel.currentID != id) {
gFFI.chatModel.changeCurrentID(id);
}
if (_isShowChatPage) {
_isShowChatPage = !_isShowChatPage;
notifyListeners();
await windowManager.setSize(Size(300, 400));
await windowManager.setAlignment(Alignment.topRight);
2022-08-22 20:12:58 +08:00
} else {
await windowManager.setSize(Size(600, 400));
2022-08-22 20:12:58 +08:00
await Future.delayed(Duration(milliseconds: 100));
await windowManager.setAlignment(Alignment.topRight);
2022-08-22 20:12:58 +08:00
_isShowChatPage = !_isShowChatPage;
notifyListeners();
}
}
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-09-27 20:35:02 +08:00
final client = parent.target?.serverModel.clients
2022-08-22 20:12:58 +08:00
.firstWhere((client) => client.id == id);
2022-05-16 00:01:27 +08:00
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(
2022-08-04 17:24:02 +08:00
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) async {
if (text.isEmpty) return;
2022-08-22 20:12:58 +08:00
// mobile: first message show overlay icon
if (chatIconOverlayEntry == null) {
showChatIconOverlay();
}
2022-08-22 20:12:58 +08:00
// desktop: show chat page
if (!_isShowChatPage) {
toggleCMChatPage(id);
}
2022-09-27 20:35:02 +08:00
parent.target?.serverModel.jumpTo(id);
2022-08-22 20:12:58 +08:00
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(
2022-09-27 20:35:02 +08:00
firstName: parent.target?.ffiModel.pi.username,
id: await bind.mainGetLastRemoteId(),
2022-03-25 16:34:27 +08:00
);
2022-04-05 00:51:47 +08:00
} else {
2022-09-27 20:35:02 +08:00
final client = parent.target?.serverModel.clients
.firstWhere((client) => client.id == id);
2022-05-16 00:01:27 +08:00
if (client == null) {
return debugPrint("Failed to receive msg,user doesn't exist");
}
if (isDesktop) {
window_on_top(null);
2022-09-27 20:35:02 +08:00
var index = parent.target?.serverModel.clients
.indexWhere((client) => client.id == id);
if (index != null && index >= 0) {
gFFI.serverModel.tabController.jumpTo(index);
}
}
2022-08-04 17:24:02 +08:00
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, []);
}
2022-08-04 17:24:02 +08:00
_messages[id]!.insert(
ChatMessage(text: text, user: chatUser, createdAt: DateTime.now()));
_currentID = id;
notifyListeners();
}
send(ChatMessage message) {
2022-08-04 17:24:02 +08:00
if (message.text.isNotEmpty) {
_messages[_currentID]?.insert(message);
if (_currentID == clientModeID) {
2022-09-27 20:35:02 +08:00
if (parent.target != null) {
bind.sessionSendChat(id: parent.target!.id, text: message.text);
}
} else {
2022-08-17 17:23:55 +08:00
bind.cmSendChat(connId: _currentID, msg: message.text);
}
}
notifyListeners();
}
2022-03-25 16:34:27 +08:00
close() {
hideChatIconOverlay();
hideChatWindowOverlay();
2022-08-11 10:19:12 +08:00
_overlayState = null;
notifyListeners();
}
2022-05-16 00:01:27 +08:00
resetClientMode() {
_messages[clientModeID]?.clear();
}
}