updateAvatar method

  1. @override
Future<void> updateAvatar(
  1. NativeFile? file, {
  2. CropAreaInput? crop,
  3. void onSendProgress(
    1. int count,
    2. int total
    )?,
})
override

Updates or resets the MyUser.avatar field with the provided image file.

Implementation

@override
Future<void> updateAvatar(
  NativeFile? file, {
  CropAreaInput? crop,
  void Function(int count, int total)? onSendProgress,
}) async {
  Log.debug(
    'updateAvatar($file, crop: $crop, onSendProgress)',
    '$runtimeType',
  );

  dio.MultipartFile? upload;

  if (file != null) {
    await file.ensureCorrectMediaType();

    upload = await file.toMultipartFile();
  }

  final UserAvatar? avatar = myUser.value?.avatar;
  if (file == null) {
    myUser.update((u) => u?.avatar = null);
  }

  try {
    await Backoff.run(
      () async {
        await _graphQlProvider.updateUserAvatar(
          upload,
          crop,
          onSendProgress: onSendProgress,
        );
      },
      retryIf: (e) => e.isNetworkRelated,
      retries: 10,
    );
  } catch (_) {
    if (file == null) {
      myUser.update((u) => u?.avatar = avatar);
    }
    rethrow;
  }
}