@@ -40,6 +40,7 @@ class AlarmTimerBloc extends Bloc<AlarmTimerEvent, AlarmTimerState> {
4040 on < AlarmTimerStepNextShifted > (_onStepNext);
4141 on < AlarmTimerStepFinalized > (_onStepFinalized);
4242 on < AlarmTimerStepsUpdated > (_onStepUpdated);
43+ on < AlarmTimerPreparationsTimeOvered > (_onPreparationsTimeOvered);
4344 }
4445
4546 void _onStepUpdated (
@@ -99,6 +100,28 @@ class AlarmTimerBloc extends Bloc<AlarmTimerEvent, AlarmTimerState> {
99100 }
100101 }
101102
103+ void _startTicker (int duration, Emitter <AlarmTimerState > emit) {
104+ _tickerSubscription? .cancel ();
105+ _tickerSubscription = Stream .periodic (const Duration (seconds: 1 ), (x) => x)
106+ .take (duration)
107+ .listen ((tick) {
108+ final newElapsed = tick + 1 ;
109+ final newRemaining = duration - newElapsed;
110+ final updatedTotalRemaining = state.totalRemainingTime - 1 ;
111+
112+ final updatedBeforeOutTime = state.beforeOutTime - 1 ;
113+ final updatedIsLate = updatedBeforeOutTime <= 0 ;
114+
115+ add (AlarmTimerStepTicked (
116+ preparationRemainingTime: newRemaining,
117+ preparationElapsedTime: newElapsed,
118+ totalRemainingTime: updatedTotalRemaining,
119+ beforeOutTime: updatedBeforeOutTime,
120+ isLate: updatedIsLate,
121+ ));
122+ });
123+ }
124+
102125 void _onStepSkipped (
103126 AlarmTimerStepSkipped event, Emitter <AlarmTimerState > emit) {
104127 final updatedStates =
@@ -125,17 +148,18 @@ class AlarmTimerBloc extends Bloc<AlarmTimerEvent, AlarmTimerState> {
125148 AlarmTimerStepNextShifted event, Emitter <AlarmTimerState > emit) {
126149 _tickerSubscription? .cancel ();
127150
128- if (state.currentStepIndex + 1 < state.preparationSteps.length) {
129- final nextStepIndex = state.currentStepIndex + 1 ;
151+ final isLastStep =
152+ state.currentStepIndex + 1 >= state.preparationSteps.length ;
130153
154+ if (! isLastStep) {
155+ final nextStepIndex = state.currentStepIndex + 1 ;
131156 final nextStepDuration =
132157 state.preparationSteps[nextStepIndex].preparationTime.inSeconds;
133158
134159 final updatedStepStates =
135160 List <PreparationStateEnum >.from (state.preparationStepStates);
136161
137162 updatedStepStates[state.currentStepIndex] = PreparationStateEnum .done;
138-
139163 updatedStepStates[nextStepIndex] = PreparationStateEnum .now;
140164
141165 emit (state.copyWith (
@@ -146,7 +170,13 @@ class AlarmTimerBloc extends Bloc<AlarmTimerEvent, AlarmTimerState> {
146170
147171 add (AlarmTimerStepStarted (nextStepDuration));
148172 } else {
149- add (const AlarmTimerStepFinalized ());
173+ final wasSkipped = state.preparationStepStates[state.currentStepIndex] ==
174+ PreparationStateEnum .done;
175+ if (wasSkipped) {
176+ add (const AlarmTimerStepFinalized ());
177+ } else {
178+ add (const AlarmTimerPreparationsTimeOvered ());
179+ }
150180 }
151181 }
152182
@@ -172,31 +202,45 @@ class AlarmTimerBloc extends Bloc<AlarmTimerEvent, AlarmTimerState> {
172202 ));
173203 }
174204
175- void _startTicker (int duration, Emitter <AlarmTimerState > emit) {
205+ @override
206+ Future <void > close () {
176207 _tickerSubscription? .cancel ();
177- _tickerSubscription = Stream .periodic (const Duration (seconds: 1 ), (x) => x)
178- .take (duration)
179- .listen ((tick) {
180- final newElapsed = tick + 1 ;
181- final newRemaining = duration - newElapsed;
182- final updatedTotalRemaining = state.totalRemainingTime - 1 ;
208+ return super .close ();
209+ }
183210
211+ void _onPreparationsTimeOvered (
212+ AlarmTimerPreparationsTimeOvered event,
213+ Emitter <AlarmTimerState > emit,
214+ ) {
215+ final updatedStates =
216+ List <PreparationStateEnum >.from (state.preparationStepStates);
217+ updatedStates[state.currentStepIndex] = PreparationStateEnum .done;
218+
219+ emit (AlarmTimerPreparationsTimeOver (
220+ preparationSteps: state.preparationSteps,
221+ currentStepIndex: state.currentStepIndex,
222+ stepElapsedTimes: state.stepElapsedTimes,
223+ preparationStepStates: updatedStates,
224+ preparationRemainingTime: 0 ,
225+ totalRemainingTime: 0 ,
226+ totalPreparationTime: state.totalPreparationTime,
227+ progress: 1.0 ,
228+ beforeOutTime: state.beforeOutTime,
229+ isLate: state.isLate,
230+ ));
231+
232+ _tickerSubscription? .cancel ();
233+ _tickerSubscription =
234+ Stream .periodic (const Duration (seconds: 1 ), (x) => x).listen ((tick) {
184235 final updatedBeforeOutTime = state.beforeOutTime - 1 ;
185236 final updatedIsLate = updatedBeforeOutTime <= 0 ;
186237
187- add (AlarmTimerStepTicked (
188- preparationRemainingTime: newRemaining,
189- preparationElapsedTime: newElapsed,
190- totalRemainingTime: updatedTotalRemaining,
191- beforeOutTime: updatedBeforeOutTime,
192- isLate: updatedIsLate,
193- ));
238+ emit (
239+ (state as AlarmTimerPreparationsTimeOver ).copyWith (
240+ beforeOutTime: updatedBeforeOutTime,
241+ isLate: updatedIsLate,
242+ ),
243+ );
194244 });
195245 }
196-
197- @override
198- Future <void > close () {
199- _tickerSubscription? .cancel ();
200- return super .close ();
201- }
202246}
0 commit comments