worldhopper/lib/providers/series_navigation_provider.dart

55 lines
1.7 KiB
Dart

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:worldhopper/models/opds_entry.dart';
import 'package:worldhopper/providers/opds_provider.dart';
// Request object for resolving the next entry in a series feed
class NextInSeriesRequest {
final String serverId;
final String feedUrl;
final String currentEntryId;
const NextInSeriesRequest({
required this.serverId,
required this.feedUrl,
required this.currentEntryId,
});
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is NextInSeriesRequest &&
runtimeType == other.runtimeType &&
serverId == other.serverId &&
feedUrl == other.feedUrl &&
currentEntryId == other.currentEntryId;
@override
int get hashCode =>
serverId.hashCode ^ feedUrl.hashCode ^ currentEntryId.hashCode;
}
/// Provider that fetches a feed and returns the next entry after the current one.
/// Returns null if the current entry is the last in the feed or not found.
final nextInSeriesProvider =
FutureProvider.family<OPDSEntry?, NextInSeriesRequest>(
(ref, request) async {
final repository = ref.watch(opdsRepositoryProvider);
final feed = await repository.fetchFeed(
request.serverId,
request.feedUrl,
);
final entries = feed.entries;
for (var i = 0; i < entries.length; i++) {
if (entries[i].id == request.currentEntryId) {
if (i + 1 < entries.length) {
return entries[i + 1];
}
// TODO: If this is the last entry on a paginated page,
// fetch feed.nextLink to find the next entry.
return null;
}
}
return null;
},
);