You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I know this is really poorly structured, but I'm not sure how to cache this "correct"ly.
Code
/// This library provides a service to stream posts in DB to the UI.library;
import'package:fast_immutable_collections/fast_immutable_collections.dart';
import'package:hooks_riverpod/hooks_riverpod.dart';
import'package:riverpod_annotation/riverpod_annotation.dart';
import'../../auth/application/auth_service.dart';
import'../data/post_repository.dart';
import'../domain/feed_entity.dart';
import'../domain/feed_model.dart';
import'../domain/post_entity.dart';
part'feed_demo_service.g.dart';
/// Provide the values of a feed.@Riverpod(keepAlive:true)
baseclassFeedServiceextends_$FeedService {
@overrideFeedModelbuild(FeedEntity feed) {
returnconstFeedModel(ids:IList.empty(), cursorPos:null);
}
/// Fetch the next posts for a given [feed]. /// /// Handles pagination automatically. /// Returns `true` if more were fetched, and false if not.Future<bool> fetchMore() async {
final postRepo = ref.read(postRepositoryProvider);
// Fetch the specific batchfinal fetchedPosts =await postRepo.readPosts(feed, state.cursorPos);
if (fetchedPosts.isEmpty) returnfalse;
// Store each fetched post in the provider without directly calling setPostfinal newPostIds =<PostId>[];
for (final post in fetchedPosts) {
// Store the post in the provider
ref.watch(singlePostProvider(post.id).notifier).setPost(post);
// Collect the post ID
newPostIds.add(post.id);
}
// Update the state with the new batch of post IDs and cursor
state = state.copyWith(
ids: state.ids.addAll(newPostIds),
cursorPos: newPostIds.lastOrNull,
);
returntrue;
}
}
/// Fetches a single post from the feed.////// [postIndex] is the index of the current post the user is viewing.@Riverpod(keepAlive:true)
FutureOr<PostId?> feedPost(Ref ref, FeedEntity feed, int postIndex) async {
// Keep previous posts in cache to make scrolling up possible.// Otherwise, the `ListView` freaks out.if (postIndex !=0) {
await ref.watch(feedPostProvider(feed, postIndex -1).future);
}
final ids = ref.watch(feedServiceProvider(feed).select((s) => s.ids));
var next = ids.elementAtOrNull(postIndex);
var moreToGet =true;
while (moreToGet && next ==null) {
moreToGet =await ref.watch(feedServiceProvider(feed).notifier).fetchMore();
next = ids.elementAtOrNull(postIndex);
}
return next;
}
/// Store a post independently of any feed for memory efficiency.////// [id] is the unique identifier for the post.@Riverpod(keepAlive:true)
baseclassSinglePostextends_$SinglePost {
@overridePostEntity?build(PostId id) {
// Initially, the post is null. It will be set when fetched.returnnull;
}
/// Set the post data.// TODO(lishaduck): Be a good person.// ignore: use_setters_to_change_propertiesvoidsetPost(PostEntity post) {
state = post;
}
}
In my database, I've got filtered lists of posts that I fetch. Users can view multiple feeds at once and I'd like to keep them cached.
I don't know a better way to pass values for Riverpod to cache without setting those values as the key of the family, which isn't what I want here.
Is there a less hacky way to do this?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I know this is really poorly structured, but I'm not sure how to cache this "correct"ly.
Code
In my database, I've got filtered lists of posts that I fetch. Users can view multiple feeds at once and I'd like to keep them cached.
I don't know a better way to pass values for Riverpod to cache without setting those values as the key of the family, which isn't what I want here.
Is there a less hacky way to do this?
Beta Was this translation helpful? Give feedback.
All reactions