Skip to content

Commit

Permalink
feat: Profile Image upload
Browse files Browse the repository at this point in the history
  • Loading branch information
SanudaKJ committed Apr 11, 2024
1 parent 7ae9caf commit a3ba8dd
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 71 deletions.
27 changes: 12 additions & 15 deletions lib/data/repositories/user/user_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,17 @@ class UserRepository extends GetxController {
}


Future<String> 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<String> 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;
}
}
}
25 changes: 15 additions & 10 deletions lib/features/event/screens/profile/profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 1 addition & 23 deletions lib/features/event/screens/profile/widgets/editprofile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -82,7 +81,6 @@ class EditProfile extends StatelessWidget {
const SizedBox(
height: 50,
),

Form(
child: Padding(
padding: const EdgeInsets.all(30.0),
Expand Down Expand Up @@ -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,
// ),
// ),
],
),
),
Expand Down
51 changes: 28 additions & 23 deletions lib/features/personalization/controllers/user_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@ 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<UserModel> user = UserModel.empty().obs;
final userRepository = Get.put(UserRepository());


@override
void onInit() {
super.onInit();
fetchUserRecord();
}

// Fetch user record
// Fetch user record
Future<void> fetchUserRecord() async {
try {
profileLodaing.value = true;
Expand All @@ -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<void> 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<void> 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;
}
}
}
}

0 comments on commit a3ba8dd

Please sign in to comment.