refactor skills hobbies bloc and screen

feature/passo/PASSO-#1-Sync-data-from-device-to-postgre-and-vice-versa
PGAN-MIS 2023-03-31 10:27:45 +08:00
parent e686cd1f9f
commit e921eaf7bd
7 changed files with 203 additions and 121 deletions

View File

@ -31,6 +31,7 @@ class HoobiesBloc extends Bloc<HobbiesEvent, HobbiesState> {
on<ShowHobbySkillAddForm>((event, emit) async { on<ShowHobbySkillAddForm>((event, emit) async {
emit(HobbiesLoadingState()); emit(HobbiesLoadingState());
try { try {
////get all skills and hobbies
if (allSkillsAndHobbies.isEmpty) { if (allSkillsAndHobbies.isEmpty) {
allSkillsAndHobbies = allSkillsAndHobbies =
await SkillsHobbiesServices.instance.getAllSkillsHobbies(); await SkillsHobbiesServices.instance.getAllSkillsHobbies();
@ -99,7 +100,7 @@ class HoobiesBloc extends Bloc<HobbiesEvent, HobbiesState> {
////DELETE ////DELETE
on<DeleteSkillHobbies>((event,emit)async{ on<DeleteSkillHobbies>((event,emit)async{
emit(HobbiesLoadingState()); emit(HobbiesLoadingState());
try{ // try{
bool success = await SkillsHobbiesServices.instance.delete(profileId: event.profileId, token: event.token, skillsHobbies: event.skillsHobbies); bool success = await SkillsHobbiesServices.instance.delete(profileId: event.profileId, token: event.token, skillsHobbies: event.skillsHobbies);
if(success){ if(success){
skillsAndHobbies.removeWhere((element) => element.id == event.skillsHobbies[0].id); skillsAndHobbies.removeWhere((element) => element.id == event.skillsHobbies[0].id);
@ -107,9 +108,9 @@ class HoobiesBloc extends Bloc<HobbiesEvent, HobbiesState> {
}else{ }else{
emit(HobbiesAndSkillsDeletedState(skillsHobbies: skillsAndHobbies, success: success)); emit(HobbiesAndSkillsDeletedState(skillsHobbies: skillsAndHobbies, success: success));
} }
}catch(e){ // }catch(e){
emit (HobbiesErrorState(message: e.toString())); // emit (HobbiesErrorState(message: e.toString()));
} // }
}); });
} }
} }

View File

@ -15,8 +15,8 @@ class GetSkillsHobbies extends HobbiesEvent{
List<Object> get props => [profileId,token]; List<Object> get props => [profileId,token];
} }
class ShowHobbySkillAddForm extends HobbiesEvent{ class ShowHobbySkillAddForm extends HobbiesEvent{
final List<SkillsHobbies> mySkillsAndHobbies;
const ShowHobbySkillAddForm({required this.mySkillsAndHobbies}); const ShowHobbySkillAddForm();
} }
class AddHobbyAndSkills extends HobbiesEvent{ class AddHobbyAndSkills extends HobbiesEvent{

View File

@ -36,9 +36,12 @@ class AddHobbySkillState extends HobbiesState{
class HobbiesLoadingState extends HobbiesState{ class HobbiesLoadingState extends HobbiesState{
} }
//// show add modal for adding new skills and hobbies
class ShowAddModalState extends HobbiesState{ class ShowAddModalState extends HobbiesState{
} }
//// hobbies and skills already added
class HobbiesAndSkillsAddedState extends HobbiesState{ class HobbiesAndSkillsAddedState extends HobbiesState{
final List<SkillsHobbies> mySkillsAndHobbies; final List<SkillsHobbies> mySkillsAndHobbies;
final Map<dynamic,dynamic> status; final Map<dynamic,dynamic> status;

View File

@ -20,15 +20,23 @@ import '../../../../bloc/profile/other_information/hobbies/hoobies_bloc.dart';
import '../../../../theme-data.dart/btn-style.dart'; import '../../../../theme-data.dart/btn-style.dart';
import '../../../../utils/alerts.dart'; import '../../../../utils/alerts.dart';
class SkillHobbiesScreen extends StatelessWidget { class SkillHobbiesScreen extends StatefulWidget {
const SkillHobbiesScreen({super.key}); const SkillHobbiesScreen({super.key});
@override
State<SkillHobbiesScreen> createState() => _SkillHobbiesScreenState();
}
class _SkillHobbiesScreenState extends State<SkillHobbiesScreen> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
String token; String token;
int profileId; int profileId;
final bloc = BlocProvider.of<HoobiesBloc>(context); final bloc = BlocProvider.of<HoobiesBloc>(context);
List<SkillsHobbies>? mySkillsAndHobbies; String output = '';
String? deletedChip, deletedChipIndex;
final keySimpleChipsInput = GlobalKey<FormState>();
final FocusNode focusNode = FocusNode();
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: const Text(skillAndHobbiesTitle), title: const Text(skillAndHobbiesTitle),
@ -37,13 +45,78 @@ class SkillHobbiesScreen extends StatelessWidget {
actions: context.watch<HoobiesBloc>().state is AddHobbySkillState actions: context.watch<HoobiesBloc>().state is AddHobbySkillState
? [ ? [
AddLeading(onPressed: () { AddLeading(onPressed: () {
context.read<HoobiesBloc>().add(ShowAddModal()); showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text("Add Skills and Hobbies"),
content: SimpleChipsInput(
separatorCharacter: ",",
createCharacter: ",",
focusNode: focusNode,
validateInput: true,
autoFocus: true,
formKey: keySimpleChipsInput,
onSubmitted: (p0) {
setState(() {
output = p0;
});
},
onChipDeleted: (p0, p1) {
setState(() {
deletedChip = p0;
deletedChipIndex = p1.toString();
});
},
onSaved: ((p0) {
setState(() {
output = p0;
});
}),
chipTextStyle: const TextStyle(
color: Colors.white,
fontSize: 16,
),
deleteIcon: const Icon(
Icons.delete,
size: 14.0,
color: second,
),
widgetContainerDecoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16.0),
border: Border.all(color: Colors.blue[100]!),
),
chipContainerDecoration: BoxDecoration(
color: second,
borderRadius: BorderRadius.circular(50),
),
placeChipsSectionAbove: false,
),
actions: [
ElevatedButton(
onPressed: () {
keySimpleChipsInput.currentState!.save();
Navigator.of(context, rootNavigator: true).pop('dialog');
bloc.add(
GetAddedHobbiesSkills(
addedHobbiesSkills: output));
},
style: mainBtnStyle(
primary, Colors.transparent, second),
child: const Text(submit),
)
],
);
});
}) })
] ]
: [ : [
AddLeading(onPressed: () { AddLeading(onPressed: () {
context.read<HoobiesBloc>().add(ShowHobbySkillAddForm( bloc.add(const ShowHobbySkillAddForm(
mySkillsAndHobbies: mySkillsAndHobbies!)); ));
}) })
]), ]),
body: ProgressHUD( body: ProgressHUD(
@ -51,6 +124,7 @@ class SkillHobbiesScreen extends StatelessWidget {
backgroundColor: Colors.black87, backgroundColor: Colors.black87,
indicatorWidget: const SpinKitFadingCircle(color: Colors.white), indicatorWidget: const SpinKitFadingCircle(color: Colors.white),
child: BlocBuilder<UserBloc, UserState>( child: BlocBuilder<UserBloc, UserState>(
buildWhen: (previous, current) => false,
builder: (context, state) { builder: (context, state) {
if (state is UserLoggedIn) { if (state is UserLoggedIn) {
token = state.userData!.user!.login!.token!; token = state.userData!.user!.login!.token!;
@ -118,7 +192,7 @@ class SkillHobbiesScreen extends StatelessWidget {
}, },
builder: (context, state) { builder: (context, state) {
if (state is HobbiesLoadedState) { if (state is HobbiesLoadedState) {
mySkillsAndHobbies = state.skillsAndHobbies;
if (state.skillsAndHobbies.isNotEmpty) { if (state.skillsAndHobbies.isNotEmpty) {
return Padding( return Padding(
padding: const EdgeInsets.all(12), padding: const EdgeInsets.all(12),
@ -136,15 +210,15 @@ class SkillHobbiesScreen extends StatelessWidget {
return Wrap( return Wrap(
children: [ children: [
Container( Container(
padding: const EdgeInsets.only( padding:
left: 6), const EdgeInsets.only(left: 6),
child: Wrap( child: Wrap(
clipBehavior: Clip.antiAlias, clipBehavior: Clip.antiAlias,
alignment: WrapAlignment.center, alignment: WrapAlignment.center,
crossAxisAlignment: crossAxisAlignment:
WrapCrossAlignment.center, WrapCrossAlignment.center,
runAlignment: WrapAlignment.center, runAlignment:
WrapAlignment.center,
children: [ children: [
Text( Text(
sh.name!, sh.name!,
@ -154,21 +228,23 @@ class SkillHobbiesScreen extends StatelessWidget {
), ),
IconButton( IconButton(
onPressed: () { onPressed: () {
confirmAlert( confirmAlert(
context, context,
() => context.read<HoobiesBloc>().add(DeleteSkillHobbies( () => context
profileId: .read<
profileId, HoobiesBloc>()
skillsHobbies: [ .add(DeleteSkillHobbies(
sh profileId:
], profileId,
token: skillsHobbies: [
token)), sh
"Delete", ],
"Confirm Delete"); token:
token)),
"Delete",
"Confirm Delete");
}, },
icon:const Icon( icon: const Icon(
Icons.delete, Icons.delete,
size: 16, size: 16,
color: second, color: second,
@ -191,9 +267,9 @@ class SkillHobbiesScreen extends StatelessWidget {
token: token, token: token,
); );
} }
if (state is ShowAddModalState) { // if (state is ShowAddModalState) {
return AddModal(bloc: bloc); // return AddModal(bloc: bloc);
} // }
return Container(); return Container();
}, },
); );
@ -209,83 +285,80 @@ class SkillHobbiesScreen extends StatelessWidget {
} }
} }
class AddModal extends StatefulWidget { // class AddModal extends StatefulWidget {
final HoobiesBloc bloc; // final HoobiesBloc bloc;
const AddModal({super.key, required this.bloc}); // const AddModal({super.key, required this.bloc});
@override // @override
State<AddModal> createState() => _AddModalState(); // State<AddModal> createState() => _AddModalState();
} // }
String output = '';
String? deletedChip, deletedChipIndex;
final keySimpleChipsInput = GlobalKey<FormState>();
final FocusNode focusNode = FocusNode();
class _AddModalState extends State<AddModal> {
@override // class _AddModalState extends State<AddModal> {
Widget build(BuildContext context) { // @override
return BlocBuilder<HoobiesBloc, HobbiesState>( // Widget build(BuildContext context) {
builder: (context, state) { // return BlocBuilder<HoobiesBloc, HobbiesState>(
return AlertDialog( // builder: (context, state) {
title: const Text("Add Skills and Hobbies"), // return AlertDialog(
content: SimpleChipsInput( // title: const Text("Add Skills and Hobbies"),
separatorCharacter: ",", // content: SimpleChipsInput(
createCharacter: ",", // separatorCharacter: ",",
focusNode: focusNode, // createCharacter: ",",
validateInput: true, // focusNode: focusNode,
autoFocus: true, // validateInput: true,
formKey: keySimpleChipsInput, // autoFocus: true,
onSubmitted: (p0) { // formKey: keySimpleChipsInput,
setState(() { // onSubmitted: (p0) {
output = p0; // setState(() {
}); // output = p0;
}, // });
onChipDeleted: (p0, p1) { // },
setState(() { // onChipDeleted: (p0, p1) {
deletedChip = p0; // setState(() {
deletedChipIndex = p1.toString(); // deletedChip = p0;
}); // deletedChipIndex = p1.toString();
}, // });
onSaved: ((p0) { // },
setState(() { // onSaved: ((p0) {
output = p0; // setState(() {
}); // output = p0;
}), // });
chipTextStyle: const TextStyle( // }),
color: Colors.white, // chipTextStyle: const TextStyle(
fontSize: 16, // color: Colors.white,
), // fontSize: 16,
deleteIcon: const Icon( // ),
Icons.delete, // deleteIcon: const Icon(
size: 14.0, // Icons.delete,
color: second, // size: 14.0,
), // color: second,
widgetContainerDecoration: BoxDecoration( // ),
color: Colors.white, // widgetContainerDecoration: BoxDecoration(
borderRadius: BorderRadius.circular(16.0), // color: Colors.white,
border: Border.all(color: Colors.blue[100]!), // borderRadius: BorderRadius.circular(16.0),
), // border: Border.all(color: Colors.blue[100]!),
chipContainerDecoration: BoxDecoration( // ),
color: second, // chipContainerDecoration: BoxDecoration(
borderRadius: BorderRadius.circular(50), // color: second,
), // borderRadius: BorderRadius.circular(50),
placeChipsSectionAbove: false, // ),
), // placeChipsSectionAbove: false,
actions: [ // ),
ElevatedButton( // actions: [
onPressed: () { // ElevatedButton(
keySimpleChipsInput.currentState!.save(); // onPressed: () {
context // keySimpleChipsInput.currentState!.save();
.read<HoobiesBloc>() // context
.add(GetAddedHobbiesSkills(addedHobbiesSkills: output)); // .read<HoobiesBloc>()
}, // .add(GetAddedHobbiesSkills(addedHobbiesSkills: output));
style: mainBtnStyle(primary, Colors.transparent, second), // },
child: const Text(submit), // style: mainBtnStyle(primary, Colors.transparent, second),
) // child: const Text(submit),
], // )
); // ],
}, // );
); // },
} // );
} // }
// }

View File

@ -36,7 +36,7 @@ class _AddHobbiesAndSkillsScreenState extends State<AddHobbiesAndSkillsScreen> {
selectedListData: selectedList, selectedListData: selectedList,
onApplyButtonClick: (list) { onApplyButtonClick: (list) {
// Navigator.pop(context, list); // Navigator.pop(context, list);
context.read<HoobiesBloc>().add(AddHobbyAndSkills(profileId: widget.profileId, token: widget.token, skillsHobbies: selectedList)); context.read<HoobiesBloc>().add(AddHobbyAndSkills(profileId: widget.profileId, token: widget.token, skillsHobbies: list!));
}, },
choiceChipLabel: (item) { choiceChipLabel: (item) {

View File

@ -48,7 +48,11 @@ class SkillsHobbiesServices {
'Content-Type': 'application/json; charset=UTF-8', 'Content-Type': 'application/json; charset=UTF-8',
'Authorization': authToken 'Authorization': authToken
}; };
Map body = {"skill_hobby": skillsHobbies}; List<Map<String,dynamic>> skillsHobbiesBody=[];
for(var element in skillsHobbies){
skillsHobbiesBody.add({"id":element.id,"name":element.name});
}
Map body = {"skill_hobby": skillsHobbiesBody};
Map<dynamic, dynamic> statusResponse = {}; Map<dynamic, dynamic> statusResponse = {};
try { try {
http.Response response = await Request.instance http.Response response = await Request.instance
@ -76,21 +80,22 @@ class SkillsHobbiesServices {
'Content-Type': 'application/json; charset=UTF-8', 'Content-Type': 'application/json; charset=UTF-8',
'Authorization': authToken 'Authorization': authToken
}; };
Map<String, dynamic> params = {"force_mode": "true"};
Map body = { Map body = {
"skill_hobby": [skillsHobbies] "skill_hobby": [{"id":skillsHobbies[0].id, "name":skillsHobbies[0].name}]
}; };
try { // try {
http.Response response = await Request.instance.deleteRequest( http.Response response = await Request.instance.deleteRequest(
path: path, headers: headers, body: body, param: {}); path: path, headers: headers, body: body, param: params);
if (response.statusCode == 200) { if (response.statusCode == 200) {
Map data = jsonDecode(response.body); Map data = jsonDecode(response.body);
success = data['success']; success = data['success'];
} }
} catch (e) { // } catch (e) {
throw e.toString(); // throw e.toString();
} // }
return success; return success;
} }

View File

@ -5,9 +5,9 @@ class Url {
String host() { String host() {
// return '192.168.10.221:3003'; // return '192.168.10.221:3003';
// return 'agusandelnorte.gov.ph'; // return 'agusandelnorte.gov.ph';
return "192.168.10.219:3000"; // return "192.168.10.219:3000";
// return "devweb.agusandelnorte.gov.ph"; // return "devweb.agusandelnorte.gov.ph";
// return 'devapi.agusandelnorte.gov.ph:3004'; return 'devapi.agusandelnorte.gov.ph:3004';
} }
String authentication() { String authentication() {