downloads and offline playback

This commit is contained in:
Dr-Blank 2024-08-20 08:36:39 -04:00
parent 1c95d1e4bb
commit c24541f1cd
No known key found for this signature in database
GPG key ID: 7452CC63F210A266
38 changed files with 1590 additions and 109 deletions

View file

@ -0,0 +1,185 @@
// download manager to handle download tasks of files
import 'dart:io';
import 'package:background_downloader/background_downloader.dart';
import 'package:logging/logging.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shelfsdk/audiobookshelf_api.dart';
import 'package:whispering_pages/shared/extensions/model_conversions.dart';
final _logger = Logger('AudiobookDownloadManager');
final tq = MemoryTaskQueue();
const downloadDirectory = BaseDirectory.applicationSupport;
class AudiobookDownloadManager {
// takes in the baseUrl and the token
AudiobookDownloadManager({
required this.baseUrl,
required this.token,
this.requiresWiFi = true,
this.retries = 0,
this.allowPause = false,
// /// The maximum number of concurrent tasks to run at any given time.
// int maxConcurrent = 3,
// /// The maximum number of concurrent tasks to run for the same host.
// int maxConcurrentByHost = 2,
// /// The maximum number of concurrent tasks to run for the same group.
// int maxConcurrentByGroup = 3,
}) {
// initialize the download manager
FileDownloader().addTaskQueue(tq);
_logger.fine('initialized with baseUrl: $baseUrl, token: $token');
_logger.fine(
'requiresWiFi: $requiresWiFi, retries: $retries, allowPause: $allowPause',
);
}
// the base url for the audio files
final String baseUrl;
// the authentication token to access the [AudioTrack.contentUrl]
final String token;
// whether to download only on wifi
final bool requiresWiFi;
// the number of retries to attempt
final int retries;
// whether to allow pausing of downloads
final bool allowPause;
// final List<DownloadTask> _downloadTasks = [];
Future<void> queueAudioBookDownload(LibraryItemExpanded item) async {
_logger.info('queuing download for item: ${item.toJson()}');
// create a download task for each file in the item
final directory = await getApplicationSupportDirectory();
for (final file in item.libraryFiles) {
// check if the file is already downloaded
if (isFileDownloaded(
constructFilePath(directory, item, file),
)) {
_logger.info('file already downloaded: ${file.metadata.filename}');
continue;
}
final task = DownloadTask(
taskId: file.ino,
url: file.url(baseUrl, item.id, token).toString(),
directory: item.relPath,
filename: file.metadata.filename,
requiresWiFi: requiresWiFi,
retries: retries,
allowPause: allowPause,
group: item.id,
baseDirectory: downloadDirectory,
// metaData: token
);
// _downloadTasks.add(task);
tq.add(task);
_logger.info('queued task: ${task.toJson()}');
}
FileDownloader().registerCallbacks(
group: item.id,
taskProgressCallback: (update) {
_logger.info('Group: ${item.id}, Progress Update: ${update.progress}');
},
taskStatusCallback: (update) {
switch (update.status) {
case TaskStatus.complete:
_logger.info('Group: ${item.id}, Download Complete');
break;
case TaskStatus.failed:
_logger.warning('Group: ${item.id}, Download Failed');
break;
default:
_logger
.info('Group: ${item.id}, Download Status: ${update.status}');
}
},
taskNotificationTapCallback: (task, notificationType) {
_logger.info('Group: ${item.id}, Task: ${task.toJson()}');
},
);
}
String constructFilePath(
Directory directory,
LibraryItemExpanded item,
LibraryFile file,
) =>
'${directory.path}/${item.relPath}/${file.metadata.filename}';
// void startDownload() {
// for (final task in _downloadTasks) {
// _logger.fine('enqueuing task: $task');
// FileDownloader().enqueue(task);
// }
// }
void dispose() {
// tq.removeAll();
_logger.fine('disposed');
}
bool isFileDownloaded(String filePath) {
// Check if the file exists
final fileExists = File(filePath).existsSync();
return fileExists;
}
Future<bool> isItemDownloaded(LibraryItemExpanded item) async {
final directory = await getApplicationSupportDirectory();
for (final file in item.libraryFiles) {
if (!isFileDownloaded(constructFilePath(directory, item, file))) {
_logger.info('file not downloaded: ${file.metadata.filename}');
return false;
}
}
return true;
}
Future<void> deleteDownloadedItem(LibraryItemExpanded item) async {
final directory = await getApplicationSupportDirectory();
for (final file in item.libraryFiles) {
final filePath = constructFilePath(directory, item, file);
if (isFileDownloaded(filePath)) {
File(filePath).deleteSync();
}
_logger.info('deleted file: $filePath');
}
}
Future<List<Uri>> getDownloadedFiles(LibraryItemExpanded item) async {
final directory = await getApplicationSupportDirectory();
final files = <Uri>[];
for (final file in item.libraryFiles) {
final filePath = constructFilePath(directory, item, file);
if (isFileDownloaded(filePath)) {
files.add(Uri.file(filePath));
}
}
return files;
}
}
Future<void> initDownloadManager() async {
// initialize the download manager
var fileDownloader = FileDownloader();
fileDownloader.configureNotification(
running: const TaskNotification('Downloading', 'file: {filename}'),
progressBar: true,
);
await fileDownloader.trackTasks();
}

