debounce and refactored CommunitiesBloc.

This commit is contained in:
Faris Armoush
2025-06-22 12:01:32 +03:00
parent 51c088d998
commit 65ed94eb08
5 changed files with 204 additions and 88 deletions

View File

@ -15,7 +15,6 @@ class CommunitiesBloc extends Bloc<CommunitiesEvent, CommunitiesState> {
super(const CommunitiesState()) {
on<LoadCommunities>(_onLoadCommunities);
on<LoadMoreCommunities>(_onLoadMoreCommunities);
on<SearchCommunities>(_onSearchCommunities);
}
final CommunitiesService _communitiesService;
@ -39,19 +38,9 @@ class CommunitiesBloc extends Bloc<CommunitiesEvent, CommunitiesState> {
),
);
} on APIException catch (e) {
emit(
state.copyWith(
status: CommunitiesStatus.failure,
errorMessage: e.message,
),
);
_onApiException(e, emit);
} catch (e) {
emit(
state.copyWith(
status: CommunitiesStatus.failure,
errorMessage: e.toString(),
),
);
_onError(e, emit);
}
}
@ -83,59 +72,30 @@ class CommunitiesBloc extends Bloc<CommunitiesEvent, CommunitiesState> {
),
);
} on APIException catch (e) {
emit(
state.copyWith(
isLoadingMore: false,
errorMessage: e.message,
),
);
_onApiException(e, emit);
} catch (e) {
emit(
state.copyWith(
isLoadingMore: false,
errorMessage: e.toString(),
),
);
_onError(e, emit);
}
}
Future<void> _onSearchCommunities(
SearchCommunities event,
void _onApiException(
APIException e,
Emitter<CommunitiesState> emit,
) async {
try {
emit(state.copyWith(status: CommunitiesStatus.loading));
) {
emit(
state.copyWith(
isLoadingMore: false,
errorMessage: e.message,
),
);
}
final param = LoadCommunitiesParam(
page: 1,
search: event.searchQuery,
);
final paginationResponse = await _communitiesService.getCommunity(param);
emit(
CommunitiesState(
status: CommunitiesStatus.success,
communities: paginationResponse.communities,
hasNext: paginationResponse.hasNext,
currentPage: paginationResponse.page,
searchQuery: event.searchQuery,
),
);
} on APIException catch (e) {
emit(
state.copyWith(
status: CommunitiesStatus.failure,
errorMessage: e.message,
),
);
} catch (e) {
emit(
state.copyWith(
status: CommunitiesStatus.failure,
errorMessage: e.toString(),
),
);
}
void _onError(Object e, Emitter<CommunitiesState> emit) {
emit(
state.copyWith(
isLoadingMore: false,
errorMessage: e.toString(),
),
);
}
}

View File

@ -22,12 +22,3 @@ class LoadMoreCommunities extends CommunitiesEvent {
@override
List<Object?> get props => [];
}
class SearchCommunities extends CommunitiesEvent {
const SearchCommunities(this.searchQuery);
final String searchQuery;
@override
List<Object?> get props => [searchQuery];
}