From 17e097d48397d97cc7a970d6e3af21bf904e51f8 Mon Sep 17 00:00:00 2001 From: Rafael <1024481@stud.hs-mannheim.de> Date: Mon, 17 Jun 2024 12:51:30 +0200 Subject: [PATCH] UserProfile.fromDocument did not set languages and locations. Replaced with getUserProfileFromDocument. --- lib/models/location.dart | 10 +-- lib/models/user_profile.dart | 48 -------------- lib/pages/user_matching_page.dart | 55 +--------------- lib/services/user_service.dart | 103 +++++++++++++++++++++++------- 4 files changed, 87 insertions(+), 129 deletions(-) diff --git a/lib/models/location.dart b/lib/models/location.dart index 0ced8a1..40f6c0a 100644 --- a/lib/models/location.dart +++ b/lib/models/location.dart @@ -32,14 +32,14 @@ class MyLocation { factory MyLocation.fromDocument(DocumentSnapshot doc) { Map data = doc.data() as Map; return MyLocation( - street: data[Constants.dbFieldLocationStreet] ?? '', - country: data[Constants.dbFieldLocationCountry] ?? '', + street: data[Constants.dbFieldLocationStreet], + country: data[Constants.dbFieldLocationCountry], administrativeArea: data[Constants.dbFieldLocationArea], - locality: data[Constants.dbFieldLocationLocality] ?? '', + locality: data[Constants.dbFieldLocationLocality], subLocality: data[Constants.dbFieldLocationSubLocality], postalCode: data[Constants.dbFieldLocationPostalCode], - latitude: (data[Constants.dbFieldLocationLatitude] as num?)?.toDouble(), - longitude: (data[Constants.dbFieldLocationLongitude] as num?)?.toDouble(), + latitude: data[Constants.dbFieldLocationLatitude], + longitude: data[Constants.dbFieldLocationLongitude], ); } diff --git a/lib/models/user_profile.dart b/lib/models/user_profile.dart index d283ee5..c55a196 100644 --- a/lib/models/user_profile.dart +++ b/lib/models/user_profile.dart @@ -1,8 +1,4 @@ -import 'package:cloud_firestore/cloud_firestore.dart'; - -import '../constants.dart'; import '../enumerations.dart'; -import '../services/user_service.dart'; import 'language.dart'; import 'location.dart'; @@ -50,48 +46,4 @@ class UserProfile { required this.languages, required this.locations, }); - - factory UserProfile.fromDocument(DocumentSnapshot doc) { - Map data = doc.data() as Map; - - List skillsOffered = UserService.convertSkillStringToEnum( - data[Constants.dbFieldUsersSkills]); - List skillsSought = UserService.convertSkillStringToEnum( - data[Constants.dbFieldUsersSkillsSought]); - List visions = UserService.convertVisionStringToEnum( - data[Constants.dbFieldUsersVisions]); - List works = UserService.convertWorkValuesStringToEnum( - data[Constants.dbFieldUsersWorkValues]); - RiskTolerance risk = - RiskTolerance.fromString(data[Constants.dbFieldUsersRiskTolerance]); - AvailabilityOption availability = - AvailabilityOption.fromString(data[Constants.dbFieldUsersAvailability]); - CultureOption culture = - CultureOption.fromString(data[Constants.dbFieldUsersCorpCulture]); - CommunicationPreference communication = CommunicationPreference.fromString( - data[Constants.dbFieldUsersCommunication]); - - return UserProfile( - id: doc.id, - uid: data[Constants.dbFieldUsersID] ?? '', - email: data[Constants.dbFieldUsersEmail] ?? '', - name: data[Constants.dbFieldUsersName] ?? '', - firstName: data[Constants.dbFieldUsersFirstName] ?? '', - lastName: data[Constants.dbFieldUsersLastName] ?? '', - skills: skillsOffered, - skillsSought: skillsSought, - visions: visions, - risk: risk, - availability: availability, - culture: culture, - communication: communication, - workValues: works, - profilePictureUrl: data[Constants.dbFieldUsersProfilePic], - bio: data[Constants.dbFieldUsersBio], - gender: Gender.values[data[Constants.dbFieldUsersGender] ?? 0], - born: data[Constants.dbFieldUsersYearBorn], - languages: [], - locations: {}, - ); - } } diff --git a/lib/pages/user_matching_page.dart b/lib/pages/user_matching_page.dart index 65abe67..b52402b 100644 --- a/lib/pages/user_matching_page.dart +++ b/lib/pages/user_matching_page.dart @@ -9,10 +9,9 @@ import '../components/language_list.dart'; import '../components/text_with_bold.dart'; import '../constants.dart'; import '../forms/matched_screen.dart'; -import '../models/language.dart'; -import '../models/location.dart'; import '../models/user_profile.dart'; import '../services/auth/auth_service.dart'; +import '../services/user_service.dart'; import '../utils/helper.dart'; import '../utils/math.dart'; import 'chat_page.dart'; @@ -90,41 +89,8 @@ class UserMatchingPageState extends State { .toSet(); for (var userDoc in usersSnapshot.docs) { - final languagesSnapshot = await userDoc.reference - .collection(Constants.dbCollectionLanguages) - .get(); - - // get languages - final languages = languagesSnapshot.docs.map((doc) { - final data = doc.data(); - return Language( - code: data['code'], - name: data['name'], - nativeName: data['nativeName'], - iconFile: data['iconFile'], - ); - }).toList(); - - // get locations - final locationsSnapshot = await userDoc.reference - .collection(Constants.dbCollectionLocations) - .get(); - final mainDoc = locationsSnapshot.docs - .firstWhereOrNull((doc) => doc.id == Constants.dbDocMainLocation); - final secondaryDoc = locationsSnapshot.docs - .firstWhereOrNull((doc) => doc.id == Constants.dbDocSecondLocation); - final locations = { - Constants.dbDocMainLocation: - mainDoc != null ? _createLocationFromDoc(mainDoc.data()) : null, - Constants.dbDocSecondLocation: secondaryDoc != null - ? _createLocationFromDoc(secondaryDoc.data()) - : null, - }; - - // create userProfile including the data above - UserProfile userProfile = UserProfile.fromDocument(userDoc); - userProfile.locations.addAll(locations); - userProfile.languages.addAll(languages); + UserProfile userProfile = + await UserService.getUserProfileFromDocument(userDoc); // add profiles accordingly allUsers.add(userProfile); @@ -145,21 +111,6 @@ class UserMatchingPageState extends State { }); } - MyLocation? _createLocationFromDoc(Map? data) { - if (data == null || data.isEmpty) return null; - - return MyLocation( - street: data[Constants.dbFieldLocationStreet], - country: data[Constants.dbFieldLocationCountry], - administrativeArea: data[Constants.dbFieldLocationArea], - locality: data[Constants.dbFieldLocationLocality], - subLocality: data[Constants.dbFieldLocationSubLocality], - postalCode: data[Constants.dbFieldLocationPostalCode], - latitude: data[Constants.dbFieldLocationLatitude], - longitude: data[Constants.dbFieldLocationLongitude], - ); - } - void _swipeLeft() { _controller.next( swipeDirection: SwipeDirection.left, duration: Durations.extralong1); diff --git a/lib/services/user_service.dart b/lib/services/user_service.dart index b1c796c..e1a9666 100644 --- a/lib/services/user_service.dart +++ b/lib/services/user_service.dart @@ -1,4 +1,5 @@ import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:collection/collection.dart'; import 'package:firebase_auth/firebase_auth.dart'; import '../constants.dart'; import '../enumerations.dart'; @@ -116,6 +117,82 @@ class UserService { } } + /// Get UserProfile from Document + static Future getUserProfileFromDocument( + DocumentSnapshot userDoc) async { + Map data = userDoc.data() as Map; + + List skillsOffered = UserService.convertSkillStringToEnum( + data[Constants.dbFieldUsersSkills]); + List skillsSought = UserService.convertSkillStringToEnum( + data[Constants.dbFieldUsersSkillsSought]); + List visions = UserService.convertVisionStringToEnum( + data[Constants.dbFieldUsersVisions]); + List works = UserService.convertWorkValuesStringToEnum( + data[Constants.dbFieldUsersWorkValues]); + RiskTolerance risk = + RiskTolerance.fromString(data[Constants.dbFieldUsersRiskTolerance]); + AvailabilityOption availability = + AvailabilityOption.fromString(data[Constants.dbFieldUsersAvailability]); + CultureOption culture = + CultureOption.fromString(data[Constants.dbFieldUsersCorpCulture]); + CommunicationPreference communication = CommunicationPreference.fromString( + data[Constants.dbFieldUsersCommunication]); + + // Retrieve sub collections + QuerySnapshot languagesSnapshot = await userDoc.reference + .collection(Constants.dbCollectionLanguages) + .get(); + List languages = languagesSnapshot.docs + .map((doc) => Language.fromDocument(doc)) + .toList(); + + QuerySnapshot locationsSnapshot = await userDoc.reference + .collection(Constants.dbCollectionLocations) + .get(); + + final mainDoc = locationsSnapshot.docs + .firstWhereOrNull((doc) => doc.id == Constants.dbDocMainLocation); + final secondaryDoc = locationsSnapshot.docs + .firstWhereOrNull((doc) => doc.id == Constants.dbDocSecondLocation); + final locations = { + Constants.dbDocMainLocation: + mainDoc != null ? MyLocation.fromDocument(mainDoc) : null, + }; + if (secondaryDoc != null) { + locations.addAll({ + Constants.dbDocSecondLocation: MyLocation.fromDocument(secondaryDoc) + }); + } + // Map locations = { + // for (var doc in locationsSnapshot.docs) + // doc.id: MyLocation.fromDocument(doc) + // }; + + return UserProfile( + id: userDoc.id, + uid: data[Constants.dbFieldUsersID] ?? '', + email: data[Constants.dbFieldUsersEmail] ?? '', + name: data[Constants.dbFieldUsersName] ?? '', + firstName: data[Constants.dbFieldUsersFirstName] ?? '', + lastName: data[Constants.dbFieldUsersLastName] ?? '', + skills: skillsOffered, + skillsSought: skillsSought, + visions: visions, + risk: risk, + availability: availability, + culture: culture, + communication: communication, + workValues: works, + profilePictureUrl: data[Constants.dbFieldUsersProfilePic], + bio: data[Constants.dbFieldUsersBio], + gender: Gender.values[data[Constants.dbFieldUsersGender] ?? 0], + born: data[Constants.dbFieldUsersYearBorn], + languages: languages, + locations: locations, + ); + } + /// Get UserProfile for given [userId] static Future getUserProfileById(String userId) async { FirebaseFirestore firestore = FirebaseFirestore.instance; @@ -125,31 +202,9 @@ class UserService { .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(); + UserProfile result = await getUserProfileFromDocument(userDoc); - 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; + return result; } // get users stream