From c83572cfb1a5a6767ae70193fd78579cf9bc1df3 Mon Sep 17 00:00:00 2001 From: PGAN-MIS Date: Mon, 6 Mar 2023 08:57:39 +0800 Subject: [PATCH] Personal refences refactor and created its own bloc --- lib/bloc/references/references_bloc.dart | 25 +++ lib/bloc/references/references_event.dart | 16 ++ lib/bloc/references/references_state.dart | 29 +++ .../learning_and_development_screen.dart | 2 +- .../profile/components/references_screen.dart | 188 +++++++++++------- lib/screens/profile/profile.dart | 14 +- lib/sevices/profile/references_services.dart | 40 ++++ lib/utils/urls.dart | 9 +- 8 files changed, 246 insertions(+), 77 deletions(-) create mode 100644 lib/bloc/references/references_bloc.dart create mode 100644 lib/bloc/references/references_event.dart create mode 100644 lib/bloc/references/references_state.dart create mode 100644 lib/sevices/profile/references_services.dart diff --git a/lib/bloc/references/references_bloc.dart b/lib/bloc/references/references_bloc.dart new file mode 100644 index 0000000..569f774 --- /dev/null +++ b/lib/bloc/references/references_bloc.dart @@ -0,0 +1,25 @@ +import 'package:bloc/bloc.dart'; +import 'package:equatable/equatable.dart'; +import 'package:unit2/sevices/profile/references_services.dart'; +import '../../model/profile/references.dart'; + +part 'references_event.dart'; +part 'references_state.dart'; + + +class ReferencesBloc extends Bloc { + List references = []; + ReferencesBloc() : super(ReferencesInitial()) { + on((event, emit) async{ + emit(ReferencesLoadingState()); + try{ + List refs = await ReferencesServices.instace.getRefences(event.profileId, event.token); + references = refs; + emit(ReferencesLoadedState(references: references)); + }catch(e){ + ReferencesErrorState(message: e.toString()); + } + + }); + } +} diff --git a/lib/bloc/references/references_event.dart b/lib/bloc/references/references_event.dart new file mode 100644 index 0000000..4b6737a --- /dev/null +++ b/lib/bloc/references/references_event.dart @@ -0,0 +1,16 @@ +part of 'references_bloc.dart'; + +abstract class ReferencesEvent extends Equatable { + const ReferencesEvent(); + + @override + List get props => []; +} + +class GetReferences extends ReferencesEvent{ + final int profileId; + final String token; + const GetReferences({required this.profileId, required this.token}); + @override + List get props => [profileId,token]; +} diff --git a/lib/bloc/references/references_state.dart b/lib/bloc/references/references_state.dart new file mode 100644 index 0000000..13c2284 --- /dev/null +++ b/lib/bloc/references/references_state.dart @@ -0,0 +1,29 @@ +part of 'references_bloc.dart'; + +abstract class ReferencesState extends Equatable { + const ReferencesState(); + + @override + List get props => []; +} + +class ReferencesInitial extends ReferencesState {} + +class ReferencesLoadedState extends ReferencesState{ + final List references; + const ReferencesLoadedState({required this.references}); + + @override + List get props => [references]; + +} +class ReferencesErrorState extends ReferencesState{ + final String message; + const ReferencesErrorState({required this.message}); + + @override + List get props => [message]; +} +class ReferencesLoadingState extends ReferencesState{ + +} diff --git a/lib/screens/profile/components/learning_and_development_screen.dart b/lib/screens/profile/components/learning_and_development_screen.dart index d77859c..7f9f7cd 100644 --- a/lib/screens/profile/components/learning_and_development_screen.dart +++ b/lib/screens/profile/components/learning_and_development_screen.dart @@ -132,7 +132,7 @@ class LearningAndDevelopmentScreen extends StatelessWidget { height: 5, ), Text( - "$duration : $start to $end'", + "$duration: $start to $end", style: Theme.of(context) .textTheme .labelMedium, diff --git a/lib/screens/profile/components/references_screen.dart b/lib/screens/profile/components/references_screen.dart index b02608e..cd9b6ae 100644 --- a/lib/screens/profile/components/references_screen.dart +++ b/lib/screens/profile/components/references_screen.dart @@ -1,6 +1,12 @@ import 'package:flutter/material.dart'; import 'package:flutter/src/widgets/framework.dart'; import 'package:flutter/src/widgets/placeholder.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:flutter_progress_hud/flutter_progress_hud.dart'; +import 'package:flutter_spinkit/flutter_spinkit.dart'; +import 'package:unit2/bloc/profile/profile_bloc.dart'; +import 'package:unit2/bloc/references/references_bloc.dart'; +import 'package:unit2/bloc/user/user_bloc.dart'; import 'package:unit2/model/profile/references.dart'; import 'package:unit2/theme-data.dart/box_shadow.dart'; import 'package:unit2/theme-data.dart/colors.dart'; @@ -9,78 +15,124 @@ import 'package:unit2/widgets/Leadings/add_leading.dart'; import 'package:unit2/widgets/empty_data.dart'; class ReferencesScreen extends StatelessWidget { - final List references; - const ReferencesScreen({super.key, required this.references}); - + const ReferencesScreen({super.key}); @override Widget build(BuildContext context) { return Scaffold( - appBar: AppBar( - title: const Text(referencesScreenTitle), - centerTitle: true, - backgroundColor: primary, - actions: [AddLeading(onPressed: (){})], - ), - body: references.isNotEmpty? ListView.builder( - padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 10), - itemCount: references.length, - itemBuilder: (BuildContext context, int index) { - String fullname = - "${references[index].firstName} ${references[index].middleName} ${references[index].lastName}"; - String addres = - "${references[index].address!.cityMunicipality!.description}, ${references[index].address!.cityMunicipality!.province!.description}, ${references[0].address!.cityMunicipality!.province!.region!.description}"; - String mobile = references[index].contactNo.toString(); - return Column( - children: [ - Container( + appBar: AppBar( + title: const Text(referencesScreenTitle), + centerTitle: true, + backgroundColor: primary, + actions: [AddLeading(onPressed: () {})], + ), + body: ProgressHUD( + padding: const EdgeInsets.all(24), + backgroundColor: Colors.black87, + indicatorWidget: const SpinKitFadingCircle( + color: Colors.white), + child: BlocBuilder( + builder: (context, state) { + //userbloc + if (state is UserLoggedIn) { + return BlocBuilder( + builder: (context, state) { + //profilebloc + if (state is ProfileLoaded) { + return BlocConsumer( + //listener + listener: (context, state) { + if(state is ReferencesLoadingState){ + final progress = ProgressHUD.of(context); + progress!.showWithText("Please wait..."); + } + if(state is ReferencesLoadedState || state is ReferencesErrorState){ + final progress = ProgressHUD.of(context); + progress!.dismiss(); + } + }, + //builder + builder: (context, state) { + //references bloc + if (state is ReferencesLoadedState) { + if (state.references.isNotEmpty) { + return ListView.builder( padding: - const EdgeInsets.symmetric(horizontal: 12, vertical: 8), - decoration: box1(), - child: Row( - children: [ - Expanded( - child: Column( - mainAxisAlignment: MainAxisAlignment.start, - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text(fullname, - style: Theme.of(context) - .textTheme - .titleMedium! - .copyWith(fontWeight: FontWeight.w500)), - const Divider(), - const SizedBox( - height: 5, - ), - Text(addres, - style: Theme.of(context) - .textTheme - .titleSmall! - .copyWith(fontWeight: FontWeight.w500)), - const SizedBox( - height: 8, - ), - Text("${mobileOrPhone.toUpperCase()} : $mobile", - style: - Theme.of(context).textTheme.labelMedium!), - ], + const EdgeInsets.symmetric(vertical: 8, horizontal: 10), + itemCount: state.references.length, + itemBuilder: (BuildContext context, int index) { + String fullname = + "${state.references[index].firstName} ${state.references[index].middleName} ${state.references[index].lastName}"; + String addres = + "${state.references[index].address!.cityMunicipality!.description}, ${state.references[index].address!.cityMunicipality!.province!.description}, ${state.references[0].address!.cityMunicipality!.province!.region!.description}"; + String mobile = state.references[index].contactNo.toString(); + return Column( + children: [ + Container( + padding: const EdgeInsets.symmetric( + horizontal: 12, vertical: 8), + decoration: box1(), + child: Row( + children: [ + Expanded( + child: Column( + mainAxisAlignment: MainAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text(fullname, + style: Theme.of(context) + .textTheme + .titleMedium! + .copyWith( + fontWeight: FontWeight.w500)), + const Divider(), + const SizedBox( + height: 5, + ), + Text(addres, + style: Theme.of(context) + .textTheme + .titleSmall! + .copyWith( + fontWeight: FontWeight.w500)), + const SizedBox( + height: 8, + ), + Text("${mobileOrPhone.toUpperCase()} : $mobile", + style: Theme.of(context) + .textTheme + .labelMedium!), + ], + ), + ), + IconButton( + onPressed: () {}, + icon: const Icon( + Icons.more_vert, + color: Colors.grey, + )) + ], + ), ), - ), - IconButton( - onPressed: () {}, - icon: const Icon( - Icons.more_vert, - color: Colors.grey, - )) - ], - ), - ), - const SizedBox( - height: 5, - ), - ], - ); - }):const EmptyData(message: "You don't have any References added. Please click + to add."), - ); + const SizedBox( + height: 5, + ), + ], + ); + }); + } + } + return Container(); + }, + ); + } + return Container(); + }, + ); + } + return Container(); + }, + ), + )); } } + diff --git a/lib/screens/profile/profile.dart b/lib/screens/profile/profile.dart index 7441db5..951b2e7 100644 --- a/lib/screens/profile/profile.dart +++ b/lib/screens/profile/profile.dart @@ -12,6 +12,7 @@ import 'package:unit2/bloc/education/education_bloc.dart'; import 'package:unit2/bloc/eligibility/eligibility_bloc.dart'; import 'package:unit2/bloc/learningDevelopment/learning_development_bloc.dart'; import 'package:unit2/bloc/profile/profile_bloc.dart'; +import 'package:unit2/bloc/references/references_bloc.dart'; import 'package:unit2/bloc/workHistory/workHistory_bloc.dart'; import 'package:unit2/model/login_data/employee_info/employee_info.dart'; import 'package:unit2/screens/profile/components/basic_information/address_screen.dart'; @@ -230,12 +231,13 @@ class _ProfileInfoState extends State { icon: Brandico.codepen, title: "Personal References", onTap: () { - // Navigator.push(context, MaterialPageRoute( - // builder: (BuildContext context) { - // return ReferencesScreen( - // references: - // state.profileInformation.references); - // })); + Navigator.push(context, MaterialPageRoute( + builder: (BuildContext context) { + return BlocProvider( + create: (context) => ReferencesBloc()..add(GetReferences(profileId: profileId!, token: token!)), + child: const ReferencesScreen(), + ); + })); }, ), ExpandableGroup( diff --git a/lib/sevices/profile/references_services.dart b/lib/sevices/profile/references_services.dart new file mode 100644 index 0000000..f425f0d --- /dev/null +++ b/lib/sevices/profile/references_services.dart @@ -0,0 +1,40 @@ + + + +import 'dart:convert'; + +import 'package:unit2/utils/request.dart'; + +import '../../model/profile/references.dart'; +import '../../utils/urls.dart'; +import 'package:http/http.dart' as http; +class ReferencesServices{ + static final ReferencesServices _instance = ReferencesServices(); + static ReferencesServices get instace => _instance; + + Future> getRefences(int profileId, String token)async{ + +List references = []; + String authToken = "Token $token"; + String path = "${Url.instance.getRefences()}$profileId/"; + Map headers = { + 'Content-Type': 'application/json; charset=UTF-8', + 'Authorization': authToken + }; + try{ + http.Response response = await Request.instance.getRequest(path: path,param: {},headers: headers); + if(response.statusCode == 200){ + Map data = jsonDecode(response.body); + if(data['data'] != null){ + data['data'].forEach((var ref){ + PersonalReference reference = PersonalReference.fromJson(ref); + references.add(reference); + }); + } + } + }catch(e){ + throw e.toString(); + } + return references; + } +} \ No newline at end of file diff --git a/lib/utils/urls.dart b/lib/utils/urls.dart index b5bd7db..412a4b2 100644 --- a/lib/utils/urls.dart +++ b/lib/utils/urls.dart @@ -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() { @@ -57,6 +57,11 @@ String getLearningAndDevelopments(){ return "api/jobnet_app/profile/pds/learning_development/"; } +//// references paths +String getRefences(){ + return "/api/jobnet_app/profile/pds/personal_reference/"; +} + // location utils path String getCounties(){ return "/api/jobnet_app/countries/";