mirror of
https://github.com/Dr-Blank/Vaani.git
synced 2026-02-17 14:59:35 +00:00
更改播放逻辑
This commit is contained in:
parent
290b68336f
commit
420438c0df
29 changed files with 810 additions and 514 deletions
|
|
@ -1,192 +1,111 @@
|
|||
import 'package:audio_service/audio_service.dart';
|
||||
import 'package:audio_session/audio_session.dart';
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:media_kit/media_kit.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:shelfsdk/audiobookshelf_api.dart' as api;
|
||||
import 'package:vaani/api/api_provider.dart';
|
||||
import 'package:vaani/api/library_item_provider.dart';
|
||||
import 'package:vaani/features/downloads/providers/download_manager.dart';
|
||||
import 'package:vaani/features/per_book_settings/providers/book_settings_provider.dart';
|
||||
import 'package:vaani/features/player/core/abs_audio_handler.dart' as core;
|
||||
import 'package:vaani/features/player/core/abs_audio_player.dart' as core;
|
||||
import 'package:vaani/features/player/core/audiobook_player.dart';
|
||||
import 'package:vaani/features/settings/app_settings_provider.dart';
|
||||
import 'package:vaani/globals.dart';
|
||||
import 'package:vaani/shared/extensions/chapter.dart';
|
||||
import 'package:vaani/shared/audio_player.dart' as core;
|
||||
import 'package:vaani/shared/audio_player_mpv.dart';
|
||||
|
||||
part 'abs_provider.g.dart';
|
||||
|
||||
final _logger = Logger('AbsPlayerProvider');
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
Future<core.AbsAudioHandler> absAudioHandlerInit(Ref ref) async {
|
||||
// for playing audio on windows, linux
|
||||
MediaKit.ensureInitialized();
|
||||
// for configuring how this app will interact with other audio apps
|
||||
final session = await AudioSession.instance;
|
||||
await session.configure(const AudioSessionConfiguration.speech());
|
||||
|
||||
final audioService = await AudioService.init(
|
||||
builder: () => core.AbsAudioHandler(ref),
|
||||
config: const AudioServiceConfig(
|
||||
androidNotificationChannelId: 'dr.blank.vaani.channel.audio',
|
||||
androidNotificationChannelName: 'ABSPlayback',
|
||||
androidNotificationChannelDescription:
|
||||
'Needed to control audio from lock screen',
|
||||
androidNotificationOngoing: false,
|
||||
androidStopForegroundOnPause: false,
|
||||
androidNotificationIcon: 'drawable/ic_stat_logo',
|
||||
preloadArtwork: true,
|
||||
// fastForwardInterval: Duration(seconds: 20),
|
||||
// rewindInterval: Duration(seconds: 20),
|
||||
),
|
||||
);
|
||||
|
||||
_logger.finer('created simple player');
|
||||
return audioService;
|
||||
}
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
class AbsPlayer extends _$AbsPlayer {
|
||||
class AbsAudioPlayer extends _$AbsAudioPlayer {
|
||||
@override
|
||||
core.AbsAudioHandler build() {
|
||||
return ref.read(absAudioHandlerInitProvider).valueOrNull!;
|
||||
}
|
||||
}
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
class AbsState extends _$AbsState {
|
||||
@override
|
||||
core.AbsPlayerState build() {
|
||||
return core.AbsPlayerState();
|
||||
core.AbsAudioPlayer build() {
|
||||
final player = AbsMpvAudioPlayer();
|
||||
return player;
|
||||
}
|
||||
|
||||
// 加载书籍
|
||||
Future<void> load(api.BookExpanded book, Duration? currentTime) async {
|
||||
final player = ref.read(absPlayerProvider);
|
||||
if (state.book == book || state.book?.libraryItemId == book.libraryItemId) {
|
||||
appLogger.info('Book was already set');
|
||||
player.playOrPause();
|
||||
Future<void> load(
|
||||
api.BookExpanded book, {
|
||||
Duration? initialPosition,
|
||||
}) async {
|
||||
if (state.book == book) {
|
||||
state.playOrPause();
|
||||
return;
|
||||
}
|
||||
|
||||
final appSettings = ref.read(appSettingsProvider);
|
||||
final api = ref.read(authenticatedApiProvider);
|
||||
final downloadManager = ref.read(simpleDownloadManagerProvider);
|
||||
final libItem =
|
||||
await ref.read(libraryItemProvider(book.libraryItemId).future);
|
||||
final downloadedUris = await downloadManager.getDownloadedFilesUri(libItem);
|
||||
|
||||
var bookPlayerSettings =
|
||||
ref.read(bookSettingsProvider(book.libraryItemId)).playerSettings;
|
||||
var appPlayerSettings = ref.read(appSettingsProvider).playerSettings;
|
||||
|
||||
var configurePlayerForEveryBook =
|
||||
appPlayerSettings.configurePlayerForEveryBook;
|
||||
final trackToPlay = book.findTrackAtTime(currentTime ?? Duration.zero);
|
||||
final chapterToPlay = book.findChapterAtTime(currentTime ?? Duration.zero);
|
||||
final initialIndex = book.tracks.indexOf(trackToPlay);
|
||||
final initialPositionInTrack =
|
||||
currentTime != null ? currentTime - trackToPlay.startOffset : null;
|
||||
final album = appSettings.notificationSettings.primaryTitle
|
||||
.formatNotificationTitle(book);
|
||||
final artlist = appSettings.notificationSettings.secondaryTitle
|
||||
.formatNotificationTitle(book);
|
||||
|
||||
final id = _getUri(trackToPlay, downloadedUris,
|
||||
baseUrl: api.baseUrl, token: api.token!)
|
||||
.toString();
|
||||
final item = MediaItem(
|
||||
id: id,
|
||||
title: chapterToPlay.title,
|
||||
album: album,
|
||||
artist: artlist,
|
||||
duration: chapterToPlay.duration,
|
||||
artUri: Uri.parse(
|
||||
'${api.baseUrl}/api/items/${book.libraryItemId}/cover?token=${api.token!}',
|
||||
),
|
||||
await state.load(
|
||||
book,
|
||||
baseUrl: api.baseUrl,
|
||||
token: api.token!,
|
||||
initialPosition: initialPosition,
|
||||
);
|
||||
|
||||
state = state.copyWith(
|
||||
book: book,
|
||||
currentChapter: chapterToPlay,
|
||||
currentIndex: initialIndex,
|
||||
);
|
||||
player.playMediaItem(item);
|
||||
await Future.wait([
|
||||
player.setMediaItem(item),
|
||||
// player.setVolume(
|
||||
// configurePlayerForEveryBook
|
||||
// ? bookPlayerSettings.preferredDefaultVolume ??
|
||||
// appPlayerSettings.preferredDefaultVolume
|
||||
// : appPlayerSettings.preferredDefaultVolume,
|
||||
// ),
|
||||
player.setSpeed(
|
||||
configurePlayerForEveryBook
|
||||
? bookPlayerSettings.preferredDefaultSpeed ??
|
||||
appPlayerSettings.preferredDefaultSpeed
|
||||
: appPlayerSettings.preferredDefaultSpeed,
|
||||
),
|
||||
player.play(),
|
||||
]);
|
||||
|
||||
// player.setSourceAudiobook(
|
||||
// book,
|
||||
// baseUrl: api.baseUrl,
|
||||
// token: api.token!,
|
||||
// initialPosition: currentTime,
|
||||
// downloadedUris: downloadedUris,
|
||||
// volume: configurePlayerForEveryBook
|
||||
// ? bookPlayerSettings.preferredDefaultVolume ??
|
||||
// appPlayerSettings.preferredDefaultVolume
|
||||
// : appPlayerSettings.preferredDefaultVolume,
|
||||
// speed: configurePlayerForEveryBook
|
||||
// ? bookPlayerSettings.preferredDefaultSpeed ??
|
||||
// appPlayerSettings.preferredDefaultSpeed
|
||||
// : appPlayerSettings.preferredDefaultSpeed,
|
||||
// );
|
||||
await state.play();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> next() async {}
|
||||
|
||||
Future<void> previous() async {}
|
||||
void updataPlaying(bool playing) {
|
||||
state = state.copyWith(playing: playing);
|
||||
}
|
||||
|
||||
Stream<Duration> get positionStreamInChapter {
|
||||
final player = ref.read(absPlayerProvider);
|
||||
|
||||
return player.stream.position.distinct().map((position) {
|
||||
return position +
|
||||
(state.book?.tracks[state.currentIndex].startOffset ??
|
||||
Duration.zero) -
|
||||
(state.currentChapter?.start ?? Duration.zero);
|
||||
@riverpod
|
||||
class PlayerState extends _$PlayerState {
|
||||
@override
|
||||
core.PlayerState build() {
|
||||
final player = ref.read(absAudioPlayerProvider);
|
||||
player.playerStateStream.listen((playerState) {
|
||||
if (playerState != state) {
|
||||
state = playerState;
|
||||
}
|
||||
});
|
||||
return player.playerState;
|
||||
}
|
||||
}
|
||||
|
||||
Uri _getUri(
|
||||
api.AudioTrack track,
|
||||
List<Uri>? downloadedUris, {
|
||||
required Uri baseUrl,
|
||||
required String token,
|
||||
}) {
|
||||
// check if the track is in the downloadedUris
|
||||
final uri = downloadedUris?.firstWhereOrNull(
|
||||
(element) {
|
||||
return element.pathSegments.last == track.metadata?.filename;
|
||||
},
|
||||
);
|
||||
@riverpod
|
||||
class CurrentBook extends _$CurrentBook {
|
||||
@override
|
||||
api.BookExpanded? build() {
|
||||
final player = ref.read(absAudioPlayerProvider);
|
||||
player.bookStream.listen((book) {
|
||||
if (book != state) {
|
||||
state = book;
|
||||
}
|
||||
});
|
||||
return player.book;
|
||||
}
|
||||
}
|
||||
|
||||
return uri ??
|
||||
Uri.parse('${baseUrl.toString()}${track.contentUrl}?token=$token');
|
||||
@riverpod
|
||||
bool isPlayerActive(Ref ref) {
|
||||
final player = ref.read(absAudioPlayerProvider);
|
||||
player.bookStream.listen((book) {
|
||||
ref.invalidateSelf();
|
||||
});
|
||||
return player.book != null;
|
||||
}
|
||||
|
||||
@riverpod
|
||||
class CurrentChapter extends _$CurrentChapter {
|
||||
@override
|
||||
api.BookChapter? build() {
|
||||
final player = ref.read(absAudioPlayerProvider);
|
||||
player.chapterStream.listen((chapter) {
|
||||
if (chapter != state) {
|
||||
state = chapter;
|
||||
}
|
||||
});
|
||||
return player.currentChapter;
|
||||
}
|
||||
}
|
||||
|
||||
@riverpod
|
||||
Stream<Duration> positionChapter(Ref ref) {
|
||||
return ref.watch(absStateProvider.notifier).positionStreamInChapter;
|
||||
return ref.read(absAudioPlayerProvider).positionInChapterStream;
|
||||
}
|
||||
|
||||
@riverpod
|
||||
List<api.BookChapter> currentChapters(Ref ref) {
|
||||
final book = ref.watch(currentBookProvider);
|
||||
if (book == null) {
|
||||
return [];
|
||||
}
|
||||
final currentChapter = ref.watch(currentChapterProvider);
|
||||
if (currentChapter == null) {
|
||||
return [];
|
||||
}
|
||||
final index = book.chapters.indexOf(currentChapter);
|
||||
final total = book.chapters.length;
|
||||
final start = index - 3 >= 0 ? index - 3 : 0;
|
||||
final end = start + 20 <= total ? start + 20 : total;
|
||||
return book.chapters.sublist(start, end);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,26 +6,24 @@ part of 'abs_provider.dart';
|
|||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$absAudioHandlerInitHash() =>
|
||||
r'bb46f715e9d51bb6269d0d77e314665601a6bdb0';
|
||||
String _$isPlayerActiveHash() => r'52fc689deeba4d21a33a73290d297f128324ae99';
|
||||
|
||||
/// See also [absAudioHandlerInit].
|
||||
@ProviderFor(absAudioHandlerInit)
|
||||
final absAudioHandlerInitProvider =
|
||||
FutureProvider<core.AbsAudioHandler>.internal(
|
||||
absAudioHandlerInit,
|
||||
name: r'absAudioHandlerInitProvider',
|
||||
/// See also [isPlayerActive].
|
||||
@ProviderFor(isPlayerActive)
|
||||
final isPlayerActiveProvider = AutoDisposeProvider<bool>.internal(
|
||||
isPlayerActive,
|
||||
name: r'isPlayerActiveProvider',
|
||||
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
|
||||
? null
|
||||
: _$absAudioHandlerInitHash,
|
||||
: _$isPlayerActiveHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@Deprecated('Will be removed in 3.0. Use Ref instead')
|
||||
// ignore: unused_element
|
||||
typedef AbsAudioHandlerInitRef = FutureProviderRef<core.AbsAudioHandler>;
|
||||
String _$positionChapterHash() => r'b1d19345bceb2e54399e15fbb16a534f4be5ba43';
|
||||
typedef IsPlayerActiveRef = AutoDisposeProviderRef<bool>;
|
||||
String _$positionChapterHash() => r'68f7ca4df2ac6f6f78a645d98e2dbfaf2ffe46bf';
|
||||
|
||||
/// See also [positionChapter].
|
||||
@ProviderFor(positionChapter)
|
||||
|
|
@ -42,35 +40,85 @@ final positionChapterProvider = AutoDisposeStreamProvider<Duration>.internal(
|
|||
@Deprecated('Will be removed in 3.0. Use Ref instead')
|
||||
// ignore: unused_element
|
||||
typedef PositionChapterRef = AutoDisposeStreamProviderRef<Duration>;
|
||||
String _$absPlayerHash() => r'c313a2456bb221b83f3cd2142ae63d6463ef304b';
|
||||
String _$currentChaptersHash() => r'2d694aaa17f7eed8f97859d83e5b61f22966c35a';
|
||||
|
||||
/// See also [AbsPlayer].
|
||||
@ProviderFor(AbsPlayer)
|
||||
final absPlayerProvider =
|
||||
NotifierProvider<AbsPlayer, core.AbsAudioHandler>.internal(
|
||||
AbsPlayer.new,
|
||||
name: r'absPlayerProvider',
|
||||
debugGetCreateSourceHash:
|
||||
const bool.fromEnvironment('dart.vm.product') ? null : _$absPlayerHash,
|
||||
/// See also [currentChapters].
|
||||
@ProviderFor(currentChapters)
|
||||
final currentChaptersProvider =
|
||||
AutoDisposeProvider<List<api.BookChapter>>.internal(
|
||||
currentChapters,
|
||||
name: r'currentChaptersProvider',
|
||||
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
|
||||
? null
|
||||
: _$currentChaptersHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
typedef _$AbsPlayer = Notifier<core.AbsAudioHandler>;
|
||||
String _$absStateHash() => r'6b4ca07c7998304a1522a07b23955c3e54a441e3';
|
||||
@Deprecated('Will be removed in 3.0. Use Ref instead')
|
||||
// ignore: unused_element
|
||||
typedef CurrentChaptersRef = AutoDisposeProviderRef<List<api.BookChapter>>;
|
||||
String _$absAudioPlayerHash() => r'04636b3275f16747eeeb008c8b4dda4e8a1f8ed2';
|
||||
|
||||
/// See also [AbsState].
|
||||
@ProviderFor(AbsState)
|
||||
final absStateProvider =
|
||||
NotifierProvider<AbsState, core.AbsPlayerState>.internal(
|
||||
AbsState.new,
|
||||
name: r'absStateProvider',
|
||||
debugGetCreateSourceHash:
|
||||
const bool.fromEnvironment('dart.vm.product') ? null : _$absStateHash,
|
||||
/// See also [AbsAudioPlayer].
|
||||
@ProviderFor(AbsAudioPlayer)
|
||||
final absAudioPlayerProvider =
|
||||
NotifierProvider<AbsAudioPlayer, core.AbsAudioPlayer>.internal(
|
||||
AbsAudioPlayer.new,
|
||||
name: r'absAudioPlayerProvider',
|
||||
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
|
||||
? null
|
||||
: _$absAudioPlayerHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
typedef _$AbsState = Notifier<core.AbsPlayerState>;
|
||||
typedef _$AbsAudioPlayer = Notifier<core.AbsAudioPlayer>;
|
||||
String _$playerStateHash() => r'ed07c487fdad5fd0e21dfd32a274eecc470e83a4';
|
||||
|
||||
/// See also [PlayerState].
|
||||
@ProviderFor(PlayerState)
|
||||
final playerStateProvider =
|
||||
AutoDisposeNotifierProvider<PlayerState, core.PlayerState>.internal(
|
||||
PlayerState.new,
|
||||
name: r'playerStateProvider',
|
||||
debugGetCreateSourceHash:
|
||||
const bool.fromEnvironment('dart.vm.product') ? null : _$playerStateHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
typedef _$PlayerState = AutoDisposeNotifier<core.PlayerState>;
|
||||
String _$currentBookHash() => r'40c24ad45aab37afc32e8e8383d6abbe19b714bc';
|
||||
|
||||
/// See also [CurrentBook].
|
||||
@ProviderFor(CurrentBook)
|
||||
final currentBookProvider =
|
||||
AutoDisposeNotifierProvider<CurrentBook, api.BookExpanded?>.internal(
|
||||
CurrentBook.new,
|
||||
name: r'currentBookProvider',
|
||||
debugGetCreateSourceHash:
|
||||
const bool.fromEnvironment('dart.vm.product') ? null : _$currentBookHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
typedef _$CurrentBook = AutoDisposeNotifier<api.BookExpanded?>;
|
||||
String _$currentChapterHash() => r'89868a72b106e0916883ee92bf3d18650288c586';
|
||||
|
||||
/// See also [CurrentChapter].
|
||||
@ProviderFor(CurrentChapter)
|
||||
final currentChapterProvider =
|
||||
AutoDisposeNotifierProvider<CurrentChapter, api.BookChapter?>.internal(
|
||||
CurrentChapter.new,
|
||||
name: r'currentChapterProvider',
|
||||
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
|
||||
? null
|
||||
: _$currentChapterHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
typedef _$CurrentChapter = AutoDisposeNotifier<api.BookChapter?>;
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member, deprecated_member_use_from_same_package
|
||||
|
|
|
|||
|
|
@ -9,89 +9,72 @@ import 'package:vaani/features/player/providers/audiobook_player.dart';
|
|||
import 'package:vaani/features/settings/app_settings_provider.dart';
|
||||
import 'package:vaani/globals.dart';
|
||||
|
||||
part 'currently_playing_provider.g.dart';
|
||||
// part 'currently_playing_provider.g.dart';
|
||||
|
||||
@riverpod
|
||||
class CurrentBook extends _$CurrentBook {
|
||||
@override
|
||||
core.BookExpanded? build() {
|
||||
return null;
|
||||
}
|
||||
// @riverpod
|
||||
// class CurrentBook extends _$CurrentBook {
|
||||
// @override
|
||||
// core.BookExpanded? build() {
|
||||
// return null;
|
||||
// }
|
||||
|
||||
Future<void> update(core.BookExpanded book, Duration? currentTime) async {
|
||||
final audioService = ref.read(playerProvider);
|
||||
if (state == book) {
|
||||
appLogger.info('Book was already set');
|
||||
if (audioService.player.playing) {
|
||||
appLogger.info('Pausing the book');
|
||||
await audioService.pause();
|
||||
return;
|
||||
} else {
|
||||
await audioService.play();
|
||||
}
|
||||
}
|
||||
state = book;
|
||||
final api = ref.read(authenticatedApiProvider);
|
||||
final downloadManager = ref.read(simpleDownloadManagerProvider);
|
||||
final libItem =
|
||||
await ref.read(libraryItemProvider(state!.libraryItemId).future);
|
||||
final downloadedUris = await downloadManager.getDownloadedFilesUri(libItem);
|
||||
// Future<void> update(core.BookExpanded book, Duration? currentTime) async {
|
||||
// final audioService = ref.read(playerProvider);
|
||||
// if (state == book) {
|
||||
// appLogger.info('Book was already set');
|
||||
// if (audioService.player.playing) {
|
||||
// appLogger.info('Pausing the book');
|
||||
// await audioService.pause();
|
||||
// return;
|
||||
// } else {
|
||||
// await audioService.play();
|
||||
// }
|
||||
// }
|
||||
// state = book;
|
||||
// final api = ref.read(authenticatedApiProvider);
|
||||
// final downloadManager = ref.read(simpleDownloadManagerProvider);
|
||||
// final libItem =
|
||||
// await ref.read(libraryItemProvider(state!.libraryItemId).future);
|
||||
// final downloadedUris = await downloadManager.getDownloadedFilesUri(libItem);
|
||||
|
||||
var bookPlayerSettings =
|
||||
ref.read(bookSettingsProvider(state!.libraryItemId)).playerSettings;
|
||||
var appPlayerSettings = ref.read(appSettingsProvider).playerSettings;
|
||||
// var bookPlayerSettings =
|
||||
// ref.read(bookSettingsProvider(state!.libraryItemId)).playerSettings;
|
||||
// var appPlayerSettings = ref.read(appSettingsProvider).playerSettings;
|
||||
|
||||
var configurePlayerForEveryBook =
|
||||
appPlayerSettings.configurePlayerForEveryBook;
|
||||
audioService.setSourceAudiobook(
|
||||
state!,
|
||||
baseUrl: api.baseUrl,
|
||||
token: api.token!,
|
||||
initialPosition: currentTime,
|
||||
downloadedUris: downloadedUris,
|
||||
volume: configurePlayerForEveryBook
|
||||
? bookPlayerSettings.preferredDefaultVolume ??
|
||||
appPlayerSettings.preferredDefaultVolume
|
||||
: appPlayerSettings.preferredDefaultVolume,
|
||||
speed: configurePlayerForEveryBook
|
||||
? bookPlayerSettings.preferredDefaultSpeed ??
|
||||
appPlayerSettings.preferredDefaultSpeed
|
||||
: appPlayerSettings.preferredDefaultSpeed,
|
||||
);
|
||||
}
|
||||
}
|
||||
// var configurePlayerForEveryBook =
|
||||
// appPlayerSettings.configurePlayerForEveryBook;
|
||||
// audioService.setSourceAudiobook(
|
||||
// state!,
|
||||
// baseUrl: api.baseUrl,
|
||||
// token: api.token!,
|
||||
// initialPosition: currentTime,
|
||||
// downloadedUris: downloadedUris,
|
||||
// volume: configurePlayerForEveryBook
|
||||
// ? bookPlayerSettings.preferredDefaultVolume ??
|
||||
// appPlayerSettings.preferredDefaultVolume
|
||||
// : appPlayerSettings.preferredDefaultVolume,
|
||||
// speed: configurePlayerForEveryBook
|
||||
// ? bookPlayerSettings.preferredDefaultSpeed ??
|
||||
// appPlayerSettings.preferredDefaultSpeed
|
||||
// : appPlayerSettings.preferredDefaultSpeed,
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
|
||||
@riverpod
|
||||
class CurrentChapter extends _$CurrentChapter {
|
||||
@override
|
||||
core.BookChapter? build() {
|
||||
final player = ref.watch(playerProvider);
|
||||
player.chapterStream.distinct().listen((chapter) {
|
||||
update(chapter);
|
||||
});
|
||||
return player.currentChapter;
|
||||
}
|
||||
// @riverpod
|
||||
// class CurrentChapter extends _$CurrentChapter {
|
||||
// @override
|
||||
// core.BookChapter? build() {
|
||||
// final player = ref.watch(playerProvider);
|
||||
// player.chapterStream.distinct().listen((chapter) {
|
||||
// update(chapter);
|
||||
// });
|
||||
// return player.currentChapter;
|
||||
// }
|
||||
|
||||
void update(core.BookChapter? chapter) {
|
||||
if (state != chapter) {
|
||||
state = chapter;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@riverpod
|
||||
List<core.BookChapter> currentChapters(Ref ref) {
|
||||
final book = ref.watch(currentBookProvider);
|
||||
if (book == null) {
|
||||
return [];
|
||||
}
|
||||
final currentChapter = ref.watch(currentChapterProvider);
|
||||
if (currentChapter == null) {
|
||||
return [];
|
||||
}
|
||||
final index = book.chapters.indexOf(currentChapter);
|
||||
final total = book.chapters.length;
|
||||
final start = index - 3 >= 0 ? index - 3 : 0;
|
||||
final end = start + 20 <= total ? start + 20 : total;
|
||||
return book.chapters.sublist(start, end);
|
||||
}
|
||||
// void update(core.BookChapter? chapter) {
|
||||
// if (state != chapter) {
|
||||
// state = chapter;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
|
|
|||
|
|
@ -1,59 +0,0 @@
|
|||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'currently_playing_provider.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$currentChaptersHash() => r'6574b3f4ee0af8006f233aaf76cc507d188c6305';
|
||||
|
||||
/// See also [currentChapters].
|
||||
@ProviderFor(currentChapters)
|
||||
final currentChaptersProvider =
|
||||
AutoDisposeProvider<List<core.BookChapter>>.internal(
|
||||
currentChapters,
|
||||
name: r'currentChaptersProvider',
|
||||
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
|
||||
? null
|
||||
: _$currentChaptersHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@Deprecated('Will be removed in 3.0. Use Ref instead')
|
||||
// ignore: unused_element
|
||||
typedef CurrentChaptersRef = AutoDisposeProviderRef<List<core.BookChapter>>;
|
||||
String _$currentBookHash() => r'5143d08375c2c58918e82f8d368998bb38d7b790';
|
||||
|
||||
/// See also [CurrentBook].
|
||||
@ProviderFor(CurrentBook)
|
||||
final currentBookProvider =
|
||||
AutoDisposeNotifierProvider<CurrentBook, core.BookExpanded?>.internal(
|
||||
CurrentBook.new,
|
||||
name: r'currentBookProvider',
|
||||
debugGetCreateSourceHash:
|
||||
const bool.fromEnvironment('dart.vm.product') ? null : _$currentBookHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
typedef _$CurrentBook = AutoDisposeNotifier<core.BookExpanded?>;
|
||||
String _$currentChapterHash() => r'f5f6d9e49cb7e455d032f7370f364d9ce30b8eb1';
|
||||
|
||||
/// See also [CurrentChapter].
|
||||
@ProviderFor(CurrentChapter)
|
||||
final currentChapterProvider =
|
||||
AutoDisposeNotifierProvider<CurrentChapter, core.BookChapter?>.internal(
|
||||
CurrentChapter.new,
|
||||
name: r'currentChapterProvider',
|
||||
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
|
||||
? null
|
||||
: _$currentChapterHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
typedef _$CurrentChapter = AutoDisposeNotifier<core.BookChapter?>;
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member, deprecated_member_use_from_same_package
|
||||
Loading…
Add table
Add a link
Reference in a new issue