Skip to content

Commit f675469

Browse files
authored
Merge pull request #258 from DevKor-github/257-alarm-screen-준비-시간-만료-시
Alarm Screen 시간 만료시 로직
2 parents 1f3ad0c + 1796b47 commit f675469

File tree

4 files changed

+238
-80
lines changed

4 files changed

+238
-80
lines changed

lib/presentation/alarm/bloc/alarm_timer/alarm_timer_bloc.dart

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

lib/presentation/alarm/bloc/alarm_timer/alarm_timer_event.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,7 @@ class AlarmTimerStepsUpdated extends AlarmTimerEvent {
6060
@override
6161
List<Object?> get props => [preparationSteps];
6262
}
63+
64+
class AlarmTimerPreparationsTimeOvered extends AlarmTimerEvent {
65+
const AlarmTimerPreparationsTimeOvered();
66+
}

lib/presentation/alarm/bloc/alarm_timer/alarm_timer_state.dart

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ class AlarmTimerPreparationCompletion extends AlarmTimerState {
208208
required super.isLate,
209209
});
210210

211+
// 준비 완료 상태
211212
@override
212213
AlarmTimerPreparationCompletion copyWith({
213214
List<PreparationStepEntity>? preparationSteps,
@@ -237,3 +238,48 @@ class AlarmTimerPreparationCompletion extends AlarmTimerState {
237238
);
238239
}
239240
}
241+
242+
// 준비 시간 만료 상태
243+
class AlarmTimerPreparationsTimeOver extends AlarmTimerState {
244+
const AlarmTimerPreparationsTimeOver({
245+
required super.preparationSteps,
246+
required super.currentStepIndex,
247+
required super.stepElapsedTimes,
248+
required super.preparationStepStates,
249+
required super.preparationRemainingTime,
250+
required super.totalRemainingTime,
251+
required super.totalPreparationTime,
252+
required super.progress,
253+
required super.beforeOutTime,
254+
required super.isLate,
255+
});
256+
257+
@override
258+
AlarmTimerPreparationsTimeOver copyWith({
259+
List<PreparationStepEntity>? preparationSteps,
260+
int? currentStepIndex,
261+
List<int>? stepElapsedTimes,
262+
List<PreparationStateEnum>? preparationStepStates,
263+
int? preparationRemainingTime,
264+
int? totalRemainingTime,
265+
int? totalPreparationTime,
266+
double? progress,
267+
int? beforeOutTime,
268+
bool? isLate,
269+
}) {
270+
return AlarmTimerPreparationsTimeOver(
271+
preparationSteps: preparationSteps ?? this.preparationSteps,
272+
currentStepIndex: currentStepIndex ?? this.currentStepIndex,
273+
stepElapsedTimes: stepElapsedTimes ?? this.stepElapsedTimes,
274+
preparationStepStates:
275+
preparationStepStates ?? this.preparationStepStates,
276+
preparationRemainingTime:
277+
preparationRemainingTime ?? this.preparationRemainingTime,
278+
totalRemainingTime: totalRemainingTime ?? this.totalRemainingTime,
279+
totalPreparationTime: totalPreparationTime ?? this.totalPreparationTime,
280+
progress: progress ?? this.progress,
281+
beforeOutTime: beforeOutTime ?? this.beforeOutTime,
282+
isLate: isLate ?? this.isLate,
283+
);
284+
}
285+
}

0 commit comments

Comments
 (0)