sendImageFile method

Future<void> sendImageFile(
  1. BuildContext context,
  2. File file
)

Implementation

Future<void> sendImageFile(BuildContext context, File file) async {
  Uint8List imageBytes = file.readAsBytesSync();
  var size = imageBytes.length;
  if (size > 512000) {
    ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
        content: Text('Image exceeds the maximum limit of 512KB. Please try with an image of lower size.')));
  } else {
    var key = AtKey()
      ..key = chatImageKey + (isGroupChat ? groupChatId! : '') + DateTime.now().millisecondsSinceEpoch.toString()
      ..sharedBy = currentAtSign!
      ..sharedWith = chatWithAtSign
      ..metadata = Metadata();
    key.metadata.ccd = true;
    key.metadata.ttr = -1;
    key.metadata.isBinary = true;

    if (isGroupChat) {
      await Future.forEach(groupChatMembers!, (dynamic member) async {
        if (member != currentAtSign) {
          key.sharedWith = member;
          await atClientManager.atClient.put(key, imageBytes);
          // send notification for groupChat
        }
      });
    } else {
      await atClientManager.atClient.put(key, imageBytes);
      //send notification
    }
    await setChatHistory(
        Message(
          message: key.toString(),
          sender: currentAtSign,
          time: DateTime.now().millisecondsSinceEpoch,
          type: MessageType.outgoing,
          contentType: MessageContentType.image,
        ),
        imageData: imageBytes);
  }
}