toggleMessageReaction method

Future<NIMResult<void>> toggleMessageReaction(
  1. ChatMessage message,
  2. int index
)

切换当前账号对消息的指定表情快捷回复

Implementation

Future<NIMResult<void>> toggleMessageReaction(
  ChatMessage message,
  int index,
) async {
  final identity = MessageReactionHelper.messageIdentity(message.nimMessage);
  final accountId = currentAccountId;
  if (!MessageReactionHelper.isSupported(message) ||
      identity == null ||
      accountId?.isNotEmpty != true ||
      !MessageReactionConfig.supportedIndexes.contains(index)) {
    return NIMResult<void>.failure(
      message: 'message reaction is not supported',
    );
  }
  final hasConnectivity =
      await (_messageReactionConnectivityChecker?.call() ??
          haveConnectivity());
  if (!hasConnectivity) {
    return NIMResult<void>.failure(
      code: messageReactionNetworkUnavailableCode,
      message: 'network unavailable',
    );
  }
  if (_isDisposed) {
    return NIMResult<void>.failure(message: 'chat page disposed');
  }
  final operationKey = '$identity:$index';
  final state =
      _messageReactionStates[identity] ?? MessageReactionState.empty;
  final currentSelected = state.summaries[index]?.contains(accountId) == true;
  _reactionConfirmedSelections.putIfAbsent(
    operationKey,
    () => currentSelected,
  );
  _reactionConfirmedCreateTimes.putIfAbsent(
    operationKey,
    () => state.summaries[index]?.operatorCreateTimes[accountId],
  );
  _reactionDesiredSelections[operationKey] = !currentSelected;
  _setSelfReactionSelection(
    identity,
    index,
    accountId!,
    !currentSelected,
    pending: true,
    createTime: DateTime.now().millisecondsSinceEpoch,
  );
  notifyListeners();

  final completer = Completer<NIMResult<void>>();
  _reactionOperationCompleters
      .putIfAbsent(operationKey, () => <Completer<NIMResult<void>>>[])
      .add(completer);
  if (_runningReactionOperations.add(operationKey)) {
    unawaited(_runReactionOperation(
      operationKey,
      identity,
      index,
      message.nimMessage,
      accountId,
    ));
  }
  return completer.future;
}