sleeptimer

This commit is contained in:
Dr-Blank 2024-06-06 15:35:30 -04:00
parent d372a6b096
commit b98188d7fb
No known key found for this signature in database
GPG key ID: 7452CC63F210A266
17 changed files with 1262 additions and 363 deletions

View 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');
}
}