import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_auth/firebase_auth.dart'; import '../constants.dart'; import '../enumerations.dart'; import '../models/language.dart'; import '../models/location.dart'; import '../models/user_profile.dart'; class UserService { UserService._(); // Private constructor to prevent instantiation static Future saveUserData(UserCredential userCredential, String email, String firstname, String lastname) async { // create full name String fullName = (firstname.isNotEmpty && lastname.isNotEmpty) ? '$firstname $lastname' : (firstname.isNotEmpty ? firstname : (lastname.isNotEmpty ? lastname : '')); // save user info to users document await FirebaseFirestore.instance .collection(Constants.dbCollectionUsers) .doc(userCredential.user!.uid) .set( { Constants.dbFieldUsersID: userCredential.user!.uid, Constants.dbFieldUsersEmail: email, Constants.dbFieldUsersFirstName: firstname, Constants.dbFieldUsersLastName: lastname, Constants.dbFieldUsersName: fullName, }, ); } static Future> getSkillsFromFirebase( bool skillsSought, String userId) async { // Fetch skills from Firestore DocumentSnapshot userDoc = await FirebaseFirestore.instance .collection(Constants.dbCollectionUsers) .doc(userId) .get(); if (userDoc.exists && userDoc.data() != null) { Map userData = userDoc.data()! as Map; // Explicit cast List? skills; if (skillsSought) { skills = userData[Constants.dbFieldUsersSkillsSought]; } else { skills = userData[ Constants.dbFieldUsersSkills]; //as List?; // Explicit cast } if (skills != null && skills.isNotEmpty) { // Convert skills from strings to enum values List userSkills = skills .map((skill) => SkillOption.values .firstWhere((x) => x.toString() == 'SkillOption.$skill')) .toList(); return userSkills; } } return []; } static Future saveSkillsToFirebase(List selectedOptions, bool skillsSought, String userId) async { try { // Convert enum values to strings, removing leading EnumType with split List skills = selectedOptions .map((option) => option.toString().split('.').last) .toList(); // Update the corresponding 'skills' field in the user's document String keyToUpdate = skillsSought ? Constants.dbFieldUsersSkillsSought : Constants.dbFieldUsersSkills; FirebaseFirestore.instance .collection(Constants.dbCollectionUsers) .doc(userId) .update({keyToUpdate: skills}); return true; } catch (e) { return false; } } /// Get UserProfile for given [userId] static Future getUserProfileById(String userId) async { DocumentSnapshot doc = await FirebaseFirestore.instance .collection(Constants.dbCollectionUsers) .doc(userId) .get(); return UserProfile.fromDocument(doc); } static Future getDataById(String userId) async { FirebaseFirestore firestore = FirebaseFirestore.instance; DocumentSnapshot userDoc = await firestore .collection(Constants.dbCollectionUsers) .doc(userId) .get(); QuerySnapshot languagesSnapshot = await firestore .collection(Constants.dbCollectionUsers) .doc(userId) .collection(Constants.dbCollectionLanguages) .get(); List languages = languagesSnapshot.docs .map((doc) => Language.fromDocument(doc)) .toList(); QuerySnapshot locationsSnapshot = await firestore .collection(Constants.dbCollectionUsers) .doc(userId) .collection(Constants.dbCollectionLocations) .get(); Map locations = { for (var doc in locationsSnapshot.docs) doc.id: MyLocation.fromDocument(doc) }; // Fill UserProfile including its sub collections UserProfile userProfile = UserProfile.fromDocument(userDoc); userProfile.languages.addAll(languages); userProfile.locations.addAll(locations); return userProfile; } }