View file

@ -0,0 +1,51 @@
import 'package:background_downloader/background_downloader.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:shelfsdk/audiobookshelf_api.dart';
import 'package:whispering_pages/api/api_provider.dart';
import 'package:whispering_pages/features/downloads/core/download_manager.dart'
as core;
import 'package:whispering_pages/settings/app_settings_provider.dart';
part 'download_manager.g.dart';
@Riverpod(keepAlive: true)
class SimpleDownloadManager extends _$SimpleDownloadManager {
@override
core.AudiobookDownloadManager build() {
final api = ref.watch(authenticatedApiProvider);
final downloadSettings = ref.watch(appSettingsProvider).downloadSettings;
final manager = core.AudiobookDownloadManager(
baseUrl: api.baseUrl.toString(),
token: api.token!,
requiresWiFi: downloadSettings.requiresWiFi,
retries: downloadSettings.retries,
allowPause: downloadSettings.allowPause,
);
core.tq.maxConcurrent = downloadSettings.maxConcurrent;
core.tq.maxConcurrentByHost = downloadSettings.maxConcurrentByHost;
core.tq.maxConcurrentByGroup = downloadSettings.maxConcurrentByGroup;
ref.onDispose(() {
manager.dispose();
});
return manager;
}
}
@riverpod
FutureOr<List<TaskRecord>> downloadHistory(
DownloadHistoryRef ref, {
String? group,
}) async {
return await FileDownloader().database.allRecords(group: group);
}
@Riverpod(keepAlive: false)
FutureOr<bool> downloadStatus(
DownloadStatusRef ref,
LibraryItemExpanded item,
) async {
final manager = ref.read(simpleDownloadManagerProvider);
return manager.isItemDownloaded(item);
}

View file

