feat: ability to change library (#77)

* feat: add AbsIcons font and update pubspec.yaml for font integration

* feat: implement library selection in YouPage

* fix: optimize authenticatedApi provider to not rebuild unnecessarily

* feat: add LibrarySwitchChip widget and integrate it into YouPage and ScaffoldWithNavBar

* feat: enhance library selection UI with refresh functionality and error handling

* fix: change library switcher activation from long press to double tap

* feat: show current library on nav bar

* feat: refactor LibraryBrowserPage to use CustomScrollView and enhance app bar with dynamic library icon and title
This commit is contained in:
Dr.Blank 2025-04-19 19:17:31 +05:30 committed by GitHub
parent 37c44f1c6b
commit 5986482baf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 758 additions and 83 deletions

View file

@ -49,8 +49,7 @@ AudiobookshelfApi audiobookshelfApi(Ref ref, Uri? baseUrl) {
/// if the user is not authenticated throw an error
@Riverpod(keepAlive: true)
AudiobookshelfApi authenticatedApi(Ref ref) {
final apiSettings = ref.watch(apiSettingsProvider);
final user = apiSettings.activeUser;
final user = ref.watch(apiSettingsProvider.select((s) => s.activeUser));
if (user == null) {
_logger.severe('No active user can not provide authenticated api');
throw StateError('No active user');

View file

@ -170,7 +170,7 @@ class _AudiobookshelfApiProviderElement
Uri? get baseUrl => (origin as AudiobookshelfApiProvider).baseUrl;
}
String _$authenticatedApiHash() => r'5cf3329fe3074e3a09e266b4bae78b53e9c01220';
String _$authenticatedApiHash() => r'284be2c39823c20fb70035a136c430862c28fa27';
/// get the api instance for the authenticated user
///

View file

@ -0,0 +1,58 @@
import 'package:hooks_riverpod/hooks_riverpod.dart' show Ref;
import 'package:logging/logging.dart' show Logger;
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:shelfsdk/audiobookshelf_api.dart' show Library;
import 'package:vaani/api/api_provider.dart' show authenticatedApiProvider;
import 'package:vaani/settings/api_settings_provider.dart'
show apiSettingsProvider;
part 'library_provider.g.dart';
final _logger = Logger('LibraryProvider');
@riverpod
Future<Library?> library(Ref ref, String id) async {
final api = ref.watch(authenticatedApiProvider);
final library = await api.libraries.get(libraryId: id);
if (library == null) {
_logger.warning('No library found through id: $id');
// try to get the library from the list of libraries
final libraries = await ref.watch(librariesProvider.future);
for (final lib in libraries) {
if (lib.id == id) {
return lib;
}
}
_logger.warning('No library found in the list of libraries');
return null;
}
_logger.fine('Fetched library: ${library}');
return library.library;
}
@riverpod
Future<Library?> currentLibrary(Ref ref) async {
final libraryId =
ref.watch(apiSettingsProvider.select((s) => s.activeLibraryId));
if (libraryId == null) {
_logger.warning('No active library id found');
return null;
}
return await ref.watch(libraryProvider(libraryId).future);
}
@riverpod
class Libraries extends _$Libraries {
@override
FutureOr<List<Library>> build() async {
final api = ref.watch(authenticatedApiProvider);
final libraries = await api.libraries.getAll();
if (libraries == null) {
_logger.warning('Failed to fetch libraries');
return [];
}
_logger.fine('Fetched ${libraries.length} libraries');
ref.keepAlive();
return libraries;
}
}

View file

@ -0,0 +1,192 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'library_provider.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
String _$libraryHash() => r'b62d976f8ab83b2f5823a0fb7dac52fde8fcbffc';
/// 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));
}
}
/// See also [library].
@ProviderFor(library)
const libraryProvider = LibraryFamily();
/// See also [library].
class LibraryFamily extends Family<AsyncValue<Library?>> {
/// See also [library].
const LibraryFamily();
/// See also [library].
LibraryProvider call(
String id,
) {
return LibraryProvider(
id,
);
}
@override
LibraryProvider getProviderOverride(
covariant LibraryProvider 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'libraryProvider';
}
/// See also [library].
class LibraryProvider extends AutoDisposeFutureProvider<Library?> {
/// See also [library].
LibraryProvider(
String id,
) : this._internal(
(ref) => library(
ref as LibraryRef,
id,
),
from: libraryProvider,
name: r'libraryProvider',
debugGetCreateSourceHash:
const bool.fromEnvironment('dart.vm.product')
? null
: _$libraryHash,
dependencies: LibraryFamily._dependencies,
allTransitiveDependencies: LibraryFamily._allTransitiveDependencies,
id: id,
);
LibraryProvider._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
Override overrideWith(
FutureOr<Library?> Function(LibraryRef provider) create,
) {
return ProviderOverride(
origin: this,
override: LibraryProvider._internal(
(ref) => create(ref as LibraryRef),
from: from,
name: null,
dependencies: null,
allTransitiveDependencies: null,
debugGetCreateSourceHash: null,
id: id,
),
);
}
@override
AutoDisposeFutureProviderElement<Library?> createElement() {
return _LibraryProviderElement(this);
}
@override
bool operator ==(Object other) {
return other is LibraryProvider && other.id == id;
}
@override
int get hashCode {
var hash = _SystemHash.combine(0, runtimeType.hashCode);
hash = _SystemHash.combine(hash, id.hashCode);
return _SystemHash.finish(hash);
}
}
@Deprecated('Will be removed in 3.0. Use Ref instead')
// ignore: unused_element
mixin LibraryRef on AutoDisposeFutureProviderRef<Library?> {
/// The parameter `id` of this provider.
String get id;
}
class _LibraryProviderElement extends AutoDisposeFutureProviderElement<Library?>
with LibraryRef {
_LibraryProviderElement(super.provider);
@override
String get id => (origin as LibraryProvider).id;
}
String _$currentLibraryHash() => r'658498a531e04a01e2b3915a3319101285601118';
/// See also [currentLibrary].
@ProviderFor(currentLibrary)
final currentLibraryProvider = AutoDisposeFutureProvider<Library?>.internal(
currentLibrary,
name: r'currentLibraryProvider',
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
? null
: _$currentLibraryHash,
dependencies: null,
allTransitiveDependencies: null,
);
@Deprecated('Will be removed in 3.0. Use Ref instead')
// ignore: unused_element
typedef CurrentLibraryRef = AutoDisposeFutureProviderRef<Library?>;
String _$librariesHash() => r'95ebd4d1ac0cc2acf7617dc22895eff0ca30600f';
/// See also [Libraries].
@ProviderFor(Libraries)
final librariesProvider =
AutoDisposeAsyncNotifierProvider<Libraries, List<Library>>.internal(
Libraries.new,
name: r'librariesProvider',
debugGetCreateSourceHash:
const bool.fromEnvironment('dart.vm.product') ? null : _$librariesHash,
dependencies: null,
allTransitiveDependencies: null,
);
typedef _$Libraries = AutoDisposeAsyncNotifier<List<Library>>;
// ignore_for_file: type=lint
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member, deprecated_member_use_from_same_package