2025-03-25 22:01:16 +05:30
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2024-05-14 10:11:25 -04:00
|
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
2025-11-28 17:05:35 +08:00
|
|
|
import 'package:shelfsdk/audiobookshelf_api.dart' as core;
|
2024-08-23 04:21:46 -04:00
|
|
|
import 'package:vaani/features/player/providers/audiobook_player.dart';
|
2024-05-14 10:11:25 -04:00
|
|
|
|
|
|
|
|
part 'currently_playing_provider.g.dart';
|
|
|
|
|
|
|
|
|
|
@riverpod
|
2025-11-28 17:05:35 +08:00
|
|
|
class CurrentChapter extends _$CurrentChapter {
|
|
|
|
|
@override
|
|
|
|
|
core.BookChapter? build() {
|
|
|
|
|
final player = ref.watch(playerProvider);
|
|
|
|
|
player.chapterStream.distinct().listen((chapter) {
|
|
|
|
|
update(chapter);
|
|
|
|
|
});
|
|
|
|
|
return player.currentChapter;
|
2024-09-16 23:51:50 -04:00
|
|
|
}
|
2024-05-19 09:51:06 -04:00
|
|
|
|
2025-11-28 17:05:35 +08:00
|
|
|
void update(core.BookChapter? chapter) {
|
|
|
|
|
if (state != chapter) {
|
|
|
|
|
state = chapter;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-19 08:53:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@riverpod
|
2025-11-28 17:05:35 +08:00
|
|
|
List<core.BookChapter> currentChapters(Ref ref) {
|
|
|
|
|
final session = ref.watch(sessionProvider);
|
|
|
|
|
if (session == null) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
final currentChapter = ref.watch(currentChapterProvider);
|
|
|
|
|
if (currentChapter == null) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
final index = session.chapters.indexOf(currentChapter);
|
|
|
|
|
final total = session.chapters.length;
|
|
|
|
|
return session.chapters
|
|
|
|
|
.sublist(index - 3, (total - 3) <= (index + 17) ? total : index + 17);
|
2024-05-19 08:53:21 -04:00
|
|
|
}
|