Merged with DEV

This commit is contained in:
Abdullah Alassaf
2024-08-01 10:06:05 +03:00
4 changed files with 77 additions and 74 deletions

View File

@ -24,8 +24,10 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
String regionSelected = ''; String regionSelected = '';
final TextEditingController searchController = TextEditingController(); final TextEditingController searchController = TextEditingController();
final TextEditingController nameController = TextEditingController(text: '${HomeCubit.user!.firstName} ${HomeCubit.user!.lastName}'); final TextEditingController nameController = TextEditingController(text: '${HomeCubit.user!.firstName} ${HomeCubit.user!.lastName}');
List<TimeZone>? timeZoneList;
List<RegionModel>? regionList;
List<RegionModel> allRegions = [];
List<TimeZone> allTimeZone = [];
ProfileBloc() : super(InitialState()) { ProfileBloc() : super(InitialState()) {
@ -90,9 +92,9 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
Future _fetchTimeZone(TimeZoneInitialEvent event, Emitter<ProfileState> emit) async { Future _fetchTimeZone(TimeZoneInitialEvent event, Emitter<ProfileState> emit) async {
emit(LoadingInitialState()); emit(LoadingInitialState());
try { try {
timeZoneList = await ProfileApi.fetchTimeZone(); allTimeZone = await ProfileApi.fetchTimeZone();
emit(UpdateState(timeZoneList: timeZoneList!)); emit(TimeZoneLoadedState(timezone: allTimeZone));
return timeZoneList; return allTimeZone;
} catch (e) { } catch (e) {
emit(FailedState(errorMessage: e.toString())); emit(FailedState(errorMessage: e.toString()));
return; return;
@ -123,33 +125,36 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
} }
} }
Future searchRegion(SearchRegionEvent event, Emitter<ProfileState> emit) async {
Future<void> searchRegion(SearchRegionEvent event, Emitter<ProfileState> emit) async {
emit(LoadingInitialState()); emit(LoadingInitialState());
final query = event.query.toLowerCase(); final query = event.query.toLowerCase();
if(event.query.isNotEmpty){ if (allRegions.isEmpty) {
final filteredRegions = regionList?.where((region) { allRegions = await ProfileApi.fetchRegion();
}
if (query.isNotEmpty) {
final filteredRegions = allRegions.where((region) {
return region.name.toLowerCase().contains(query); return region.name.toLowerCase().contains(query);
}).toList() ?? []; }).toList();
regionList = filteredRegions;
emit(RegionsLoadedState(regions: filteredRegions)); emit(RegionsLoadedState(regions: filteredRegions));
}else{ } else {
regionList = await ProfileApi.fetchRegion(); emit(RegionsLoadedState(regions: allRegions));
emit(RegionsLoadedState(regions: regionList!));
} }
} }
Future searchTimeZone(SearchTimeZoneEvent event, Emitter<ProfileState> emit) async { Future<void> searchTimeZone(SearchTimeZoneEvent event, Emitter<ProfileState> emit) async {
emit(LoadingInitialState()); emit(LoadingInitialState());
final query = event.query.toLowerCase(); final query = event.query.toLowerCase();
if(event.query.isNotEmpty){ if (allTimeZone.isEmpty) {
final filtered = timeZoneList?.where((region) { allTimeZone = await ProfileApi.fetchTimeZone();
}
if (query.isNotEmpty) {
final filteredRegions = allTimeZone.where((region) {
return region.name.toLowerCase().contains(query); return region.name.toLowerCase().contains(query);
}).toList() ?? []; }).toList();
timeZoneList = filtered; emit(TimeZoneLoadedState(timezone: filteredRegions));
emit(TimeZoneLoadedState(regions: filtered)); } else {
}else{ emit(TimeZoneLoadedState(timezone: allTimeZone));
timeZoneList = await ProfileApi.fetchTimeZone();
emit(UpdateState(timeZoneList: timeZoneList!));
} }
} }
@ -157,9 +162,8 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
void _fetchRegion(RegionInitialEvent event, Emitter<ProfileState> emit) async { void _fetchRegion(RegionInitialEvent event, Emitter<ProfileState> emit) async {
try { try {
emit(LoadingInitialState()); emit(LoadingInitialState());
allRegions = await ProfileApi.fetchRegion();
regionList = await ProfileApi.fetchRegion(); emit(RegionsLoadedState(regions: allRegions));
emit(RegionsLoadedState(regions: regionList!));
} catch (e) { } catch (e) {
emit(FailedState(errorMessage: e.toString())); emit(FailedState(errorMessage: e.toString()));
} }

View File

@ -51,7 +51,7 @@ class RegionsLoadedState extends ProfileState {
} }
class TimeZoneLoadedState extends ProfileState { class TimeZoneLoadedState extends ProfileState {
final List<TimeZone> regions; final List<TimeZone> timezone;
const TimeZoneLoadedState({required this.regions}); const TimeZoneLoadedState({required this.timezone});
} }

View File

@ -9,7 +9,6 @@ import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
class RegionPage extends StatelessWidget { class RegionPage extends StatelessWidget {
const RegionPage({super.key}); const RegionPage({super.key});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocProvider( return BlocProvider(
@ -29,7 +28,7 @@ class RegionPage extends StatelessWidget {
}, },
builder: (context, state) { builder: (context, state) {
final profileBloc = BlocProvider.of<ProfileBloc>(context); final profileBloc = BlocProvider.of<ProfileBloc>(context);
final regionList = profileBloc.regionList ?? []; // Safeguard against null final regionList = state is RegionsLoadedState ? state.regions : [];
return DefaultScaffold( return DefaultScaffold(
padding: const EdgeInsets.all(0), padding: const EdgeInsets.all(0),
title: 'Region', title: 'Region',

View File

@ -15,60 +15,60 @@ class TimeZoneScreenPage extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocProvider( return BlocProvider(
create: (BuildContext context) => ProfileBloc()..add(TimeZoneInitialEvent()), create: (BuildContext context) => ProfileBloc()..add(TimeZoneInitialEvent()),
child: child: BlocConsumer<ProfileBloc, ProfileState>(listener: (context, state) {
BlocConsumer<ProfileBloc, ProfileState>(listener: (context, state) { if (state is FailedState) {
if (state is FailedState) { ScaffoldMessenger.of(context).showSnackBar(
ScaffoldMessenger.of(context).showSnackBar( SnackBar(
SnackBar( content: Text(state.errorMessage),
content: Text(state.errorMessage), backgroundColor: Colors.red,
backgroundColor: Colors.red,
), ),
); );
}if (state is SaveState) { }
Navigator.of(context).pop(true); if (state is SaveState) {
Navigator.of(context).pop(true);
} }
}, },
builder: (context, state) { builder: (context, state) {
final profileBloc = BlocProvider.of<ProfileBloc>(context); final profileBloc = BlocProvider.of<ProfileBloc>(context);
final timeZoneList = profileBloc.timeZoneList ?? []; // Safeguard against null final timeZoneList = state is TimeZoneLoadedState ?state.timezone : [];
return DefaultScaffold( return DefaultScaffold(
padding: const EdgeInsets.all(0), padding: const EdgeInsets.all(0),
title: 'Time Zone', title: 'Time Zone',
child: state is LoadingInitialState child: state is LoadingInitialState
? const Center(child: CircularProgressIndicator()) ? const Center(child: CircularProgressIndicator())
: Column( : Column(
children: [ children: [
TextFormField( TextFormField(
controller:profileBloc.searchController , controller:profileBloc.searchController ,
onChanged: (value) { onChanged: (value) {
profileBloc.add(SearchTimeZoneEvent(query: value)); profileBloc.add(SearchTimeZoneEvent(query: value));
}, },
decoration: const InputDecoration( decoration: const InputDecoration(
prefixIcon: Icon(Icons.search), prefixIcon: Icon(Icons.search),
hintText: 'Search', hintText: 'Search',
fillColor: ColorsManager.textGray, fillColor: ColorsManager.textGray,
),
),
const SizedBox(height: 10),
Expanded(
child: Container(
decoration: const BoxDecoration(
color: ColorsManager.onPrimaryColor,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(20),
), ),
), ),
const SizedBox(height: 10), child: Padding(
Expanded( padding: const EdgeInsets.only(bottom: 10.0,top: 10.0,left: 15,right: 15),
child: Container( child: ListView.builder(
decoration: const BoxDecoration(
color: ColorsManager.onPrimaryColor,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(20),
),
),
child: Padding(
padding: const EdgeInsets.only(bottom: 10.0,top: 10.0,left: 15,right: 15),
child: ListView.builder(
itemCount: timeZoneList.length, itemCount: timeZoneList.length,
itemBuilder: (context, index) { itemBuilder: (context, index) {
return InkWell( return InkWell(
onTap: () { onTap: () {
profileBloc.add(SelectTimeZoneEvent(val: timeZoneList[index].id, context: context)); profileBloc.add(SelectTimeZoneEvent(val: timeZoneList[index].id, context: context));
}, },
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
@ -84,7 +84,7 @@ class TimeZoneScreenPage extends StatelessWidget {
fontSize: 13, fontSize: 13,
fontColor: ColorsManager.textGray,), fontColor: ColorsManager.textGray,),
leading: BodyMedium( leading: BodyMedium(
fontSize: 15, fontSize: 13,
text: timeZoneList[index].name,),), text: timeZoneList[index].name,),),
), ),
), ),
@ -99,7 +99,7 @@ class TimeZoneScreenPage extends StatelessWidget {
], ],
), ),
); );
}) })
); );
} }
} }