|
|
|
@ -43,11 +43,12 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
"6": 0, |
|
|
|
|
}; |
|
|
|
|
int state = 0; |
|
|
|
|
List<String> userIds = []; |
|
|
|
|
List<String> conversationIds = []; |
|
|
|
|
Map<String, Message> lastMessageMap = {}; |
|
|
|
|
Map<String, int> unreadCountMap = {}; |
|
|
|
|
Map<String, ImUser> contactMap = {}; |
|
|
|
|
int insertIndex = 0; |
|
|
|
|
String selfUserId; |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
void onMessage(txt) { |
|
|
|
@ -77,14 +78,14 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
|
|
|
|
|
initSocketClient() async { |
|
|
|
|
SharedPreferences shared = await SharedPreferences.getInstance(); |
|
|
|
|
String userId = shared.getString("userId"); |
|
|
|
|
socketClient.addCallback(userId, (Message message) { |
|
|
|
|
if (userIds.contains(message.fromId)) { |
|
|
|
|
userIds.remove(message.fromId); |
|
|
|
|
selfUserId = shared.getString("userId"); |
|
|
|
|
socketClient.addCallback(selfUserId, (Message message) { |
|
|
|
|
if (conversationIds.contains(message.conversationId)) { |
|
|
|
|
conversationIds.remove(message.conversationId); |
|
|
|
|
} |
|
|
|
|
userIds.insert(insertIndex, message.fromId); |
|
|
|
|
conversationIds.insert(insertIndex, message.conversationId); |
|
|
|
|
|
|
|
|
|
lastMessageMap[message.fromId] = message; |
|
|
|
|
lastMessageMap[message.conversationId] = message; |
|
|
|
|
|
|
|
|
|
listenerRefresh(message); |
|
|
|
|
}); |
|
|
|
@ -99,12 +100,9 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
listenerRefresh(Message message) async { |
|
|
|
|
SharedPreferences shared = await SharedPreferences.getInstance(); |
|
|
|
|
String userId = shared.getString("userId"); |
|
|
|
|
|
|
|
|
|
await sortConversation(lastMessageMap); |
|
|
|
|
|
|
|
|
|
await queryUnreadCount(userIds, userId); |
|
|
|
|
await queryUnreadCount(conversationId); |
|
|
|
|
|
|
|
|
|
debugPrint("messages_records : ${message.toJson()}"); |
|
|
|
|
if (contactMap[message.fromId] == null) { |
|
|
|
@ -115,38 +113,35 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
loadMessageList() async { |
|
|
|
|
SharedPreferences shared = await SharedPreferences.getInstance(); |
|
|
|
|
String userId = shared.getString("userId"); |
|
|
|
|
messages = await hxDatabase.queryList(); |
|
|
|
|
|
|
|
|
|
lastMessageMap = messages.lGroupBy((p0) => p0.toId != userId ? p0.toId : p0.fromId).mGroupItem(key: (p1) => num.parse(p1.time)); |
|
|
|
|
lastMessageMap = messages.lGroupBy((p0) => p0.conversationId).mGroupItem(key: (p1) => num.parse(p1.time)); |
|
|
|
|
|
|
|
|
|
await sortConversation(lastMessageMap); |
|
|
|
|
await queryImUserInfo(messages.map((e) => e.toId != selfUserId ? e.toId : e.fromId).toList()); |
|
|
|
|
|
|
|
|
|
await queryUnreadCount(userIds, userId); |
|
|
|
|
await sortConversation(lastMessageMap); |
|
|
|
|
|
|
|
|
|
await queryImUserInfo(userIds); |
|
|
|
|
await queryUnreadCount(conversationIds); |
|
|
|
|
|
|
|
|
|
refreshState(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// update conversation by time sort |
|
|
|
|
sortConversation(lastMessageMap) async { |
|
|
|
|
SharedPreferences shared = await SharedPreferences.getInstance(); |
|
|
|
|
String userId = shared.getString("userId"); |
|
|
|
|
List<Message> sortMessages = lastMessageMap.values.toList(); |
|
|
|
|
sortMessages.sort((a, b) => (num.parse(b.time)).compareTo(num.parse(a.time))); |
|
|
|
|
userIds = sortMessages |
|
|
|
|
.map((e) => e.toId != userId ? e.toId : e.fromId) |
|
|
|
|
conversationIds = sortMessages |
|
|
|
|
.map((e) => e.conversationId) |
|
|
|
|
.toSet() |
|
|
|
|
.where((element) => element != userId) |
|
|
|
|
.toList(); |
|
|
|
|
conversationIds.forEach((element) { |
|
|
|
|
debugPrint("conversationIds: ${element}"); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// update conversation unreadcount |
|
|
|
|
queryUnreadCount(userIds, userId) async { |
|
|
|
|
unreadCountMap = await hxDatabase.messageUnreadCount(userIds, userId); |
|
|
|
|
debugPrint("unreadCount: $unreadCountMap"); |
|
|
|
|
queryUnreadCount(conversationId) async { |
|
|
|
|
unreadCountMap = await hxDatabase.messageUnreadCount(conversationId); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// update imuser info by mids |
|
|
|
@ -162,33 +157,31 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
contactMap = contacts.lGroupBy((p0) => p0.mid).mGroupItem(); |
|
|
|
|
List<String> topUserIds = [],notTopUserIds = []; |
|
|
|
|
contactMap = contacts.lGroupBy((p0) => conversationId(p0.mid, selfUserId)).mGroupItem(); |
|
|
|
|
List<String> topConversationIds = [],notTopUserIds = []; |
|
|
|
|
contactMap.forEach((key, value) { |
|
|
|
|
if(value.isTop == 1) |
|
|
|
|
topUserIds.add(key); |
|
|
|
|
topConversationIds.add(key); |
|
|
|
|
else |
|
|
|
|
notTopUserIds.add(key); |
|
|
|
|
}); |
|
|
|
|
insertIndex = topUserIds.length; |
|
|
|
|
this.userIds = topUserIds..addAll(notTopUserIds); |
|
|
|
|
insertIndex = topConversationIds.length; |
|
|
|
|
this.conversationIds = topConversationIds..addAll(notTopUserIds); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// update one conversation last message ,and update conversation sort |
|
|
|
|
void updateLastMessage(String userId) async { |
|
|
|
|
Message message = await hxDatabase.lastMessage(userId); |
|
|
|
|
debugPrint("lastmessage: $userId ${message.content} ${message.toJson()}"); |
|
|
|
|
void updateLastMessage(String conversationId) async { |
|
|
|
|
Message message = await hxDatabase.lastMessage(conversationId); |
|
|
|
|
debugPrint("lastmessage: $conversationId ${message.content} ${message.toJson()}"); |
|
|
|
|
if (message != null) { |
|
|
|
|
lastMessageMap[userId] = message; |
|
|
|
|
lastMessageMap[conversationId] = message; |
|
|
|
|
await sortConversation(lastMessageMap); |
|
|
|
|
refreshState(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void updateUnreadCount() async { |
|
|
|
|
SharedPreferences shared = await SharedPreferences.getInstance(); |
|
|
|
|
String userId = shared.getString("userId"); |
|
|
|
|
unreadCountMap = await hxDatabase.messageUnreadCount(userIds, userId); |
|
|
|
|
unreadCountMap = await hxDatabase.messageUnreadCount(conversationIds); |
|
|
|
|
refreshState(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -216,8 +209,8 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
await hxDatabase.insertOrUpdateImUser(element.toJson()); |
|
|
|
|
}); |
|
|
|
|
baseData.data.forEach((element) { |
|
|
|
|
if (contactMap[element.mid] == null) { |
|
|
|
|
contactMap[element.mid] = element; |
|
|
|
|
if (contactMap[conversationId(element.mid, selfUserId)] == null) { |
|
|
|
|
contactMap[conversationId(element.mid, selfUserId)] = element; |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
refreshState(); |
|
|
|
@ -233,7 +226,21 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
Widget build(BuildContext context) { |
|
|
|
|
return Scaffold( |
|
|
|
|
backgroundColor: Color(0xFFFFFFFF), |
|
|
|
|
body: SmartRefresher( |
|
|
|
|
body: Container( |
|
|
|
|
decoration: BoxDecoration( |
|
|
|
|
gradient: LinearGradient( |
|
|
|
|
begin: Alignment.topCenter, |
|
|
|
|
end: Alignment.bottomCenter, |
|
|
|
|
colors: [ |
|
|
|
|
Color(0xFFD9FFDE), |
|
|
|
|
Color(0xFFD9FFDE), |
|
|
|
|
Color(0xFFFFFFFF), |
|
|
|
|
Color(0xFFFFFFFF), |
|
|
|
|
], |
|
|
|
|
stops: [0, 0.2, 0.4, 1], |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
child: SmartRefresher( |
|
|
|
|
enablePullDown: true, |
|
|
|
|
enablePullUp: false, |
|
|
|
|
header: MyHeader(), |
|
|
|
@ -249,25 +256,8 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
onLoading: () { |
|
|
|
|
_refresh(); |
|
|
|
|
}, |
|
|
|
|
child: Container( |
|
|
|
|
// color: Colors.white, |
|
|
|
|
decoration: BoxDecoration( |
|
|
|
|
gradient: LinearGradient( |
|
|
|
|
begin: Alignment.topCenter, |
|
|
|
|
end: Alignment.bottomCenter, |
|
|
|
|
colors: [ |
|
|
|
|
Color(0xFFD9FFDE), |
|
|
|
|
Color(0xFFD9FFDE), |
|
|
|
|
Color(0xFFFFFFFF), |
|
|
|
|
Color(0xFFFFFFFF), |
|
|
|
|
], |
|
|
|
|
stops: [0, 0.2, 0.4, 1], |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
child: SingleChildScrollView( |
|
|
|
|
physics: BouncingScrollPhysics(), |
|
|
|
|
child: Container( |
|
|
|
|
padding: EdgeInsets.only(bottom: 30.h), |
|
|
|
|
child: Column( |
|
|
|
|
children: [ |
|
|
|
|
Container( |
|
|
|
@ -275,7 +265,8 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
top: MediaQuery.of(context).padding.top + 12.h, |
|
|
|
|
bottom: 15.h, |
|
|
|
|
right: 16.w, |
|
|
|
|
left: 16.w), |
|
|
|
|
left: 16.w, |
|
|
|
|
), |
|
|
|
|
child: Row( |
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween, |
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center, |
|
|
|
@ -293,8 +284,8 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
GestureDetector( |
|
|
|
|
behavior: HitTestBehavior.opaque, |
|
|
|
|
onTap: () { |
|
|
|
|
Navigator.of(context) |
|
|
|
|
.pushNamed('/router/chat_friend_group').then((value) { |
|
|
|
|
Navigator.of(context).pushNamed('/router/chat_friend_group') |
|
|
|
|
.then((value) { |
|
|
|
|
_refresh(); |
|
|
|
|
}); |
|
|
|
|
}, |
|
|
|
@ -317,13 +308,11 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
), |
|
|
|
|
imPageSearch(), |
|
|
|
|
chatList(), |
|
|
|
|
// buildMessage(),fgg |
|
|
|
|
], |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -372,12 +361,13 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
Widget chatList() { |
|
|
|
|
return Container( |
|
|
|
|
child: SlidableAutoCloseBehavior( |
|
|
|
|
child: ListView.builder( |
|
|
|
|
child: ListView( |
|
|
|
|
padding: EdgeInsets.only(top: 16), |
|
|
|
|
itemCount: userIds.length, |
|
|
|
|
shrinkWrap: true, |
|
|
|
|
physics: NeverScrollableScrollPhysics(), |
|
|
|
|
itemBuilder: (context, position) { |
|
|
|
|
children: conversationIds.map((e) { |
|
|
|
|
debugPrint("---conversationIds: ${e}"); |
|
|
|
|
int position = conversationIds.indexOf(e); |
|
|
|
|
return ClipRRect( |
|
|
|
|
// borderRadius: BorderRadius.all(Radius.circular(4)), |
|
|
|
|
child: Slidable( |
|
|
|
@ -388,7 +378,7 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
children: [ |
|
|
|
|
CustomSlidableAction( |
|
|
|
|
onPressed: (bc) { |
|
|
|
|
showDelDialog(userIds[position]); |
|
|
|
|
showDelDialog(conversationIds[position]); |
|
|
|
|
}, |
|
|
|
|
backgroundColor: Color(0xFFFB312B), |
|
|
|
|
foregroundColor: Colors.white, |
|
|
|
@ -408,42 +398,41 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
), |
|
|
|
|
], |
|
|
|
|
), |
|
|
|
|
child:GestureDetector( |
|
|
|
|
child: GestureDetector( |
|
|
|
|
behavior: HitTestBehavior.opaque, |
|
|
|
|
onTap: () { |
|
|
|
|
if (userIds[position] == sharedPreferences.getString("userId")) { |
|
|
|
|
SmartDialog.showToast("不能跟自己聊天", alignment: Alignment.center); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
Navigator.of(context).pushNamed( |
|
|
|
|
'/router/chat_details_page', |
|
|
|
|
arguments: { |
|
|
|
|
"toUser": contactMap[userIds[position]], |
|
|
|
|
"toUser": contactMap[conversationIds[position]], |
|
|
|
|
}, |
|
|
|
|
).then((value) { |
|
|
|
|
unreadCountMap[userIds[position]] = 0; |
|
|
|
|
updateLastMessage(userIds[position]); |
|
|
|
|
unreadCountMap[conversationIds[position]] = 0; |
|
|
|
|
updateLastMessage(conversationIds[position]); |
|
|
|
|
_refresh(); |
|
|
|
|
}); |
|
|
|
|
}, |
|
|
|
|
child: chatItem(userIds[position])), |
|
|
|
|
child: chatItem(conversationIds[position]), |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
); |
|
|
|
|
}, |
|
|
|
|
}).toList(), |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Widget chatItem(userId) { |
|
|
|
|
Widget chatItem(conversationId) { |
|
|
|
|
return Container( |
|
|
|
|
padding: EdgeInsets.only(left: 16.w, right: 17.w, bottom: 18.h), |
|
|
|
|
padding: EdgeInsets.only( |
|
|
|
|
left: 16.w, right: 17.w, bottom: 18.h, |
|
|
|
|
), |
|
|
|
|
child: Row( |
|
|
|
|
children: [ |
|
|
|
|
MImage( |
|
|
|
|
!contactMap.containsKey(userId) |
|
|
|
|
!contactMap.containsKey(conversationId) |
|
|
|
|
? null |
|
|
|
|
: contactMap[userId]?.avatar ?? "", |
|
|
|
|
: contactMap[conversationId]?.avatar ?? "", |
|
|
|
|
isCircle: true, |
|
|
|
|
height: 54.h, |
|
|
|
|
width: 54.h, |
|
|
|
@ -462,9 +451,9 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
children: [ |
|
|
|
|
Expanded( |
|
|
|
|
child: Text( |
|
|
|
|
!contactMap.containsKey(userId) |
|
|
|
|
!contactMap.containsKey(conversationId) |
|
|
|
|
? "" |
|
|
|
|
: contactMap[userId]?.nickname ?? "", |
|
|
|
|
: contactMap[conversationId]?.nickname ?? "", |
|
|
|
|
// overflow: TextOverflow.fade, |
|
|
|
|
maxLines: 1, |
|
|
|
|
style: TextStyle( |
|
|
|
@ -475,10 +464,10 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
Text( |
|
|
|
|
lastMessageMap[userId]?.time != null |
|
|
|
|
lastMessageMap[conversationId]?.time != null |
|
|
|
|
? AppUtils.timeFormatter( |
|
|
|
|
DateTime.fromMillisecondsSinceEpoch(num.parse( |
|
|
|
|
lastMessageMap[userId]?.time ?? ""))) |
|
|
|
|
lastMessageMap[conversationId]?.time ?? ""))) |
|
|
|
|
: "", |
|
|
|
|
style: TextStyle( |
|
|
|
|
fontSize: 12.sp, |
|
|
|
@ -495,7 +484,7 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
children: [ |
|
|
|
|
Expanded( |
|
|
|
|
child: Text( |
|
|
|
|
lastMessageMap[userId]?.content ?? "", |
|
|
|
|
lastMessageMap[conversationId]?.content ?? "", |
|
|
|
|
maxLines: 1, |
|
|
|
|
overflow: TextOverflow.ellipsis, |
|
|
|
|
style: TextStyle( |
|
|
|
@ -505,7 +494,7 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
if (unreadCountMap[userId] != null && unreadCountMap[userId] > 0) |
|
|
|
|
if (unreadCountMap[conversationId] != null && unreadCountMap[conversationId] > 0) |
|
|
|
|
Container( |
|
|
|
|
width: 16, |
|
|
|
|
height: 16, |
|
|
|
@ -514,7 +503,7 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
color: Color(0xFFFF441A), |
|
|
|
|
), |
|
|
|
|
child: RoundButton( |
|
|
|
|
text: "${unreadCountMap[userId]}", |
|
|
|
|
text: "${unreadCountMap[conversationId]}", |
|
|
|
|
textColor: Colors.white, |
|
|
|
|
fontWeight: MyFontWeight.regular, |
|
|
|
|
backgroup: Color(0xFFFF441A), |
|
|
|
@ -534,7 +523,7 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///确认删除弹窗 |
|
|
|
|
showDelDialog(userId) { |
|
|
|
|
showDelDialog(conversationId) { |
|
|
|
|
showDialog( |
|
|
|
|
context: context, |
|
|
|
|
builder: (context) { |
|
|
|
@ -560,7 +549,8 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
fontWeight: MyFontWeight.bold, |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
)), |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
// Spacer(), |
|
|
|
|
Container( |
|
|
|
|
height:1.h, |
|
|
|
@ -596,7 +586,7 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
child: GestureDetector( |
|
|
|
|
behavior: HitTestBehavior.opaque, |
|
|
|
|
onTap: () async{ |
|
|
|
|
await hxDatabase.deleteByUser(contactMap[userId]?.mid ?? ""); |
|
|
|
|
await hxDatabase.deleteByUser(conversationId); |
|
|
|
|
_refresh(); |
|
|
|
|
Navigator.of(context).pop(); |
|
|
|
|
}, |
|
|
|
@ -605,10 +595,10 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
|
textAlign: TextAlign.center, |
|
|
|
|
style: TextStyle( |
|
|
|
|
color: Color(0xFFFF370A), |
|
|
|
|
) |
|
|
|
|
) |
|
|
|
|
) |
|
|
|
|
) |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
], |
|
|
|
|
) |
|
|
|
|
], |
|
|
|
|