Skip to content

Commit

Permalink
Merge pull request #191 from les-crepes/167-ajouter-photos-dans-firebase
Browse files Browse the repository at this point in the history
167 ajouter photos dans firebase
  • Loading branch information
LucaCoduriV authored Sep 6, 2022
2 parents a3f101f + 6136344 commit 40a932d
Show file tree
Hide file tree
Showing 24 changed files with 305 additions and 111 deletions.
Binary file added assets/images/default_user_pic.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions lib/api/firebase_user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class FirebaseUser extends FirebaseAPI implements IUser {
lastName: doc['lastName'],
phoneNumber: doc['phoneNumber'],
email: doc['email'],
photoUrl: doc['photoUrl'],
uid: doc['uid']))
.toList();
return clients;
Expand Down
3 changes: 3 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/services.dart';
import 'package:get_it/get_it.dart';
import 'package:pdg_app/api/firebase_file.dart';
import 'package:pdg_app/provider/auth_provider.dart';
import 'package:pdg_app/router/auth_gard.dart';
import 'package:pdg_app/router/chat_guard.dart';
Expand All @@ -21,6 +23,7 @@ Future<void> setup() async {
AuthProvider(
auth: FirebaseConnection(),
clientApi: FirebaseUser(FirebaseFirestore.instance),
fileApi: FirebaseFile(FirebaseStorage.instance),
),
);
await GetIt.I.get<AuthProvider>().init();
Expand Down
30 changes: 27 additions & 3 deletions lib/provider/auth_provider.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:pdg_app/api/iauth.dart';
import 'package:pdg_app/api/ifile.dart';
import 'package:pdg_app/api/iuser.dart';

import '../model/user.dart';

