Aidra Driver 1.3.5+68
Aidra Driver - Your path to green energy
Loading...
Searching...
No Matches
routing_planning_bloc.dart
Go to the documentation of this file.
1import 'package:bloc/bloc.dart';
2import 'package:equatable/equatable.dart';
3import 'package:google_maps_flutter/google_maps_flutter.dart';
4
5import '../../../../../core/error/failures.dart';
6import '../../../../../core/services/service_locator.dart';
7import '../../../domain/entities/req/req_create_routing_entity.dart';
8import '../../../domain/entities/res/res_create_routing_entity.dart';
9import '../../../domain/entities/res/res_weekly_collection_entity.dart';
10import '../../../domain/entities/res/res_collection_to_schedule_entity.dart';
11import '../../../domain/usecases/create_routing_usecase.dart';
12
13part 'routing_planning_event.dart';
14part 'routing_planning_state.dart';
15
17 extends Bloc<RoutingPlanningEvent, RoutingPlanningState> {
18 final CreateRoutingUseCase createRoutingUseCase = sl<CreateRoutingUseCase>();
19
20 RoutingPlanningBloc() : super(RoutingPlanningInitial()) {
21 on<SetStartLocationEvent>(_onSetStartLocation);
22 on<SetSelectedCollectionsEvent>(_onSetSelectedCollections);
23 on<SetCollectionsToScheduleEvent>(_onSetCollectionsToSchedule);
24 on<SetEndLocationEvent>(_onSetEndLocation);
25 on<CreateRoutingEvent>(_onCreateRouting);
26 on<ResetRoutingPlanningEvent>(_onResetRoutingPlanning);
27 }
28
31 Emitter<RoutingPlanningState> emit,
32 ) {
33 final currentState = state;
34 if (currentState is RoutingPlanningLoaded) {
35 emit(currentState.copyWith(
36 startLocation: event.location,
37 startAddress: event.address,
38 ));
39 } else {
41 startLocation: event.location,
42 startAddress: event.address,
43 ));
44 }
45 }
46
48 SetSelectedCollectionsEvent event,
49 Emitter<RoutingPlanningState> emit,
50 ) {
51 final currentState = state;
52 if (currentState is RoutingPlanningLoaded) {
53 emit(currentState.copyWith(selectedCollections: event.collections));
54 } else {
55 emit(RoutingPlanningLoaded(selectedCollections: event.collections));
56 }
57 }
58
61 Emitter<RoutingPlanningState> emit,
62 ) {
63 final currentState = state;
64 if (currentState is RoutingPlanningLoaded) {
65 emit(currentState.copyWith(collectionsToSchedule: event.collections));
66 } else {
67 emit(RoutingPlanningLoaded(collectionsToSchedule: event.collections));
68 }
69 }
70
73 Emitter<RoutingPlanningState> emit,
74 ) {
75 final currentState = state;
76 if (currentState is RoutingPlanningLoaded) {
77 emit(currentState.copyWith(
78 endLocation: event.location,
79 endAddress: event.address,
80 ));
81 } else {
83 endLocation: event.location,
84 endAddress: event.address,
85 ));
86 }
87 }
88
89 Future<void> _onCreateRouting(
90 CreateRoutingEvent event,
91 Emitter<RoutingPlanningState> emit,
92 ) async {
93 final currentState = state;
94
95 // Handle both RoutingPlanningLoaded and RoutingPlanningError states
96 LatLng? startLocation;
98 Set<ResWeeklyCollectionEntity>? selectedCollections;
99 List<ResCollectionToScheduleEntity>? collectionsToSchedule;
100 LatLng? endLocation;
102 bool isComplete = false;
103
104 if (currentState is RoutingPlanningLoaded) {
105 startLocation = currentState.startLocation;
106 startAddress = currentState.startAddress;
107 selectedCollections = currentState.selectedCollections;
108 collectionsToSchedule = currentState.collectionsToSchedule;
109 endLocation = currentState.endLocation;
110 endAddress = currentState.endAddress;
111 isComplete = currentState.isComplete;
112 } else if (currentState is RoutingPlanningError) {
113 startLocation = currentState.startLocation;
114 startAddress = currentState.startAddress;
115 selectedCollections = currentState.selectedCollections;
116 collectionsToSchedule = currentState.collectionsToSchedule;
117 endLocation = currentState.endLocation;
118 endAddress = currentState.endAddress;
119 isComplete = currentState.isComplete;
120 } else {
121 return;
122 }
123
124 if (!isComplete) {
126 failure: const ValidationFailure(
127 message: 'Missing required data for routing creation'),
134 ));
135 return;
136 }
137
138 emit(RoutingPlanningLoading());
139
140 final purchaseOrders = collectionsToSchedule!
142 id: collection.id,
143 reference: collection.reference,
144 ))
145 .toList();
146
147 final drivers = [DriverEntity(id: event.driverId)];
148
150 duration: event.duration,
151 startLocationLatitude: startLocation!.latitude.toString(),
152 startLocationLongitude: startLocation.longitude.toString(),
153 endLocationLatitude: endLocation!.latitude.toString(),
154 endLocationLongitude: endLocation.longitude.toString(),
155 purchaseOrders: purchaseOrders,
156 drivers: drivers,
157 );
158
159 final result = await createRoutingUseCase(request);
160
161 result.fold(
170 )),
171 (routing) => emit(RoutingPlanningSuccess(routing: routing)),
172 );
173 }
174
176 ResetRoutingPlanningEvent event,
177 Emitter<RoutingPlanningState> emit,
178 ) {
179 emit(RoutingPlanningInitial());
180 }
181}
sealed class AuthenticationState extends Equatable failure
void _onResetRoutingPlanning(ResetRoutingPlanningEvent event, Emitter< RoutingPlanningState > emit,)
void _onSetCollectionsToSchedule(SetCollectionsToScheduleEvent event, Emitter< RoutingPlanningState > emit,)
Future< void > _onCreateRouting(CreateRoutingEvent event, Emitter< RoutingPlanningState > emit,) async
void _onSetEndLocation(SetEndLocationEvent event, Emitter< RoutingPlanningState > emit,)
void _onSetStartLocation(SetStartLocationEvent event, Emitter< RoutingPlanningState > emit,)
void _onSetSelectedCollections(SetSelectedCollectionsEvent event, Emitter< RoutingPlanningState > emit,)
abstract class CollectionsToScheduleEvent extends Equatable request
class Partner String
final String message
Definition failures.dart:0
DriverEntity({required this.id})
const SetCollectionsToScheduleEvent({required this.collections})
const SetStartLocationEvent({ required this.location, required this.address, })
const SetEndLocationEvent({ required this.location, required this.address, })
const RoutingPlanningLoaded({ this.startLocation, this.startAddress, this.selectedCollections, this.collectionsToSchedule, this.endLocation, this.endAddress, })
final String startAddress
final String endAddress
const RoutingPlanningError({ required this.failure, this.startLocation, this.startAddress, this.selectedCollections, this.collectionsToSchedule, this.endLocation, this.endAddress, })
sealed class RoutingPlanningState extends Equatable startLocation
bool get isComplete
final Set< ResWeeklyCollectionEntity > selectedCollections
final List< ResCollectionToScheduleEntity > collectionsToSchedule
final LatLng endLocation
final sl
class SearchWeeklyCollectionsEvent extends WeeklyCollectionsEvent collection