From 0149f4b07440f1e63ba3288753569a77aa9fe2ea Mon Sep 17 00:00:00 2001 From: Rafael <1024481@stud.hs-mannheim.de> Date: Wed, 8 May 2024 00:59:16 +0200 Subject: [PATCH] UserDataPage: year of birth --- lib/constants.dart | 3 ++ lib/pages/user_data_page.dart | 59 ++++++++++++++++++++++++++++------- 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/lib/constants.dart b/lib/constants.dart index 27dbea1..9aa3a36 100644 --- a/lib/constants.dart +++ b/lib/constants.dart @@ -9,5 +9,8 @@ class Constants { static const String dbCollectionChatRooms = 'chat_rooms'; static const String dbCollectionMessages = 'messages'; + static const String dbFieldUsersGender = 'gender'; + static const String dbFieldUsersYearBorn = 'born'; + static const String pathLanguagesJson = 'lib/assets/languages.json'; } diff --git a/lib/pages/user_data_page.dart b/lib/pages/user_data_page.dart index 286a40f..ed20ea3 100644 --- a/lib/pages/user_data_page.dart +++ b/lib/pages/user_data_page.dart @@ -21,7 +21,6 @@ enum Gender { none, male, female, divers } class _UserDataPageState extends State { final TextEditingController _locationController = TextEditingController(); - final TextEditingController _birthdayController = TextEditingController(); List languagesList = []; final List _selectedLanguages = []; @@ -30,6 +29,8 @@ class _UserDataPageState extends State { final FirebaseFirestore _firestore = FirebaseFirestore.instance; final AuthService _authService = AuthService(); + int? _selectedYear; + int? _yearFromDb; Gender genderView = Gender.none; int _genderFromDb = 0; List _languagesFromDb = []; @@ -50,14 +51,17 @@ class _UserDataPageState extends State { // Fetch user document fields (email, uid, gender, ...) from database DocumentSnapshot userSnapshot = await _firestore .collection(Constants.dbCollectionUsers) - .doc(_authService.getCurrentUser()!.uid) + .doc(currentUserId) .get(); - // Extract gender - setState(() { - _genderFromDb = userSnapshot['gender']; - genderView = Gender.values[_genderFromDb]; - }); + // Extract gender and birth year + _genderFromDb = userSnapshot[Constants.dbFieldUsersGender]; + _yearFromDb = userSnapshot + .data() + .toString() + .contains(Constants.dbFieldUsersYearBorn) + ? userSnapshot[Constants.dbFieldUsersYearBorn] + : null; // Fetch languages QuerySnapshot languagesSnapshot = await _firestore @@ -77,6 +81,8 @@ class _UserDataPageState extends State { )); } setState(() { + genderView = Gender.values[_genderFromDb]; + _selectedYear = _yearFromDb; _languagesFromDb = userLanguages; }); @@ -128,6 +134,16 @@ class _UserDataPageState extends State { CollectionReference languagesRef = 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 if (_genderFromDb != genderView.index) { await userRef.update( @@ -214,13 +230,32 @@ class _UserDataPageState extends State { controller: _locationController, ), const Text( - 'Birthday', + 'Age', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), ), - MyTextField( - hintText: "Birthday", - obscureText: false, - controller: _birthdayController, + Row( + children: [ + const Padding(padding: EdgeInsets.symmetric(horizontal: 8)), + 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( + value: DateTime.now().year - 16 - index, + label: '${DateTime.now().year - 16 - index}', + ); + }), + label: const Text('birth year'), + initialSelection: _yearFromDb, + ), + ], ), Text( 'Gender (${genderView.name} selected)',