@@ -3,6 +3,7 @@ import 'dart:async';
33import 'package:equatable/equatable.dart' ;
44import 'package:flutter_bloc/flutter_bloc.dart' ;
55import 'package:injectable/injectable.dart' ;
6+ import 'package:on_time_front/core/dio/api_error_message.dart' ;
67import 'package:on_time_front/core/logging/app_logger.dart' ;
78import 'package:on_time_front/domain/entities/preparation_entity.dart' ;
89import 'package:on_time_front/domain/entities/schedule_entity.dart' ;
@@ -35,19 +36,15 @@ class MonthlySchedulesBloc
3536 on < MonthlySchedulesPreparationsPrefetchRequested > (
3637 _onPreparationsPrefetchRequested,
3738 );
38- on < MonthlySchedulesPreparationsStreamChanged > (
39- _onPreparationsStreamChanged,
40- );
39+ on < MonthlySchedulesPreparationsStreamChanged > (_onPreparationsStreamChanged);
4140
42- _preparationSubscription = _streamPreparationsUseCase ().listen (
43- (preparations) {
44- add (
45- MonthlySchedulesPreparationsStreamChanged (
46- preparations: preparations,
47- ),
48- );
49- },
50- );
41+ _preparationSubscription = _streamPreparationsUseCase ().listen ((
42+ preparations,
43+ ) {
44+ add (
45+ MonthlySchedulesPreparationsStreamChanged (preparations: preparations),
46+ );
47+ });
5148 }
5249
5350 final LoadSchedulesForMonthUseCase _loadSchedulesForMonthUseCase;
@@ -85,9 +82,8 @@ class MonthlySchedulesBloc
8582 _requestVisibleDatePreparationPrefetch (nextState);
8683 return nextState;
8784 },
88- onError: (error, stackTrace) => state.copyWith (
89- status: () => MonthlySchedulesStatus .error,
90- ),
85+ onError: (error, stackTrace) =>
86+ state.copyWith (status: () => MonthlySchedulesStatus .error),
9187 );
9288 }
9389
@@ -119,12 +115,14 @@ class MonthlySchedulesBloc
119115 ? event.endDate
120116 : state.endDate! ;
121117
122- emit (state.copyWith (
123- status: () => MonthlySchedulesStatus .loading,
124- schedules: () => state.schedules,
125- startDate: () => startDate,
126- endDate: () => endDate,
127- ));
118+ emit (
119+ state.copyWith (
120+ status: () => MonthlySchedulesStatus .loading,
121+ schedules: () => state.schedules,
122+ startDate: () => startDate,
123+ endDate: () => endDate,
124+ ),
125+ );
128126
129127 try {
130128 await _loadSchedulesForMonthUseCase (event.date);
@@ -149,9 +147,7 @@ class MonthlySchedulesBloc
149147 return nextState;
150148 },
151149 onError: (error, stackTrace) {
152- return state.copyWith (
153- status: () => MonthlySchedulesStatus .error,
154- );
150+ return state.copyWith (status: () => MonthlySchedulesStatus .error);
155151 },
156152 );
157153 }
@@ -160,19 +156,29 @@ class MonthlySchedulesBloc
160156 MonthlySchedulesScheduleDeleted event,
161157 Emitter <MonthlySchedulesState > emit,
162158 ) async {
159+ final previousPreparationMap = Map <String , Duration >.from (
160+ state.preparationDurationByScheduleId,
161+ );
163162 try {
164- final updatedPreparationMap =
165- Map <String , Duration >.from (state.preparationDurationByScheduleId)
166- ..remove (event.schedule.id);
167- emit (state.copyWith (
168- lastDeletedSchedule: () => event.schedule,
169- preparationDurationByScheduleId: () => updatedPreparationMap,
170- ));
163+ final updatedPreparationMap = Map <String , Duration >.from (
164+ previousPreparationMap,
165+ )..remove (event.schedule.id);
166+ emit (
167+ state.copyWith (
168+ lastDeletedSchedule: () => event.schedule,
169+ preparationDurationByScheduleId: () => updatedPreparationMap,
170+ ),
171+ );
171172 await _deleteScheduleUseCase (event.schedule);
172173 } catch (e) {
173- emit (state.copyWith (
174- status: () => MonthlySchedulesStatus .error,
175- ));
174+ emit (
175+ state.copyWith (
176+ lastDeletedSchedule: () => null ,
177+ preparationDurationByScheduleId: () => previousPreparationMap,
178+ deleteFailureMessage: () => ApiErrorMessage .fromException (e),
179+ deleteFailureCount: () => state.deleteFailureCount + 1 ,
180+ ),
181+ );
176182 }
177183 }
178184
@@ -183,18 +189,19 @@ class MonthlySchedulesBloc
183189 try {
184190 await _loadSchedulesForMonthUseCase (event.date);
185191 } catch (_) {
186- emit (state.copyWith (
187- status: () => MonthlySchedulesStatus .error,
188- ));
192+ emit (state.copyWith (status: () => MonthlySchedulesStatus .error));
189193 }
190194 }
191195
192196 void _onVisibleDateChanged (
193197 MonthlySchedulesVisibleDateChanged event,
194198 Emitter <MonthlySchedulesState > emit,
195199 ) {
196- final normalizedDate =
197- DateTime (event.date.year, event.date.month, event.date.day);
200+ final normalizedDate = DateTime (
201+ event.date.year,
202+ event.date.month,
203+ event.date.day,
204+ );
198205 final nextState = state.copyWith (visibleDate: () => normalizedDate);
199206 emit (nextState);
200207 _requestVisibleDatePreparationPrefetch (nextState);
@@ -221,8 +228,9 @@ class MonthlySchedulesBloc
221228 // Stream update already has fresher data.
222229 continue ;
223230 }
224- final preparation =
225- await _getPreparationByScheduleIdUseCase (scheduleId);
231+ final preparation = await _getPreparationByScheduleIdUseCase (
232+ scheduleId,
233+ );
226234 fetchedDurations[scheduleId] = preparation.totalDuration;
227235 hasUpdates = true ;
228236 } catch (_) {
@@ -234,11 +242,7 @@ class MonthlySchedulesBloc
234242 final updatedMap = Map <String , Duration >.from (
235243 state.preparationDurationByScheduleId,
236244 )..addAll (fetchedDurations);
237- emit (
238- state.copyWith (
239- preparationDurationByScheduleId: () => updatedMap,
240- ),
241- );
245+ emit (state.copyWith (preparationDurationByScheduleId: () => updatedMap));
242246 }
243247 }
244248
@@ -272,15 +276,12 @@ class MonthlySchedulesBloc
272276 return ;
273277 }
274278
275- emit (
276- state.copyWith (
277- preparationDurationByScheduleId: () => nextDurations,
278- ),
279- );
279+ emit (state.copyWith (preparationDurationByScheduleId: () => nextDurations));
280280 }
281281
282282 void _requestVisibleDatePreparationPrefetch (
283- MonthlySchedulesState sourceState) {
283+ MonthlySchedulesState sourceState,
284+ ) {
284285 final visibleDate = sourceState.visibleDate;
285286 if (visibleDate == null ) {
286287 return ;
@@ -292,29 +293,30 @@ class MonthlySchedulesBloc
292293 if (scheduleIds.isEmpty) {
293294 return ;
294295 }
295- add (MonthlySchedulesPreparationsPrefetchRequested (
296- scheduleIds: scheduleIds));
296+ add (
297+ MonthlySchedulesPreparationsPrefetchRequested (scheduleIds: scheduleIds),
298+ );
297299 }
298300
299301 Map <DateTime , List <ScheduleEntity >> _groupSchedulesByDate (
300302 List <ScheduleEntity > schedules,
301303 ) {
302- return schedules.fold <Map <DateTime , List <ScheduleEntity >>>(
303- {} ,
304- (previousValue, element) {
305- final scheduleTime = DateTime (
306- element.scheduleTime.year,
307- element.scheduleTime.month ,
308- element.scheduleTime.day ,
309- );
310- if (previousValue. containsKey (scheduleTime)) {
311- previousValue[scheduleTime] ! . add (element);
312- } else {
313- previousValue[scheduleTime] = [element];
314- }
315- return previousValue;
316- },
317- );
304+ return schedules.fold <Map <DateTime , List <ScheduleEntity >>>({}, (
305+ previousValue ,
306+ element,
307+ ) {
308+ final scheduleTime = DateTime (
309+ element.scheduleTime.year ,
310+ element.scheduleTime.month ,
311+ element.scheduleTime.day,
312+ );
313+ if ( previousValue. containsKey (scheduleTime)) {
314+ previousValue[scheduleTime] ! . add (element);
315+ } else {
316+ previousValue[scheduleTime] = [element];
317+ }
318+ return previousValue;
319+ } );
318320 }
319321
320322 List <String > _getScheduleIdsForDate (
@@ -329,7 +331,8 @@ class MonthlySchedulesBloc
329331 }
330332
331333 Set <String > _getCachedScheduleIds (
332- Map <DateTime , List <ScheduleEntity >> schedules) {
334+ Map <DateTime , List <ScheduleEntity >> schedules,
335+ ) {
333336 final ids = < String > {};
334337 for (final scheduleList in schedules.values) {
335338 for (final schedule in scheduleList) {
0 commit comments