|
- import 'package:chat/data/constants.dart';
- import 'package:chat/utils/MessageMgr.dart';
- import 'package:chat/utils/sp_utils.dart';
-
- class UnreadCountProvider {
- //未读消息条数管理
- Map<int, int> unreadCountMap = {};
-
- //是否有@我的消息
- Map<int, int> isHaveAlterme = {};
-
- updateUnreadCount(int sessionId, int count) {
- if (unreadCountMap[sessionId] == null) {
- unreadCountMap[sessionId] = 0;
- }
- unreadCountMap[sessionId] += count;
-
- MessageMgr().emit('Update UnreadCount', sessionId);
- }
-
- initUnreadAlter() async {
- //初始化@消息
- List<String> alterList = await SPUtils.getStringList(Constants.GroupAlterKey) ?? [];
- for (int i = 0; i < alterList.length; i++) {
- var list = alterList[i].split('-');
- if (list.length == 2) {
- isHaveAlterme[int.parse(list[0])] = int.parse(list[1]);
- }
- }
- }
-
- getHavaAltertime(int sessionId) {
- return isHaveAlterme[sessionId] ?? null;
- }
-
- setAlterMe(int sessionId, int time) {
- if (isHaveAlterme[sessionId] == null) {
- isHaveAlterme[sessionId] = time;
- saveGroupAlterToLocal();
- }
- }
-
- saveGroupAlterToLocal() {
- List<String> alterList = [];
- isHaveAlterme.forEach((k, v) {
- if (v != null) {
- alterList.add('$k-$v');
- }
- });
- SPUtils.saveList(Constants.GroupAlterKey, alterList);
- }
-
- signUnreadAlter(int sessionId) {
- isHaveAlterme[sessionId] = null;
- saveGroupAlterToLocal();
- }
-
- clear() {
- unreadCountMap.clear();
- }
-
- int getUnreadCount(int sessionId) {
- return unreadCountMap[sessionId] ?? 0;
- }
-
- bool checkUnreadMsg() {
- for (var v in unreadCountMap.values) {
- if (v > 0) {
- return true;
- }
- }
- return false;
- }
-
- signRead(int sessionId) {
- print('消除未读标记:$sessionId');
-
- int count = getUnreadCount(sessionId);
- signUnreadAlter(sessionId);
- if (count > 0) {
- unreadCountMap[sessionId] = 0;
-
- MessageMgr().emit('Update UnreadCount', sessionId);
- }
- }
- }
|