You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
527 lines
16 KiB
527 lines
16 KiB
import 'dart:async'; |
|
import 'dart:collection'; |
|
|
|
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'; |
|
import 'package:huixiang/im/out/message.pb.dart'; |
|
import 'package:huixiang/main.dart'; |
|
import 'package:huixiang/retrofit/data/base_data.dart'; |
|
import 'package:huixiang/retrofit/data/msg_stats.dart'; |
|
import 'package:huixiang/retrofit/data/page.dart'; |
|
import 'package:huixiang/retrofit/retrofit_api.dart'; |
|
import 'package:huixiang/utils/font_weight.dart'; |
|
import 'package:huixiang/view_widget/classic_header.dart'; |
|
import 'package:huixiang/view_widget/my_footer.dart'; |
|
import 'package:huixiang/view_widget/round_button.dart'; |
|
import 'package:intl/intl.dart'; |
|
import 'package:pull_to_refresh/pull_to_refresh.dart'; |
|
import 'package:shared_preferences/shared_preferences.dart'; |
|
import 'package:flutter_screenutil/flutter_screenutil.dart'; |
|
import '../../retrofit/data/im_user_list.dart'; |
|
import '../../utils/flutter_utils.dart'; |
|
import '../../view_widget/custom_image.dart'; |
|
import 'on_chat_message.dart'; |
|
import 'on_chat_msg_instance.dart'; |
|
|
|
class IMPage extends StatefulWidget { |
|
IMPage(Key key) : super(key: key); |
|
|
|
@override |
|
State<StatefulWidget> createState() { |
|
return _IMPage(); |
|
} |
|
} |
|
|
|
class _IMPage extends State<IMPage> implements OnChatMessage { |
|
ApiService apiService; |
|
int pageNum = 1; |
|
List<Message> messages = []; |
|
Map<String, int> msgNumber = { |
|
"1": 0, |
|
"2": 0, |
|
"3": 0, |
|
"4": 0, |
|
"5": 0, |
|
"6": 0, |
|
}; |
|
int state = 0; |
|
final TextEditingController imEditingController = TextEditingController(); |
|
|
|
@override |
|
void onMessage(txt) { |
|
// SmartDialog.showToast("列表 $txt", alignment: Alignment.center); |
|
} |
|
|
|
@override |
|
void dispose() { |
|
super.dispose(); |
|
OnChatMsgInstance.instance.onChatMessage = null; |
|
|
|
socketClient.removeCallback(socketClient.userId); |
|
} |
|
|
|
@override |
|
void initState() { |
|
super.initState(); |
|
OnChatMsgInstance.instance.onChatMessage = this; |
|
|
|
loadMessageList(); |
|
SharedPreferences.getInstance().then((value) { |
|
apiService = |
|
ApiService(Dio(), token: value.getString("token"), context: context); |
|
queryMsgStats(); |
|
}); |
|
} |
|
|
|
_refresh() { |
|
pageNum = 1; |
|
loadMessageList(); |
|
queryMsgStats(); |
|
} |
|
|
|
List<String> userIds = []; |
|
Map<String, Message> lastMessageMap = {}; |
|
Map<String, int> unreadCountMap = {}; |
|
Map<String, ImUserList> contactMap = {}; |
|
|
|
loadMessageList() async { |
|
SharedPreferences shared = await SharedPreferences.getInstance(); |
|
String userId = shared.getString("userId"); |
|
|
|
debugPrint("messages: loadMessageList"); |
|
socketClient.addCallback(userId, (Message message) { |
|
if (userIds.contains(message.fromId)) { |
|
userIds.remove(message.fromId); |
|
} |
|
userIds.insert(0, message.fromId); |
|
|
|
lastMessageMap[message.fromId] = message; |
|
|
|
debugPrint("messages_records : ${message.toJson()}"); |
|
if (contactMap[message.fromId] == null) { |
|
queryMemberInfo([message.fromId]); |
|
return; |
|
} |
|
if (mounted) { |
|
setState(() {}); |
|
} |
|
}); |
|
|
|
debugPrint("messages: queryList"); |
|
messages = await hxDatabase.queryList(userId); |
|
messages.forEach((element) { |
|
debugPrint("messages: ${element.toJson()}"); |
|
}); |
|
userIds = messages |
|
.map((e) => e.toId != userId ? e.toId : e.fromId) |
|
.toSet() |
|
.where((element) => element != userId) |
|
.toList(); |
|
List<ImUserList> contacts = (await hxDatabase.queryImUser(userIds)) ?? []; |
|
|
|
unreadCountMap = await hxDatabase.messageUnreadCount(userIds, userId); |
|
lastMessageMap = messages.lGroupBy((p0) => p0.toId != userId ? p0.toId : p0.fromId).mGroupItem; |
|
|
|
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(() {}); |
|
} |
|
} |
|
|
|
void updateLastMessage(String userId) async { |
|
Message message = await hxDatabase.lastMessage(userId); |
|
if (message != null) { |
|
lastMessageMap[userId] = message; |
|
refreshState(); |
|
} |
|
} |
|
|
|
void updateUnreadCount() async { |
|
SharedPreferences shared = await SharedPreferences.getInstance(); |
|
String userId = shared.getString("userId"); |
|
unreadCountMap = await hxDatabase.messageUnreadCount(userIds, userId); |
|
refreshState(); |
|
} |
|
|
|
refreshState() { |
|
if (mounted) setState(() {}); |
|
} |
|
|
|
// queryMessage() async { |
|
// BaseData<PageInfo<Message>> baseData = await apiService.msgList({ |
|
// "pageNum": pageNum, |
|
// "pageSize": 10, |
|
// "searchKey": "", |
|
// "state": "", |
|
// "typed": "" |
|
// }).catchError((onError) { |
|
// _refreshController.loadFailed(); |
|
// _refreshController.refreshFailed(); |
|
// }); |
|
// |
|
// if (baseData != null && baseData.isSuccess) { |
|
// if (pageNum == 1) { |
|
// messages.clear(); |
|
// } |
|
// List<Message> message = []; |
|
// message.addAll(baseData.data.list); |
|
// message.forEach((element) { |
|
// if (element.typed == 2 || element.typed == 3) { |
|
// messages.add(element); |
|
// } |
|
// }); |
|
// _refreshController.loadComplete(); |
|
// _refreshController.refreshCompleted(); |
|
// if (mounted) setState(() {}); |
|
// if (pageNum * 10 > int.tryParse(baseData.data.total)) { |
|
// _refreshController.loadNoData(); |
|
// } else { |
|
// pageNum += 1; |
|
// } |
|
// } else { |
|
// _refreshController.loadFailed(); |
|
// _refreshController.refreshFailed(); |
|
// } |
|
// } |
|
|
|
queryMsgStats() async { |
|
if (apiService == null) { |
|
SharedPreferences value = await SharedPreferences.getInstance(); |
|
apiService = ApiService( |
|
Dio(), |
|
context: context, |
|
token: value.getString("token"), |
|
); |
|
} |
|
BaseData<List<MsgStats>> baseData = await apiService.stats().catchError((onError) { |
|
debugPrint("stats.error: $onError"); |
|
}); |
|
if (baseData != null && baseData.isSuccess) { |
|
setState(() { |
|
msgNumber.forEach((key, value) { |
|
msgNumber[key] = 0; |
|
}); |
|
baseData.data.forEach((element) { |
|
if (msgNumber.containsKey(element.name)) { |
|
msgNumber[element.name] = element.number; |
|
} |
|
}); |
|
}); |
|
_refreshController.loadComplete(); |
|
_refreshController.refreshCompleted(); |
|
} |
|
EasyLoading.dismiss(); |
|
} |
|
|
|
///批量查询用户信息 |
|
queryMemberInfo(List<String> mids) async { |
|
BaseData<List<ImUserList>> baseData = await apiService.memberInfoByIds({ |
|
"mids": mids, |
|
}).catchError((error) { |
|
SmartDialog.showToast(AppUtils.dioErrorTypeToString(error.type), |
|
alignment: Alignment.center); |
|
}); |
|
if (baseData != null && baseData.isSuccess) { |
|
baseData.data.forEach((element) async { |
|
await hxDatabase.insertOrUpdateImUser(element.toJson()); |
|
}); |
|
contactMap = baseData.data.lGroupBy((p0) => p0.mid).mGroupItem; |
|
setState(() {}); |
|
} else { |
|
SmartDialog.showToast(baseData.msg, alignment: Alignment.center); |
|
} |
|
} |
|
|
|
RefreshController _refreshController = RefreshController(); |
|
|
|
@override |
|
Widget build(BuildContext context) { |
|
return Scaffold( |
|
backgroundColor: Color(0xFFFFFFFF), |
|
body: SmartRefresher( |
|
enablePullDown: true, |
|
enablePullUp: false, |
|
header: MyHeader(), |
|
physics: BouncingScrollPhysics(), |
|
footer: CustomFooter( |
|
loadStyle: LoadStyle.ShowWhenLoading, |
|
builder: (BuildContext context, LoadStatus mode) { |
|
return (messages.length == 0) ? Container() : MyFooter(mode); |
|
}, |
|
), |
|
controller: _refreshController, |
|
onRefresh: _refresh, |
|
onLoading: () { |
|
setState(() { |
|
_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( |
|
padding: EdgeInsets.only( |
|
top: MediaQuery.of(context).padding.top + 12.h, |
|
bottom: 15.h, |
|
right: 16.w, |
|
left: 16.w), |
|
child: Row( |
|
mainAxisAlignment: MainAxisAlignment.spaceBetween, |
|
crossAxisAlignment: CrossAxisAlignment.center, |
|
children: [ |
|
Expanded( |
|
child: Text( |
|
S.of(context).xiaoxi, |
|
style: TextStyle( |
|
color: Colors.black, |
|
fontSize: 18.sp, |
|
fontWeight: MyFontWeight.bold, |
|
), |
|
), |
|
), |
|
GestureDetector( |
|
behavior: HitTestBehavior.opaque, |
|
onTap: () { |
|
Navigator.of(context) |
|
.pushNamed('/router/chat_friend_group').then((value) { |
|
_refresh(); |
|
}); |
|
}, |
|
child: Container( |
|
padding: EdgeInsets.all(12), |
|
decoration: BoxDecoration( |
|
color: Color(0xFFFFFFFF), |
|
borderRadius: BorderRadius.circular(20.r), |
|
), |
|
child: Image.asset( |
|
"assets/image/friend_grouping.webp", |
|
fit: BoxFit.fill, |
|
height: 14.h, |
|
width: 14.h, |
|
), |
|
), |
|
), |
|
], |
|
), |
|
), |
|
imSearchItem(), |
|
chatList(), |
|
// buildMessage(),fgg |
|
], |
|
), |
|
), |
|
), |
|
), |
|
), |
|
); |
|
} |
|
|
|
///搜索 |
|
Widget imSearchItem() { |
|
return Container( |
|
margin: EdgeInsets.fromLTRB(16.w, 0, 16.w, 0), |
|
padding: EdgeInsets.symmetric(vertical: 13.h), |
|
decoration: BoxDecoration( |
|
color: Color(0xFFFFFFFF), |
|
borderRadius: BorderRadius.circular(4), |
|
boxShadow: [ |
|
BoxShadow( |
|
color: Colors.black.withAlpha(12), |
|
offset: Offset(0, 3), |
|
blurRadius: 14, |
|
spreadRadius: 0, |
|
), |
|
], |
|
), |
|
child: TextField( |
|
textInputAction: TextInputAction.search, |
|
onEditingComplete: () { |
|
FocusScope.of(context).requestFocus(FocusNode()); |
|
}, |
|
controller: imEditingController, |
|
style: TextStyle( |
|
fontSize: 14.sp, |
|
), |
|
decoration: InputDecoration( |
|
hintText: "搜索", |
|
hintStyle: TextStyle( |
|
fontSize: 14.sp, |
|
color: Color(0xFFA29E9E), |
|
), |
|
isCollapsed: true, |
|
prefixIcon: Padding( |
|
padding: EdgeInsets.only(left: 15.w, right: 5.w), |
|
child: Image.asset( |
|
"assets/image/icon_search.webp", |
|
width: 14.h, |
|
height: 14.h, |
|
color: Color(0xFF353535), |
|
), |
|
), |
|
prefixIconConstraints: BoxConstraints(), |
|
border: InputBorder.none, |
|
), |
|
), |
|
); |
|
} |
|
|
|
///聊天列表 |
|
Widget chatList() { |
|
return Container( |
|
child: ListView.builder( |
|
padding: EdgeInsets.only(top: 16), |
|
itemCount: userIds.length, |
|
shrinkWrap: true, |
|
physics: NeverScrollableScrollPhysics(), |
|
itemBuilder: (context, position) { |
|
return GestureDetector( |
|
behavior: HitTestBehavior.opaque, |
|
onTap: () { |
|
Navigator.of(context).pushNamed( |
|
'/router/chat_details_page', |
|
arguments: { |
|
"toUser": contactMap[userIds[position]], |
|
}, |
|
).then((value) { |
|
updateLastMessage(userIds[position]); |
|
_refresh(); |
|
}); |
|
}, |
|
child: chatItem(userIds[position]), |
|
); |
|
}, |
|
), |
|
); |
|
} |
|
|
|
Widget chatItem(userId) { |
|
return Container( |
|
padding: EdgeInsets.only(left: 16.w, right: 17.w, bottom: 18.h), |
|
child: Row( |
|
children: [ |
|
MImage( |
|
!contactMap.containsKey(userId) |
|
? null |
|
: contactMap[userId]?.avatar ?? "", |
|
isCircle: true, |
|
height: 54.h, |
|
width: 54.h, |
|
fit: BoxFit.cover, |
|
errorSrc: "assets/image/fuka_zj.webp", |
|
fadeSrc: "assets/image/fuka_zj.webp", |
|
), |
|
SizedBox( |
|
width: 12.w, |
|
), |
|
Expanded( |
|
child: Column( |
|
crossAxisAlignment: CrossAxisAlignment.start, |
|
children: [ |
|
Row( |
|
children: [ |
|
Expanded( |
|
child: Text( |
|
!contactMap.containsKey(userId) |
|
? "" |
|
: contactMap[userId]?.nickname ?? "", |
|
// overflow: TextOverflow.fade, |
|
maxLines: 1, |
|
style: TextStyle( |
|
fontSize: 16.sp, |
|
color: Color(0xFF060606), |
|
fontWeight: MyFontWeight.semi_bold, |
|
), |
|
), |
|
), |
|
Text( |
|
lastMessageMap[userId]?.time != null |
|
? TimeFormatter.formatTime( |
|
DateTime.fromMillisecondsSinceEpoch(num.parse( |
|
lastMessageMap[userId]?.time ?? ""))) |
|
: "", |
|
style: TextStyle( |
|
fontSize: 12.sp, |
|
color: Color(0xFFA29E9E), |
|
fontWeight: MyFontWeight.regular, |
|
), |
|
), |
|
], |
|
), |
|
SizedBox( |
|
height: 7.h, |
|
), |
|
Row( |
|
children: [ |
|
Expanded( |
|
child: Text( |
|
lastMessageMap[userId]?.content ?? "", |
|
maxLines: 1, |
|
overflow: TextOverflow.ellipsis, |
|
style: TextStyle( |
|
fontSize: 12.sp, |
|
color: Color(0xFF353535), |
|
fontWeight: MyFontWeight.regular, |
|
), |
|
), |
|
), |
|
Container( |
|
width: 16, |
|
height: 16, |
|
decoration: BoxDecoration( |
|
borderRadius: BorderRadius.circular(100), |
|
color: Color(0xFFFF441A), |
|
), |
|
child: RoundButton( |
|
text: "99", |
|
textColor: Colors.white, |
|
fontWeight: MyFontWeight.regular, |
|
backgroup: Color(0xFFFF441A), |
|
fontSize: 10.sp, |
|
radius: 100, |
|
), |
|
), |
|
], |
|
), |
|
], |
|
), |
|
), |
|
], |
|
), |
|
); |
|
} |
|
}
|
|
|