UserProfile.fromDocument did not set languages and locations. Replaced with getUserProfileFromDocument.

master
Rafael 2024-06-17 12:51:30 +02:00
parent 6370e8b748
commit 17e097d483
4 changed files with 87 additions and 129 deletions

View File

@ -32,14 +32,14 @@ class MyLocation {
factory MyLocation.fromDocument(DocumentSnapshot doc) { factory MyLocation.fromDocument(DocumentSnapshot doc) {
Map<String, dynamic> data = doc.data() as Map<String, dynamic>; Map<String, dynamic> data = doc.data() as Map<String, dynamic>;
return MyLocation( return MyLocation(
street: data[Constants.dbFieldLocationStreet] ?? '', street: data[Constants.dbFieldLocationStreet],
country: data[Constants.dbFieldLocationCountry] ?? '', country: data[Constants.dbFieldLocationCountry],
administrativeArea: data[Constants.dbFieldLocationArea], administrativeArea: data[Constants.dbFieldLocationArea],
locality: data[Constants.dbFieldLocationLocality] ?? '', locality: data[Constants.dbFieldLocationLocality],
subLocality: data[Constants.dbFieldLocationSubLocality], subLocality: data[Constants.dbFieldLocationSubLocality],
postalCode: data[Constants.dbFieldLocationPostalCode], postalCode: data[Constants.dbFieldLocationPostalCode],
latitude: (data[Constants.dbFieldLocationLatitude] as num?)?.toDouble(), latitude: data[Constants.dbFieldLocationLatitude],
longitude: (data[Constants.dbFieldLocationLongitude] as num?)?.toDouble(), longitude: data[Constants.dbFieldLocationLongitude],
); );
} }

View File

@ -1,8 +1,4 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import '../constants.dart';
import '../enumerations.dart'; import '../enumerations.dart';
import '../services/user_service.dart';
import 'language.dart'; import 'language.dart';
import 'location.dart'; import 'location.dart';
@ -50,48 +46,4 @@ class UserProfile {
required this.languages, required this.languages,
required this.locations, required this.locations,
}); });
factory UserProfile.fromDocument(DocumentSnapshot doc) {
Map<String, dynamic> data = doc.data() as Map<String, dynamic>;
List<SkillOption> skillsOffered = UserService.convertSkillStringToEnum(
data[Constants.dbFieldUsersSkills]);
List<SkillOption> skillsSought = UserService.convertSkillStringToEnum(
data[Constants.dbFieldUsersSkillsSought]);
List<VisionOption> visions = UserService.convertVisionStringToEnum(
data[Constants.dbFieldUsersVisions]);
List<WorkValueOption> 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: {},
);
}
} }

View File

@ -9,10 +9,9 @@ import '../components/language_list.dart';
import '../components/text_with_bold.dart'; import '../components/text_with_bold.dart';
import '../constants.dart'; import '../constants.dart';
import '../forms/matched_screen.dart'; import '../forms/matched_screen.dart';
import '../models/language.dart';
import '../models/location.dart';
import '../models/user_profile.dart'; import '../models/user_profile.dart';
import '../services/auth/auth_service.dart'; import '../services/auth/auth_service.dart';
import '../services/user_service.dart';
import '../utils/helper.dart'; import '../utils/helper.dart';
import '../utils/math.dart'; import '../utils/math.dart';
import 'chat_page.dart'; import 'chat_page.dart';
@ -90,41 +89,8 @@ class UserMatchingPageState extends State<UserMatchingPage> {
.toSet(); .toSet();
for (var userDoc in usersSnapshot.docs) { for (var userDoc in usersSnapshot.docs) {
final languagesSnapshot = await userDoc.reference UserProfile userProfile =
.collection(Constants.dbCollectionLanguages) await UserService.getUserProfileFromDocument(userDoc);
.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);
// add profiles accordingly // add profiles accordingly
allUsers.add(userProfile); allUsers.add(userProfile);
@ -145,21 +111,6 @@ class UserMatchingPageState extends State<UserMatchingPage> {
}); });
} }
MyLocation? _createLocationFromDoc(Map<String, dynamic>? 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() { void _swipeLeft() {
_controller.next( _controller.next(
swipeDirection: SwipeDirection.left, duration: Durations.extralong1); swipeDirection: SwipeDirection.left, duration: Durations.extralong1);

View File

@ -1,4 +1,5 @@
import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:collection/collection.dart';
import 'package:firebase_auth/firebase_auth.dart'; import 'package:firebase_auth/firebase_auth.dart';
import '../constants.dart'; import '../constants.dart';
import '../enumerations.dart'; import '../enumerations.dart';
@ -116,6 +117,82 @@ class UserService {
} }
} }
/// Get UserProfile from Document
static Future<UserProfile> getUserProfileFromDocument(
DocumentSnapshot userDoc) async {
Map<String, dynamic> data = userDoc.data() as Map<String, dynamic>;
List<SkillOption> skillsOffered = UserService.convertSkillStringToEnum(
data[Constants.dbFieldUsersSkills]);
List<SkillOption> skillsSought = UserService.convertSkillStringToEnum(
data[Constants.dbFieldUsersSkillsSought]);
List<VisionOption> visions = UserService.convertVisionStringToEnum(
data[Constants.dbFieldUsersVisions]);
List<WorkValueOption> 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<Language> 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<String, MyLocation?> 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] /// Get UserProfile for given [userId]
static Future<UserProfile> getUserProfileById(String userId) async { static Future<UserProfile> getUserProfileById(String userId) async {
FirebaseFirestore firestore = FirebaseFirestore.instance; FirebaseFirestore firestore = FirebaseFirestore.instance;
@ -125,31 +202,9 @@ class UserService {
.doc(userId) .doc(userId)
.get(); .get();
QuerySnapshot languagesSnapshot = await firestore UserProfile result = await getUserProfileFromDocument(userDoc);
.collection(Constants.dbCollectionUsers)
.doc(userId)
.collection(Constants.dbCollectionLanguages)
.get();
List<Language> languages = languagesSnapshot.docs
.map((doc) => Language.fromDocument(doc))
.toList();
QuerySnapshot locationsSnapshot = await firestore return result;
.collection(Constants.dbCollectionUsers)
.doc(userId)
.collection(Constants.dbCollectionLocations)
.get();
Map<String, MyLocation?> 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;
} }
// get users stream // get users stream