class AuthProvider extends ChangeNotifier {
final Auth _auth;
final IUser _userApi;
final IFile _fileApi;
bool _isAdmin = false;

User? _client;
User? _clientDietitian;

AuthProvider({required Auth auth, required IUser clientApi})
AuthProvider(
{required Auth auth, required IUser clientApi, required IFile fileApi})
: _auth = auth,
_userApi = clientApi;
_userApi = clientApi,
_fileApi = fileApi;

Future<void> init() async {
if (isConnected()) {
Expand Down Expand Up @@ -70,10 +75,29 @@ class AuthProvider extends ChangeNotifier {
notifyListeners();
}

// Return the profile pic URL
Future<String?> uploadProfilePic(XFile? pic, String userUid) async {
String? picUrl;
if (pic != null) {
String path = "images/profile/$userUid.jpg";
picUrl = await _fileApi.uploadFile(pic.path, path);
log(picUrl);
log(pic.path);
}

return picUrl;
}

/// register a new user using [email] and [password] as credentials.
Future<void> register(String email, String password, User user) async {
Future<void> register(
String email, String password, User user, XFile? pic) async {
String? picUrl = await uploadProfilePic(pic, user.uid);

await _auth.register(email: email, password: password);
user.uid = _auth.uid;

user.photoUrl = picUrl;

await _userApi.createUser(user);
_client = user;
notifyListeners();
Expand Down
29 changes: 27 additions & 2 deletions lib/provider/meal_provider.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import 'dart:developer';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/foundation.dart';
import 'package:image_picker/image_picker.dart';
import 'package:pdg_app/api/firebase_file.dart';
import 'package:pdg_app/api/firebase_meal.dart';
import 'package:pdg_app/api/imeal.dart';

import '../api/ifile.dart';
import '../model/meal.dart';

class MealProvider extends ChangeNotifier {
final IMeal _mealApi = FirebaseMeal(FirebaseFirestore.instance);
final String _uid;
bool _isFetching = false;
final IFile _fileApi = FirebaseFile(FirebaseStorage.instance);

MealProvider(this._uid) {
fetchMeals();
Expand Down Expand Up @@ -37,13 +44,31 @@ class MealProvider extends ChangeNotifier {
}).toList();
}

Future<void> addMeal(Meal meal) async {
Future<void> addMeal(Meal meal, XFile? pic) async {
String? picUrl = await uploadMealPic(pic, meal.uid);
meal.photo = picUrl;
await _mealApi.createMeal(meal);
notifyListeners();
}

Future<void> updateMeal(Meal meal) async {
Future<void> updateMeal(Meal meal, XFile? pic) async {
// Not really optimized to reload each time but is working
String? picUrl = await uploadMealPic(pic, meal.uid);
meal.photo = picUrl;
await _mealApi.updateMeal(meal);
notifyListeners();
}

// Return the meal pic URL
Future<String?> uploadMealPic(XFile? pic, String mealUid) async {
String? picUrl;
if (pic != null) {
String path = "images/diary/$mealUid.jpg";
picUrl = await _fileApi.uploadFile(pic.path, path);
log(picUrl);
log(pic.path);
}

return picUrl;
}
}
4 changes: 3 additions & 1 deletion lib/router/router.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:auto_route/auto_route.dart';
import 'package:auto_route/empty_router_widgets.dart';
import 'package:image_picker/image_picker.dart';
import 'package:pdg_app/model/aftercare.dart';
import 'package:pdg_app/router/chat_guard.dart';
import 'package:pdg_app/screens/add_meal.dart';
Expand All @@ -14,6 +15,7 @@ import 'package:pdg_app/screens/profile.dart';
import 'package:pdg_app/screens/register.dart';
import 'package:pdg_app/widgets/register/register_first_page.dart';
import 'package:pdg_app/widgets/register/register_second_page.dart';
import 'package:tuple/tuple.dart';

import '../model/meal.dart';
import '../screens/home.dart';
Expand Down Expand Up @@ -89,7 +91,7 @@ import 'home_guard.dart';
path: '',
page: DiaryScreen,
),
AutoRoute<Meal?>(
AutoRoute<Tuple2<Meal?, XFile?>>(
path: 'add',
page: AddMealScreen,
),
Expand Down
47 changes: 25 additions & 22 deletions lib/router/router.gr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'package:auto_route/auto_route.dart' as _i18;
import 'package:auto_route/empty_router_widgets.dart' as _i5;
import 'package:flutter/foundation.dart' as _i27;
import 'package:flutter/material.dart' as _i19;
import 'package:image_picker/image_picker.dart' as _i26;
import 'package:tuple/tuple.dart' as _i24;

import '../model/aftercare.dart' as _i23;
import '../model/meal.dart' as _i24;
import '../model/user.dart' as _i25;
import '../model/meal.dart' as _i25;
import '../model/user.dart' as _i28;
import '../screens/add_meal.dart' as _i14;
import '../screens/chat.dart' as _i7;
import '../screens/client_list.dart' as _i10;
Expand Down Expand Up @@ -129,7 +132,7 @@ class AppRouter extends _i18.RootStackRouter {
},
AddMealScreenRoute.name: (routeData) {
final args = routeData.argsAs<AddMealScreenRouteArgs>();
return _i18.MaterialPageX<_i24.Meal?>(
return _i18.MaterialPageX<_i24.Tuple2<_i25.Meal?, _i26.XFile?>>(
routeData: routeData,
child: _i14.AddMealScreen(
day: args.day, meal: args.meal, key: args.key));
Expand Down Expand Up @@ -289,7 +292,7 @@ class ProfileScreenRoute extends _i18.PageRouteInfo<void> {
/// generated route for
/// [_i7.ChatScreen]
class ChatScreenRoute extends _i18.PageRouteInfo<ChatScreenRouteArgs> {
ChatScreenRoute({_i19.Key? key, _i25.User? otherUser})
ChatScreenRoute({_i27.Key? key, _i28.User? otherUser})
: super(ChatScreenRoute.name,
path: 'onechat',
args: ChatScreenRouteArgs(key: key, otherUser: otherUser));
Expand All @@ -300,9 +303,9 @@ class ChatScreenRoute extends _i18.PageRouteInfo<ChatScreenRouteArgs> {
class ChatScreenRouteArgs {
const ChatScreenRouteArgs({this.key, this.otherUser});

final _i19.Key? key;
final _i27.Key? key;

final _i25.User? otherUser;
final _i28.User? otherUser;

@override
String toString() {
Expand All @@ -323,7 +326,7 @@ class DiscussionListScreenRoute extends _i18.PageRouteInfo<void> {
/// [_i9.DocumentListScreen]
class DocumentListScreenRoute
extends _i18.PageRouteInfo<DocumentListScreenRouteArgs> {
DocumentListScreenRoute({_i19.Key? key, required _i25.User user})
DocumentListScreenRoute({_i27.Key? key, required _i28.User user})
: super(DocumentListScreenRoute.name,
path: 'documents',
args: DocumentListScreenRouteArgs(key: key, user: user));
Expand All @@ -334,9 +337,9 @@ class DocumentListScreenRoute
class DocumentListScreenRouteArgs {
const DocumentListScreenRouteArgs({this.key, required this.user});

final _i19.Key? key;
final _i27.Key? key;

final _i25.User user;
final _i28.User user;

@override
String toString() {
Expand Down Expand Up @@ -375,7 +378,7 @@ class ClientListScreenRoute extends _i18.PageRouteInfo<void> {
/// [_i11.ClientRecordScreen]
class ClientRecordScreenRoute
extends _i18.PageRouteInfo<ClientRecordScreenRouteArgs> {
ClientRecordScreenRoute({required _i25.User user, _i19.Key? key})
ClientRecordScreenRoute({required _i28.User user, _i27.Key? key})
: super(ClientRecordScreenRoute.name,
path: 'record',
args: ClientRecordScreenRouteArgs(user: user, key: key));
Expand All @@ -386,9 +389,9 @@ class ClientRecordScreenRoute
class ClientRecordScreenRouteArgs {
const ClientRecordScreenRouteArgs({required this.user, this.key});

final _i25.User user;
final _i28.User user;

final _i19.Key? key;
final _i27.Key? key;

@override
String toString() {
Expand All @@ -401,7 +404,7 @@ class ClientRecordScreenRouteArgs {
class UpdateClientRecordScreenRoute
extends _i18.PageRouteInfo<UpdateClientRecordScreenRouteArgs> {
UpdateClientRecordScreenRoute(
{required dynamic user, _i23.Aftercare? aftercare, _i19.Key? key})
{required dynamic user, _i23.Aftercare? aftercare, _i27.Key? key})
: super(UpdateClientRecordScreenRoute.name,
path: 'update',
args: UpdateClientRecordScreenRouteArgs(
Expand All @@ -418,7 +421,7 @@ class UpdateClientRecordScreenRouteArgs {

final _i23.Aftercare? aftercare;

final _i19.Key? key;
final _i27.Key? key;

@override
String toString() {
Expand All @@ -429,7 +432,7 @@ class UpdateClientRecordScreenRouteArgs {
/// generated route for
/// [_i13.DiaryScreen]
class DiaryScreenRoute extends _i18.PageRouteInfo<DiaryScreenRouteArgs> {
DiaryScreenRoute({_i25.User? client, _i19.Key? key})
DiaryScreenRoute({_i28.User? client, _i27.Key? key})
: super(DiaryScreenRoute.name,
path: '', args: DiaryScreenRouteArgs(client: client, key: key));

Expand All @@ -439,9 +442,9 @@ class DiaryScreenRoute extends _i18.PageRouteInfo<DiaryScreenRouteArgs> {
class DiaryScreenRouteArgs {
const DiaryScreenRouteArgs({this.client, this.key});

final _i25.User? client;
final _i28.User? client;

final _i19.Key? key;
final _i27.Key? key;

@override
String toString() {
Expand All @@ -452,7 +455,7 @@ class DiaryScreenRouteArgs {
/// generated route for
/// [_i14.AddMealScreen]
class AddMealScreenRoute extends _i18.PageRouteInfo<AddMealScreenRouteArgs> {
AddMealScreenRoute({required DateTime day, _i24.Meal? meal, _i19.Key? key})
AddMealScreenRoute({required DateTime day, _i25.Meal? meal, _i27.Key? key})
: super(AddMealScreenRoute.name,
path: 'add',
args: AddMealScreenRouteArgs(day: day, meal: meal, key: key));
Expand All @@ -465,9 +468,9 @@ class AddMealScreenRouteArgs {

final DateTime day;

final _i24.Meal? meal;
final _i25.Meal? meal;

final _i19.Key? key;
final _i27.Key? key;

@override
String toString() {
Expand All @@ -487,7 +490,7 @@ class RegisterFirstPageRoute extends _i18.PageRouteInfo<void> {
/// [_i16.RegisterSecondPage]
class RegisterSecondPageRoute
extends _i18.PageRouteInfo<RegisterSecondPageRouteArgs> {
RegisterSecondPageRoute({_i19.Key? key})
RegisterSecondPageRoute({_i27.Key? key})
: super(RegisterSecondPageRoute.name,
path: '1', args: RegisterSecondPageRouteArgs(key: key));

Expand All @@ -497,7 +500,7 @@ class RegisterSecondPageRoute
class RegisterSecondPageRouteArgs {
const RegisterSecondPageRouteArgs({this.key});

final _i19.Key? key;
final _i27.Key? key;

@override
String toString() {
Expand Down
Loading

0 comments on commit 40a932d

Please sign in to comment.