@ -0,0 +1,306 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'download_manager.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
String _$downloadHistoryHash() => r'76c449e8abfa61d57566991686f534a06dc7fef7';
/// Copied from Dart SDK
class _SystemHash {
_SystemHash._();
static int combine(int hash, int value) {
// ignore: parameter_assignments
hash = 0x1fffffff & (hash + value);
// ignore: parameter_assignments
hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10));
return hash ^ (hash >> 6);
}
static int finish(int hash) {
// ignore: parameter_assignments
hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3));
// ignore: parameter_assignments
hash = hash ^ (hash >> 11);
return 0x1fffffff & (hash + ((0x00003fff & hash) << 15));
}
}
/// See also [downloadHistory].
@ProviderFor(downloadHistory)
const downloadHistoryProvider = DownloadHistoryFamily();
/// See also [downloadHistory].
class DownloadHistoryFamily extends Family<AsyncValue<List<TaskRecord>>> {
/// See also [downloadHistory].
const DownloadHistoryFamily();
/// See also [downloadHistory].
DownloadHistoryProvider call({
String? group,
}) {
return DownloadHistoryProvider(
group: group,
);
}
@override
DownloadHistoryProvider getProviderOverride(
covariant DownloadHistoryProvider provider,
) {
return call(
group: provider.group,
);
}
static const Iterable<ProviderOrFamily>? _dependencies = null;
@override
Iterable<ProviderOrFamily>? get dependencies => _dependencies;
static const Iterable<ProviderOrFamily>? _allTransitiveDependencies = null;
@override
Iterable<ProviderOrFamily>? get allTransitiveDependencies =>
_allTransitiveDependencies;
@override
String? get name => r'downloadHistoryProvider';
}
/// See also [downloadHistory].
class DownloadHistoryProvider
extends AutoDisposeFutureProvider<List<TaskRecord>> {
/// See also [downloadHistory].
DownloadHistoryProvider({
String? group,
}) : this._internal(
(ref) => downloadHistory(
ref as DownloadHistoryRef,
group: group,
),
from: downloadHistoryProvider,
name: r'downloadHistoryProvider',
debugGetCreateSourceHash:
const bool.fromEnvironment('dart.vm.product')
? null
: _$downloadHistoryHash,
dependencies: DownloadHistoryFamily._dependencies,
allTransitiveDependencies:
DownloadHistoryFamily._allTransitiveDependencies,
group: group,
);
DownloadHistoryProvider._internal(
super._createNotifier, {
required super.name,
required super.dependencies,
required super.allTransitiveDependencies,
required super.debugGetCreateSourceHash,
required super.from,
required this.group,
}) : super.internal();
final String? group;
@override
Override overrideWith(
FutureOr<List<TaskRecord>> Function(DownloadHistoryRef provider) create,
) {
return ProviderOverride(
origin: this,
override: DownloadHistoryProvider._internal(
(ref) => create(ref as DownloadHistoryRef),
from: from,
name: null,
dependencies: null,
allTransitiveDependencies: null,
debugGetCreateSourceHash: null,
group: group,
),
);
}
@override
AutoDisposeFutureProviderElement<List<TaskRecord>> createElement() {
return _DownloadHistoryProviderElement(this);
}
@override
bool operator ==(Object other) {
return other is DownloadHistoryProvider && other.group == group;
}
@override
int get hashCode {
var hash = _SystemHash.combine(0, runtimeType.hashCode);
hash = _SystemHash.combine(hash, group.hashCode);
return _SystemHash.finish(hash);
}
}
mixin DownloadHistoryRef on AutoDisposeFutureProviderRef<List<TaskRecord>> {
/// The parameter `group` of this provider.
String? get group;
}
class _DownloadHistoryProviderElement
extends AutoDisposeFutureProviderElement<List<TaskRecord>>
with DownloadHistoryRef {
_DownloadHistoryProviderElement(super.provider);
@override
String? get group => (origin as DownloadHistoryProvider).group;
}
String _$downloadStatusHash() => r'f37b4678d3c2a7c6e985b0149d72ea0f9b1b42ca';
/// See also [downloadStatus].
@ProviderFor(downloadStatus)
const downloadStatusProvider = DownloadStatusFamily();
/// See also [downloadStatus].
class DownloadStatusFamily extends Family<AsyncValue<bool>> {
/// See also [downloadStatus].
const DownloadStatusFamily();
/// See also [downloadStatus].
DownloadStatusProvider call(
LibraryItemExpanded item,
) {
return DownloadStatusProvider(
item,
);
}
@override
DownloadStatusProvider getProviderOverride(
covariant DownloadStatusProvider provider,
) {
return call(
provider.item,
);
}
static const Iterable<ProviderOrFamily>? _dependencies = null;
@override
Iterable<ProviderOrFamily>? get dependencies => _dependencies;
static const Iterable<ProviderOrFamily>? _allTransitiveDependencies = null;
@override
Iterable<ProviderOrFamily>? get allTransitiveDependencies =>
_allTransitiveDependencies;
@override
String? get name => r'downloadStatusProvider';
}
/// See also [downloadStatus].
class DownloadStatusProvider extends AutoDisposeFutureProvider<bool> {
/// See also [downloadStatus].
DownloadStatusProvider(
LibraryItemExpanded item,
) : this._internal(
(ref) => downloadStatus(
ref as DownloadStatusRef,
item,
),
from: downloadStatusProvider,
name: r'downloadStatusProvider',
debugGetCreateSourceHash:
const bool.fromEnvironment('dart.vm.product')
? null
: _$downloadStatusHash,
dependencies: DownloadStatusFamily._dependencies,
allTransitiveDependencies:
DownloadStatusFamily._allTransitiveDependencies,
item: item,
);
DownloadStatusProvider._internal(
super._createNotifier, {
required super.name,
required super.dependencies,
required super.allTransitiveDependencies,
required super.debugGetCreateSourceHash,
required super.from,
required this.item,
}) : super.internal();
final LibraryItemExpanded item;
@override
Override overrideWith(
FutureOr<bool> Function(DownloadStatusRef provider) create,
) {
return ProviderOverride(
origin: this,
override: DownloadStatusProvider._internal(
(ref) => create(ref as DownloadStatusRef),
from: from,
name: null,
dependencies: null,
allTransitiveDependencies: null,
debugGetCreateSourceHash: null,
item: item,
),
);
}
@override
AutoDisposeFutureProviderElement<bool> createElement() {
return _DownloadStatusProviderElement(this);
}
@override
bool operator ==(Object other) {
return other is DownloadStatusProvider && other.item == item;
}
@override
int get hashCode {
var hash = _SystemHash.combine(0, runtimeType.hashCode);
hash = _SystemHash.combine(hash, item.hashCode);
return _SystemHash.finish(hash);
}
}
mixin DownloadStatusRef on AutoDisposeFutureProviderRef<bool> {
/// The parameter `item` of this provider.
LibraryItemExpanded get item;
}
class _DownloadStatusProviderElement
extends AutoDisposeFutureProviderElement<bool> with DownloadStatusRef {
_DownloadStatusProviderElement(super.provider);
@override
LibraryItemExpanded get item => (origin as DownloadStatusProvider).item;
}
String _$simpleDownloadManagerHash() =>
r'cec95717c86e422f88f78aa014d29e800e5a2089';
/// See also [SimpleDownloadManager].
@ProviderFor(SimpleDownloadManager)
final simpleDownloadManagerProvider = NotifierProvider<SimpleDownloadManager,
core.AudiobookDownloadManager>.internal(
SimpleDownloadManager.new,
name: r'simpleDownloadManagerProvider',
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
? null
: _$simpleDownloadManagerHash,
dependencies: null,
allTransitiveDependencies: null,
);
typedef _$SimpleDownloadManager = Notifier<core.AudiobookDownloadManager>;
// ignore_for_file: type=lint
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member

