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

View File

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

View File

@ -36,9 +36,12 @@ class AddHobbySkillState extends HobbiesState{
class HobbiesLoadingState extends HobbiesState{
}
//// show add modal for adding new skills and hobbies
class ShowAddModalState extends HobbiesState{
}
//// hobbies and skills already added
class HobbiesAndSkillsAddedState extends HobbiesState{
final List<SkillsHobbies> mySkillsAndHobbies;
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 '../../../../utils/alerts.dart';
class SkillHobbiesScreen extends StatelessWidget {
class SkillHobbiesScreen extends StatefulWidget {
const SkillHobbiesScreen({super.key});
@override
State<SkillHobbiesScreen> createState() => _SkillHobbiesScreenState();
}
class _SkillHobbiesScreenState extends State<SkillHobbiesScreen> {
@override
Widget build(BuildContext context) {
String token;
int profileId;
final bloc = BlocProvider.of<HoobiesBloc>(context);
List<SkillsHobbies>? mySkillsAndHobbies;
String output = '';
String? deletedChip, deletedChipIndex;
final keySimpleChipsInput = GlobalKey<FormState>();
final FocusNode focusNode = FocusNode();
return Scaffold(
appBar: AppBar(
title: const Text(skillAndHobbiesTitle),
@ -37,13 +45,78 @@ class SkillHobbiesScreen extends StatelessWidget {
actions: context.watch<HoobiesBloc>().state is AddHobbySkillState
? [
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: () {
context.read<HoobiesBloc>().add(ShowHobbySkillAddForm(
mySkillsAndHobbies: mySkillsAndHobbies!));
bloc.add(const ShowHobbySkillAddForm(
));
})
]),
body: ProgressHUD(
@ -51,6 +124,7 @@ class SkillHobbiesScreen extends StatelessWidget {
backgroundColor: Colors.black87,
indicatorWidget: const SpinKitFadingCircle(color: Colors.white),
child: BlocBuilder<UserBloc, UserState>(
buildWhen: (previous, current) => false,
builder: (context, state) {
if (state is UserLoggedIn) {
token = state.userData!.user!.login!.token!;
@ -118,7 +192,7 @@ class SkillHobbiesScreen extends StatelessWidget {
},
builder: (context, state) {
if (state is HobbiesLoadedState) {
mySkillsAndHobbies = state.skillsAndHobbies;
if (state.skillsAndHobbies.isNotEmpty) {
return Padding(
padding: const EdgeInsets.all(12),
@ -136,15 +210,15 @@ class SkillHobbiesScreen extends StatelessWidget {
return Wrap(
children: [
Container(
padding: const EdgeInsets.only(
left: 6),
padding:
const EdgeInsets.only(left: 6),
child: Wrap(
clipBehavior: Clip.antiAlias,
alignment: WrapAlignment.center,
crossAxisAlignment:
WrapCrossAlignment.center,
runAlignment: WrapAlignment.center,
runAlignment:
WrapAlignment.center,
children: [
Text(
sh.name!,
@ -154,21 +228,23 @@ class SkillHobbiesScreen extends StatelessWidget {
),
IconButton(
onPressed: () {
confirmAlert(
context,
() => context.read<HoobiesBloc>().add(DeleteSkillHobbies(
profileId:
profileId,
skillsHobbies: [
sh
],
token:
token)),
"Delete",
"Confirm Delete");
confirmAlert(
context,
() => context
.read<
HoobiesBloc>()
.add(DeleteSkillHobbies(
profileId:
profileId,
skillsHobbies: [
sh
],
token:
token)),
"Delete",
"Confirm Delete");
},
icon:const Icon(
icon: const Icon(
Icons.delete,
size: 16,
color: second,
@ -191,9 +267,9 @@ class SkillHobbiesScreen extends StatelessWidget {
token: token,
);
}
if (state is ShowAddModalState) {
return AddModal(bloc: bloc);
}
// if (state is ShowAddModalState) {
// return AddModal(bloc: bloc);
// }
return Container();
},
);
@ -209,83 +285,80 @@ class SkillHobbiesScreen extends StatelessWidget {
}
}
class AddModal extends StatefulWidget {
final HoobiesBloc bloc;
const AddModal({super.key, required this.bloc});
// class AddModal extends StatefulWidget {
// final HoobiesBloc bloc;
// const AddModal({super.key, required this.bloc});
@override
State<AddModal> createState() => _AddModalState();
}
// @override
// State<AddModal> createState() => _AddModalState();
// }
String output = '';
String? deletedChip, deletedChipIndex;
final keySimpleChipsInput = GlobalKey<FormState>();
final FocusNode focusNode = FocusNode();
class _AddModalState extends State<AddModal> {
@override
Widget build(BuildContext context) {
return BlocBuilder<HoobiesBloc, HobbiesState>(
builder: (context, state) {
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();
context
.read<HoobiesBloc>()
.add(GetAddedHobbiesSkills(addedHobbiesSkills: output));
},
style: mainBtnStyle(primary, Colors.transparent, second),
child: const Text(submit),
)
],
);
},
);
}
}
// class _AddModalState extends State<AddModal> {
// @override
// Widget build(BuildContext context) {
// return BlocBuilder<HoobiesBloc, HobbiesState>(
// builder: (context, state) {
// 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();
// context
// .read<HoobiesBloc>()
// .add(GetAddedHobbiesSkills(addedHobbiesSkills: output));
// },
// style: mainBtnStyle(primary, Colors.transparent, second),
// child: const Text(submit),
// )
// ],
// );
// },
// );
// }
// }

View File

@ -36,7 +36,7 @@ class _AddHobbiesAndSkillsScreenState extends State<AddHobbiesAndSkillsScreen> {
selectedListData: selectedList,
onApplyButtonClick: (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) {

View File

@ -48,7 +48,11 @@ class SkillsHobbiesServices {
'Content-Type': 'application/json; charset=UTF-8',
'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 = {};
try {
http.Response response = await Request.instance
@ -76,21 +80,22 @@ class SkillsHobbiesServices {
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': authToken
};
Map<String, dynamic> params = {"force_mode": "true"};
Map body = {
"skill_hobby": [skillsHobbies]
"skill_hobby": [{"id":skillsHobbies[0].id, "name":skillsHobbies[0].name}]
};
try {
// try {
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) {
Map data = jsonDecode(response.body);
success = data['success'];
}
} catch (e) {
throw e.toString();
}
// } catch (e) {
// throw e.toString();
// }
return success;
}

View File

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