Skip to content

Commit

Permalink
refactor: update cover image handling to use item IDs and simplify li…
Browse files Browse the repository at this point in the history
…brary item actions
  • Loading branch information
Dr-Blank committed Sep 23, 2024
1 parent d25d23a commit 405d625
Show file tree
Hide file tree
Showing 15 changed files with 305 additions and 362 deletions.
24 changes: 13 additions & 11 deletions lib/api/image_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:logging/logging.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:shelfsdk/audiobookshelf_api.dart';
import 'package:vaani/api/api_provider.dart';
import 'package:vaani/api/library_item_provider.dart';
import 'package:vaani/db/cache_manager.dart';

/// provides cover images for the audiobooks
Expand All @@ -19,52 +20,53 @@ final _logger = Logger('cover_image_provider');
@Riverpod(keepAlive: true)
class CoverImage extends _$CoverImage {
@override
Stream<Uint8List> build(LibraryItem libraryItem) async* {
Stream<Uint8List> build(String itemId) async* {
final api = ref.watch(authenticatedApiProvider);

// ! artifical delay for testing
// await Future.delayed(const Duration(seconds: 2));

// try to get the image from the cache
final file = await imageCacheManager.getFileFromMemory(libraryItem.id) ??
await imageCacheManager.getFileFromCache(libraryItem.id);
final file = await imageCacheManager.getFileFromMemory(itemId) ??
await imageCacheManager.getFileFromCache(itemId);

if (file != null) {
// if the image is in the cache, yield it
_logger.fine(
'cover image found in cache for ${libraryItem.id} at ${file.file.path}',
'cover image found in cache for $itemId at ${file.file.path}',
);
yield await file.file.readAsBytes();
final libraryItem = await ref.watch(libraryItemProvider(itemId).future);
// return if no need to fetch from the server
if (libraryItem.updatedAt.isBefore(await file.file.lastModified())) {
_logger.fine(
'cover image is up to date for ${libraryItem.id}, no need to fetch from the server',
'cover image is up to date for $itemId, no need to fetch from the server',
);
return;
} else {
_logger.fine(
'cover image stale for ${libraryItem.id}, fetching from the server',
'cover image stale for $itemId, fetching from the server',
);
}
} else {
_logger.fine('cover image not found in cache for ${libraryItem.id}');
_logger.fine('cover image not found in cache for $itemId');
}

// check if the image is in the cache
final coverImage = await api.items.getCover(
libraryItemId: libraryItem.id,
libraryItemId: itemId,
parameters: const GetImageReqParams(width: 1200),
);
// save the image to the cache
if (coverImage != null) {
final newFile = await imageCacheManager.putFile(
libraryItem.id,
itemId,
coverImage,
key: libraryItem.id,
key: itemId,
fileExtension: 'jpg',
);
_logger.fine(
'cover image fetched for for ${libraryItem.id}, file time: ${await newFile.lastModified()}',
'cover image fetched for for $itemId, file time: ${await newFile.lastModified()}',
);
}

Expand Down
38 changes: 19 additions & 19 deletions lib/api/image_provider.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/api/library_item_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ part 'library_item_provider.g.dart';
final _logger = Logger('LibraryItemProvider');

/// provides the library item for the given id
@riverpod
@Riverpod(keepAlive: true)
class LibraryItem extends _$LibraryItem {
@override
Stream<shelfsdk.LibraryItemExpanded> build(String id) async* {
Expand All @@ -22,7 +22,7 @@ class LibraryItem extends _$LibraryItem {
_logger.fine('LibraryItemProvider fetching library item: $id');

// ! this is a mock delay
// await Future.delayed(const Duration(seconds: 10));
// await Future.delayed(const Duration(seconds: 150));

// look for the item in the cache
final key = CacheKey.libraryItem(id);
Expand Down
19 changes: 9 additions & 10 deletions lib/api/library_item_provider.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/features/explore/view/explore_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class BookSearchResultMini extends HookConsumerWidget {
final item = ref.watch(libraryItemProvider(book.libraryItemId)).valueOrNull;
final image = item == null
? const AsyncValue.loading()
: ref.watch(coverImageProvider(item));
: ref.watch(coverImageProvider(item.id));
return ListTile(
leading: SizedBox(
width: 50,
Expand Down
16 changes: 9 additions & 7 deletions lib/features/item_viewer/view/library_item_actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@ import 'package:vaani/shared/extensions/model_conversions.dart';
import 'package:vaani/shared/utils.dart';

class LibraryItemActions extends HookConsumerWidget {
LibraryItemActions({
const LibraryItemActions({
super.key,
required this.item,
}) {
book = item.media.asBookExpanded;
}
required this.id,
});

final String id;

final shelfsdk.LibraryItemExpanded item;
late final shelfsdk.BookExpanded book;
@override
Widget build(BuildContext context, WidgetRef ref) {
final item = ref.watch(libraryItemProvider(id)).valueOrNull;
if (item == null) {
return const SizedBox.shrink();
}
final downloadHistory = ref.watch(downloadHistoryProvider(group: item.id));
final apiSettings = ref.watch(apiSettingsProvider);

Expand Down
Loading

0 comments on commit 405d625

Please sign in to comment.