fix: adde books to the download manager when using the "read now" function (#169)
This commit is contained in:
@@ -49,7 +49,6 @@ de.doen1el.calibreWebCompanion.yml
|
||||
|
||||
# Don't track generated localization files
|
||||
lib/l10n/app_localizations*.dart
|
||||
lib/l10n/app_localizations*.darttest/test_env.dart
|
||||
test/test_env.dart
|
||||
release_notes.md
|
||||
distribution/whatsnew/
|
||||
|
||||
@@ -8,6 +8,7 @@ import 'package:logger/logger.dart';
|
||||
import 'package:calibre_web_companion/features/offline/data/models/offline_book_model.dart';
|
||||
import 'package:calibre_web_companion/features/offline/data/repositories/offline_library_repository.dart';
|
||||
|
||||
import 'package:calibre_web_companion/features/book_details/data/models/book_details_model.dart';
|
||||
import 'package:calibre_web_companion/features/book_details/bloc/book_details_event.dart';
|
||||
import 'package:calibre_web_companion/features/book_details/bloc/book_details_state.dart';
|
||||
|
||||
@@ -309,30 +310,12 @@ class BookDetailsBloc extends Bloc<BookDetailsEvent, BookDetailsState> {
|
||||
|
||||
final uuid = state.bookViewModel?.uuid ?? state.bookDetails!.uuid;
|
||||
await downloadManager.registerDownload(uuid, filePath);
|
||||
|
||||
try {
|
||||
final details = state.bookDetails!;
|
||||
final coverBytes = await repository.fetchCoverBytes(
|
||||
details.id,
|
||||
details.coverUrl,
|
||||
await _cacheOfflineSnapshot(
|
||||
uuid,
|
||||
state.bookDetails!,
|
||||
filePath,
|
||||
event.format,
|
||||
);
|
||||
await GetIt.instance<OfflineLibraryRepository>().saveBook(
|
||||
OfflineBookModel(
|
||||
uuid: uuid,
|
||||
id: details.id,
|
||||
title: details.title,
|
||||
authors: details.authors,
|
||||
series: details.series,
|
||||
seriesIndex: details.seriesIndex,
|
||||
filePath: filePath,
|
||||
format: event.format,
|
||||
savedAt: DateTime.now().millisecondsSinceEpoch,
|
||||
),
|
||||
coverBytes: coverBytes,
|
||||
);
|
||||
} catch (e) {
|
||||
logger.w('Could not cache offline metadata: $e');
|
||||
}
|
||||
|
||||
logger.i('Download completed successfully: $filePath');
|
||||
emit(
|
||||
@@ -373,6 +356,36 @@ class BookDetailsBloc extends Bloc<BookDetailsEvent, BookDetailsState> {
|
||||
emit(state.copyWith(downloadProgress: event.progress));
|
||||
}
|
||||
|
||||
Future<void> _cacheOfflineSnapshot(
|
||||
String uuid,
|
||||
BookDetailsModel details,
|
||||
String filePath,
|
||||
String format,
|
||||
) async {
|
||||
try {
|
||||
final coverBytes = await repository.fetchCoverBytes(
|
||||
details.id,
|
||||
details.coverUrl,
|
||||
);
|
||||
await GetIt.instance<OfflineLibraryRepository>().saveBook(
|
||||
OfflineBookModel(
|
||||
uuid: uuid,
|
||||
id: details.id,
|
||||
title: details.title,
|
||||
authors: details.authors,
|
||||
series: details.series,
|
||||
seriesIndex: details.seriesIndex,
|
||||
filePath: filePath,
|
||||
format: format,
|
||||
savedAt: DateTime.now().millisecondsSinceEpoch,
|
||||
),
|
||||
coverBytes: coverBytes,
|
||||
);
|
||||
} catch (e) {
|
||||
logger.w('Could not cache offline metadata: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onOpenBookInReader(
|
||||
OpenBookInReader event,
|
||||
Emitter<BookDetailsState> emit,
|
||||
@@ -396,14 +409,26 @@ class BookDetailsBloc extends Bloc<BookDetailsEvent, BookDetailsState> {
|
||||
),
|
||||
);
|
||||
|
||||
final details = state.bookDetails!;
|
||||
final uuid = state.bookViewModel?.uuid ?? details.uuid;
|
||||
final format =
|
||||
details.formats.isNotEmpty
|
||||
? details.formats.first.toLowerCase()
|
||||
: 'epub';
|
||||
|
||||
final success = await repository.openInReader(
|
||||
state.bookDetails!,
|
||||
details,
|
||||
event.selectedDirectory,
|
||||
event.schema,
|
||||
progressCallback: (progress) {
|
||||
logger.d('Reader download progress: $progress%');
|
||||
emit(state.copyWith(downloadProgress: progress));
|
||||
},
|
||||
onFileDownloaded: (path) async {
|
||||
await downloadManager.registerDownload(uuid, path);
|
||||
await _cacheOfflineSnapshot(uuid, details, path, format);
|
||||
emit(state.copyWith(downloadFilePath: path, isDownloaded: true));
|
||||
},
|
||||
);
|
||||
|
||||
if (success) {
|
||||
|
||||
@@ -577,6 +577,7 @@ class BookDetailsRemoteDatasource {
|
||||
DocumentFile? selectedDirectory,
|
||||
DownloadSchema schema, {
|
||||
Function(int)? progressCallback,
|
||||
Future<void> Function(String path)? onFileDownloaded,
|
||||
}) async {
|
||||
try {
|
||||
logger.i('Opening book in reader: ${book.title}');
|
||||
@@ -587,12 +588,14 @@ class BookDetailsRemoteDatasource {
|
||||
}
|
||||
|
||||
String localPath;
|
||||
String durablePath;
|
||||
if (selectedDirectory == null) {
|
||||
localPath = await downloadBookToDevice(
|
||||
book,
|
||||
format: format,
|
||||
progressCallback: progressCallback,
|
||||
);
|
||||
durablePath = localPath;
|
||||
} else {
|
||||
final filePath = await downloadBookToPath(
|
||||
book: book,
|
||||
@@ -616,8 +619,11 @@ class BookDetailsRemoteDatasource {
|
||||
return false;
|
||||
}
|
||||
localPath = cachedFile.path;
|
||||
durablePath = filePath;
|
||||
}
|
||||
|
||||
await onFileDownloaded?.call(durablePath);
|
||||
|
||||
final result = await OpenFile.open(localPath);
|
||||
|
||||
if (result.type != ResultType.done) {
|
||||
|
||||
@@ -55,6 +55,7 @@ class BookDetailsRepository {
|
||||
DocumentFile? selectedDirectory,
|
||||
DownloadSchema schema, {
|
||||
Function(int)? progressCallback,
|
||||
Future<void> Function(String path)? onFileDownloaded,
|
||||
}) async {
|
||||
try {
|
||||
return await datasource.openInReader(
|
||||
@@ -62,6 +63,7 @@ class BookDetailsRepository {
|
||||
selectedDirectory,
|
||||
schema,
|
||||
progressCallback: progressCallback,
|
||||
onFileDownloaded: onFileDownloaded,
|
||||
);
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
|
||||
Reference in New Issue
Block a user