together_mobile/lib/models/websocket_model.dart

530 lines
15 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:together_mobile/models/route_state_model.dart';
import 'package:together_mobile/request/user_profile.dart';
import 'package:together_mobile/utils/app_dir.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
import 'package:web_socket_channel/status.dart' as status;
import 'package:together_mobile/database/box_type.dart';
import 'package:together_mobile/models/apply_list_model.dart';
import 'package:together_mobile/models/contact_model.dart';
import 'package:together_mobile/models/init_get_it.dart';
import 'package:together_mobile/models/user_model.dart';
import 'package:together_mobile/notification_api.dart';
enum SocketStatus {
connected,
closed,
error,
}
Map<String, (int, List<Message>)> messages = {};
class WebSocketManager extends ChangeNotifier {
late Uri wsUrl;
late WebSocketChannel channel;
String id = '';
SocketStatus socketStatus = SocketStatus.closed;
Timer? heartBeatTimer;
Timer? serverTimer;
Timer? reconnectTimer;
int reconnectCount = 30;
int reconnectTimes = 0;
Duration timeout = const Duration(seconds: 4);
void connect(String userId) {
id = userId;
wsUrl = Uri.parse('ws://10.0.2.2:8000/ws/$id');
channel = WebSocketChannel.connect(wsUrl);
socketStatus = SocketStatus.connected;
print('websocket connected <$channel>');
heartBeatInspect();
if (reconnectTimer != null) {
reconnectTimer!.cancel();
reconnectTimer = null;
reconnectTimes = 0;
}
channel.stream.listen(onData, onError: onError, onDone: onDone);
}
void disconnect() {
channel.sink.close();
wsUrl = Uri();
id = '';
socketStatus = SocketStatus.closed;
heartBeatTimer?.cancel();
serverTimer?.cancel();
reconnectTimer?.cancel();
heartBeatTimer = null;
serverTimer = null;
reconnectTimer = null;
reconnectTimes = 0;
}
void onData(jsonData) {
heartBeatInspect();
Map<String, dynamic> data = json.decode(jsonData);
switch (data['event']) {
case 'friend-chat-msg':
receiveFriendMsg(data);
case 'apply-friend':
receiveApplyFriend(data);
case 'friend-added':
receiveFriendAdded(data);
case 'friend-deleted':
receiveFriendDeleted(data);
case 'friend-chat-image' || 'group-chat-image':
receiveChatImages(data);
case 'group-chat-creation':
receiveGroupChatCreation(data);
case 'group-chat-msg':
receiveGroupChatMsg(data);
}
}
void onDone() {
print('websocket disconnected <$channel>');
if (socketStatus == SocketStatus.closed) {
return;
}
reconnect();
}
void onError(Object error) {}
void heartBeatInspect() {
if (heartBeatTimer != null) {
heartBeatTimer!.cancel();
heartBeatTimer = null;
}
if (serverTimer != null) {
serverTimer!.cancel();
serverTimer = null;
}
print('start heartbeat inspect......');
heartBeatTimer = Timer(timeout, () {
channel.sink.add(json.encode({'event': 'ping'}));
serverTimer = Timer(timeout, () {
channel.sink.close(status.internalServerError);
socketStatus = SocketStatus.closed;
});
});
}
void reconnect() {
if (socketStatus == SocketStatus.error) {
if (heartBeatTimer != null) {
heartBeatTimer!.cancel();
heartBeatTimer = null;
}
if (serverTimer != null) {
serverTimer!.cancel();
serverTimer = null;
}
return;
}
print('websocket reconnecting......');
if (heartBeatTimer != null) {
heartBeatTimer!.cancel();
heartBeatTimer = null;
}
if (serverTimer != null) {
serverTimer!.cancel();
serverTimer = null;
}
reconnectTimer = Timer.periodic(timeout, (timer) {
if (reconnectTimes < reconnectCount) {
connect(id);
reconnectTimes++;
} else {
print('reconnection times exceed the max times......');
// If it is still disconnection after reconnect 30 times, set the socket
// status to error, means the network is bad, and stop reconnecting.
socketStatus = SocketStatus.error;
channel.sink.close();
timer.cancel();
}
});
}
}
void receiveFriendMsg(Map<String, dynamic> msg) async {
print('=================收到了好友信息事件==================');
print(msg);
print('=======================================');
String senderId = msg['senderId'] as String;
late Box<MessageT> messageTBox;
try {
messageTBox = Hive.box('message_$senderId');
} catch (e) {
messageTBox = await Hive.openBox('message_$senderId');
}
Box<ChatSetting> chatSettingBox = Hive.box<ChatSetting>('chat_setting');
ChatSetting? chatSetting = chatSettingBox.get(senderId);
DateTime dateTime = DateTime.parse(msg['dateTime'] as String);
if (chatSetting == null) {
chatSettingBox.put(
senderId,
ChatSetting(
senderId,
0,
false,
true,
false,
dateTime,
1,
),
);
} else {
chatSetting.isOpen = true;
chatSetting.latestDateTime = dateTime;
chatSetting.unreadCount++;
chatSettingBox.put(senderId, chatSetting);
}
List<String> attachments = List.from(msg['attachments']);
messageTBox.add(
MessageT(
senderId,
msg['text'],
msg['type'],
DateTime.parse(msg['dateTime'] as String),
msg['isShowTime'],
attachments,
),
);
String name = getIt.get<Contact>().friends[senderId]!.friendRemark.isEmpty
? getIt.get<ContactAccountProfile>().friends[senderId]!.nickname
: getIt.get<Contact>().friends[senderId]!.friendRemark;
String routeName = getIt.get<RouteState>().currentPathName;
String avatar = getIt.get<ContactAccountProfile>().friends[senderId]!.avatar;
if (!getIt.get<RouteState>().isVisible) {
_showMessageNotifications(
senderId: senderId,
name: name,
avatar: avatar,
text: msg['text'] as String,
);
} else if (routeName == 'Message') {
if (!getIt.get<RouteState>().query.containsValue(senderId)) {
_showMessageNotifications(
senderId: senderId,
name: name,
avatar: avatar,
text: msg['text'] as String,
);
}
} else if (routeName != 'Chat') {
_showMessageNotifications(
senderId: senderId,
name: name,
avatar: avatar,
text: msg['text'] as String,
);
}
}
void receiveApplyFriend(Map<String, dynamic> msg) {
print('=================收到了申请好友事件==================');
print(msg);
print('=======================================');
getIt.get<ApplyList>().addJson(msg);
}
void receiveFriendAdded(Map<String, dynamic> msg) {
print('=================收到了申请好友通过事件==================');
print(msg);
print('=======================================');
getIt.get<Contact>().addFriend(msg['friendId'], msg['setting']);
getIt
.get<ContactAccountProfile>()
.addFriendAccountProfile(msg['friendId'], msg['accountProfile']);
}
void receiveFriendDeleted(Map<String, dynamic> msg) {
print('=================收到了解除好友事件==================');
print(msg);
print('=======================================');
getIt.get<Contact>().removeFriend(msg['friendId']);
getIt.get<ContactAccountProfile>().removeFriend(msg['friendId']);
}
void receiveChatImages(Map<String, dynamic> msg) async {
print('=================收到了聊天图片事件==================');
print(msg);
print('=======================================');
String chatImageDir = getIt.get<UserProfile>().baseImageDir;
File file = await File('$chatImageDir/${msg['filename']}').create(
recursive: true,
);
await file.writeAsBytes(msg['bytes']);
}
void receiveGroupChatCreation(Map<String, dynamic> msg) {
print('=================收到了群聊邀请事件==================');
print(msg);
print('=======================================');
String groupChatId = msg['groupChat']['id'];
getIt.get<Contact>().addGroupChat(groupChatId);
getIt.get<ContactAccountProfile>().addGroupChatProfile(groupChatId, msg);
}
void receiveGroupChatMsg(Map<String, dynamic> msg) async {
print('=================收到了群聊信息事件==================');
print(msg);
print('=======================================');
String senderId = msg['senderId'] as String;
String groupChatId = msg['groupChatId'] as String;
late Box<MessageT> messageTBox;
try {
messageTBox = Hive.box('message_$groupChatId');
} catch (e) {
messageTBox = await Hive.openBox('message_$groupChatId');
}
Box<ChatSetting> chatSettingBox = Hive.box<ChatSetting>('chat_setting');
ChatSetting? chatSetting = chatSettingBox.get(groupChatId);
DateTime dateTime = DateTime.parse(msg['dateTime'] as String);
if (chatSetting == null) {
chatSettingBox.put(
groupChatId,
ChatSetting(
groupChatId,
1,
false,
true,
false,
dateTime,
1,
),
);
} else {
chatSetting.isOpen = true;
chatSetting.latestDateTime = dateTime;
chatSetting.unreadCount++;
chatSettingBox.put(groupChatId, chatSetting);
}
List<String> attachments = List.from(msg['attachments']);
messageTBox.add(
MessageT(
senderId,
msg['text'],
msg['type'],
DateTime.parse(msg['dateTime'] as String),
msg['isShowTime'],
attachments,
),
);
String avatar =
getIt.get<ContactAccountProfile>().groupChats[groupChatId]!.avatar;
late String name;
if (getIt.get<Contact>().friends.containsKey(senderId)) {
name = getIt.get<Contact>().friends[senderId]!.friendRemark.isEmpty
? getIt.get<ContactAccountProfile>().friends[senderId]!.nickname
: getIt.get<Contact>().friends[senderId]!.friendRemark;
} else if (getIt
.get<ContactAccountProfile>()
.grouChatMemberProfiles
.containsKey(groupChatId)) {
if (getIt
.get<ContactAccountProfile>()
.grouChatMemberProfiles[groupChatId]!
.containsKey(senderId)) {
name = getIt
.get<ContactAccountProfile>()
.grouChatMemberProfiles[groupChatId]![senderId]!
.remark
.isEmpty
? getIt
.get<ContactAccountProfile>()
.grouChatMemberProfiles[groupChatId]![senderId]!
.nickname
.isEmpty
? senderId.substring(0, 6)
: getIt
.get<ContactAccountProfile>()
.grouChatMemberProfiles[groupChatId]![senderId]!
.nickname
: getIt
.get<ContactAccountProfile>()
.grouChatMemberProfiles[groupChatId]![senderId]!
.remark;
}
} else {
name = senderId.substring(0, 6);
}
String routeName = getIt.get<RouteState>().currentPathName;
if (!getIt.get<RouteState>().isVisible) {
_showMessageNotifications(
senderId: senderId,
name: name,
avatar: avatar,
text: msg['text'] as String,
groupChatId: groupChatId,
);
} else if (routeName == 'Message') {
if (!getIt.get<RouteState>().query.containsValue(senderId)) {
_showMessageNotifications(
senderId: senderId,
name: name,
avatar: avatar,
text: msg['text'] as String,
groupChatId: groupChatId,
);
}
} else if (routeName != 'Chat') {
_showMessageNotifications(
senderId: senderId,
name: name,
avatar: avatar,
text: msg['text'] as String,
groupChatId: groupChatId,
);
}
}
Future<void> _showMessageNotifications({
required String senderId,
required String name,
required String avatar,
required String text,
String? groupChatId,
}) async {
String groupChannelId = 'TOGETHER_${getIt.get<UserAccount>().id}';
const String groupChannelName = 'together messages';
String avatarPath = '';
if (avatar.isNotEmpty) {
if (groupChatId == null) {
avatarPath = await getAvatarPath('user', avatar);
if (!File(avatarPath).existsSync()) {
File file = await File(avatarPath).create(recursive: true);
final bytes = await downloadUserAvatar(avatar);
await file.writeAsBytes(bytes);
}
} else {
// TODO: download group chat avatar. This will done after changing group chat avatar is implemented
}
}
late Person person;
if (avatarPath.isNotEmpty) {
person = Person(
name: name,
key: senderId,
bot: false,
icon: BitmapFilePathAndroidIcon(avatarPath),
);
} else {
person = Person(
name: name,
key: senderId,
bot: false,
);
}
if (groupChatId == null) {
if (messages.containsKey(senderId)) {
messages[senderId]!.$2.add(Message(text, DateTime.now(), person));
} else {
messages[senderId] = (++id, [Message(text, DateTime.now(), person)]);
}
} else {
if (messages.containsKey(groupChatId)) {
messages[groupChatId]!.$2.add(Message(text, DateTime.now(), person));
} else {
messages[groupChatId] = (++id, [Message(text, DateTime.now(), person)]);
}
}
late final MessagingStyleInformation messagingStyle;
if (groupChatId == null) {
messagingStyle = MessagingStyleInformation(
person,
messages: messages[senderId]!.$2,
conversationTitle: name,
);
} else {
String groupChatName =
getIt.get<Contact>().groupChats[groupChatId]!.nameRemark.isEmpty
? getIt.get<ContactAccountProfile>().groupChats[groupChatId]!.name
: getIt.get<Contact>().groupChats[groupChatId]!.nameRemark;
messagingStyle = MessagingStyleInformation(
person,
messages: messages[groupChatId]!.$2,
conversationTitle: groupChatName,
groupConversation: true,
);
}
AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails(
groupChannelId,
groupChannelName,
importance: Importance.max,
priority: Priority.high,
category: AndroidNotificationCategory.message,
styleInformation: messagingStyle,
);
NotificationDetails notificationDetails = NotificationDetails(
android: androidNotificationDetails,
);
late String payload;
if (groupChatId == null) {
payload = json.encode({
'event': 'friend-message',
'friendId': senderId,
});
await flutterLocalNotificationsPlugin.show(
messages[senderId]!.$1,
name,
text,
notificationDetails,
payload: payload,
);
} else {
payload = json.encode({
'event': 'group-chat-message',
'groupChatId': groupChatId,
});
await flutterLocalNotificationsPlugin.show(
messages[groupChatId]!.$1,
name,
text,
notificationDetails,
payload: payload,
);
}
}