Rename all OPDS-prefixed models to generic names (Server, Feed, Entry, Link, StreamLink), expand the ServerSoftware interface to cover all server interactions, and implement a full Kavita REST API client that replaces the OPDS delegation. - Rename OPDS* models to generic names across ~60 files - Add database migrations 13 (opds_id → entry_id) and 14 (unify credentials) - Create OPDSServerSoftware wrapping existing OPDS services - Create KavitaApiClient for direct Kavita REST API calls - Create KavitaFeedMapper to convert Kavita JSON to Feed/Entry models - Rewrite KavitaServerSoftware to use native API (no OPDS delegation) - Unify server credentials (remove softwareUsername/softwarePassword) - Simplify add/edit server UI to single auth section - Add test connection button to server add/edit screens - Add progress indicator to PublicationCard using local and server data - Eliminate softwareType branching in reader screens - Add EntryProgress and fetchEntryProgress to ServerSoftware interface - Fix Entry.acquisitionLink crash on empty links - Add 59 new tests covering models, services, and providers
349 lines
13 KiB
Dart
349 lines
13 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:worldhopper/l10n/app_localizations.dart';
|
|
import 'package:worldhopper/config/constants.dart';
|
|
import 'package:worldhopper/helpers/responsive_helper.dart';
|
|
import 'package:worldhopper/helpers/snackbar_helper.dart';
|
|
import 'package:worldhopper/models/feed.dart';
|
|
import 'package:worldhopper/models/entry.dart';
|
|
import 'package:worldhopper/providers/feed_provider.dart';
|
|
import 'package:worldhopper/providers/server_provider.dart';
|
|
import 'package:worldhopper/providers/connectivity_provider.dart';
|
|
import 'package:worldhopper/services/url_helper.dart';
|
|
import 'package:worldhopper/widgets/publication_card.dart';
|
|
import 'package:worldhopper/widgets/worldhopper_app_bar.dart';
|
|
|
|
/// Screen for browsing an OPDS server's root feed
|
|
class LibraryBrowserScreen extends ConsumerStatefulWidget {
|
|
final String serverId;
|
|
|
|
const LibraryBrowserScreen({
|
|
super.key,
|
|
required this.serverId,
|
|
});
|
|
|
|
@override
|
|
ConsumerState<LibraryBrowserScreen> createState() =>
|
|
_LibraryBrowserScreenState();
|
|
}
|
|
|
|
class _LibraryBrowserScreenState extends ConsumerState<LibraryBrowserScreen> {
|
|
List<Entry> _allEntries = [];
|
|
String? _nextPageUrl;
|
|
bool _isLoadingMore = false;
|
|
bool _initialized = false;
|
|
final ScrollController _scrollController = ScrollController();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_scrollController.addListener(_onScroll);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_scrollController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
void _onScroll() {
|
|
if (_isLoadingMore || _nextPageUrl == null) return;
|
|
|
|
// Load more when user scrolls to 80% of the content
|
|
final maxScroll = _scrollController.position.maxScrollExtent;
|
|
final currentScroll = _scrollController.position.pixels;
|
|
final threshold = maxScroll * 0.8;
|
|
|
|
if (currentScroll >= threshold) {
|
|
_loadMoreEntries();
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final l10n = AppLocalizations.of(context);
|
|
final serverAsync = ref.watch(serverProvider(widget.serverId));
|
|
final feedRequest = RootFeedRequest(serverId: widget.serverId);
|
|
final feedAsync = ref.watch(rootFeedProvider(feedRequest));
|
|
|
|
return Scaffold(
|
|
appBar: WorldhopperAppBar(
|
|
title: serverAsync.when(
|
|
data: (server) => Text(server?.name ?? l10n.browserLibrary),
|
|
loading: () => Text(l10n.browserLoading),
|
|
error: (_, __) => Text(l10n.browserLibrary),
|
|
),
|
|
),
|
|
body: feedAsync.when(
|
|
data: (feed) => _buildFeedContent(context, ref, feed),
|
|
loading: () => const Center(child: CircularProgressIndicator()),
|
|
error: (error, stack) => _buildErrorState(context, ref, error),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildFeedContent(BuildContext context, WidgetRef ref, Feed feed) {
|
|
final l10n = AppLocalizations.of(context);
|
|
|
|
// Initialize entries only once when first loaded
|
|
if (!_initialized) {
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (mounted && !_initialized) {
|
|
setState(() {
|
|
_allEntries = List.from(feed.entries);
|
|
_nextPageUrl = feed.nextLink?.href;
|
|
_initialized = true;
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
return RefreshIndicator(
|
|
onRefresh: () async {
|
|
// Clear the repository cache for this server's root feed
|
|
final repository = ref.read(feedRepositoryProvider);
|
|
final server = await ref.read(serverProvider(widget.serverId).future);
|
|
if (server != null) {
|
|
final resolvedUrl = UrlHelper.resolveUrl(server.url, server.url);
|
|
repository.clearFeedCache(widget.serverId, resolvedUrl);
|
|
}
|
|
// Reset pagination state
|
|
setState(() {
|
|
_allEntries = [];
|
|
_nextPageUrl = null;
|
|
_initialized = false;
|
|
});
|
|
// Invalidate the provider to force refetch
|
|
final refreshRequest = RootFeedRequest(serverId: widget.serverId);
|
|
ref.invalidate(rootFeedProvider(refreshRequest));
|
|
// Wait for the new data to load
|
|
await ref.read(rootFeedProvider(refreshRequest).future);
|
|
},
|
|
child: _allEntries.isEmpty
|
|
? _buildEmptyState(context, l10n)
|
|
: LayoutBuilder(
|
|
builder: (context, constraints) {
|
|
return SingleChildScrollView(
|
|
physics: const AlwaysScrollableScrollPhysics(),
|
|
controller: _scrollController,
|
|
child: ConstrainedBox(
|
|
constraints: BoxConstraints(
|
|
minHeight: constraints.maxHeight,
|
|
),
|
|
child: GridView.builder(
|
|
shrinkWrap: true,
|
|
physics: const NeverScrollableScrollPhysics(),
|
|
padding: const EdgeInsets.all(16),
|
|
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
|
|
crossAxisCount: gridColumnCount(constraints.maxWidth),
|
|
childAspectRatio: 0.65,
|
|
crossAxisSpacing: 12,
|
|
mainAxisSpacing: 12,
|
|
),
|
|
itemCount: _allEntries.length + (_isLoadingMore ? 1 : 0),
|
|
itemBuilder: (context, index) {
|
|
// Show loading indicator at the end
|
|
if (index == _allEntries.length) {
|
|
return Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: CircularProgressIndicator(
|
|
color: Theme.of(context).colorScheme.primary,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
final entry = _allEntries[index];
|
|
return PublicationCard(
|
|
entry: entry,
|
|
serverId: widget.serverId,
|
|
onTap: () async {
|
|
if (entry.isNavigation) {
|
|
// Navigate to subsection feed
|
|
final subsectionLink = entry.links.firstWhere(
|
|
(link) =>
|
|
link.rel == 'subsection' ||
|
|
link.rel.contains('navigation') ||
|
|
link.rel == 'self',
|
|
orElse: () => entry.links.first,
|
|
);
|
|
|
|
context.push(
|
|
'/browse/${widget.serverId}/feed',
|
|
extra: {
|
|
'feedUrl': subsectionLink.href,
|
|
'feedTitle': entry.title,
|
|
},
|
|
);
|
|
} else {
|
|
// Check connectivity before navigating to publication detail
|
|
final connectivityState =
|
|
ref.read(connectivityStateProvider);
|
|
final isOnline = connectivityState.whenOrNull(
|
|
data: (online) => online,
|
|
) ??
|
|
true;
|
|
|
|
// If offline, check content availability and show warning
|
|
if (!isOnline) {
|
|
final checker =
|
|
ref.read(offlineContentCheckerProvider);
|
|
final isAvailable = await checker
|
|
.isContentAvailableOffline(entry);
|
|
|
|
if (!isAvailable) {
|
|
if (entry.isEpub) {
|
|
// Warn for uncached EPUBs
|
|
if (!context.mounted) return;
|
|
context.showInfoSnackBar(
|
|
l10n.browserOfflineEpubNotCached,
|
|
duration: const Duration(seconds: 3),
|
|
);
|
|
} else if (entry.hasStreamLink) {
|
|
// Warn for OPDS-PS streams
|
|
if (!context.mounted) return;
|
|
context.showInfoSnackBar(
|
|
l10n.browserOfflinePagesWarning,
|
|
duration: const Duration(seconds: 3),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Navigate to publication detail
|
|
if (!context.mounted) return;
|
|
context.push(
|
|
AppConstants.routePublicationDetail,
|
|
extra: {
|
|
'entry': entry,
|
|
'serverId': widget.serverId,
|
|
},
|
|
);
|
|
}
|
|
},
|
|
);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> _loadMoreEntries() async {
|
|
if (_isLoadingMore || _nextPageUrl == null) return;
|
|
|
|
setState(() {
|
|
_isLoadingMore = true;
|
|
});
|
|
|
|
try {
|
|
final repository = ref.read(feedRepositoryProvider);
|
|
final nextFeed = await repository.fetchFeed(
|
|
widget.serverId,
|
|
_nextPageUrl!,
|
|
);
|
|
|
|
setState(() {
|
|
_allEntries.addAll(nextFeed.entries);
|
|
_nextPageUrl = nextFeed.nextLink?.href;
|
|
_isLoadingMore = false;
|
|
});
|
|
} catch (e) {
|
|
setState(() {
|
|
_isLoadingMore = false;
|
|
});
|
|
|
|
if (mounted) {
|
|
final l10n = AppLocalizations.of(context);
|
|
context.showErrorSnackBar(l10n.browserFailedToLoadMore(e.toString()));
|
|
}
|
|
}
|
|
}
|
|
|
|
Widget _buildEmptyState(BuildContext context, AppLocalizations l10n) {
|
|
return LayoutBuilder(
|
|
builder: (context, constraints) {
|
|
return SingleChildScrollView(
|
|
physics: const AlwaysScrollableScrollPhysics(),
|
|
child: ConstrainedBox(
|
|
constraints: BoxConstraints(
|
|
minHeight: constraints.maxHeight,
|
|
),
|
|
child: Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Icon(
|
|
Icons.library_books_outlined,
|
|
size: 80,
|
|
color: Theme.of(context)
|
|
.colorScheme
|
|
.secondary
|
|
.withValues(alpha: 0.5),
|
|
),
|
|
const SizedBox(height: 24),
|
|
Text(
|
|
l10n.browserNoContent,
|
|
style: Theme.of(context).textTheme.headlineSmall,
|
|
),
|
|
const SizedBox(height: 8),
|
|
Text(
|
|
l10n.browserLibraryEmpty,
|
|
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
|
color: Theme.of(context)
|
|
.colorScheme
|
|
.onSurface
|
|
.withValues(alpha: 0.7),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
Widget _buildErrorState(BuildContext context, WidgetRef ref, Object error) {
|
|
final l10n = AppLocalizations.of(context);
|
|
|
|
return Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(32),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
const Icon(Icons.error_outline, size: 64, color: Colors.red),
|
|
const SizedBox(height: 24),
|
|
Text(
|
|
l10n.browserFailedToLoadLibrary,
|
|
style: Theme.of(context).textTheme.titleLarge,
|
|
textAlign: TextAlign.center,
|
|
),
|
|
const SizedBox(height: 12),
|
|
Text(
|
|
error.toString(),
|
|
style: Theme.of(context).textTheme.bodyMedium,
|
|
textAlign: TextAlign.center,
|
|
),
|
|
const SizedBox(height: 24),
|
|
ElevatedButton.icon(
|
|
onPressed: () {
|
|
final refreshRequest =
|
|
RootFeedRequest(serverId: widget.serverId);
|
|
ref.invalidate(rootFeedProvider(refreshRequest));
|
|
},
|
|
icon: const Icon(Icons.refresh),
|
|
label: Text(l10n.browserRetry),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|