mirror of
https://github.com/Dr-Blank/Vaani.git
synced 2025-12-08 03:59:29 +00:00
kickoff library item
This commit is contained in:
parent
f8597f7430
commit
6c60d1c6ed
13 changed files with 439 additions and 99 deletions
|
|
@ -83,19 +83,20 @@ class PersonalizedView extends _$PersonalizedView {
|
|||
final apiSettings = ref.watch(apiSettingsProvider);
|
||||
final user = apiSettings.activeUser;
|
||||
if (apiSettings.activeLibraryId == null) {
|
||||
// set it to default user library by logging in and getting the library id
|
||||
final login =
|
||||
await api.login(username: user!.username!, password: user.password!);
|
||||
// set it to default user library by logging in and getting the library id
|
||||
final login =
|
||||
await api.login(username: user!.username!, password: user.password!);
|
||||
ref.read(apiSettingsProvider.notifier).updateState(
|
||||
apiSettings.copyWith(activeLibraryId: login!.userDefaultLibraryId),
|
||||
);
|
||||
}
|
||||
}
|
||||
// try to find in cache
|
||||
// final cacheKey = 'personalizedView:${apiSettings.activeLibraryId}';
|
||||
var key = 'personalizedView:${apiSettings.activeLibraryId! + user!.id!}';
|
||||
final cachedRes = await apiResponseCacheManager.getFileFromCache(
|
||||
key,
|
||||
);
|
||||
final cachedRes = await apiResponseCacheManager.getFileFromMemory(
|
||||
key,
|
||||
) ??
|
||||
await apiResponseCacheManager.getFileFromCache(key);
|
||||
if (cachedRes != null) {
|
||||
final resJson = jsonDecode(await cachedRes.file.readAsString()) as List;
|
||||
final res = [
|
||||
|
|
@ -107,8 +108,8 @@ class PersonalizedView extends _$PersonalizedView {
|
|||
}
|
||||
|
||||
// ! exagerated delay
|
||||
await Future.delayed(const Duration(seconds: 2));
|
||||
final res = await api.libraries
|
||||
// await Future.delayed(const Duration(seconds: 2));
|
||||
final res = await api.libraries
|
||||
.getPersonalized(libraryId: apiSettings.activeLibraryId!);
|
||||
// debugPrint('personalizedView: ${res!.map((e) => e).toSet()}');
|
||||
// save to cache
|
||||
|
|
|
|||
|
|
@ -348,7 +348,7 @@ final fetchContinueListeningProvider =
|
|||
|
||||
typedef FetchContinueListeningRef
|
||||
= AutoDisposeFutureProviderRef<GetUserSessionsResponse>;
|
||||
String _$personalizedViewHash() => r'52a89c46ce668238ca11b5394fd1d14c910947f5';
|
||||
String _$personalizedViewHash() => r'2e70fe2bfc766a963f7a8e94211ad50d959fbaa2';
|
||||
|
||||
/// fetch the personalized view
|
||||
///
|
||||
|
|
|
|||
|
|
@ -24,7 +24,8 @@ class CoverImage extends _$CoverImage {
|
|||
// await Future.delayed(const Duration(seconds: 2));
|
||||
|
||||
// try to get the image from the cache
|
||||
final file = await imageCacheManager.getFileFromCache(libraryItem.id);
|
||||
final file = await imageCacheManager.getFileFromMemory(libraryItem.id) ??
|
||||
await imageCacheManager.getFileFromCache(libraryItem.id);
|
||||
|
||||
if (file != null) {
|
||||
// if the image is in the cache, yield it
|
||||
|
|
@ -34,6 +35,9 @@ class CoverImage extends _$CoverImage {
|
|||
yield await file.file.readAsBytes();
|
||||
// return if no need to fetch from the server
|
||||
if (libraryItem.updatedAt.isBefore(await file.file.lastModified())) {
|
||||
debugPrint(
|
||||
'cover image is up to date for ${libraryItem.id}, no need to fetch from the server',
|
||||
);
|
||||
return;
|
||||
} else {
|
||||
debugPrint(
|
||||
|
|
@ -45,17 +49,20 @@ class CoverImage extends _$CoverImage {
|
|||
// check if the image is in the cache
|
||||
final coverImage = await api.items.getCover(
|
||||
libraryItemId: libraryItem.id,
|
||||
parameters: const GetImageReqParams(width: 1000),
|
||||
parameters: const GetImageReqParams(width: 1200),
|
||||
);
|
||||
// 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()}',
|
||||
);
|
||||
if (coverImage != null) {
|
||||
final newFile = await imageCacheManager.putFile(
|
||||
libraryItem.id,
|
||||
coverImage,
|
||||
key: libraryItem.id,
|
||||
);
|
||||
debugPrint(
|
||||
'cover image fetched for for ${libraryItem.id}, file time: ${await newFile.lastModified()}',
|
||||
);
|
||||
}
|
||||
|
||||
yield coverImage ?? Uint8List(0);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ part of 'image_provider.dart';
|
|||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$coverImageHash() => r'57a164772b0350cd451535ed9d6347ff74671d2e';
|
||||
String _$coverImageHash() => r'010200735fbe7567ffdaad68bc5a98a475dfda42';
|
||||
|
||||
/// Copied from Dart SDK
|
||||
class _SystemHash {
|
||||
|
|
|
|||
49
lib/api/library_item_provider.dart
Normal file
49
lib/api/library_item_provider.dart
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:shelfsdk/audiobookshelf_api.dart' as shelfsdk;
|
||||
import 'package:whispering_pages/api/api_provider.dart';
|
||||
import 'package:whispering_pages/db/cache/cache_key.dart';
|
||||
import 'package:whispering_pages/db/cache_manager.dart';
|
||||
|
||||
part 'library_item_provider.g.dart';
|
||||
|
||||
/// provides the library item for the given id
|
||||
@riverpod
|
||||
class LibraryItem extends _$LibraryItem {
|
||||
@override
|
||||
Stream<shelfsdk.LibraryItem> build(String id) async* {
|
||||
final api = ref.watch(authenticatedApiProvider);
|
||||
|
||||
debugPrint('fetching library item: $id');
|
||||
|
||||
// ! this is a mock delay
|
||||
// await Future.delayed(const Duration(seconds: 10));
|
||||
|
||||
// look for the item in the cache
|
||||
final key = CacheKey.libraryItem(id);
|
||||
final cachedFile = await apiResponseCacheManager.getFileFromMemory(key) ??
|
||||
await apiResponseCacheManager.getFileFromCache(key);
|
||||
if (cachedFile != null) {
|
||||
debugPrint('reading from cache for $id from ${cachedFile.file}');
|
||||
// read file as json
|
||||
final cachedItem = shelfsdk.LibraryItem.fromJson(
|
||||
jsonDecode(await cachedFile.file.readAsString()),
|
||||
);
|
||||
yield cachedItem;
|
||||
}
|
||||
final item = await api.items.get(libraryItemId: id);
|
||||
if (item != null) {
|
||||
// save to cache
|
||||
final newFile = await apiResponseCacheManager.putFile(
|
||||
key,
|
||||
utf8.encode(jsonEncode(item)),
|
||||
fileExtension: 'json',
|
||||
key: key,
|
||||
);
|
||||
debugPrint('writing to cache: $newFile');
|
||||
yield item;
|
||||
}
|
||||
}
|
||||
}
|
||||
187
lib/api/library_item_provider.g.dart
Normal file
187
lib/api/library_item_provider.g.dart
Normal file
|
|
@ -0,0 +1,187 @@
|
|||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'library_item_provider.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$libraryItemHash() => r'c7919065062e066a0d086508ca6c44187b0bc257';
|
||||
|
||||
/// 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));
|
||||
}
|
||||
}
|
||||
|
||||
abstract class _$LibraryItem
|
||||
extends BuildlessAutoDisposeStreamNotifier<shelfsdk.LibraryItem> {
|
||||
late final String id;
|
||||
|
||||
Stream<shelfsdk.LibraryItem> build(
|
||||
String id,
|
||||
);
|
||||
}
|
||||
|
||||
/// provides the library item for the given id
|
||||
///
|
||||
/// Copied from [LibraryItem].
|
||||
@ProviderFor(LibraryItem)
|
||||
const libraryItemProvider = LibraryItemFamily();
|
||||
|
||||
/// provides the library item for the given id
|
||||
///
|
||||
/// Copied from [LibraryItem].
|
||||
class LibraryItemFamily extends Family<AsyncValue<shelfsdk.LibraryItem>> {
|
||||
/// provides the library item for the given id
|
||||
///
|
||||
/// Copied from [LibraryItem].
|
||||
const LibraryItemFamily();
|
||||
|
||||
/// provides the library item for the given id
|
||||
///
|
||||
/// Copied from [LibraryItem].
|
||||
LibraryItemProvider call(
|
||||
String id,
|
||||
) {
|
||||
return LibraryItemProvider(
|
||||
id,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
LibraryItemProvider getProviderOverride(
|
||||
covariant LibraryItemProvider provider,
|
||||
) {
|
||||
return call(
|
||||
provider.id,
|
||||
);
|
||||
}
|
||||
|
||||
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'libraryItemProvider';
|
||||
}
|
||||
|
||||
/// provides the library item for the given id
|
||||
///
|
||||
/// Copied from [LibraryItem].
|
||||
class LibraryItemProvider extends AutoDisposeStreamNotifierProviderImpl<
|
||||
LibraryItem, shelfsdk.LibraryItem> {
|
||||
/// provides the library item for the given id
|
||||
///
|
||||
/// Copied from [LibraryItem].
|
||||
LibraryItemProvider(
|
||||
String id,
|
||||
) : this._internal(
|
||||
() => LibraryItem()..id = id,
|
||||
from: libraryItemProvider,
|
||||
name: r'libraryItemProvider',
|
||||
debugGetCreateSourceHash:
|
||||
const bool.fromEnvironment('dart.vm.product')
|
||||
? null
|
||||
: _$libraryItemHash,
|
||||
dependencies: LibraryItemFamily._dependencies,
|
||||
allTransitiveDependencies:
|
||||
LibraryItemFamily._allTransitiveDependencies,
|
||||
id: id,
|
||||
);
|
||||
|
||||
LibraryItemProvider._internal(
|
||||
super._createNotifier, {
|
||||
required super.name,
|
||||
required super.dependencies,
|
||||
required super.allTransitiveDependencies,
|
||||
required super.debugGetCreateSourceHash,
|
||||
required super.from,
|
||||
required this.id,
|
||||
}) : super.internal();
|
||||
|
||||
final String id;
|
||||
|
||||
@override
|
||||
Stream<shelfsdk.LibraryItem> runNotifierBuild(
|
||||
covariant LibraryItem notifier,
|
||||
) {
|
||||
return notifier.build(
|
||||
id,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Override overrideWith(LibraryItem Function() create) {
|
||||
return ProviderOverride(
|
||||
origin: this,
|
||||
override: LibraryItemProvider._internal(
|
||||
() => create()..id = id,
|
||||
from: from,
|
||||
name: null,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
debugGetCreateSourceHash: null,
|
||||
id: id,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
AutoDisposeStreamNotifierProviderElement<LibraryItem, shelfsdk.LibraryItem>
|
||||
createElement() {
|
||||
return _LibraryItemProviderElement(this);
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return other is LibraryItemProvider && other.id == id;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
var hash = _SystemHash.combine(0, runtimeType.hashCode);
|
||||
hash = _SystemHash.combine(hash, id.hashCode);
|
||||
|
||||
return _SystemHash.finish(hash);
|
||||
}
|
||||
}
|
||||
|
||||
mixin LibraryItemRef
|
||||
on AutoDisposeStreamNotifierProviderRef<shelfsdk.LibraryItem> {
|
||||
/// The parameter `id` of this provider.
|
||||
String get id;
|
||||
}
|
||||
|
||||
class _LibraryItemProviderElement
|
||||
extends AutoDisposeStreamNotifierProviderElement<LibraryItem,
|
||||
shelfsdk.LibraryItem> with LibraryItemRef {
|
||||
_LibraryItemProviderElement(super.provider);
|
||||
|
||||
@override
|
||||
String get id => (origin as LibraryItemProvider).id;
|
||||
}
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member
|
||||
Loading…
Add table
Add a link
Reference in a new issue