fetchItemList method

void fetchItemList()

Implementation

void fetchItemList() async {
  if (_isEmpty) {
    return;
  }
  if (_isFetching) return;
  _isFetching = true;
  List<Message> list;
  if (searchedMsg != null && hasSearched == false) {
    List<Message> searchList =
        await ChatUIKit.instance.loadLocalMessagesByTimestamp(
      conversationId: profile.id,
      type: conversationType,
      count: 100,
      startTime: searchedMsg!.serverTime - 100000,
      endTime: DateTime.now().millisecondsSinceEpoch,
    );

    List<Message> beforeSearchList =
        await ChatUIKit.instance.loadLocalMessages(
      conversationId: profile.id,
      type: conversationType,
      count: pageSize,
      startId: searchList.first.msgId,
    );
    list = beforeSearchList + searchList;
    _lastMessageId = list.first.msgId;
    hasSearched = true;
    lastActionType = MessageLastActionType.topPosition;
  } else {
    list = await ChatUIKit.instance.loadLocalMessages(
      conversationId: profile.id,
      type: conversationType,
      count: pageSize,
      startId: _lastMessageId,
    );
    lastActionType = MessageLastActionType.originalPosition;
  }
  if (list.length < pageSize) {
    _isEmpty = true;
  }
  if (list.isNotEmpty) {
    List<MessageModel> modelLists = [];
    _lastMessageId = list.first.msgId;

    for (var msg in list) {
      List<MessageReaction> reactions = await msg.reactionList();
      ChatThread? threadOverView = await msg.chatThread();
      MessagePinInfo? pinInfo = await msg.pinInfo();
      modelLists.add(
        MessageModel(
          message: msg,
          reactions: reactions,
          thread: threadOverView,
          pinInfo: pinInfo,
        ),
      );
      // 先从缓存的profile中取
      ChatUIKitProfile? profile =
          ChatUIKitProvider.instance.getProfileById(msg.from!);
      if (profile != null) {
        userMap[msg.from!] = profile;
      } else {
        ChatUIKitProfile? mapProfile = userMap[msg.from!];
        if ((mapProfile?.timestamp ?? 0) < msg.fromProfile.timestamp) {
          userMap[msg.from!] = msg.fromProfile;
        }
      }
    }
    msgModelList.addAll(modelLists.reversed);
    for (var model in msgModelList) {
      if (ChatUIKitURLHelper().isFetching(model.message.msgId)) {
        model.message.status = MessageStatus.PROGRESS;
      }
    }
    refresh();
  }

  _isFetching = false;
}