This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
/
conversation_list_page.dart
194 lines (173 loc) · 6.7 KB
/
conversation_list_page.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import 'dart:async';
import 'dart:core';
import 'dart:developer' as developer;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:rongcloud_im_plugin/rongcloud_im_plugin.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../other/login_page.dart';
import '../util/dialog_util.dart';
import '../util/event_bus.dart';
import '../util/style.dart';
import 'item/conversation_list_item.dart';
import 'item/widget_util.dart';
class ConversationListPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return new _ConversationListPageState();
}
}
class _ConversationListPageState extends State<ConversationListPage> implements ConversationListItemDelegate {
String pageName = "example.ConversationListPage";
List conList = [];
List<int> displayConversationType = [
RCConversationType.Private,
RCConversationType.Group,
];
ScrollController? _scrollController;
double mPosition = 0;
@override
void initState() {
super.initState();
addIMhandler();
updateConversationList();
EventBus.instance!.addListener(EventKeys.ConversationPageDispose, (arg) {
Timer(Duration(milliseconds: 10), () {
addIMhandler();
updateConversationList();
_renfreshUI();
});
});
}
@override
void dispose() {
super.dispose();
EventBus.instance!.removeListener(EventKeys.ConversationPageDispose);
}
updateConversationList() async {
List? list = await RongIMClient.getConversationList(displayConversationType);
if (list != null) {
// list.sort((a,b) => b.sentTime.compareTo(a.sentTime));
conList = list;
}
_renfreshUI();
}
void _renfreshUI() {
if (mounted) setState(() {});
}
addIMhandler() {
EventBus.instance!.addListener(EventKeys.ReceiveMessage, (map) {
Message msg = map["message"];
int? left = map["left"];
bool hasPackage = map["hasPackage"];
bool isDisplayConversation = msg.conversationType != null && displayConversationType.contains(msg.conversationType);
//如果离线消息过多,那么可以等到 hasPackage 为 false 并且 left == 0 时更新会话列表
if (!hasPackage && left == 0 && isDisplayConversation) {
updateConversationList();
}
});
RongIMClient.onConnectionStatusChange = (int? connectionStatus) {
if (RCConnectionStatus.KickedByOtherClient == connectionStatus || RCConnectionStatus.TokenIncorrect == connectionStatus || RCConnectionStatus.UserBlocked == connectionStatus) {
String toast = "连接状态变化 $connectionStatus, 请退出后重新登录";
DialogUtil.showAlertDiaLog(context, toast,
confirmButton: TextButton(
onPressed: () async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.remove("token");
Navigator.of(context).pushAndRemoveUntil(new MaterialPageRoute(builder: (context) => new LoginPage()), (route) => route == null);
},
child: Text("重新登录")));
} else if (RCConnectionStatus.Connected == connectionStatus) {
updateConversationList();
}
};
RongIMClient.onRecallMessageReceived = (Message? message) {
updateConversationList();
};
RongIMClient.onDatabaseOpened = (int? status) {
updateConversationList();
};
}
void _deleteConversation(Conversation conversation) {
//删除会话需要刷新会话列表数据
RongIMClient.removeConversation(conversation.conversationType!, conversation.targetId!, (bool? success) {
if (success!) {
updateConversationList();
// // 如果需要删除会话中的消息调用下面的接口
// RongIMClient.deleteMessages(
// conversation.conversationType, conversation.targetId, (int code) {
// updateConversationList();
// });
}
});
}
void _clearConversationUnread(Conversation conversation) async {
//清空未读需要刷新会话列表数据
bool success = await (RongIMClient.clearMessagesUnreadStatus(conversation.conversationType!, conversation.targetId!) as FutureOr<bool>);
if (success) {
updateConversationList();
}
}
void _setConversationToTop(Conversation conversation, bool isTop) {
RongIMClient.setConversationToTop(conversation.conversationType!, conversation.targetId!, isTop, (bool? status, int? code) {
if (code == 0) {
updateConversationList();
}
});
}
void _addScroolListener() {
_scrollController!.addListener(() {
mPosition = _scrollController!.position.pixels;
});
}
Widget _buildConversationListView() {
return new ListView.builder(
scrollDirection: Axis.vertical,
itemCount: conList.length,
controller: _scrollController,
itemBuilder: (BuildContext context, int index) {
if (conList.length <= 0) {
return WidgetUtil.buildEmptyWidget();
}
return ConversationListItem(delegate: this, conversation: conList[index]);
},
);
}
@override
Widget build(BuildContext context) {
this._scrollController = ScrollController(initialScrollOffset: mPosition);
_addScroolListener();
return new Scaffold(
appBar: AppBar(
title: Text("RongCloud IM"),
),
key: UniqueKey(),
body: _buildConversationListView(),
);
}
@override
void didLongPressConversation(Conversation? conversation, Offset? tapPos) {
Map<String, String> actionMap = {RCLongPressAction.DeleteConversationKey: RCLongPressAction.DeleteConversationValue, RCLongPressAction.ClearUnreadKey: RCLongPressAction.ClearUnreadValue, RCLongPressAction.SetConversationToTopKey: conversation!.isTop! ? RCLongPressAction.CancelConversationToTopValue : RCLongPressAction.SetConversationToTopValue};
WidgetUtil.showLongPressMenu(context, tapPos!, actionMap, (String? key) {
developer.log("当前选中的是 " + key!, name: pageName);
if (key == RCLongPressAction.DeleteConversationKey) {
_deleteConversation(conversation);
} else if (key == RCLongPressAction.ClearUnreadKey) {
_clearConversationUnread(conversation);
} else if (key == RCLongPressAction.SetConversationToTopKey) {
bool isTop = true;
if (conversation.isTop!) {
isTop = false;
}
_setConversationToTop(conversation, isTop);
} else {
developer.log("未实现操作 " + key, name: pageName);
}
});
}
@override
void didTapConversation(Conversation? conversation) {
Map arg = {"coversationType": conversation!.conversationType, "targetId": conversation.targetId};
Navigator.pushNamed(context, "/conversation", arguments: arg);
}
}