| 
									
										
										
										
											2022-03-22 16:40:23 +08:00
										 |  |  | import 'dart:convert'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-03 14:58:57 +08:00
										 |  |  | import 'package:dash_chat/dash_chat.dart'; | 
					
						
							| 
									
										
										
										
											2022-02-28 21:26:44 +08:00
										 |  |  | import 'package:flutter/material.dart'; | 
					
						
							| 
									
										
										
										
											2022-03-03 14:58:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-28 22:44:54 +08:00
										 |  |  | import '../widgets/overlay.dart'; | 
					
						
							| 
									
										
										
										
											2022-03-03 14:58:57 +08:00
										 |  |  | import 'model.dart'; | 
					
						
							| 
									
										
										
										
											2022-02-28 21:26:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-16 00:01:27 +08:00
										 |  |  | 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(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-28 21:26:44 +08:00
										 |  |  | class ChatModel with ChangeNotifier { | 
					
						
							| 
									
										
										
										
											2022-03-22 16:40:23 +08:00
										 |  |  |   static final clientModeID = -1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-03 14:58:57 +08:00
										 |  |  |   final ChatUser me = ChatUser( | 
					
						
							| 
									
										
										
										
											2022-04-05 00:51:47 +08:00
										 |  |  |     uid: "", | 
					
						
							|  |  |  |     name: "Me", | 
					
						
							| 
									
										
										
										
											2022-03-03 14:58:57 +08:00
										 |  |  |   ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-16 00:01:27 +08:00
										 |  |  |   late final Map<int, MessageBody> _messages = Map() | 
					
						
							|  |  |  |     ..[clientModeID] = MessageBody(me, []); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-25 16:34:27 +08:00
										 |  |  |   final _scroller = ScrollController(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-22 16:40:23 +08:00
										 |  |  |   var _currentID = clientModeID; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-25 16:34:27 +08:00
										 |  |  |   ScrollController get scroller => _scroller; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-03-03 14:58:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-04 14:54:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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(); | 
					
						
							| 
									
										
										
										
											2022-04-04 14:54:00 +08:00
										 |  |  |     } 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-04-04 14:54:00 +08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2022-05-16 00:01:27 +08:00
										 |  |  |       final chatUser = ChatUser( | 
					
						
							|  |  |  |         uid: client.peerId, | 
					
						
							|  |  |  |         name: client.name, | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |       _messages[id] = MessageBody(chatUser, []); | 
					
						
							| 
									
										
										
										
											2022-04-04 14:54:00 +08:00
										 |  |  |       _currentID = id; | 
					
						
							| 
									
										
										
										
											2022-05-16 00:01:27 +08:00
										 |  |  |       notifyListeners(); | 
					
						
							| 
									
										
										
										
											2022-03-25 16:34:27 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-03-22 16:40:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   receive(int id, String text) { | 
					
						
							| 
									
										
										
										
											2022-03-03 14:58:57 +08:00
										 |  |  |     if (text.isEmpty) return; | 
					
						
							|  |  |  |     // first message show overlay icon
 | 
					
						
							| 
									
										
										
										
											2022-04-28 22:44:54 +08:00
										 |  |  |     if (chatIconOverlayEntry == null) { | 
					
						
							| 
									
										
										
										
											2022-03-03 14:58:57 +08:00
										 |  |  |       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( | 
					
						
							|  |  |  |         name: FFI.ffiModel.pi.username, | 
					
						
							|  |  |  |         uid: FFI.getId(), | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											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(uid: client.peerId, name: 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-03-22 16:40:23 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-04-05 00:51:47 +08:00
										 |  |  |     _messages[id]!.add(ChatMessage(text: text, user: chatUser)); | 
					
						
							| 
									
										
										
										
											2022-03-22 16:40:23 +08:00
										 |  |  |     _currentID = id; | 
					
						
							| 
									
										
										
										
											2022-03-03 14:58:57 +08:00
										 |  |  |     notifyListeners(); | 
					
						
							| 
									
										
										
										
											2022-03-25 16:34:27 +08:00
										 |  |  |     scrollToBottom(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-05 00:51:47 +08:00
										 |  |  |   scrollToBottom() { | 
					
						
							| 
									
										
										
										
											2022-03-25 16:34:27 +08:00
										 |  |  |     Future.delayed(Duration(milliseconds: 500), () { | 
					
						
							| 
									
										
										
										
											2022-04-05 00:51:47 +08:00
										 |  |  |       _scroller.animateTo(_scroller.position.maxScrollExtent, | 
					
						
							| 
									
										
										
										
											2022-03-25 16:34:27 +08:00
										 |  |  |           duration: Duration(milliseconds: 200), | 
					
						
							|  |  |  |           curve: Curves.fastLinearToSlowEaseIn); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2022-03-03 14:58:57 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-22 16:40:23 +08:00
										 |  |  |   send(ChatMessage message) { | 
					
						
							|  |  |  |     if (message.text != null && message.text!.isNotEmpty) { | 
					
						
							|  |  |  |       _messages[_currentID]?.add(message); | 
					
						
							|  |  |  |       if (_currentID == clientModeID) { | 
					
						
							| 
									
										
										
										
											2022-03-24 17:58:33 +08:00
										 |  |  |         FFI.setByName("chat_client_mode", message.text!); | 
					
						
							| 
									
										
										
										
											2022-03-22 16:40:23 +08:00
										 |  |  |       } else { | 
					
						
							|  |  |  |         final msg = Map() | 
					
						
							|  |  |  |           ..["id"] = _currentID | 
					
						
							|  |  |  |           ..["text"] = message.text!; | 
					
						
							| 
									
										
										
										
											2022-03-24 17:58:33 +08:00
										 |  |  |         FFI.setByName("chat_server_mode", jsonEncode(msg)); | 
					
						
							| 
									
										
										
										
											2022-03-22 16:40:23 +08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2022-03-03 14:58:57 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     notifyListeners(); | 
					
						
							| 
									
										
										
										
											2022-03-25 16:34:27 +08:00
										 |  |  |     scrollToBottom(); | 
					
						
							| 
									
										
										
										
											2022-03-03 14:58:57 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-02-28 21:26:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-25 16:34:27 +08:00
										 |  |  |   close() { | 
					
						
							| 
									
										
										
										
											2022-03-03 14:58:57 +08:00
										 |  |  |     hideChatIconOverlay(); | 
					
						
							|  |  |  |     hideChatWindowOverlay(); | 
					
						
							|  |  |  |     notifyListeners(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-05-16 00:01:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   resetClientMode() { | 
					
						
							|  |  |  |     _messages[clientModeID]?.clear(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-03-22 16:40:23 +08:00
										 |  |  | } |