Vaani/lib/features/skip_start_end/skip_start_end.dart

59 lines
1.6 KiB
Dart
Raw Normal View History

import 'dart:async';
2025-11-22 15:54:29 +08:00
import 'package:vaani/features/player/core/audiobook_player_session.dart';
2025-11-13 17:53:23 +08:00
import 'package:vaani/shared/extensions/chapter.dart';
import 'package:vaani/shared/utils/throttler.dart';
class SkipStartEnd {
final Duration start;
final Duration end;
2025-11-22 15:54:29 +08:00
final AbsAudioHandler player;
final List<StreamSubscription> _subscriptions = [];
2025-11-22 15:54:29 +08:00
final throttlerStart = Throttler(delay: Duration(seconds: 3));
final throttlerEnd = Throttler(delay: Duration(seconds: 3));
2025-11-13 17:53:23 +08:00
SkipStartEnd({
required this.start,
required this.end,
required this.player,
}) {
2025-11-22 15:54:29 +08:00
if (start > Duration.zero) {
_subscriptions.add(
2025-11-22 15:54:29 +08:00
player.chapterStream.listen((chapter) {
if (chapter != null &&
player.positionInChapter < Duration(seconds: 1)) {
Future.microtask(
() => throttlerStart
.call(() => player.seekInBook(chapter.start + start)),
);
}
}),
);
}
2025-11-22 15:54:29 +08:00
if (end > Duration.zero) {
_subscriptions.add(
player.positionStreamInChapter.listen((positionChapter) {
if (end >
(player.currentChapter?.duration ?? Duration.zero) -
positionChapter) {
Future.microtask(
() => throttlerEnd.call(() => player.skipToNext()),
);
}
}),
);
}
}
2025-11-13 17:53:23 +08:00
/// dispose the timer
void dispose() {
2025-11-13 17:53:23 +08:00
for (var sub in _subscriptions) {
sub.cancel();
}
2025-11-22 15:54:29 +08:00
throttlerStart.dispose();
throttlerEnd.dispose();
2025-11-13 17:53:23 +08:00
// _playbackController.close();
}
}