UserDataPage: year of birth
parent
feb9335e29
commit
0149f4b074
|
@ -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';
|
||||
}
|
||||
|
|
|
@ -21,7 +21,6 @@ enum Gender { none, male, female, divers }
|
|||
|
||||
class _UserDataPageState extends State<UserDataPage> {
|
||||
final TextEditingController _locationController = TextEditingController();
|
||||
final TextEditingController _birthdayController = TextEditingController();
|
||||
|
||||
List<LanguageSetting> languagesList = [];
|
||||
final List<Language> _selectedLanguages = [];
|
||||
|
@ -30,6 +29,8 @@ class _UserDataPageState extends State<UserDataPage> {
|
|||
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
|
||||
final AuthService _authService = AuthService();
|
||||
|
||||
int? _selectedYear;
|
||||
int? _yearFromDb;
|
||||
Gender genderView = Gender.none;
|
||||
int _genderFromDb = 0;
|
||||
List<Language> _languagesFromDb = [];
|
||||
|
@ -50,14 +51,17 @@ class _UserDataPageState extends State<UserDataPage> {
|
|||
// 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<UserDataPage> {
|
|||
));
|
||||
}
|
||||
setState(() {
|
||||
genderView = Gender.values[_genderFromDb];
|
||||
_selectedYear = _yearFromDb;
|
||||
_languagesFromDb = userLanguages;
|
||||
});
|
||||
|
||||
|
@ -128,6 +134,16 @@ class _UserDataPageState extends State<UserDataPage> {
|
|||
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<UserDataPage> {
|
|||
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<int>(
|
||||
value: DateTime.now().year - 16 - index,
|
||||
label: '${DateTime.now().year - 16 - index}',
|
||||
);
|
||||
}),
|
||||
label: const Text('birth year'),
|
||||
initialSelection: _yearFromDb,
|
||||
),
|
||||
],
|
||||
),
|
||||
Text(
|
||||
'Gender (${genderView.name} selected)',
|
||||
|
|
Loading…
Reference in New Issue