View file

@ -0,0 +1,51 @@
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:whispering_pages/features/downloads/providers/download_manager.dart';
class DownloadsPage extends HookConsumerWidget {
const DownloadsPage({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final manager = ref.read(simpleDownloadManagerProvider);
final downloadHistory = ref.watch(downloadHistoryProvider());
return Scaffold(
appBar: AppBar(
title: const Text('Downloads'),
backgroundColor: Colors.transparent,
),
body: Center(
// history of downloads
child: downloadHistory.when(
data: (records) {
// each group is one list tile, which contains the files downloaded
final uniqueGroups = records.map((e) => e.group).toSet();
return ListView.builder(
itemCount: uniqueGroups.length,
itemBuilder: (context, index) {
final group = uniqueGroups.elementAt(index);
final groupRecords = records.where((e) => e.group == group);
return ExpansionTile(
title: Text(group ?? 'No Group'),
children: groupRecords
.map(
(e) => ListTile(
title: Text('${e.task.directory}/${e.task.filename}'),
subtitle: Text(e.task.creationTime.toString()),
),
)
.toList(),
);
},
);
},
loading: () => const CircularProgressIndicator(),
error: (error, stackTrace) {
return Text('Error: $error');
},
),
),
);
}
}

View file

@ -1,9 +1,18 @@
import 'package:background_downloader/background_downloader.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:shelfsdk/audiobookshelf_api.dart' as shelfsdk;
import 'package:whispering_pages/api/library_item_provider.dart';
import 'package:whispering_pages/constants/hero_tag_conventions.dart';
import 'package:whispering_pages/features/downloads/providers/download_manager.dart'
show
downloadHistoryProvider,
downloadStatusProvider,
simpleDownloadManagerProvider;
import 'package:whispering_pages/features/item_viewer/view/library_item_page.dart';
import 'package:whispering_pages/features/player/providers/audiobook_player.dart';
import 'package:whispering_pages/main.dart';
import 'package:whispering_pages/settings/app_settings_provider.dart';
import 'package:whispering_pages/shared/extensions/model_conversions.dart';
@ -19,7 +28,10 @@ class LibraryItemActions extends HookConsumerWidget {
late final shelfsdk.BookExpanded book;
@override
Widget build(BuildContext context, WidgetRef ref) {
final player = ref.read(audiobookPlayerProvider);
final manager = ref.read(simpleDownloadManagerProvider);
final downloadHistory = ref.watch(downloadHistoryProvider(group: item.id));
final isItemDownloaded = ref.watch(downloadStatusProvider(item));
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 8.0),
child: Row(
@ -55,16 +67,150 @@ class LibraryItemActions extends HookConsumerWidget {
onPressed: () {},
icon: const Icon(Icons.share_rounded),
),
// download button
IconButton(
onPressed: () {},
icon: const Icon(
Icons.download_rounded,
),
// check if the book is downloaded using manager.isItemDownloaded
isItemDownloaded.when(
data: (isDownloaded) {
if (isDownloaded) {
// already downloaded button
return IconButton(
onPressed: () {
appLogger
.fine('Pressed already downloaded button');
// manager.openDownloadedFile(item);
// open popup menu to open or delete the file
showModalBottomSheet(
// useRootNavigator: true,
context: context,
builder: (context) {
return Padding(
padding: const EdgeInsets.symmetric(
vertical: 8.0,
),
child: DownloadSheet(
item: item,
),
);
},
);
},
icon: const Icon(
Icons.download_done_rounded,
),
);
}
// download button
return IconButton(
onPressed: () {
appLogger.fine('Pressed download button');
manager.queueAudioBookDownload(item);
},
icon: const Icon(
Icons.download_rounded,
),
);
},
loading: () => const CircularProgressIndicator(),
error: (error, stackTrace) {
return IconButton(
onPressed: () {
appLogger.warning(
'Error checking download status: $error',
);
},
icon: const Icon(Icons.error_rounded),
);
},
),
// more button
IconButton(
onPressed: () {},
onPressed: () {
// show the bottom sheet with download history
showModalBottomSheet(
context: context,
builder: (context) {
return downloadHistory.when(
data: (downloadHistory) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: ListView.builder(
itemCount: downloadHistory.length,
itemBuilder: (context, index) {
final record = downloadHistory[index];
return ListTile(
title: Text(record.task.filename),
subtitle: Text(
'${record.task.directory}/${record.task.baseDirectory}',
),
trailing: const Icon(
Icons.open_in_new_rounded,
),
onLongPress: () {
// show the delete dialog
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Delete'),
content: Text(
'Are you sure you want to delete ${record.task.filename}?',
),
actions: [
TextButton(
onPressed: () {
// delete the file
FileDownloader()
.database
.deleteRecordWithId(
record
.task.taskId,
);
Navigator.pop(context);
},
child: const Text('Yes'),
),
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('No'),
),
],
);
},
);
},
onTap: () async {
// open the file location
final didOpen =
await FileDownloader().openFile(
task: record.task,
);
if (!didOpen) {
appLogger.warning(
'Failed to open file: ${record.task.filename} at ${record.task.directory}',
);
return;
}
appLogger.fine(
'Opened file: ${record.task.filename} at ${record.task.directory}',
);
},
);
},
),
);
},
loading: () => const Center(
child: CircularProgressIndicator(),
),
error: (error, stackTrace) => Center(
child: Text('Error: $error'),
),
);
},
);
},
icon: const Icon(
Icons.more_vert_rounded,
),
@ -81,6 +227,84 @@ class LibraryItemActions extends HookConsumerWidget {
}
}
class DownloadSheet extends HookConsumerWidget {
const DownloadSheet({
super.key,
required this.item,
});
final shelfsdk.LibraryItemExpanded item;
@override
Widget build(BuildContext context, WidgetRef ref) {
final manager = ref.read(simpleDownloadManagerProvider);
return Column(
mainAxisSize: MainAxisSize.min,
children: [
// ListTile(
// title: const Text('Open'),
// onTap: () async {
// final didOpen =
// await FileDownloader().openFile(
// task: manager.getTaskForItem(item),
// );
// if (!didOpen) {
// appLogger.warning(
// 'Failed to open file: ${item.title}',
// );
// return;
// }
// appLogger.fine(
// 'Opened file: ${item.title}',
// );
// },
// ),
ListTile(
title: const Text('Delete'),
leading: const Icon(
Icons.delete_rounded,
),
onTap: () {
// show the delete dialog
showDialog(
useRootNavigator: false,
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Delete'),
content: Text(
'Are you sure you want to delete ${item.media.metadata.title}?',
),
actions: [
TextButton(
onPressed: () {
// delete the file
manager.deleteDownloadedItem(
item,
);
GoRouter.of(context).pop();
},
child: const Text('Yes'),
),
TextButton(
onPressed: () {
GoRouter.of(context).pop();
},
child: const Text('No'),
),
],
);
},
);
},
),
],
);
}
}
class _LibraryItemPlayButton extends HookConsumerWidget {
const _LibraryItemPlayButton({
super.key,
@ -122,7 +346,10 @@ class _LibraryItemPlayButton extends HookConsumerWidget {
return ElevatedButton.icon(
onPressed: () => libraryItemPlayButtonOnPressed(
ref: ref, book: book, userMediaProgress: userMediaProgress),
ref: ref,
book: book,
userMediaProgress: userMediaProgress,
),
icon: Hero(
tag: HeroTagPrefixes.libraryItemPlayButton + book.libraryItemId,
child: DynamicItemPlayIcon(
@ -182,9 +409,14 @@ Future<void> libraryItemPlayButtonOnPressed({
if (!isCurrentBookSetInPlayer) {
debugPrint('Setting the book ${book.libraryItemId}');
debugPrint('Initial position: ${userMediaProgress?.currentTime}');
await player.setSourceAudioBook(
final downloadManager = ref.watch(simpleDownloadManagerProvider);
final libItem =
await ref.read(libraryItemProvider(book.libraryItemId).future);
final downloadedUris = await downloadManager.getDownloadedFiles(libItem);
await player.setSourceAudiobook(
book,
initialPosition: userMediaProgress?.currentTime,
downloadedUris: downloadedUris,
);
} else {
debugPrint('Book was already set');

View file

@ -6,6 +6,7 @@ import 'package:flutter_animate/flutter_animate.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:whispering_pages/api/library_item_provider.dart';
import 'package:whispering_pages/features/item_viewer/view/library_item_sliver_app_bar.dart';
import 'package:whispering_pages/features/player/providers/player_form.dart';
import 'package:whispering_pages/router/models/library_item_extras.dart';
import 'package:whispering_pages/settings/app_settings_provider.dart';
import 'package:whispering_pages/shared/extensions/model_conversions.dart';
@ -101,6 +102,10 @@ class LibraryItemPage extends HookConsumerWidget {
)
: const SizedBox.shrink(),
),
// a padding at the bottom to make sure the last item is not hidden by mini player
const SliverToBoxAdapter(
child: SizedBox(height: playerMinHeight),
),
],
),
),

View file

@ -0,0 +1,50 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:whispering_pages/router/router.dart';
class LibraryBrowserPage extends HookConsumerWidget {
const LibraryBrowserPage({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
return Scaffold(
appBar: AppBar(
title: const Text('Library'),
backgroundColor: Colors.transparent,
),
// a list redirecting to authors, genres, and series pages
body: ListView(
children: [
ListTile(
title: const Text('Authors'),
leading: const Icon(Icons.person),
trailing: const Icon(Icons.chevron_right),
onTap: () {},
),
ListTile(
title: const Text('Genres'),
leading: const Icon(Icons.category),
trailing: const Icon(Icons.chevron_right),
onTap: () {},
),
ListTile(
title: const Text('Series'),
leading: const Icon(Icons.list),
trailing: const Icon(Icons.chevron_right),
onTap: () {},
),
// Downloads
ListTile(
title: const Text('Downloads'),
leading: const Icon(Icons.download),
trailing: const Icon(Icons.chevron_right),
onTap: () {
GoRouter.of(context).pushNamed(Routes.downloads.name);
},
),
],
),
);
}
}

View file

@ -3,11 +3,14 @@
/// this is needed as audiobook can be a list of audio files instead of a single file
library;
import 'package:flutter/foundation.dart';
import 'package:collection/collection.dart';
import 'package:just_audio/just_audio.dart';
import 'package:just_audio_background/just_audio_background.dart';
import 'package:logging/logging.dart';
import 'package:shelfsdk/audiobookshelf_api.dart';
final _logger = Logger('AudiobookPlayer');
/// returns the sum of the duration of all the previous tracks before the [index]
Duration sumOfTracks(BookExpanded book, int? index) {
// return 0 if index is less than 0
@ -54,7 +57,7 @@ class AudiobookPlayer extends AudioPlayer {
/// the [BookExpanded] being played
///
/// to set the book, use [setSourceAudioBook]
/// to set the book, use [setSourceAudiobook]
BookExpanded? get book => _book;
/// the authentication token to access the [AudioTrack.contentUrl]
@ -70,21 +73,24 @@ class AudiobookPlayer extends AudioPlayer {
int? get availableTracks => _book?.tracks.length;
/// sets the current [AudioTrack] as the source of the player
Future<void> setSourceAudioBook(
Future<void> setSourceAudiobook(
BookExpanded? book, {
bool preload = true,
// int? initialIndex,
Duration? initialPosition,
List<Uri>? downloadedUris,
Uri? artworkUri,
}) async {
// if the book is null, stop the player
if (book == null) {
_book = null;
_logger.info('Book is null, stopping player');
return stop();
}
// see if the book is the same as the current book
if (_book == book) {
// if the book is the same, do nothing
_logger.info('Book is the same, doing nothing');
return;
}
// first stop the player and clear the source
@ -111,23 +117,29 @@ class AudiobookPlayer extends AudioPlayer {
ConcatenatingAudioSource(
useLazyPreparation: true,
children: book.tracks.map((track) {
final retrievedUri =
_getUri(track, downloadedUris, baseUrl: baseUrl, token: token);
_logger.fine(
'Setting source for track: ${track.title}, URI: $retrievedUri',
);
return AudioSource.uri(
Uri.parse('$baseUrl${track.contentUrl}?token=$token'),
retrievedUri,
tag: MediaItem(
// Specify a unique ID for each media item:
id: book.libraryItemId + track.index.toString(),
// Metadata to display in the notification:
album: book.metadata.title,
title: book.metadata.title ?? track.title,
artUri: Uri.parse(
'$baseUrl/api/items/${book.libraryItemId}/cover?token=$token&width=800',
),
artUri: artworkUri ??
Uri.parse(
'$baseUrl/api/items/${book.libraryItemId}/cover?token=$token&width=800',
),
),
);
}).toList(),
),
).catchError((error) {
debugPrint('AudiobookPlayer Error: $error');
_logger.shout('Error: $error');
});
}
@ -176,7 +188,8 @@ class AudiobookPlayer extends AudioPlayer {
if (_book == null) {
return Duration.zero;
}
return bufferedPosition + _book!.tracks[sequenceState!.currentIndex].startOffset;
return bufferedPosition +
_book!.tracks[sequenceState!.currentIndex].startOffset;
}
/// streams to override to suit the book instead of the current track
@ -237,3 +250,20 @@ class AudiobookPlayer extends AudioPlayer {
);
}
}
Uri _getUri(
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;
},
);
return uri ??
Uri.parse('${baseUrl.toString()}${track.contentUrl}?token=$token');
}

View file

@ -1,16 +1,16 @@
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:whispering_pages/api/api_provider.dart';
import 'package:whispering_pages/features/player/core/audiobook_player.dart'
as abp;
as core;
part 'audiobook_player.g.dart';
// @Riverpod(keepAlive: true)
// abp.AudiobookPlayer audiobookPlayer(
// core.AudiobookPlayer audiobookPlayer(
// AudiobookPlayerRef ref,
// ) {
// final api = ref.watch(authenticatedApiProvider);
// final player = abp.AudiobookPlayer(api.token!, api.baseUrl);
// final player = core.AudiobookPlayer(api.token!, api.baseUrl);
// ref.onDispose(player.dispose);
@ -24,9 +24,12 @@ const playerId = 'audiobook_player';
@Riverpod(keepAlive: true)
class SimpleAudiobookPlayer extends _$SimpleAudiobookPlayer {
@override
abp.AudiobookPlayer build() {
core.AudiobookPlayer build() {
final api = ref.watch(authenticatedApiProvider);
final player = abp.AudiobookPlayer(api.token!, api.baseUrl);
final player = core.AudiobookPlayer(
api.token!,
api.baseUrl,
);
ref.onDispose(player.dispose);
@ -37,7 +40,7 @@ class SimpleAudiobookPlayer extends _$SimpleAudiobookPlayer {
@Riverpod(keepAlive: true)
class AudiobookPlayer extends _$AudiobookPlayer {
@override
abp.AudiobookPlayer build() {
core.AudiobookPlayer build() {
final player = ref.watch(simpleAudiobookPlayerProvider);
ref.onDispose(player.dispose);

View file

@ -7,7 +7,7 @@ part of 'audiobook_player.dart';
// **************************************************************************
String _$simpleAudiobookPlayerHash() =>
r'b65e6d779476a2c1fa38f617771bf997acb4f5b8';
r'9e11ed2791d35e308f8cbe61a79a45cf51466ebb';
/// Simple because it doesn't rebuild when the player state changes
/// it only rebuilds when the token changes
@ -15,7 +15,7 @@ String _$simpleAudiobookPlayerHash() =>
/// Copied from [SimpleAudiobookPlayer].
@ProviderFor(SimpleAudiobookPlayer)
final simpleAudiobookPlayerProvider =
NotifierProvider<SimpleAudiobookPlayer, abp.AudiobookPlayer>.internal(
NotifierProvider<SimpleAudiobookPlayer, core.AudiobookPlayer>.internal(
SimpleAudiobookPlayer.new,
name: r'simpleAudiobookPlayerProvider',
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
@ -25,13 +25,13 @@ final simpleAudiobookPlayerProvider =
allTransitiveDependencies: null,
);
typedef _$SimpleAudiobookPlayer = Notifier<abp.AudiobookPlayer>;
String _$audiobookPlayerHash() => r'38042d0c93034e6907677fdb614a9af1b9d636af';
typedef _$SimpleAudiobookPlayer = Notifier<core.AudiobookPlayer>;
String _$audiobookPlayerHash() => r'44394b1dbbf85eb19ef1f693717e8cbc15b768e5';
/// See also [AudiobookPlayer].
@ProviderFor(AudiobookPlayer)
final audiobookPlayerProvider =
NotifierProvider<AudiobookPlayer, abp.AudiobookPlayer>.internal(
NotifierProvider<AudiobookPlayer, core.AudiobookPlayer>.internal(
AudiobookPlayer.new,
name: r'audiobookPlayerProvider',
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
@ -41,6 +41,6 @@ final audiobookPlayerProvider =
allTransitiveDependencies: null,
);
typedef _$AudiobookPlayer = Notifier<abp.AudiobookPlayer>;
typedef _$AudiobookPlayer = Notifier<core.AudiobookPlayer>;
// ignore_for_file: type=lint
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member

View file

@ -9,6 +9,7 @@ import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'player_form.g.dart';
/// The height of the player when it is minimized
const double playerMinHeight = 70;
// const miniplayerPercentageDeclaration = 0.2;

View file

@ -98,7 +98,7 @@ class AudiobookPlayer extends HookConsumerWidget {
// add a delay before closing the player
// to allow the user to see the player closing
Future.delayed(const Duration(milliseconds: 300), () {
player.setSourceAudioBook(null);
player.setSourceAudiobook(null);
});
},
curve: Curves.easeOut,

View file

@ -17,6 +17,8 @@ import 'widgets/audiobook_player_seek_button.dart';
import 'widgets/audiobook_player_seek_chapter_button.dart';
import 'widgets/player_speed_adjust_button.dart';
var pendingPlayerModals = 0;
class PlayerWhenExpanded extends HookConsumerWidget {
const PlayerWhenExpanded({
super.key,
@ -270,6 +272,7 @@ class SleepTimerButton extends HookConsumerWidget {
message: 'Sleep Timer',
child: InkWell(
onTap: () async {
pendingPlayerModals++;
// show the sleep timer dialog
final resultingDuration = await showDurationPicker(
context: context,
@ -279,6 +282,7 @@ class SleepTimerButton extends HookConsumerWidget {
.sleepTimerSettings
.defaultDuration,
);
pendingPlayerModals--;
if (resultingDuration != null) {
// if 0 is selected, cancel the timer
if (resultingDuration.inSeconds == 0) {

View file

@ -1,8 +1,12 @@
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:logging/logging.dart';
import 'package:whispering_pages/features/player/providers/audiobook_player.dart';
import 'package:whispering_pages/features/player/view/player_when_expanded.dart';
import 'package:whispering_pages/features/player/view/widgets/speed_selector.dart';
final _logger = Logger('PlayerSpeedAdjustButton');
class PlayerSpeedAdjustButton extends HookConsumerWidget {
const PlayerSpeedAdjustButton({
super.key,
@ -14,8 +18,10 @@ class PlayerSpeedAdjustButton extends HookConsumerWidget {
final notifier = ref.watch(audiobookPlayerProvider.notifier);
return TextButton(
child: Text('${player.speed}x'),
onPressed: () {
showModalBottomSheet(
onPressed: () async {
pendingPlayerModals++;
_logger.fine('opening speed selector');
await showModalBottomSheet<bool>(
context: context,
barrierLabel: 'Select Speed',
constraints: const BoxConstraints(
@ -29,6 +35,8 @@ class PlayerSpeedAdjustButton extends HookConsumerWidget {
);
},
);
pendingPlayerModals--;
_logger.fine('Closing speed selector');
},
);
}

View file

@ -30,8 +30,7 @@ class SleepTimer extends _$SleepTimer {
}
var sleepTimer = core.SleepTimer(
// duration: sleepTimerSettings.defaultDuration,
duration: const Duration(seconds: 5),
duration: sleepTimerSettings.defaultDuration,
player: ref.watch(simpleAudiobookPlayerProvider),
);
ref.onDispose(sleepTimer.dispose);

View file

@ -6,7 +6,7 @@ part of 'sleep_timer_provider.dart';
// RiverpodGenerator
// **************************************************************************
String _$sleepTimerHash() => r'de2f39febda3c2234e792f64199c51828206ea9b';
String _$sleepTimerHash() => r'ad77e82c1b513bbc62815c64ce1ed403f92fc055';
/// See also [SleepTimer].
@ProviderFor(SleepTimer)