From ebc09e5969ac6caf6eebeabfd72d5cc43ad18e76 Mon Sep 17 00:00:00 2001 From: emkartal1 Date: Mon, 7 Aug 2023 13:16:17 +0200 Subject: [PATCH] add functions for update image and pseudo :white_check_mark: --- .../justMUSIC/lib/services/UserService.dart | 26 ++++++++++++++++++- .../lib/view_model/UserViewModel.dart | 20 ++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/Sources/justMUSIC/lib/services/UserService.dart b/Sources/justMUSIC/lib/services/UserService.dart index acd3f72..c5753b6 100644 --- a/Sources/justMUSIC/lib/services/UserService.dart +++ b/Sources/justMUSIC/lib/services/UserService.dart @@ -1,4 +1,7 @@ - import 'package:cloud_firestore/cloud_firestore.dart'; + import 'dart:io'; + +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:firebase_storage/firebase_storage.dart'; import '../main.dart'; @@ -59,4 +62,25 @@ class UserService { MyApp.userViewModel.userCurrent.followed.add(id); } } + + updateImage(File image) async { + var id = MyApp.userViewModel.userCurrent.id; + var userRef = await MyApp.db.collection("posts").doc(MyApp.userViewModel.userCurrent.id); + var imageRef = FirebaseStorage.instance.ref('$id.jpg'); + await imageRef.putFile(image); + var imageUrl = await imageRef.getDownloadURL(); + userRef.update({"picture": imageUrl}); + } + + updatePseudo(String pseudo) async { + FirebaseFirestore.instance + .collection('users') + .doc(MyApp.userViewModel.userCurrent.pp) + .update({'pseudo': pseudo}).then((_) { + print("Mise à jour réussie !"); + }).catchError((error) { + print("Erreur lors de la mise à jour : $error"); + }); + } + } diff --git a/Sources/justMUSIC/lib/view_model/UserViewModel.dart b/Sources/justMUSIC/lib/view_model/UserViewModel.dart index 964beac..34278f2 100644 --- a/Sources/justMUSIC/lib/view_model/UserViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/UserViewModel.dart @@ -1,3 +1,5 @@ +import 'dart:io'; + import 'package:firebase_auth/firebase_auth.dart' as firebase_auth; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter/foundation.dart'; @@ -110,4 +112,22 @@ class UserViewModel { bool isFriend(String id) { return _userCurrent.followed.contains(id); } + + updateImage(File pp) async { + try { + await _userService.updateImage(pp); + } catch(e) { + print(e.toString()); + rethrow; + } + } + + updatePseudo(String pseudo) async { + try { + await _userService.updatePseudo(pseudo); + } catch(e) { + print(e.toString()); + rethrow; + } + } }