|
|
|
@ -5,6 +5,7 @@ import 'package:dio/dio.dart';
|
|
|
|
|
import 'package:flutter/material.dart'; |
|
|
|
|
import 'package:flutter_easyloading/flutter_easyloading.dart'; |
|
|
|
|
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; |
|
|
|
|
import 'package:huixiang/constant.dart'; |
|
|
|
|
import 'package:huixiang/generated/l10n.dart'; |
|
|
|
|
import 'package:huixiang/im/database/message.dart'; |
|
|
|
|
import 'package:huixiang/im/im_view/time_formatter.dart'; |
|
|
|
@ -41,7 +42,6 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
ApiService apiService; |
|
|
|
|
int pageNum = 1; |
|
|
|
|
List<Message> messages = []; |
|
|
|
|
Map<String, ImUserList> imUserList = {}; |
|
|
|
|
Map<String, int> msgNumber = { |
|
|
|
|
"1": 0, |
|
|
|
|
"2": 0, |
|
|
|
@ -87,9 +87,8 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
|
|
|
|
|
List<String> userIds = []; |
|
|
|
|
Map<String, Message> lastMessageMap = {}; |
|
|
|
|
Stream streamSubscription; |
|
|
|
|
Map<String, int> unreadCountMap = {}; |
|
|
|
|
Stream streamSubscription ; |
|
|
|
|
Map<String, ImUserList> contactMap = {}; |
|
|
|
|
|
|
|
|
|
loadMessageList() async { |
|
|
|
|
SharedPreferences shared = await SharedPreferences.getInstance(); |
|
|
|
@ -105,9 +104,10 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
lastMessageMap[message.fromId] = message; |
|
|
|
|
|
|
|
|
|
debugPrint("messages_records : ${message.toJson()}"); |
|
|
|
|
// if (contactMap[message.fromId] == null) { |
|
|
|
|
// /// TODO: message.fromId request Api and setState |
|
|
|
|
// } |
|
|
|
|
if (contactMap[message.fromId] == null) { |
|
|
|
|
queryMemberInfo([message.fromId]); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (mounted) { |
|
|
|
|
setState(() {}); |
|
|
|
|
} |
|
|
|
@ -126,15 +126,23 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
List<ImUserList> contacts = (await hxDatabase.queryImUser(userIds)) ?? []; |
|
|
|
|
|
|
|
|
|
unreadCountMap = await hxDatabase.messageUnreadCount(userIds, userId); |
|
|
|
|
if (contacts.isNotEmpty) imUserList = groupBy(contacts, (p0) => p0.mid); |
|
|
|
|
lastMessageMap = messages.lGroupBy((p0) => p0.toId != userId ? p0.toId : p0.fromId).mGroupItem; |
|
|
|
|
|
|
|
|
|
lastMessageMap = |
|
|
|
|
groupBy(messages, (p0) => p0.toId != userId ? p0.toId : p0.fromId); |
|
|
|
|
if (contacts?.isEmpty ?? true) { |
|
|
|
|
await queryMemberInfo(userIds); |
|
|
|
|
return; |
|
|
|
|
} else { |
|
|
|
|
List<String> queryUserIds = userIds.where((u) => contacts.where((c) => c.mid == u).isEmpty).toList(); |
|
|
|
|
if (queryUserIds.isNotEmpty) { |
|
|
|
|
await queryMemberInfo(queryUserIds); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
contactMap = contacts.lGroupBy((p0) => p0.mid).mGroupItem; |
|
|
|
|
|
|
|
|
|
if (mounted) { |
|
|
|
|
setState(() {}); |
|
|
|
|
} |
|
|
|
|
queryMemberInfo(userIds); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void updateLastMessage(String userId) async { |
|
|
|
@ -234,7 +242,7 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
baseData.data.forEach((element) async { |
|
|
|
|
await hxDatabase.insertOrUpdateImUser(element.toJson()); |
|
|
|
|
}); |
|
|
|
|
imUserList = groupBy(baseData.data, (p0) => p0.mid); |
|
|
|
|
contactMap = baseData.data.lGroupBy((p0) => p0.mid).mGroupItem; |
|
|
|
|
setState(() {}); |
|
|
|
|
} else { |
|
|
|
|
SmartDialog.showToast(baseData.msg, alignment: Alignment.center); |
|
|
|
@ -407,7 +415,7 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
Navigator.of(context).pushNamed( |
|
|
|
|
'/router/chat_details_page', |
|
|
|
|
arguments: { |
|
|
|
|
"toUser": imUserList[userIds[position]], |
|
|
|
|
"toUser": contactMap[userIds[position]], |
|
|
|
|
}, |
|
|
|
|
).then((value) { |
|
|
|
|
updateLastMessage(userIds[position]); |
|
|
|
@ -427,9 +435,9 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
child: Row( |
|
|
|
|
children: [ |
|
|
|
|
MImage( |
|
|
|
|
!imUserList.containsKey(userId) |
|
|
|
|
!contactMap.containsKey(userId) |
|
|
|
|
? null |
|
|
|
|
: imUserList[userId]?.avatar ?? "", |
|
|
|
|
: contactMap[userId]?.avatar ?? "", |
|
|
|
|
isCircle: true, |
|
|
|
|
height: 54.h, |
|
|
|
|
width: 54.h, |
|
|
|
@ -448,9 +456,9 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
children: [ |
|
|
|
|
Expanded( |
|
|
|
|
child: Text( |
|
|
|
|
!imUserList.containsKey(userId) |
|
|
|
|
!contactMap.containsKey(userId) |
|
|
|
|
? "" |
|
|
|
|
: imUserList[userId]?.nickname ?? "", |
|
|
|
|
: contactMap[userId]?.nickname ?? "", |
|
|
|
|
// overflow: TextOverflow.fade, |
|
|
|
|
maxLines: 1, |
|
|
|
|
style: TextStyle( |
|
|
|
|