Commit 62614cb8 authored by Tuan Anh Nguyen's avatar Tuan Anh Nguyen
Browse files

feat: add profile picture editing

parent 9f5e0255
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ class IoCContainer {
    get.registerSingleton(
      AuthService(
        get<UserService>(),
        get<MediaService>(),
      ),
    );
    get.registerSingleton(
+28 −3
Original line number Diff line number Diff line
@@ -67,8 +67,21 @@ class _ProfilePageState extends State<ProfilePage> {
                        minWidth: 10,
                        minHeight: 10,
                      ),
                      // TODO: add profile picture editing
                      child: _buildEditButton(() {}),
                      child: _buildEditButton(() {
                        try {
                          _authService.changeProfilePicture();
                        } on Exception catch (e) {
                          if (context.mounted) {
                            ScaffoldMessenger.of(context).showSnackBar(
                              SnackBar(
                                content: Text(
                                  e.toString(),
                                ),
                              ),
                            );
                          }
                        }
                      }),
                    ),
                  )
                ],
@@ -99,7 +112,19 @@ class _ProfilePageState extends State<ProfilePage> {
                          return;
                        }

                        try {
                          _authService.changeDisplayName(newDisplayName);
                        } on Exception catch (e) {
                          if (context.mounted) {
                            ScaffoldMessenger.of(context).showSnackBar(
                              SnackBar(
                                content: Text(
                                  e.toString(),
                                ),
                              ),
                            );
                          }
                        }
                      },
                    ),
                  ],
+32 −10
Original line number Diff line number Diff line
import 'dart:io';

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/foundation.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:image_picker/image_picker.dart';
import 'package:stamped/data/entities/user.dart' as stamped;
import 'package:stamped/data/enums/image_type.dart';
import 'package:stamped/service/media_service.dart';
import 'package:stamped/service/user_service.dart';

class AuthService {
  final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;

  final UserService _userService;
  final MediaService _mediaService;

  Stream<User?> get userStream => _firebaseAuth.userChanges();

@@ -22,7 +28,7 @@ class AuthService {

  stamped.User get currentUser => _currentUser;

  AuthService(this._userService);
  AuthService(this._userService, this._mediaService);

  Future<UserCredential> signInWithCredential(
    AuthCredential credential,
@@ -61,16 +67,32 @@ class AuthService {
  }

  Future<void> changeDisplayName(String newName) async {
    try {
    final currentAuthUser = await userStream.first;
    currentAuthUser!.updateDisplayName(newName);

    final user = await _userService.findByGoogleId(currentAuthUser.uid);
    _userService.changeUserName(id: user.id, newName: newName);
    } on Exception catch (e) {
      if (kDebugMode) {
        print(e);
  }

  Future<void> changeProfilePicture() async {
    final pickedFile =
        await ImagePicker().pickImage(source: ImageSource.gallery);

    if (pickedFile == null) {
      return;
    }

    final imageRef = await _mediaService.uploadImage(
      File(pickedFile.path),
      imageType: ImageType.profilePicture,
    );

    final imageUrl = await imageRef!.getDownloadURL();

    final currentAuthUser = await userStream.first;
    currentAuthUser!.updatePhotoURL(imageUrl);

    final user = await _userService.findByGoogleId(currentAuthUser.uid);
    _userService.changeProfilePicture(id: user.id, pictureUrl: imageUrl);
  }
}
+18 −3
Original line number Diff line number Diff line
@@ -8,8 +8,10 @@ import 'package:stamped/data/enums/image_type.dart';

class MediaService {
  final _postcardImagesRef =
      FirebaseStorage.instance.ref().child('postcardImages');
      FirebaseStorage.instance.ref().child('postcard_images');
  final _stampsRef = FirebaseStorage.instance.ref().child('stamps');
  final _profilePicturesRef =
      FirebaseStorage.instance.ref().child('profile_pictures');

  final _countryCollection =
      FirebaseFirestore.instance.collection('countries').withConverter(
@@ -37,8 +39,21 @@ class MediaService {
    File file, {
    required ImageType imageType,
  }) async {
    final storageRef =
        imageType == ImageType.postcard ? _postcardImagesRef : _stampsRef;
    late final Reference storageRef;

    switch (imageType) {
      case ImageType.postcard:
        storageRef = _postcardImagesRef;
        break;
      case ImageType.stamp:
        storageRef = _stampsRef;
        break;
      case ImageType.profilePicture:
        storageRef = _profilePicturesRef;
        break;
      default:
        break;
    }
    final fileName = _genUniqueFileName(file);
    final uploadTask = await storageRef.child(fileName).putFile(file);

+15 −0
Original line number Diff line number Diff line
@@ -136,4 +136,19 @@ class UserService {
      print(e);
    }
  }

  void changeProfilePicture({
    required String id,
    required String pictureUrl,
  }) async {
    try {
      _userCollection.doc(id).update(
        {
          'profilePicture': pictureUrl,
        },
      );
    } catch (e) {
      print(e);
    }
  }
}