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