From a3ba8ddd9c39cac268a643c24a224ae4d3a0c551 Mon Sep 17 00:00:00 2001 From: SanudaKJ Date: Thu, 11 Apr 2024 21:26:02 +0530 Subject: [PATCH] feat: Profile Image upload --- .../repositories/user/user_repository.dart | 27 +++++----- .../event/screens/profile/profile.dart | 25 +++++---- .../screens/profile/widgets/editprofile.dart | 24 +-------- .../controllers/user_controller.dart | 51 ++++++++++--------- 4 files changed, 56 insertions(+), 71 deletions(-) diff --git a/lib/data/repositories/user/user_repository.dart b/lib/data/repositories/user/user_repository.dart index 1f6aa4d..b575e1e 100644 --- a/lib/data/repositories/user/user_repository.dart +++ b/lib/data/repositories/user/user_repository.dart @@ -118,20 +118,17 @@ class UserRepository extends GetxController { } - Future uploadImage(String path, XFile image) async { - try { - final ref = FirebaseStorage.instance.ref(path).child(image.name); - await ref.putFile(File(image.path)); - final url = await ref.getDownloadURL(); - return url; - } on FirebaseException catch (e) { - throw TFirebaseException(e.code).message; - } on FormatException catch (_) { - throw const TFormatException(); - } on PlatformException catch (e) { - throw TPlatformException(e.code).message; - } catch (e) { - throw "Something went wrong"; - } + Future uploadImage(String path, XFile image) async { + try { + print('Uploading image...'); + final ref = FirebaseStorage.instance.ref(path).child(image.name); + await ref.putFile(File(image.path)); + final url = await ref.getDownloadURL(); + print('Image uploaded successfully. URL: $url'); + return url; + } catch (e) { + print('Error uploading image: $e'); + throw e; } } +} diff --git a/lib/features/event/screens/profile/profile.dart b/lib/features/event/screens/profile/profile.dart index 184f00d..2383f51 100644 --- a/lib/features/event/screens/profile/profile.dart +++ b/lib/features/event/screens/profile/profile.dart @@ -24,16 +24,21 @@ class ProfileScreen extends StatelessWidget { child: Center( child: Column( children: [ - SizedBox( - width: 150, - height: 150, - child: ClipRRect( - borderRadius: BorderRadius.circular(100), - child: const Image( - image: NetworkImage( - 'https://avatar.iran.liara.run/public/21')), - ), - ), + Obx(() { + if (controller.profileLodaing.value) { + return const CircularProgressIndicator(); + } + return SizedBox( + width: 150, + height: 150, + child: ClipRRect( + borderRadius: BorderRadius.circular(100), + child: Image( + image: NetworkImage( + controller.user.value.profilePicture)), + ), + ); + }), const SizedBox(height: TSizes.md), Obx(() { if (controller.profileLodaing.value) { diff --git a/lib/features/event/screens/profile/widgets/editprofile.dart b/lib/features/event/screens/profile/widgets/editprofile.dart index 7adbfc4..535e23a 100644 --- a/lib/features/event/screens/profile/widgets/editprofile.dart +++ b/lib/features/event/screens/profile/widgets/editprofile.dart @@ -59,8 +59,7 @@ class EditProfile extends StatelessWidget { right: 0, child: InkWell( onTap: () { - // Add your code here - // controller.updateProfilePicture(); + controller.updateProfilePicture(); }, child: Container( width: 35, @@ -82,7 +81,6 @@ class EditProfile extends StatelessWidget { const SizedBox( height: 50, ), - Form( child: Padding( padding: const EdgeInsets.all(30.0), @@ -172,26 +170,6 @@ class EditProfile extends StatelessWidget { ), ), ) - - // TextFormField( - // decoration: const InputDecoration( - // prefixIcon: Padding( - // padding: EdgeInsets.all(8.0), - // child: Padding( - // padding: EdgeInsets.only(left: 20), - // child: Icon(Iconsax.password_check), - // ), - // ), - // suffixIcon: Padding( - // padding: EdgeInsets.all(8.0), - // child: Padding( - // padding: EdgeInsets.only(right: 20), - // child: Icon(Iconsax.eye_slash), - // ), - // ), - // labelText: TTexts.password, - // ), - // ), ], ), ), diff --git a/lib/features/personalization/controllers/user_controller.dart b/lib/features/personalization/controllers/user_controller.dart index a670c81..94fb426 100644 --- a/lib/features/personalization/controllers/user_controller.dart +++ b/lib/features/personalization/controllers/user_controller.dart @@ -8,11 +8,9 @@ import 'package:uni_junction/features/personalization/models/user_model.dart'; class UserController extends GetxController { static UserController get instance => Get.find(); - final profileLodaing = false.obs; Rx user = UserModel.empty().obs; final userRepository = Get.put(UserRepository()); - @override void onInit() { @@ -20,7 +18,7 @@ class UserController extends GetxController { fetchUserRecord(); } - // Fetch user record + // Fetch user record Future fetchUserRecord() async { try { profileLodaing.value = true; @@ -34,33 +32,40 @@ class UserController extends GetxController { } } - // Update user updateRecord(UserModel user) async { await userRepository.updateUser(user); } - - // Update profile picture // Update profile picture -// Future updateProfilePicture() async { -// final ImagePicker _picker = ImagePicker(); -// final XFile? image = await _picker.pickImage(source: ImageSource.gallery); - -// if (image != null) { -// profileLodaing.value = true; -// try { -// final imageUrl = await userRepository.uploadImage('profileImages/${user.value.id}', image); -// final updatedUser = user.value.copyWith(profilePicture: imageUrl); // Corrected here -// await userRepository.updateUser(updatedUser); -// user(updatedUser); -// } finally { -// profileLodaing.value = false; -// } -// } -// } + Future updateProfilePicture() async { + final ImagePicker _picker = ImagePicker(); + final XFile? image = await _picker.pickImage(source: ImageSource.gallery); + if (image != null) { + profileLodaing.value = true; + try { + print('Updating profile picture...'); + final imageUrl = await userRepository.uploadImage( + 'profileImages/${user.value.id}', image); + print('Profile picture updated. New URL: $imageUrl'); + final updatedUser = UserModel( + id: user.value.id, + firstName: user.value.firstName, + lastName: user.value.lastName, + username: user.value.username, + email: user.value.email, + profilePicture: imageUrl, // updated profile picture + university: user.value.university, + likedEvents: user.value.likedEvents, + ); + await userRepository.updateUser(updatedUser); + user(updatedUser); + } finally { + profileLodaing.value = false; + } + } + } } -