submitAvatar method

Future<void> submitAvatar()

Uploads the current edits (avatarCrop, avatarImage and avatarDeleted).

Implementation

Future<void> submitAvatar() async {
  if (avatarCrop.value != null ||
      avatarImage.value != null ||
      avatarDeleted.value) {
    if (avatarCrop.value == null && chat?.chat.value.avatar?.crop != null) {
      avatarCrop.value = CropAreaInput(
        bottomRight: PointInput(
          x: chat!.chat.value.avatar!.crop!.bottomRight.x,
          y: chat!.chat.value.avatar!.crop!.bottomRight.y,
        ),
        topLeft: PointInput(
          x: chat!.chat.value.avatar!.crop!.topLeft.x,
          y: chat!.chat.value.avatar!.crop!.topLeft.y,
        ),
        angle: chat?.chat.value.avatar?.crop?.angle,
      );
    }

    if (avatarImage.value == null && !avatarDeleted.value) {
      final ImageFile? file = chat?.chat.value.avatar?.original;

      if (file != null) {
        final CacheEntry cache = await CacheWorker.instance.get(
          url: file.url,
          checksum: file.checksum,
        );

        avatarImage.value = NativeFile(
          name: file.name,
          size: cache.bytes!.lengthInBytes,
          bytes: cache.bytes,
        );
      }
    }

    if (avatarImage.value != null || avatarDeleted.value) {
      await _chatService.updateChatAvatar(
        chatId,
        file: avatarImage.value,
        crop: avatarCrop.value,
      );
    }
  }

  avatarImage.value = null;
  avatarCrop.value = null;
  avatarDeleted.value = false;
}