From 3aa5f2b5d34b430b7bd9a649a871ab9874469b2a Mon Sep 17 00:00:00 2001 From: Rafael <1024481@stud.hs-mannheim.de> Date: Fri, 31 May 2024 18:31:37 +0200 Subject: [PATCH] Edit Skills. --- lib/forms/skills_form.dart | 19 ++++- lib/models/user_profile.dart | 15 ++-- lib/pages/user_data_page.dart | 1 + lib/pages/user_profile_page.dart | 118 +++++++++++++++++++++++++++++++ lib/services/user_service.dart | 21 +++--- 5 files changed, 159 insertions(+), 15 deletions(-) diff --git a/lib/forms/skills_form.dart b/lib/forms/skills_form.dart index 29ceaeb..43277ba 100644 --- a/lib/forms/skills_form.dart +++ b/lib/forms/skills_form.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import '../../enumerations.dart'; import '../../services/auth/auth_service.dart'; import '../../services/user_service.dart'; +import '../constants.dart'; import '../pages/user_vision_page.dart'; import 'profile_category_form.dart'; @@ -10,6 +11,7 @@ class SkillsForm extends StatelessWidget { super.key, required this.isRegProcess, required this.skillsSought, + required this.isEditMode, }); // get instance of auth @@ -17,6 +19,7 @@ class SkillsForm extends StatelessWidget { final bool isRegProcess; final bool skillsSought; // flag to toggle offered and sought skills + final bool isEditMode; @override Widget build(BuildContext context) { @@ -74,12 +77,24 @@ class SkillsForm extends StatelessWidget { builder: (context) => SkillsForm( isRegProcess: isRegProcess, skillsSought: true, + isEditMode: false, ), ), ); } else { - // Navigate back after saving - Navigator.pop(context); + if (isEditMode == true) { + // pass selectedOptions data back to caller + String keyToUpdate = skillsSought + ? Constants.dbFieldUsersSkillsSought + : Constants.dbFieldUsersSkills; + + Navigator.pop(context, { + keyToUpdate: selectedOptions, + }); + } else { + // Navigate back after saving + Navigator.pop(context); + } } } else { ScaffoldMessenger.of(context).showSnackBar( diff --git a/lib/models/user_profile.dart b/lib/models/user_profile.dart index 80de077..fb28d8c 100644 --- a/lib/models/user_profile.dart +++ b/lib/models/user_profile.dart @@ -1,4 +1,5 @@ import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:cofounderella/services/user_service.dart'; import '../constants.dart'; import '../enumerations.dart'; @@ -17,8 +18,8 @@ class UserProfile { Gender? gender; int? born; final String risk; - final List skills; - final List skillsSought; + List skills; + List skillsSought; List languages; Map locations; @@ -43,6 +44,11 @@ class UserProfile { factory UserProfile.fromDocument(DocumentSnapshot doc) { Map data = doc.data() as Map; + List skillsOffered = UserService.convertSkillStringToEnum( + data[Constants.dbFieldUsersSkills]); + List skillsSought = UserService.convertSkillStringToEnum( + data[Constants.dbFieldUsersSkillsSought]); + return UserProfile( id: doc.id, uid: data[Constants.dbFieldUsersID] ?? '', @@ -50,9 +56,8 @@ class UserProfile { name: data[Constants.dbFieldUsersName] ?? '', firstName: data[Constants.dbFieldUsersFirstName] ?? '', lastName: data[Constants.dbFieldUsersLastName] ?? '', - skills: List.from(data[Constants.dbFieldUsersSkills] ?? []), - skillsSought: - List.from(data[Constants.dbFieldUsersSkillsSought] ?? []), + skills: skillsOffered, + skillsSought: skillsSought, risk: data[Constants.dbFieldUsersRiskTolerance] ?? '', profilePictureUrl: data[Constants.dbFieldUsersProfilePic], bio: data[Constants.dbFieldUsersBio], diff --git a/lib/pages/user_data_page.dart b/lib/pages/user_data_page.dart index 025a29d..2ac649d 100644 --- a/lib/pages/user_data_page.dart +++ b/lib/pages/user_data_page.dart @@ -329,6 +329,7 @@ class _UserDataPageState extends State { builder: (context) => SkillsForm( isRegProcess: widget.isRegProcess, skillsSought: false, + isEditMode: false, ), ), ); diff --git a/lib/pages/user_profile_page.dart b/lib/pages/user_profile_page.dart index aec307a..f7d8615 100644 --- a/lib/pages/user_profile_page.dart +++ b/lib/pages/user_profile_page.dart @@ -1,3 +1,5 @@ +import 'package:cofounderella/enumerations.dart'; +import 'package:cofounderella/forms/skills_form.dart'; import 'package:flutter/material.dart'; import 'package:firebase_auth/firebase_auth.dart'; @@ -80,6 +82,43 @@ class _UserProfilePageState extends State { } } + void _editSkills({required bool skillsSought}) async { + final updatedUserData = await Navigator.push( + context, + MaterialPageRoute( + builder: (context) => SkillsForm( + isRegProcess: false, + skillsSought: skillsSought, + isEditMode: true, + ), + ), + ); + + if (updatedUserData != null) { + setState(() { + // above Type of updatedUserData is dynamic, so convert + if (skillsSought) { + List dynamicList = + updatedUserData[Constants.dbFieldUsersSkillsSought]; + myData.skillsSought = + dynamicList.map((e) => e as SkillOption).toList(); + } else { + List dynamicList = + updatedUserData[Constants.dbFieldUsersSkills]; + myData.skills = dynamicList.map((e) => e as SkillOption).toList(); + } + }); + } + } + + void editUserSkillsInfo() async { + _editSkills(skillsSought: false); + } + + void editUserSkillsSoughtInfo() async { + _editSkills(skillsSought: true); + } + @override Widget build(BuildContext context) { return Scaffold( @@ -101,6 +140,10 @@ class _UserProfilePageState extends State { const SizedBox(height: 16), Divider(color: Theme.of(context).colorScheme.primary), const SizedBox(height: 16), + _buildSkills(context), + const SizedBox(height: 16), + Divider(color: Theme.of(context).colorScheme.primary), + const SizedBox(height: 16), ], ), ), @@ -108,6 +151,81 @@ class _UserProfilePageState extends State { ); } + Widget _buildSkills(BuildContext context) { + return Column( + children: [ + Align( + alignment: Alignment.centerLeft, + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + 'Skills offered', + style: TextStyle( + color: Theme.of(context).colorScheme.primary, + ), + ), + Text(myData.skills.map((x) => x.displayName).join(', '), + style: const TextStyle(fontSize: 16)), + ], + ), + ), + Align( + alignment: Alignment.topRight, + child: OutlinedButton.icon( + label: const Text('Edit'), + icon: const Icon(Icons.edit), + onPressed: editUserSkillsInfo, + ), + ), + ], + ), + const SizedBox(height: 16), + Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + 'Skills sought', + style: TextStyle( + color: Theme.of(context).colorScheme.primary, + ), + ), + Text( + myData.skillsSought + .map((x) => x.displayName) + .join(', '), + style: const TextStyle(fontSize: 16)), + ], + ), + ), + Align( + alignment: Alignment.topRight, + child: OutlinedButton.icon( + label: const Text('Edit'), + icon: const Icon(Icons.edit), + onPressed: editUserSkillsSoughtInfo, + ), + ), + ], + ), + ], + ), + ), + ], + ); + } + Widget _buildLocation(BuildContext context) { int age = calcAge(myData.born); return Column( diff --git a/lib/services/user_service.dart b/lib/services/user_service.dart index 0dd58e1..aca47cd 100644 --- a/lib/services/user_service.dart +++ b/lib/services/user_service.dart @@ -51,19 +51,24 @@ class UserService { skills = userData[Constants.dbFieldUsersSkills]; } - if (skills != null && skills.isNotEmpty) { - // Convert skills from strings to enum values - List userSkills = skills - .map((skill) => SkillOption.values - .firstWhere((x) => x.toString() == 'SkillOption.$skill')) - .toList(); - return userSkills; - } + return convertSkillStringToEnum(skills); } return []; } + static List convertSkillStringToEnum(List? skills) { + if (skills != null && skills.isNotEmpty) { + // Convert skills from strings to enum values + List userSkills = skills + .map((skill) => SkillOption.values + .firstWhere((x) => x.toString() == 'SkillOption.$skill')) + .toList(); + return userSkills; + } + return []; + } + static Future saveSkillsToFirebase(List selectedOptions, bool skillsSought, String userId) async { try {