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
|
|
|
|
import 'package:flutter_hbb/pages/chat_page.dart';
|
|
|
|
|
|
|
|
|
|
import 'model.dart';
|
|
|
|
|
import 'native_model.dart';
|
2022-02-28 21:26:44 +08:00
|
|
|
|
|
|
|
|
|
class ChatModel with ChangeNotifier {
|
2022-03-22 16:40:23 +08:00
|
|
|
|
// -1作为客户端模式的id,客户端模式下此id唯一
|
|
|
|
|
// 其它正整数的id,来自被控服务器模式下的其他客户端的id,每个客户端有不同的id
|
|
|
|
|
// 注意 此id和peer_id不同,服务端模式下的id等同于conn的顺序累加id
|
|
|
|
|
static final clientModeID = -1;
|
|
|
|
|
|
|
|
|
|
final Map<int, List<ChatMessage>> _messages = Map()..[clientModeID] = [];
|
2022-03-03 14:58:57 +08:00
|
|
|
|
|
|
|
|
|
final ChatUser me = ChatUser(
|
2022-03-22 16:40:23 +08:00
|
|
|
|
uid:"",
|
|
|
|
|
name: "me",
|
|
|
|
|
customProperties: Map()..["id"] = clientModeID
|
2022-03-03 14:58:57 +08:00
|
|
|
|
);
|
|
|
|
|
|
2022-03-22 16:40:23 +08:00
|
|
|
|
var _currentID = clientModeID;
|
|
|
|
|
|
2022-03-03 14:58:57 +08:00
|
|
|
|
get messages => _messages;
|
|
|
|
|
|
2022-03-22 16:40:23 +08:00
|
|
|
|
get currentID => _currentID;
|
|
|
|
|
|
|
|
|
|
receive(int id, String text) {
|
2022-03-03 14:58:57 +08:00
|
|
|
|
if (text.isEmpty) return;
|
|
|
|
|
// first message show overlay icon
|
2022-03-22 16:40:23 +08:00
|
|
|
|
if (iconOverlayEntry == null) {
|
2022-03-03 14:58:57 +08:00
|
|
|
|
showChatIconOverlay();
|
|
|
|
|
}
|
2022-03-22 16:40:23 +08:00
|
|
|
|
if(!_messages.containsKey(id)){
|
|
|
|
|
_messages[id] = [];
|
|
|
|
|
}
|
|
|
|
|
// TODO peer info
|
|
|
|
|
_messages[id]?.add(ChatMessage(
|
|
|
|
|
text: text,
|
|
|
|
|
user: ChatUser(
|
|
|
|
|
name: FFI.ffiModel.pi.username,
|
|
|
|
|
uid: FFI.getId(),
|
|
|
|
|
)));
|
|
|
|
|
_currentID = id;
|
2022-03-03 14:58:57 +08:00
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
|
|
|
|
PlatformFFI.setByName("chat_client_mode", message.text!);
|
|
|
|
|
} else {
|
|
|
|
|
final msg = Map()
|
|
|
|
|
..["id"] = _currentID
|
|
|
|
|
..["text"] = message.text!;
|
|
|
|
|
PlatformFFI.setByName("chat_server_mode", jsonEncode(msg));
|
|
|
|
|
}
|
2022-03-03 14:58:57 +08:00
|
|
|
|
}
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
2022-02-28 21:26:44 +08:00
|
|
|
|
|
2022-03-22 16:40:23 +08:00
|
|
|
|
release() {
|
2022-03-03 14:58:57 +08:00
|
|
|
|
hideChatIconOverlay();
|
|
|
|
|
hideChatWindowOverlay();
|
2022-03-22 16:40:23 +08:00
|
|
|
|
_messages.forEach((key, value) => value.clear());
|
2022-03-03 14:58:57 +08:00
|
|
|
|
_messages.clear();
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
2022-03-22 16:40:23 +08:00
|
|
|
|
}
|