mirror of
https://github.com/Dr-Blank/Vaani.git
synced 2025-12-09 04:29:29 +00:00
something
This commit is contained in:
parent
dbf4ce1959
commit
a720c977c2
115 changed files with 8819 additions and 1 deletions
55
lib/api/image_provider.dart
Normal file
55
lib/api/image_provider.dart
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter/material.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/db/cache_manager.dart';
|
||||
|
||||
/// provides cover images for the audiobooks
|
||||
///
|
||||
/// is a stream provider that provides cover images first from the cache then from the server
|
||||
/// if the image is not found in the cache, it will be fetched from the server and saved to the cache
|
||||
/// if the image is not found in the server it will throw an error
|
||||
|
||||
part 'image_provider.g.dart';
|
||||
|
||||
@riverpod
|
||||
class CoverImage extends _$CoverImage {
|
||||
@override
|
||||
Stream<Uint8List> build(LibraryItem libraryItem) async* {
|
||||
final api = ref.watch(authenticatedApiProvider);
|
||||
|
||||
// try to get the image from the cache
|
||||
final file = await imageCacheManager.getFileFromCache(libraryItem.id);
|
||||
|
||||
if (file != null) {
|
||||
// if the image is in the cache, yield it
|
||||
yield await file.file.readAsBytes();
|
||||
// return if no need to fetch from the server
|
||||
if (libraryItem.updatedAt.isBefore(await file.file.lastModified())) {
|
||||
return;
|
||||
} else {
|
||||
debugPrint(
|
||||
'cover image stale for ${libraryItem.id}, fetching from the server',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// check if the image is in the cache
|
||||
final coverImage = await api.items.getCover(
|
||||
libraryItemId: libraryItem.id,
|
||||
parameters: const GetImageReqParams(width: 500),
|
||||
);
|
||||
// save the image to the cache
|
||||
final newFile = await imageCacheManager.putFile(
|
||||
libraryItem.id,
|
||||
coverImage ?? Uint8List(0),
|
||||
key: libraryItem.id,
|
||||
);
|
||||
debugPrint(
|
||||
'cover image fetched for for ${libraryItem.id}, file time: ${await newFile.lastModified()}',
|
||||
);
|
||||
yield coverImage ?? Uint8List(0);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue