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

@ -16,8 +16,7 @@ class TimeZoneScreenPage extends StatelessWidget {
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(
@ -25,13 +24,14 @@ class TimeZoneScreenPage extends StatelessWidget {
backgroundColor: Colors.red, backgroundColor: Colors.red,
), ),
); );
}if (state is SaveState) { }
if (state is SaveState) {
Navigator.of(context).pop(true); 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',
@ -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,),),
), ),
), ),