2024-04-30 14:32:01 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2024-05-03 10:43:45 +00:00
|
|
|
import 'package:lightmeter/interactors/timer_interactor.dart';
|
2024-04-30 19:50:09 +00:00
|
|
|
import 'package:lightmeter/screens/timer/event_timer.dart';
|
|
|
|
import 'package:lightmeter/screens/timer/state_timer.dart';
|
2024-04-30 14:32:01 +00:00
|
|
|
|
2024-04-30 19:50:09 +00:00
|
|
|
class TimerBloc extends Bloc<TimerEvent, TimerState> {
|
2024-05-03 10:43:45 +00:00
|
|
|
final TimerInteractor _timerInteractor;
|
2024-05-03 08:50:35 +00:00
|
|
|
final Duration duration;
|
2024-04-30 19:50:09 +00:00
|
|
|
|
2024-05-03 10:43:45 +00:00
|
|
|
TimerBloc(this._timerInteractor, this.duration) : super(const TimerStoppedState()) {
|
2024-04-30 19:50:09 +00:00
|
|
|
on<StartTimerEvent>(_onStartTimer);
|
2024-05-03 10:43:45 +00:00
|
|
|
on<TimerEndedEvent>(_onTimerEnded);
|
2024-04-30 19:50:09 +00:00
|
|
|
on<StopTimerEvent>(_onStopTimer);
|
|
|
|
on<ResetTimerEvent>(_onResetTimer);
|
2024-05-04 17:58:19 +00:00
|
|
|
|
|
|
|
if (_timerInteractor.isAutostartTimerEnabled) add(const StartTimerEvent());
|
2024-04-30 14:32:01 +00:00
|
|
|
}
|
|
|
|
|
2024-04-30 19:50:09 +00:00
|
|
|
Future<void> _onStartTimer(StartTimerEvent _, Emitter emit) async {
|
2024-05-03 10:43:45 +00:00
|
|
|
_timerInteractor.quickVibration();
|
2024-05-03 09:16:29 +00:00
|
|
|
emit(const TimerResumedState());
|
2024-04-30 14:32:01 +00:00
|
|
|
}
|
|
|
|
|
2024-05-03 10:43:45 +00:00
|
|
|
Future<void> _onTimerEnded(TimerEndedEvent event, Emitter emit) async {
|
|
|
|
if (state is! TimerResetState) {
|
|
|
|
_timerInteractor.responseVibration();
|
|
|
|
emit(const TimerStoppedState());
|
|
|
|
}
|
2024-04-30 14:32:01 +00:00
|
|
|
}
|
|
|
|
|
2024-04-30 19:50:09 +00:00
|
|
|
Future<void> _onStopTimer(StopTimerEvent _, Emitter emit) async {
|
2024-05-03 10:43:45 +00:00
|
|
|
_timerInteractor.quickVibration();
|
2024-05-03 09:16:29 +00:00
|
|
|
emit(const TimerStoppedState());
|
2024-04-30 14:32:01 +00:00
|
|
|
}
|
|
|
|
|
2024-04-30 19:50:09 +00:00
|
|
|
Future<void> _onResetTimer(ResetTimerEvent _, Emitter emit) async {
|
2024-05-03 09:16:29 +00:00
|
|
|
emit(const TimerResetState());
|
2024-04-30 14:32:01 +00:00
|
|
|
}
|
|
|
|
}
|