-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sleep timer implt. and version bumps to 2.4.4
- Loading branch information
1 parent
b91f178
commit 17d9831
Showing
9 changed files
with
571 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import 'dart:async'; | ||
import 'dart:developer'; | ||
|
||
import 'package:Bloomee/blocs/mediaPlayer/bloomee_player_cubit.dart'; | ||
import 'package:Bloomee/main.dart'; | ||
import 'package:Bloomee/utils/ticker.dart'; | ||
import 'package:bloc/bloc.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
|
||
part 'timer_event.dart'; | ||
part 'timer_state.dart'; | ||
|
||
class TimerBloc extends Bloc<TimerEvent, TimerState> { | ||
final Ticker _ticker; | ||
static const int _duration = 0; | ||
|
||
StreamSubscription<int>? _tickerSubscription; | ||
|
||
TimerBloc({required Ticker ticker, required BloomeePlayerCubit bloomeePlayer}) | ||
: _ticker = ticker, | ||
super(const TimerInitial(_duration)) { | ||
on<TimerStarted>(_onTimerStarted); | ||
on<_TimerTicked>(_onTimerTicked); | ||
on<TimerPaused>(onTimerPaused); | ||
on<TimerResumed>(onTimerResumed); | ||
on<TimerReset>(onTimerReset); | ||
on<TimerStopped>(onTimerStopped); | ||
} | ||
|
||
void _onTimerStarted(TimerStarted event, Emitter<TimerState> emit) { | ||
emit(TimerRunInProgress(event.duration)); | ||
_tickerSubscription?.cancel(); | ||
_tickerSubscription = | ||
_ticker.tick(ticks: event.duration).listen((duration) { | ||
add(_TimerTicked(duration: duration)); | ||
}); | ||
} | ||
|
||
void onTimerPaused(TimerPaused event, Emitter<TimerState> emit) { | ||
if (state is TimerRunInProgress) { | ||
_tickerSubscription?.pause(); | ||
emit(TimerRunPause(state.duration)); | ||
} | ||
} | ||
|
||
void onTimerResumed(TimerResumed event, Emitter<TimerState> emit) { | ||
if (state is TimerRunPause) { | ||
_tickerSubscription?.resume(); | ||
emit(TimerRunInProgress(state.duration)); | ||
} | ||
} | ||
|
||
void onTimerReset(TimerReset event, Emitter<TimerState> emit) { | ||
_tickerSubscription?.cancel(); | ||
emit(const TimerInitial(_duration)); | ||
} | ||
|
||
void onTimerStopped(TimerStopped event, Emitter<TimerState> emit) { | ||
_tickerSubscription?.cancel(); | ||
emit(const TimerRunStopped()); | ||
} | ||
|
||
void _onTimerTicked(_TimerTicked event, Emitter<TimerState> emit) { | ||
// emit(event.duration > 0 | ||
// ? TimerRunInProgress(event.duration) | ||
// : const TimerRunComplete()); | ||
if (event.duration > 0) { | ||
emit(TimerRunInProgress(event.duration)); | ||
} else { | ||
emit(const TimerRunComplete()); | ||
try { | ||
bloomeePlayerCubit.bloomeePlayer.pause(); | ||
} catch (e) { | ||
log(e.toString(), name: "TimerBloc"); | ||
} | ||
} | ||
} | ||
|
||
@override | ||
Future<void> close() { | ||
_tickerSubscription?.cancel(); | ||
return super.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
part of 'timer_bloc.dart'; | ||
|
||
sealed class TimerEvent { | ||
const TimerEvent(); | ||
} | ||
|
||
final class TimerStarted extends TimerEvent { | ||
const TimerStarted({required this.duration}); | ||
final int duration; | ||
} | ||
|
||
final class TimerPaused extends TimerEvent { | ||
const TimerPaused(); | ||
} | ||
|
||
final class TimerResumed extends TimerEvent { | ||
const TimerResumed(); | ||
} | ||
|
||
class TimerReset extends TimerEvent { | ||
const TimerReset(); | ||
} | ||
|
||
class TimerStopped extends TimerEvent { | ||
const TimerStopped(); | ||
} | ||
|
||
class _TimerTicked extends TimerEvent { | ||
const _TimerTicked({required this.duration}); | ||
final int duration; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
part of 'timer_bloc.dart'; | ||
|
||
sealed class TimerState extends Equatable { | ||
const TimerState(this.duration); | ||
final int duration; | ||
|
||
@override | ||
List<Object> get props => [duration]; | ||
} | ||
|
||
final class TimerInitial extends TimerState { | ||
const TimerInitial(super.duration); | ||
|
||
@override | ||
String toString() => 'TimerInitial { duration: $duration }'; | ||
} | ||
|
||
final class TimerRunPause extends TimerState { | ||
const TimerRunPause(super.duration); | ||
|
||
@override | ||
String toString() => 'TimerRunPause { duration: $duration }'; | ||
} | ||
|
||
final class TimerRunStopped extends TimerState { | ||
const TimerRunStopped() : super(0); | ||
} | ||
|
||
final class TimerRunInProgress extends TimerState { | ||
const TimerRunInProgress(super.duration); | ||
|
||
@override | ||
String toString() => 'TimerRunInProgress { duration: $duration }'; | ||
} | ||
|
||
final class TimerRunComplete extends TimerState { | ||
const TimerRunComplete() : super(0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.