chore: run dart format
Some checks are pending
Flutter CI & Release / Test (push) Waiting to run
Flutter CI & Release / Build Android APKs (push) Blocked by required conditions
Flutter CI & Release / build_linux (push) Blocked by required conditions
Flutter CI & Release / Create GitHub Release (push) Blocked by required conditions

This commit is contained in:
Dr.Blank 2026-01-10 16:51:05 +05:30
parent a520136e01
commit e23c0b6c5f
No known key found for this signature in database
GPG key ID: BA5F87FF0560C57B
84 changed files with 1565 additions and 1945 deletions

View file

@ -67,17 +67,13 @@ class AudiobookDownloadManager {
late StreamSubscription<TaskUpdate> _updatesSubscription;
Future<void> queueAudioBookDownload(
LibraryItemExpanded item,
) async {
Future<void> queueAudioBookDownload(LibraryItemExpanded item) async {
_logger.info('queuing download for item: ${item.id}');
// 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),
)) {
if (isFileDownloaded(constructFilePath(directory, item, file))) {
_logger.info('file already downloaded: ${file.metadata.filename}');
continue;
}
@ -105,8 +101,7 @@ class AudiobookDownloadManager {
Directory directory,
LibraryItemExpanded item,
LibraryFile file,
) =>
'${directory.path}/${item.relPath}/${file.metadata.filename}';
) => '${directory.path}/${item.relPath}/${file.metadata.filename}';
void dispose() {
_updatesSubscription.cancel();

View file

@ -52,13 +52,9 @@ class DownloadManager extends _$DownloadManager {
return manager;
}
Future<void> queueAudioBookDownload(
LibraryItemExpanded item,
) async {
Future<void> queueAudioBookDownload(LibraryItemExpanded item) async {
_logger.fine('queueing download for ${item.id}');
await state.queueAudioBookDownload(
item,
);
await state.queueAudioBookDownload(item);
}
Future<void> deleteDownloadedItem(LibraryItemExpanded item) async {
@ -83,58 +79,57 @@ class ItemDownloadProgress extends _$ItemDownloadProgress {
Future<double?> build(String id) async {
final item = await ref.watch(libraryItemProvider(id).future);
final manager = ref.read(downloadManagerProvider);
manager.taskUpdateStream.map((taskUpdate) {
if (taskUpdate is! TaskProgressUpdate) {
return null;
}
if (taskUpdate.task.group == id) {
return taskUpdate;
}
}).listen((task) async {
if (task != null) {
final totalSize = item.totalSize;
// if total size is 0, return 0
if (totalSize == 0) {
state = const AsyncValue.data(0.0);
return;
}
final downloadedFiles = await manager.getDownloadedFilesMetadata(item);
// calculate total size of downloaded files and total size of item, then divide
// to get percentage
final downloadedSize = downloadedFiles.fold<int>(
0,
(previousValue, element) => previousValue + element.metadata.size,
);
manager.taskUpdateStream
.map((taskUpdate) {
if (taskUpdate is! TaskProgressUpdate) {
return null;
}
if (taskUpdate.task.group == id) {
return taskUpdate;
}
})
.listen((task) async {
if (task != null) {
final totalSize = item.totalSize;
// if total size is 0, return 0
if (totalSize == 0) {
state = const AsyncValue.data(0.0);
return;
}
final downloadedFiles = await manager.getDownloadedFilesMetadata(
item,
);
// calculate total size of downloaded files and total size of item, then divide
// to get percentage
final downloadedSize = downloadedFiles.fold<int>(
0,
(previousValue, element) => previousValue + element.metadata.size,
);
final inProgressFileSize = task.progress * task.expectedFileSize;
final totalDownloadedSize = downloadedSize + inProgressFileSize;
final progress = totalDownloadedSize / totalSize;
// if current progress is more than calculated progress, do not update
if (progress < (state.value ?? 0.0)) {
return;
}
final inProgressFileSize = task.progress * task.expectedFileSize;
final totalDownloadedSize = downloadedSize + inProgressFileSize;
final progress = totalDownloadedSize / totalSize;
// if current progress is more than calculated progress, do not update
if (progress < (state.value ?? 0.0)) {
return;
}
state = AsyncValue.data(progress.clamp(0.0, 1.0));
}
});
state = AsyncValue.data(progress.clamp(0.0, 1.0));
}
});
return null;
}
}
@riverpod
FutureOr<List<TaskRecord>> downloadHistory(
Ref ref, {
String? group,
}) async {
FutureOr<List<TaskRecord>> downloadHistory(Ref ref, {String? group}) async {
return await FileDownloader().database.allRecords(group: group);
}
@riverpod
class IsItemDownloaded extends _$IsItemDownloaded {
@override
FutureOr<bool> build(
LibraryItemExpanded item,
) {
FutureOr<bool> build(LibraryItemExpanded item) {
final manager = ref.watch(downloadManagerProvider);
return manager.isItemDownloaded(item);
}

View file

@ -11,9 +11,7 @@ class DownloadsPage extends HookConsumerWidget {
final downloadHistory = ref.watch(downloadHistoryProvider());
return Scaffold(
appBar: AppBar(
title: const Text('Downloads'),
),
appBar: AppBar(title: const Text('Downloads')),
body: Center(
// history of downloads
child: downloadHistory.when(