processScreenshots method

Future<bool> processScreenshots({
  1. ShareMode? overrideMode,
  2. bool clearAfterProcessing = true,
  3. dynamic onError(
    1. String error
    )?,
  4. dynamic onSuccess()?,
})

Process and share screenshots based on the configured share mode

Implementation

Future<bool> processScreenshots({
  ShareMode? overrideMode,
  bool clearAfterProcessing = true,
  Function(String error)? onError,
  Function()? onSuccess,
}) async {
  // Prevent multiple simultaneous operations
  if (_isSendingProcessing) return false;
  _isSendingProcessing = true;

  try {
    if (_allScreenshots.isEmpty) {
      _handleSendingError('No screenshots to process', onError);
      return false;
    }

    final config = ScreenshotConfig();
    final shareMode = overrideMode ?? config.shareMode;

    debugPrint(
        '๐Ÿ“ค Processing ${_allScreenshots.length} screenshots using mode: $shareMode');

    bool success = false;
    String? errorMessage;

    // Process based on selected share mode
    switch (shareMode) {
      case ShareMode.telegram:
        final zipBytes = await StorageService().createZipArchive(
          _allScreenshots,
          'screenshots',
        );

        final result = await TelegramService().sendDocument(
          fileBytes: zipBytes,
          filename:
              'screenshots_${DateTime.now().millisecondsSinceEpoch}.zip',
          caption: '๐Ÿ“ท Bundle of ${_allScreenshots.length} screenshots',
        );

        success = result.success;
        if (!success) errorMessage = result.errorMessage;
        break;

      case ShareMode.localSave:
        final path =
            await StorageService().saveScreenshotsLocally(_allScreenshots);
        success = path != null;
        if (!success) errorMessage = 'Failed to save to local storage';
        break;

      case ShareMode.shareWithApps:
        success = await StorageService().shareScreenshots(_allScreenshots);
        if (!success) errorMessage = 'Failed to share with external apps';
        break;

      case ShareMode.multiple:
        final zipBytes = await StorageService().createZipArchive(
          _allScreenshots,
          'screenshots',
        );
        final result = await TelegramService().sendDocument(
          fileBytes: zipBytes,
          filename:
              'screenshots_${DateTime.now().millisecondsSinceEpoch}.zip',
        );
        success = result.success;
        if (!success) errorMessage = result.errorMessage;
        break;
    }

    // Clear stored screenshots if requested and successful
    if (clearAfterProcessing && success) {
      _captureGroups.clear();
      _allScreenshots.clear();
      debugPrint('๐Ÿงน Cleared screenshots after processing');
    }

    if (success) {
      if (onSuccess != null) onSuccess();
    } else {
      _handleSendingError(errorMessage ?? 'Unknown error', onError);
    }

    _isSendingProcessing = false;
    return success;
  } catch (e) {
    debugPrint('โŒ ERROR processing screenshots: $e');
    _handleSendingError(e.toString(), onError);
    _isSendingProcessing = false;
    return false;
  }
}