completeCurrentDocumentAndContinue method

bool completeCurrentDocumentAndContinue({
  1. String? completedSelectedOptionId,
  2. int? completedFlowStep,
})

Implementation

bool completeCurrentDocumentAndContinue({String? completedSelectedOptionId, int? completedFlowStep}) {
  if (_activeChainedDocument != null) {
    _hasCompletedChainedCustomDocuments = true;
  }

  final completedKey = currentDocumentKey;
  if (completedKey == null || completedKey.isEmpty) {
    return false;
  }

  if (completedSelectedOptionId != null) {
    _completedDocumentSelectedOptions[completedKey] = completedSelectedOptionId;
  }

  if (!_completedDocumentKeys.contains(completedKey)) {
    _completedDocumentKeys.add(completedKey);
  }

  final remainingQueue =
      _activeChainedDocument != null && _pendingChainedDocuments.isNotEmpty
          ? _pendingChainedDocuments.skip(1).toList(growable: true)
          : <Map<String, dynamic>>[];

  final excludedKeys = <String>{
    ..._completedDocumentKeys,
    ...remainingQueue.map((document) => document['key']).whereType<String>(),
  };

  // completedFlowStep counts how many unique documents have been completed.
  // If not supplied by the caller, derive it from _completedDocumentKeys
  // (already updated above) so __step__:N triggers work correctly.
  final effectiveFlowStep =
      completedFlowStep ?? _completedDocumentKeys.length;

  final discoveredQueue = getNextChainedDocuments(
    completedKey,
    excludedKeys: excludedKeys.toList(growable: false),
    completedFlowStep: effectiveFlowStep,
    completedSelectedOptionId: completedSelectedOptionId ?? _completedDocumentSelectedOptions[completedKey],
  );

  for (final document in discoveredQueue) {
    final key = document['key'];
    if (key is! String) {
      continue;
    }
    final alreadyQueued = remainingQueue.any((item) => item['key'] == key);
    if (!alreadyQueued) {
      remainingQueue.add(document);
    }
  }

  if (remainingQueue.isNotEmpty) {
    _pendingChainedDocuments =
        List<Map<String, dynamic>>.from(remainingQueue);
    beginChainedDocument(_pendingChainedDocuments.first);
    return true;
  }

  _pendingChainedDocuments = const [];
  _activeChainedDocument = null;
  _selectedChainedDocumentOption = null;
  return false;
}