mirror of
https://github.com/Dr-Blank/Vaani.git
synced 2025-12-10 21:19:29 +00:00
feat: show current library on nav bar
This commit is contained in:
parent
7edd21ab01
commit
98e8ffb021
3 changed files with 98 additions and 49 deletions
|
|
@ -4,12 +4,14 @@ 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?> currentLibrary(Ref ref, String id) async {
|
||||
Future<Library?> library(Ref ref, String id) async {
|
||||
final api = ref.watch(authenticatedApiProvider);
|
||||
final library = await api.libraries.get(libraryId: id);
|
||||
if (library == null) {
|
||||
|
|
@ -28,6 +30,17 @@ Future<Library?> currentLibrary(Ref ref, String id) async {
|
|||
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
|
||||
|
|
@ -39,6 +52,7 @@ class Libraries extends _$Libraries {
|
|||
return [];
|
||||
}
|
||||
_logger.fine('Fetched ${libraries.length} libraries');
|
||||
ref.keepAlive();
|
||||
return libraries;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ part of 'library_provider.dart';
|
|||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$currentLibraryHash() => r'1b41abb16566d91cd5961973e45bccaad7c49c9a';
|
||||
String _$libraryHash() => r'b62d976f8ab83b2f5823a0fb7dac52fde8fcbffc';
|
||||
|
||||
/// Copied from Dart SDK
|
||||
class _SystemHash {
|
||||
|
|
@ -29,27 +29,27 @@ class _SystemHash {
|
|||
}
|
||||
}
|
||||
|
||||
/// See also [currentLibrary].
|
||||
@ProviderFor(currentLibrary)
|
||||
const currentLibraryProvider = CurrentLibraryFamily();
|
||||
/// See also [library].
|
||||
@ProviderFor(library)
|
||||
const libraryProvider = LibraryFamily();
|
||||
|
||||
/// See also [currentLibrary].
|
||||
class CurrentLibraryFamily extends Family<AsyncValue<Library?>> {
|
||||
/// See also [currentLibrary].
|
||||
const CurrentLibraryFamily();
|
||||
/// See also [library].
|
||||
class LibraryFamily extends Family<AsyncValue<Library?>> {
|
||||
/// See also [library].
|
||||
const LibraryFamily();
|
||||
|
||||
/// See also [currentLibrary].
|
||||
CurrentLibraryProvider call(
|
||||
/// See also [library].
|
||||
LibraryProvider call(
|
||||
String id,
|
||||
) {
|
||||
return CurrentLibraryProvider(
|
||||
return LibraryProvider(
|
||||
id,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
CurrentLibraryProvider getProviderOverride(
|
||||
covariant CurrentLibraryProvider provider,
|
||||
LibraryProvider getProviderOverride(
|
||||
covariant LibraryProvider provider,
|
||||
) {
|
||||
return call(
|
||||
provider.id,
|
||||
|
|
@ -68,32 +68,31 @@ class CurrentLibraryFamily extends Family<AsyncValue<Library?>> {
|
|||
_allTransitiveDependencies;
|
||||
|
||||
@override
|
||||
String? get name => r'currentLibraryProvider';
|
||||
String? get name => r'libraryProvider';
|
||||
}
|
||||
|
||||
/// See also [currentLibrary].
|
||||
class CurrentLibraryProvider extends AutoDisposeFutureProvider<Library?> {
|
||||
/// See also [currentLibrary].
|
||||
CurrentLibraryProvider(
|
||||
/// See also [library].
|
||||
class LibraryProvider extends AutoDisposeFutureProvider<Library?> {
|
||||
/// See also [library].
|
||||
LibraryProvider(
|
||||
String id,
|
||||
) : this._internal(
|
||||
(ref) => currentLibrary(
|
||||
ref as CurrentLibraryRef,
|
||||
(ref) => library(
|
||||
ref as LibraryRef,
|
||||
id,
|
||||
),
|
||||
from: currentLibraryProvider,
|
||||
name: r'currentLibraryProvider',
|
||||
from: libraryProvider,
|
||||
name: r'libraryProvider',
|
||||
debugGetCreateSourceHash:
|
||||
const bool.fromEnvironment('dart.vm.product')
|
||||
? null
|
||||
: _$currentLibraryHash,
|
||||
dependencies: CurrentLibraryFamily._dependencies,
|
||||
allTransitiveDependencies:
|
||||
CurrentLibraryFamily._allTransitiveDependencies,
|
||||
: _$libraryHash,
|
||||
dependencies: LibraryFamily._dependencies,
|
||||
allTransitiveDependencies: LibraryFamily._allTransitiveDependencies,
|
||||
id: id,
|
||||
);
|
||||
|
||||
CurrentLibraryProvider._internal(
|
||||
LibraryProvider._internal(
|
||||
super._createNotifier, {
|
||||
required super.name,
|
||||
required super.dependencies,
|
||||
|
|
@ -107,12 +106,12 @@ class CurrentLibraryProvider extends AutoDisposeFutureProvider<Library?> {
|
|||
|
||||
@override
|
||||
Override overrideWith(
|
||||
FutureOr<Library?> Function(CurrentLibraryRef provider) create,
|
||||
FutureOr<Library?> Function(LibraryRef provider) create,
|
||||
) {
|
||||
return ProviderOverride(
|
||||
origin: this,
|
||||
override: CurrentLibraryProvider._internal(
|
||||
(ref) => create(ref as CurrentLibraryRef),
|
||||
override: LibraryProvider._internal(
|
||||
(ref) => create(ref as LibraryRef),
|
||||
from: from,
|
||||
name: null,
|
||||
dependencies: null,
|
||||
|
|
@ -125,12 +124,12 @@ class CurrentLibraryProvider extends AutoDisposeFutureProvider<Library?> {
|
|||
|
||||
@override
|
||||
AutoDisposeFutureProviderElement<Library?> createElement() {
|
||||
return _CurrentLibraryProviderElement(this);
|
||||
return _LibraryProviderElement(this);
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return other is CurrentLibraryProvider && other.id == id;
|
||||
return other is LibraryProvider && other.id == id;
|
||||
}
|
||||
|
||||
@override
|
||||
|
|
@ -144,20 +143,37 @@ class CurrentLibraryProvider extends AutoDisposeFutureProvider<Library?> {
|
|||
|
||||
@Deprecated('Will be removed in 3.0. Use Ref instead')
|
||||
// ignore: unused_element
|
||||
mixin CurrentLibraryRef on AutoDisposeFutureProviderRef<Library?> {
|
||||
mixin LibraryRef on AutoDisposeFutureProviderRef<Library?> {
|
||||
/// The parameter `id` of this provider.
|
||||
String get id;
|
||||
}
|
||||
|
||||
class _CurrentLibraryProviderElement
|
||||
extends AutoDisposeFutureProviderElement<Library?> with CurrentLibraryRef {
|
||||
_CurrentLibraryProviderElement(super.provider);
|
||||
class _LibraryProviderElement extends AutoDisposeFutureProviderElement<Library?>
|
||||
with LibraryRef {
|
||||
_LibraryProviderElement(super.provider);
|
||||
|
||||
@override
|
||||
String get id => (origin as CurrentLibraryProvider).id;
|
||||
String get id => (origin as LibraryProvider).id;
|
||||
}
|
||||
|
||||
String _$librariesHash() => r'a57828f3b875d56db6c5815d051eca93695aefe2';
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
|||
import 'package:go_router/go_router.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:miniplayer/miniplayer.dart';
|
||||
import 'package:vaani/api/library_provider.dart' show currentLibraryProvider;
|
||||
import 'package:vaani/features/explore/providers/search_controller.dart';
|
||||
import 'package:vaani/features/player/providers/player_form.dart';
|
||||
import 'package:vaani/features/player/view/audiobook_player.dart';
|
||||
|
|
@ -9,7 +10,7 @@ import 'package:vaani/features/player/view/player_when_expanded.dart';
|
|||
import 'package:vaani/features/you/view/widgets/library_switch_chip.dart';
|
||||
import 'package:vaani/main.dart';
|
||||
import 'package:vaani/router/router.dart';
|
||||
import 'package:vaani/shared/widgets/not_implemented.dart';
|
||||
import 'package:vaani/shared/icons/abs_icons.dart' show AbsIcons;
|
||||
|
||||
// stack to track changes in navigationShell.currentIndex
|
||||
// home is always at index 0 and at the start and should be the last before popping
|
||||
|
|
@ -114,14 +115,27 @@ class ScaffoldWithNavBar extends HookConsumerWidget {
|
|||
// branches of the shell route, which can be fetched using
|
||||
// `navigationShell.route.branches`.
|
||||
destinations: _navigationItems.map((item) {
|
||||
final destinationWidget = NavigationDestination(
|
||||
icon: Icon(item.icon),
|
||||
selectedIcon: item.activeIcon != null
|
||||
? Icon(item.activeIcon)
|
||||
: Icon(item.icon),
|
||||
label: item.name,
|
||||
final isDestinationLibrary = item.name == 'Library';
|
||||
var currentLibrary =
|
||||
ref.watch(currentLibraryProvider).valueOrNull;
|
||||
final libraryIcon = AbsIcons.getIconByName(
|
||||
currentLibrary?.icon,
|
||||
);
|
||||
if (item.name == 'Library') {
|
||||
final destinationWidget = NavigationDestination(
|
||||
icon: Icon(
|
||||
isDestinationLibrary ? libraryIcon ?? item.icon : item.icon,
|
||||
),
|
||||
selectedIcon: Icon(
|
||||
isDestinationLibrary
|
||||
? libraryIcon ?? item.activeIcon
|
||||
: item.activeIcon,
|
||||
),
|
||||
label: isDestinationLibrary
|
||||
? currentLibrary?.name ?? item.name
|
||||
: item.name,
|
||||
tooltip: item.tooltip,
|
||||
);
|
||||
if (isDestinationLibrary) {
|
||||
return GestureDetector(
|
||||
onSecondaryTap: () => showLibrarySwitcher(context, ref),
|
||||
onDoubleTap: () => showLibrarySwitcher(context, ref),
|
||||
|
|
@ -202,16 +216,19 @@ const _navigationItems = [
|
|||
name: 'Library',
|
||||
icon: Icons.book_outlined,
|
||||
activeIcon: Icons.book,
|
||||
tooltip: 'Browse your library',
|
||||
),
|
||||
_NavigationItem(
|
||||
name: 'Explore',
|
||||
icon: Icons.search_outlined,
|
||||
activeIcon: Icons.search,
|
||||
tooltip: 'Search and Explore',
|
||||
),
|
||||
_NavigationItem(
|
||||
name: 'You',
|
||||
icon: Icons.account_circle_outlined,
|
||||
activeIcon: Icons.account_circle,
|
||||
tooltip: 'Your Profile and Settings',
|
||||
),
|
||||
];
|
||||
|
||||
|
|
@ -219,10 +236,12 @@ class _NavigationItem {
|
|||
const _NavigationItem({
|
||||
required this.name,
|
||||
required this.icon,
|
||||
this.activeIcon,
|
||||
required this.activeIcon,
|
||||
this.tooltip,
|
||||
});
|
||||
|
||||
final String name;
|
||||
final IconData icon;
|
||||
final IconData? activeIcon;
|
||||
final IconData activeIcon;
|
||||
final String? tooltip;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue