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/out/message.pb.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/view_widget/my_appbar.dart';
import 'package:flutter/cupertino.dart';
@ -152,7 +153,7 @@ class _ChatDetailsPage extends State<ChatDetailsPage>
WidgetsBinding.instance.removeObserver(this);
commentFocus.removeListener(_focusNodeListener);
socketClient.removeCallback(toUserId);
socketClient.removeCallback(_toUser.mid);
}
@ -784,7 +785,7 @@ class _ChatDetailsPage extends State<ChatDetailsPage>
if (commentText.trim() == "") {
return;
}
socketClient.sendMessage(toUserId, commentText).then((value) {
socketClient.sendMessage(_toUser.mid, commentText).then((value) {
Message message = value;
messages.insert(0, message);
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_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(

1
lib/retrofit/retrofit_api.dart

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

Loading…
Cancel
Save