Hibok
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

113 linhas
3.1 KiB

  1. import 'package:chat/models/ChatMsg.dart';
  2. import 'package:chat/models/last_message_model.dart';
  3. import 'package:chat/proto/all.pbserver.dart';
  4. import 'package:chat/utils/MessageMgr.dart';
  5. import 'package:flutter/material.dart';
  6. class LastMsgProvider with ChangeNotifier {
  7. List<LastMessageModel> lastMsgList = [];
  8. addMsg(MsgModel msgModel) {
  9. var lastMsg = getLastRecordBy(msgModel.sessionId);
  10. //服务器消息优先级高
  11. if (lastMsg == null) {
  12. lastMsg = LastMessageModel.fromChatMsg(msgModel);
  13. lastMsgList.insert(0, lastMsg);
  14. if (msgModel.channelType == ChatChannelType.Group.value) {
  15. MessageMgr().emit('Update Group List');
  16. } else {
  17. MessageMgr().emit('Update LastMsg',lastMsg.sessionId);
  18. }
  19. }
  20. }
  21. sortLastMsg() {
  22. lastMsgList.sort((b, a) => a.updateAt.compareTo(b.updateAt));
  23. }
  24. updateLastMsg(MsgModel msgModel) {
  25. var lastMsg = getLastRecordBy(msgModel.sessionId);
  26. if (lastMsg != null) {
  27. //更新
  28. lastMsg.updateWithMessage(msgModel);
  29. lastMsgList.sort((b, a) => a.updateAt.compareTo(b.updateAt));
  30. } else {
  31. lastMsg = LastMessageModel.fromChatMsg(msgModel);
  32. lastMsgList.insert(0, lastMsg);
  33. }
  34. if (msgModel.channelType == ChatChannelType.Group.value) {
  35. MessageMgr().emit('Update Group List');
  36. } else {
  37. MessageMgr().emit('Update LastMsg',lastMsg.sessionId);
  38. }
  39. }
  40. updateWithTranslateMsg(PushChat chat) {
  41. //更新
  42. if (chat.cType == ChatType.TextChatType) {
  43. print('更新最新数据的翻译结果');
  44. var lastMsg = getLastRecordBy(chat.targetId);
  45. if (lastMsg != null && chat.hasTencentTranslate()) {
  46. lastMsg.msgContent = chat.tencentTranslate;
  47. }
  48. if (chat.channelType == ChatChannelType.Group) {
  49. MessageMgr().emit('Update Group List');
  50. } else {
  51. MessageMgr().emit('Update LastMsg',lastMsg.sessionId);
  52. }
  53. }
  54. }
  55. addUnreadMsg(UserUnreadMsgNotice unread) {
  56. var lastMsg = getLastRecordBy(unread.targetId);
  57. if (lastMsg == null) {
  58. lastMsg = LastMessageModel.fromUnreadMsg(unread);
  59. lastMsgList.insert(0, lastMsg);
  60. //可能是新增的用户
  61. } else {
  62. lastMsg.updateWithUnreadMsg(unread);
  63. lastMsgList.sort((b, a) => a.updateAt.compareTo(b.updateAt));
  64. }
  65. if (unread.channelType == ChatChannelType.Group) {
  66. MessageMgr().emit('Update Group List');
  67. } else {
  68. MessageMgr().emit('Update LastMsg',lastMsg.sessionId);
  69. }
  70. }
  71. deleteMsg(LastMessageModel msgModel) {
  72. lastMsgList.remove(msgModel);
  73. if (msgModel.channelType == ChatChannelType.Group.value) {
  74. MessageMgr().emit('Update Group List');
  75. } else {
  76. MessageMgr().emit('Update LastMsg',msgModel.sessionId);
  77. }
  78. }
  79. LastMessageModel getLastRecordBy(int sessionId) {
  80. for (var i = 0; i < lastMsgList.length; i++) {
  81. var lastMsg = lastMsgList[i];
  82. if (lastMsg.sessionId == sessionId) {
  83. return lastMsg;
  84. }
  85. }
  86. return null;
  87. }
  88. clear() {
  89. lastMsgList.clear();
  90. }
  91. }