Browse Source

发消息修改

wr_202303
zsw 4 months ago
parent
commit
d8d0296ac2
  1. 5
      lib/im/chat_details_page.dart
  2. 40
      lib/im/im_view/im_page.dart
  3. 1
      lib/retrofit/retrofit_api.dart

5
lib/im/chat_details_page.dart

@ -9,6 +9,7 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:huixiang/im/database/message.dart'; import 'package:huixiang/im/database/message.dart';
import 'package:huixiang/im/out/message.pb.dart'; import 'package:huixiang/im/out/message.pb.dart';
import 'package:huixiang/main.dart'; import 'package:huixiang/main.dart';
import 'package:huixiang/retrofit/data/im_user_list.dart';
import 'package:huixiang/retrofit/retrofit_api.dart'; import 'package:huixiang/retrofit/retrofit_api.dart';
import 'package:huixiang/view_widget/my_appbar.dart'; import 'package:huixiang/view_widget/my_appbar.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
@ -152,7 +153,7 @@ class _ChatDetailsPage extends State<ChatDetailsPage>
WidgetsBinding.instance.removeObserver(this); WidgetsBinding.instance.removeObserver(this);
commentFocus.removeListener(_focusNodeListener); commentFocus.removeListener(_focusNodeListener);
socketClient.removeCallback(toUserId); socketClient.removeCallback(_toUser.mid);
} }
@ -784,7 +785,7 @@ class _ChatDetailsPage extends State<ChatDetailsPage>
if (commentText.trim() == "") { if (commentText.trim() == "") {
return; return;
} }
socketClient.sendMessage(toUserId, commentText).then((value) { socketClient.sendMessage(_toUser.mid, commentText).then((value) {
Message message = value; Message message = value;
messages.insert(0, message); messages.insert(0, message);
chatController.clear(); chatController.clear();

40
lib/im/im_view/im_page.dart

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

1
lib/retrofit/retrofit_api.dart

@ -663,4 +663,5 @@ abstract class ApiService {
@GET("/member/memberSearch?keyword={keyword}") @GET("/member/memberSearch?keyword={keyword}")
Future<BaseData<List<ImUserList>>> memberSearch( Future<BaseData<List<ImUserList>>> memberSearch(
@Path("keyword") String keyword); @Path("keyword") String keyword);
} }

Loading…
Cancel
Save