setImageAndUpload method

Future<void> setImageAndUpload(
  1. File image,
  2. BuildContext context
)

Implementation

Future<void> setImageAndUpload(File image, BuildContext context) async {
  if (_isDisposed) return;

  final attemptNumber = ++_uploadCounter;
  _stopAllTimers();

  if (_isDisposed || !canUpdateState) return;

  // Extract context-dependent values before async operations
  final documentStrings = DocumentStrings(context);
  final imageProcessingError = documentStrings.imageProcessingError;
  final connectionError = documentStrings.connectionError;

  try {
    await _performUpload(image, attemptNumber, connectionError);
  } catch (e) {
    if (attemptNumber == _uploadCounter && canUpdateState) {
      _safeStateUpdate(() => state.copyWith(
        isUploading: false,
        isWaitingForWebSocket: false,
        currentProgress: 0.0,
      ));

      if (canUpdateState) {
        final verificationNotifier = _ref.read(verificationStateProvider.notifier);
        verificationNotifier.showErrorMessage(imageProcessingError);
      }
    }
  }
}