fix
This commit is contained in:
@@ -9,26 +9,53 @@ class NotificationsBloc extends Bloc<NotificationsEvent, NotificationsState> {
|
||||
|
||||
NotificationsBloc(this._getNotificationsUsecase) : super(const NotificationsState()) {
|
||||
on<NotificationsFetchRequested>(_onFetchRequested);
|
||||
on<NotificationsLoadMoreRequested>(_onLoadMoreRequested);
|
||||
}
|
||||
|
||||
Future<void> _onFetchRequested(
|
||||
NotificationsFetchRequested event,
|
||||
Emitter<NotificationsState> emit,
|
||||
) async {
|
||||
emit(state.copyWith(status: NotificationsStatus.loading));
|
||||
if (event.page == 1) {
|
||||
emit(state.copyWith(status: NotificationsStatus.loading));
|
||||
}
|
||||
|
||||
try {
|
||||
final notifications = await _getNotificationsUsecase();
|
||||
final result = await _getNotificationsUsecase(
|
||||
page: event.page,
|
||||
pageSize: 100,
|
||||
);
|
||||
|
||||
final newNotifications = event.page == 1
|
||||
? result.notifications
|
||||
: [...state.notifications, ...result.notifications];
|
||||
|
||||
emit(state.copyWith(
|
||||
status: NotificationsStatus.success,
|
||||
notifications: notifications,
|
||||
notifications: newNotifications,
|
||||
currentPage: result.pagination.currentPage,
|
||||
totalPages: result.pagination.lastPage,
|
||||
hasMore: result.pagination.currentPage < result.pagination.lastPage,
|
||||
isLoadingMore: false,
|
||||
));
|
||||
} catch (e) {
|
||||
dev.log('NotificationsBloc: Ошибка загрузки: $e');
|
||||
emit(state.copyWith(
|
||||
status: NotificationsStatus.failure,
|
||||
errorMessage: e.toString(),
|
||||
status: event.page == 1 ? NotificationsStatus.failure : NotificationsStatus.success,
|
||||
errorMessage: event.page == 1 ? e.toString() : null,
|
||||
isLoadingMore: false,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onLoadMoreRequested(
|
||||
NotificationsLoadMoreRequested event,
|
||||
Emitter<NotificationsState> emit,
|
||||
) async {
|
||||
if (!state.hasMore || state.isLoadingMore) return;
|
||||
|
||||
emit(state.copyWith(isLoadingMore: true));
|
||||
final nextPage = state.currentPage + 1;
|
||||
add(NotificationsFetchRequested(page: nextPage));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user