sendText method

Future<void> sendText(
  1. String text, {
  2. Uint8List? cameraImage,
})

Sends text data to the server (Atomic Message)

Parameters:

  • text: Text message to send
  • cameraImage: Optional camera image to include in the same message (Atomic)

Implementation

Future<void> sendText(String text, {Uint8List? cameraImage}) async {
  if (text.isEmpty) {
    return;
  }

  final message = <String, dynamic>{
    'mime_type': 'text/plain',
    'data': text,
  };

  // ATOMIC MESSAGE: Embed image directly if provided
  if (cameraImage != null && cameraImage.isNotEmpty) {
    try {
      // Compress image in background
      final compressedImage = await compute(_compressImageInIsolate, {
        'imageData': cameraImage,
        'maxSizeKb': 256,
        'quality': 70,
      });

      message['image_data'] = base64Encode(compressedImage);
      message['image_mime_type'] = 'image/jpeg';
      message['include_camera_image'] =
          true; // Legacy flag, kept for compatibility

      debugPrint(
          '[CLIENT ATOMIC]: Text + Image (${compressedImage.length} bytes) sent together');
    } catch (e) {
      debugPrint('[CLIENT ERROR]: Failed to compress atomic image: $e');
    }
  }

  if (_channel == null || !isConnected) {
    await _ensureConnected();
  }

  _sendMessage(message);
}