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 = '';
final TextEditingController searchController = TextEditingController();
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()) {
@ -90,9 +92,9 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
Future _fetchTimeZone(TimeZoneInitialEvent event, Emitter<ProfileState> emit) async {
emit(LoadingInitialState());
try {
timeZoneList = await ProfileApi.fetchTimeZone();
emit(UpdateState(timeZoneList: timeZoneList!));
return timeZoneList;
allTimeZone = await ProfileApi.fetchTimeZone();
emit(TimeZoneLoadedState(timezone: allTimeZone));
return allTimeZone;
} catch (e) {
emit(FailedState(errorMessage: e.toString()));
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());
final query = event.query.toLowerCase();
if(event.query.isNotEmpty){
final filteredRegions = regionList?.where((region) {
if (allRegions.isEmpty) {
allRegions = await ProfileApi.fetchRegion();
}
if (query.isNotEmpty) {
final filteredRegions = allRegions.where((region) {
return region.name.toLowerCase().contains(query);
}).toList() ?? [];
regionList = filteredRegions;
}).toList();
emit(RegionsLoadedState(regions: filteredRegions));
} else {
regionList = await ProfileApi.fetchRegion();
emit(RegionsLoadedState(regions: regionList!));
emit(RegionsLoadedState(regions: allRegions));
}
}
Future searchTimeZone(SearchTimeZoneEvent event, Emitter<ProfileState> emit) async {
Future<void> searchTimeZone(SearchTimeZoneEvent event, Emitter<ProfileState> emit) async {
emit(LoadingInitialState());
final query = event.query.toLowerCase();
if(event.query.isNotEmpty){
final filtered = timeZoneList?.where((region) {
if (allTimeZone.isEmpty) {
allTimeZone = await ProfileApi.fetchTimeZone();
}
if (query.isNotEmpty) {
final filteredRegions = allTimeZone.where((region) {
return region.name.toLowerCase().contains(query);
}).toList() ?? [];
timeZoneList = filtered;
emit(TimeZoneLoadedState(regions: filtered));
}).toList();
emit(TimeZoneLoadedState(timezone: filteredRegions));
} else {
timeZoneList = await ProfileApi.fetchTimeZone();
emit(UpdateState(timeZoneList: timeZoneList!));
emit(TimeZoneLoadedState(timezone: allTimeZone));
}
}
@ -157,9 +162,8 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
void _fetchRegion(RegionInitialEvent event, Emitter<ProfileState> emit) async {
try {
emit(LoadingInitialState());
regionList = await ProfileApi.fetchRegion();
emit(RegionsLoadedState(regions: regionList!));
allRegions = await ProfileApi.fetchRegion();
emit(RegionsLoadedState(regions: allRegions));
} catch (e) {
emit(FailedState(errorMessage: e.toString()));
}

View File

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

View File

@ -16,8 +16,7 @@ class TimeZoneScreenPage extends StatelessWidget {
Widget build(BuildContext context) {
return BlocProvider(
create: (BuildContext context) => ProfileBloc()..add(TimeZoneInitialEvent()),
child:
BlocConsumer<ProfileBloc, ProfileState>(listener: (context, state) {
child: BlocConsumer<ProfileBloc, ProfileState>(listener: (context, state) {
if (state is FailedState) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
@ -25,13 +24,14 @@ class TimeZoneScreenPage extends StatelessWidget {
backgroundColor: Colors.red,
),
);
}if (state is SaveState) {
}
if (state is SaveState) {
Navigator.of(context).pop(true);
}
},
builder: (context, state) {
final profileBloc = BlocProvider.of<ProfileBloc>(context);
final timeZoneList = profileBloc.timeZoneList ?? []; // Safeguard against null
final timeZoneList = state is TimeZoneLoadedState ?state.timezone : [];
return DefaultScaffold(
padding: const EdgeInsets.all(0),
title: 'Time Zone',
@ -84,7 +84,7 @@ class TimeZoneScreenPage extends StatelessWidget {
fontSize: 13,
fontColor: ColorsManager.textGray,),
leading: BodyMedium(
fontSize: 15,
fontSize: 13,
text: timeZoneList[index].name,),),
),
),