UserDataPage: year of birth

master
Rafael 2024-05-08 00:59:16 +02:00
parent feb9335e29
commit 0149f4b074
2 changed files with 50 additions and 12 deletions

View File

@ -9,5 +9,8 @@ class Constants {
static const String dbCollectionChatRooms = 'chat_rooms'; static const String dbCollectionChatRooms = 'chat_rooms';
static const String dbCollectionMessages = 'messages'; static const String dbCollectionMessages = 'messages';
static const String dbFieldUsersGender = 'gender';
static const String dbFieldUsersYearBorn = 'born';
static const String pathLanguagesJson = 'lib/assets/languages.json'; static const String pathLanguagesJson = 'lib/assets/languages.json';
} }

View File

@ -21,7 +21,6 @@ enum Gender { none, male, female, divers }
class _UserDataPageState extends State<UserDataPage> { class _UserDataPageState extends State<UserDataPage> {
final TextEditingController _locationController = TextEditingController(); final TextEditingController _locationController = TextEditingController();
final TextEditingController _birthdayController = TextEditingController();
List<LanguageSetting> languagesList = []; List<LanguageSetting> languagesList = [];
final List<Language> _selectedLanguages = []; final List<Language> _selectedLanguages = [];
@ -30,6 +29,8 @@ class _UserDataPageState extends State<UserDataPage> {
final FirebaseFirestore _firestore = FirebaseFirestore.instance; final FirebaseFirestore _firestore = FirebaseFirestore.instance;
final AuthService _authService = AuthService(); final AuthService _authService = AuthService();
int? _selectedYear;
int? _yearFromDb;
Gender genderView = Gender.none; Gender genderView = Gender.none;
int _genderFromDb = 0; int _genderFromDb = 0;
List<Language> _languagesFromDb = []; List<Language> _languagesFromDb = [];
@ -50,14 +51,17 @@ class _UserDataPageState extends State<UserDataPage> {
// Fetch user document fields (email, uid, gender, ...) from database // Fetch user document fields (email, uid, gender, ...) from database
DocumentSnapshot userSnapshot = await _firestore DocumentSnapshot userSnapshot = await _firestore
.collection(Constants.dbCollectionUsers) .collection(Constants.dbCollectionUsers)
.doc(_authService.getCurrentUser()!.uid) .doc(currentUserId)
.get(); .get();
// Extract gender // Extract gender and birth year
setState(() { _genderFromDb = userSnapshot[Constants.dbFieldUsersGender];
_genderFromDb = userSnapshot['gender']; _yearFromDb = userSnapshot
genderView = Gender.values[_genderFromDb]; .data()
}); .toString()
.contains(Constants.dbFieldUsersYearBorn)
? userSnapshot[Constants.dbFieldUsersYearBorn]
: null;
// Fetch languages // Fetch languages
QuerySnapshot languagesSnapshot = await _firestore QuerySnapshot languagesSnapshot = await _firestore
@ -77,6 +81,8 @@ class _UserDataPageState extends State<UserDataPage> {
)); ));
} }
setState(() { setState(() {
genderView = Gender.values[_genderFromDb];
_selectedYear = _yearFromDb;
_languagesFromDb = userLanguages; _languagesFromDb = userLanguages;
}); });
@ -128,6 +134,16 @@ class _UserDataPageState extends State<UserDataPage> {
CollectionReference languagesRef = CollectionReference languagesRef =
userRef.collection(Constants.dbCollectionLanguages); userRef.collection(Constants.dbCollectionLanguages);
if (_selectedYear != _yearFromDb) {
await userRef.update(
{'born': _selectedYear},
);
// update local value
_yearFromDb = _selectedYear;
} else {
print("birth year did NOT change");
}
// Update Gender in database - only if value has changed // Update Gender in database - only if value has changed
if (_genderFromDb != genderView.index) { if (_genderFromDb != genderView.index) {
await userRef.update( await userRef.update(
@ -214,13 +230,32 @@ class _UserDataPageState extends State<UserDataPage> {
controller: _locationController, controller: _locationController,
), ),
const Text( const Text(
'Birthday', 'Age',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
), ),
MyTextField( Row(
hintText: "Birthday", children: [
obscureText: false, const Padding(padding: EdgeInsets.symmetric(horizontal: 8)),
controller: _birthdayController, Text(_selectedYear != null
? '${DateTime.now().year - (_selectedYear ?? 0)} years old'
: 'undefined'),
const SizedBox(width: 20),
DropdownMenu(
onSelected: (int? newValue) {
setState(() {
_selectedYear = newValue;
});
},
dropdownMenuEntries: List.generate(50, (index) {
return DropdownMenuEntry<int>(
value: DateTime.now().year - 16 - index,
label: '${DateTime.now().year - 16 - index}',
);
}),
label: const Text('birth year'),
initialSelection: _yearFromDb,
),
],
), ),
Text( Text(
'Gender (${genderView.name} selected)', 'Gender (${genderView.name} selected)',