Skip to content

Commit

Permalink
Fix avatar crash when selecting an invalid file (#673)
Browse files Browse the repository at this point in the history
  • Loading branch information
ademar111190 authored Oct 13, 2023
1 parent bd019d2 commit 9ff40e4
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions lib/routes/home/widgets/drawer/breez_avatar_dialog.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:async';
import 'dart:io';

import 'package:auto_size_text/auto_size_text.dart';
import 'package:breez_translations/breez_translations_locales.dart';
Expand All @@ -15,6 +14,9 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:image/image.dart' as dart_image;
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';
import 'package:logging/logging.dart';

final _log = Logger("BreezAvatarDialog");

class BreezAvatarDialog extends StatefulWidget {
@override
Expand Down Expand Up @@ -130,6 +132,7 @@ class BreezAvatarDialogState extends State<BreezAvatarDialog> {
}

Future<void> saveAvatarChanges() async {
_log.fine("saveAvatarChanges");
final navigator = Navigator.of(context);
final texts = context.texts();
try {
Expand Down Expand Up @@ -158,6 +161,7 @@ class BreezAvatarDialogState extends State<BreezAvatarDialog> {
}

void generateRandomProfile() {
_log.fine("generateRandomProfile");
final DefaultProfile randomUser = generateDefaultProfile();
setState(() {
nameInputController.text = "${randomUser.color} ${randomUser.animal}";
Expand All @@ -169,21 +173,34 @@ class BreezAvatarDialogState extends State<BreezAvatarDialog> {
}

pickImageFromGallery() async {
final ImagePicker picker = ImagePicker();
final XFile? pickedFile = await picker.pickImage(source: ImageSource.gallery);
final File file = File(pickedFile!.path);
CroppedFile? croppedFile = await ImageCropper().cropImage(
sourcePath: file.path,
cropStyle: CropStyle.circle,
aspectRatioPresets: [CropAspectRatioPreset.square],
);
setState(() {
pickedImage = croppedFile;
randomAvatarPath = null;
_log.fine("pickImageFromGallery");
ImagePicker().pickImage(source: ImageSource.gallery).then((pickedFile) {
final pickedFilePath = pickedFile?.path;
_log.fine("pickedFile $pickedFilePath");
if (pickedFilePath != null) {
ImageCropper().cropImage(
sourcePath: pickedFilePath,
cropStyle: CropStyle.circle,
aspectRatioPresets: [CropAspectRatioPreset.square],
).then((croppedFile) {
_log.info("croppedFile ${croppedFile?.path}");
if (croppedFile != null) {
setState(() {
pickedImage = croppedFile;
randomAvatarPath = null;
});
}
}, onError: (error) {
_log.severe("Failed to crop image", error);
});
}
}, onError: (error) {
_log.severe("Failed to pick image", error);
});
}

Future<void> uploadAvatar() async {
_log.fine("uploadAvatar ${pickedImage?.path} $randomAvatarPath");
if (pickedImage != null) {
String imageUrl = await userBloc.uploadImage(await scaleAndFormatPNG());
userBloc.updateProfile(image: imageUrl);
Expand All @@ -193,6 +210,7 @@ class BreezAvatarDialogState extends State<BreezAvatarDialog> {
}

Future<List<int>> scaleAndFormatPNG() async {
_log.fine("scaleAndFormatPNG");
const int scaledSize = 200;
try {
final image = dart_image.decodeImage(await pickedImage!.readAsBytes());
Expand Down

0 comments on commit 9ff40e4

Please sign in to comment.