Aidra Driver 1.3.5+68
Aidra Driver - Your path to green energy
Loading...
Searching...
No Matches
weekly_collections_bloc.dart
Go to the documentation of this file.
1import 'package:bloc/bloc.dart';
2import 'package:equatable/equatable.dart';
3
4import '../../../../../core/error/failures.dart';
5import '../../../../../core/services/service_locator.dart';
6import '../../../domain/entities/res/res_weekly_collection_entity.dart';
7import '../../../domain/usecases/get_weekly_collections_usecase.dart';
8
9part 'weekly_collections_event.dart';
10part 'weekly_collections_state.dart';
11
12class WeeklyCollectionsBloc extends Bloc<WeeklyCollectionsEvent, WeeklyCollectionsState> {
14
15 WeeklyCollectionsBloc() : super(WeeklyCollectionsInitial()) {
16 on<LoadWeeklyCollectionsEvent>(_onLoadWeeklyCollections);
17 on<SearchWeeklyCollectionsEvent>(_onSearchWeeklyCollections);
18 on<ToggleCollectionSelectionEvent>(_onToggleCollectionSelection);
19 on<ClearSelectionEvent>(_onClearSelection);
20 on<SelectAllCollectionsEvent>(_onSelectAllCollections);
21 }
22
25 Emitter<WeeklyCollectionsState> emit,
26 ) async {
27 emit(WeeklyCollectionsLoading());
28
29 final result = await getWeeklyCollectionsUseCase(
31 userId: event.userId,
32 startDate: event.startDate,
33 endDate: event.endDate,
34 ),
35 );
36
37 result.fold(
38 (failure) => emit(WeeklyCollectionsError(failure: failure)),
39 (collections) => emit(
44 ),
45 ),
46 );
47 }
48
50 SearchWeeklyCollectionsEvent event,
51 Emitter<WeeklyCollectionsState> emit,
52 ) {
53 if (state is WeeklyCollectionsLoaded) {
54 final currentState = state as WeeklyCollectionsLoaded;
55 final filteredCollections = currentState.allCollections.where((collection) {
56 final query = event.query.toLowerCase();
57 return collection.city!.toLowerCase().contains(query) ||
58 collection.supplierName!.toLowerCase().contains(query) ||
59 collection.internalCode!.toLowerCase().contains(query);
60 }).toList();
61
62 emit(currentState.copyWith(
64 searchQuery: event.query,
65 ));
66 }
67 }
68
71 Emitter<WeeklyCollectionsState> emit,
72 ) {
73 if (state is WeeklyCollectionsLoaded) {
74 final currentState = state as WeeklyCollectionsLoaded;
75 final selectedCollections = Set<ResWeeklyCollectionEntity>.from(currentState.selectedCollections);
76
77 if (selectedCollections.contains(event.collection)) {
78 selectedCollections.remove(event.collection);
79 } else {
80 selectedCollections.add(event.collection);
81 }
82
83 emit(currentState.copyWith(selectedCollections: selectedCollections));
84 }
85 }
86
88 ClearSelectionEvent event,
89 Emitter<WeeklyCollectionsState> emit,
90 ) {
91 if (state is WeeklyCollectionsLoaded) {
92 final currentState = state as WeeklyCollectionsLoaded;
93 emit(currentState.copyWith(selectedCollections: {}));
94 }
95 }
96
98 SelectAllCollectionsEvent event,
99 Emitter<WeeklyCollectionsState> emit,
100 ) {
101 if (state is WeeklyCollectionsLoaded) {
102 final currentState = state as WeeklyCollectionsLoaded;
103 emit(currentState.copyWith(
104 selectedCollections: Set<ResWeeklyCollectionEntity>.from(currentState.filteredCollections),
105 ));
106 }
107 }
108}
sealed class AuthenticationState extends Equatable failure
sealed class CheckInOutEvent extends Equatable userId
void _onToggleCollectionSelection(ToggleCollectionSelectionEvent event, Emitter< WeeklyCollectionsState > emit,)
void _onSelectAllCollections(SelectAllCollectionsEvent event, Emitter< WeeklyCollectionsState > emit,)
void _onClearSelection(ClearSelectionEvent event, Emitter< WeeklyCollectionsState > emit,)
Future< void > _onLoadWeeklyCollections(LoadWeeklyCollectionsEvent event, Emitter< WeeklyCollectionsState > emit,) async
void _onSearchWeeklyCollections(SearchWeeklyCollectionsEvent event, Emitter< WeeklyCollectionsState > emit,)
sealed class CollectionsState extends Equatable collections
final String startDate
final String endDate
GetWeeklyCollectionsUseCase({required this.repository})
final Set< ResWeeklyCollectionEntity > selectedCollections
final sl
class SearchWeeklyCollectionsEvent extends WeeklyCollectionsEvent collection
const ToggleCollectionSelectionEvent({required this.collection})
const LoadWeeklyCollectionsEvent({ required this.userId, required this.startDate, required this.endDate, })
final String searchQuery
const WeeklyCollectionsLoaded({ required this.allCollections, required this.filteredCollections, required this.selectedCollections, this.searchQuery='', })
sealed class WeeklyCollectionsState extends Equatable allCollections
final List< ResWeeklyCollectionEntity > filteredCollections