mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-11-30 13:34:56 +00:00
debounce and refactored CommunitiesBloc.
This commit is contained in:
@ -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(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user