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) {
Map<String, dynamic> data = doc.data() as Map<String, dynamic>;
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],
);
}

View File

@ -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<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 '../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<UserMatchingPage> {
.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<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() {
_controller.next(
swipeDirection: SwipeDirection.left, duration: Durations.extralong1);

View File

@ -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<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]
static Future<UserProfile> 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<Language> 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<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;
return result;
}
// get users stream