Hibok
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

87 рядки
2.0 KiB

  1. import 'package:chat/data/constants.dart';
  2. import 'package:chat/utils/MessageMgr.dart';
  3. import 'package:chat/utils/sp_utils.dart';
  4. class UnreadCountProvider {
  5. //未读消息条数管理
  6. Map<int, int> unreadCountMap = {};
  7. //是否有@我的消息
  8. Map<int, int> isHaveAlterme = {};
  9. updateUnreadCount(int sessionId, int count) {
  10. if (unreadCountMap[sessionId] == null) {
  11. unreadCountMap[sessionId] = 0;
  12. }
  13. unreadCountMap[sessionId] += count;
  14. MessageMgr().emit('Update UnreadCount', sessionId);
  15. }
  16. initUnreadAlter() async {
  17. //初始化@消息
  18. List<String> alterList = await SPUtils.getStringList(Constants.GroupAlterKey) ?? [];
  19. for (int i = 0; i < alterList.length; i++) {
  20. var list = alterList[i].split('-');
  21. if (list.length == 2) {
  22. isHaveAlterme[int.parse(list[0])] = int.parse(list[1]);
  23. }
  24. }
  25. }
  26. getHavaAltertime(int sessionId) {
  27. return isHaveAlterme[sessionId] ?? null;
  28. }
  29. setAlterMe(int sessionId, int time) {
  30. if (isHaveAlterme[sessionId] == null) {
  31. isHaveAlterme[sessionId] = time;
  32. saveGroupAlterToLocal();
  33. }
  34. }
  35. saveGroupAlterToLocal() {
  36. List<String> alterList = [];
  37. isHaveAlterme.forEach((k, v) {
  38. if (v != null) {
  39. alterList.add('$k-$v');
  40. }
  41. });
  42. SPUtils.saveList(Constants.GroupAlterKey, alterList);
  43. }
  44. signUnreadAlter(int sessionId) {
  45. isHaveAlterme[sessionId] = null;
  46. saveGroupAlterToLocal();
  47. }
  48. clear() {
  49. unreadCountMap.clear();
  50. }
  51. int getUnreadCount(int sessionId) {
  52. return unreadCountMap[sessionId] ?? 0;
  53. }
  54. bool checkUnreadMsg() {
  55. for (var v in unreadCountMap.values) {
  56. if (v > 0) {
  57. return true;
  58. }
  59. }
  60. return false;
  61. }
  62. signRead(int sessionId) {
  63. print('消除未读标记:$sessionId');
  64. int count = getUnreadCount(sessionId);
  65. signUnreadAlter(sessionId);
  66. if (count > 0) {
  67. unreadCountMap[sessionId] = 0;
  68. MessageMgr().emit('Update UnreadCount', sessionId);
  69. }
  70. }
  71. }