mirror of
https://github.com/Dr-Blank/Vaani.git
synced 2025-12-15 07:29:30 +00:00
sleeptimer
This commit is contained in:
parent
d372a6b096
commit
b98188d7fb
17 changed files with 1262 additions and 363 deletions
89
lib/features/sleep_timer/core/sleep_timer.dart
Normal file
89
lib/features/sleep_timer/core/sleep_timer.dart
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_animate/flutter_animate.dart';
|
||||
import 'package:just_audio/just_audio.dart';
|
||||
import 'package:whispering_pages/features/player/core/audiobook_player.dart';
|
||||
|
||||
/// this timer pauses the music player after a certain duration
|
||||
///
|
||||
/// watches the state of the music player and pauses it when the timer is up
|
||||
/// timer is cancelled when the music player is paused or stopped
|
||||
class SleepTimer {
|
||||
/// The duration after which the music player will be paused
|
||||
final Duration duration;
|
||||
|
||||
/// The player to be paused
|
||||
final AudiobookPlayer player;
|
||||
|
||||
/// The timer that will pause the player
|
||||
Timer? timer;
|
||||
|
||||
/// for internal use only
|
||||
/// when the timer was started
|
||||
DateTime? startedAt;
|
||||
|
||||
SleepTimer({required this.duration, required this.player}) {
|
||||
player.playbackEventStream.listen((event) {
|
||||
if (event.processingState == ProcessingState.completed ||
|
||||
event.processingState == ProcessingState.idle) {
|
||||
reset();
|
||||
}
|
||||
});
|
||||
|
||||
/// pause the player when the timer is up
|
||||
player.playerStateStream.listen((state) {
|
||||
if (state.playing && timer == null) {
|
||||
startTimer();
|
||||
} else if (!state.playing) {
|
||||
reset();
|
||||
}
|
||||
});
|
||||
debugPrint('SleepTimer created with duration: $duration');
|
||||
}
|
||||
|
||||
/// resets the timer
|
||||
void reset() {
|
||||
if (timer != null) {
|
||||
timer!.cancel();
|
||||
debugPrint(
|
||||
'SleepTimer cancelled timer, remaining time: $remainingTime, duration: $duration',
|
||||
);
|
||||
timer = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// starts the timer
|
||||
void startTimer() {
|
||||
reset();
|
||||
timer = Timer(duration, () {
|
||||
player.pause();
|
||||
reset();
|
||||
debugPrint('SleepTimer paused player after $duration');
|
||||
});
|
||||
startedAt = DateTime.now();
|
||||
debugPrint('SleepTimer started for $duration at $startedAt');
|
||||
}
|
||||
|
||||
Duration get remainingTime {
|
||||
if (timer == null) {
|
||||
return Duration.zero;
|
||||
}
|
||||
final elapsed = DateTime.now().difference(startedAt!);
|
||||
return duration - elapsed;
|
||||
}
|
||||
|
||||
/// a stream that emits the remaining time every second
|
||||
Stream<Duration> get remainingTimeStream async* {
|
||||
while (timer != null) {
|
||||
yield remainingTime;
|
||||
await Future.delayed(0.5.seconds);
|
||||
}
|
||||
}
|
||||
|
||||
/// dispose the timer
|
||||
void dispose() {
|
||||
reset();
|
||||
debugPrint('SleepTimer disposed');
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue