feat: add more methods to the timeline interface

This commit is contained in:
Freek van de Ven 2023-12-07 06:03:02 +01:00
parent cf129c05c0
commit 06df2b7649
2 changed files with 83 additions and 0 deletions

View file

@ -122,6 +122,50 @@ class FirebaseTimelineService with ChangeNotifier implements TimelineService {
return posts;
}
@override
Future<List<TimelinePost>> fetchPostsPaginated(
String? category,
int limit,
) async {
// only take posts that are in our category
var oldestPost = _posts
.where(
(element) => category == null || element.category == category,
)
.fold(
_posts.first,
(previousValue, element) =>
(previousValue.createdAt.isBefore(element.createdAt))
? previousValue
: element,
);
var snapshot = (category != null)
? await _db
.collection(_options.timelineCollectionName)
.where('category', isEqualTo: category)
.orderBy('created_at', descending: true)
.startAfter([oldestPost])
.limit(limit)
.get()
: await _db
.collection(_options.timelineCollectionName)
.orderBy('created_at', descending: true)
.startAfter([oldestPost.createdAt])
.limit(limit)
.get();
// add the new posts to the list
var posts = <TimelinePost>[];
for (var doc in snapshot.docs) {
var data = doc.data();
var user = await _userService.getUser(data['creator_id']);
var post = TimelinePost.fromJson(doc.id, data).copyWith(creator: user);
posts.add(post);
}
_posts = [..._posts, ...posts];
notifyListeners();
return posts;
}
@override
Future<TimelinePost> fetchPost(TimelinePost post) async {
var doc = await _db
@ -139,6 +183,43 @@ class FirebaseTimelineService with ChangeNotifier implements TimelineService {
return updatedPost;
}
@override
Future<List<TimelinePost>> refreshPosts(String? category) async {
// fetch all posts between now and the newest posts we have
var newestPostWeHave = _posts
.where(
(element) => category == null || element.category == category,
)
.fold(
_posts.first,
(previousValue, element) =>
(previousValue.createdAt.isAfter(element.createdAt))
? previousValue
: element,
);
var snapshot = (category != null)
? await _db
.collection(_options.timelineCollectionName)
.where('category', isEqualTo: category)
.orderBy('created_at', descending: true)
.endBefore([newestPostWeHave.createdAt]).get()
: await _db
.collection(_options.timelineCollectionName)
.orderBy('created_at', descending: true)
.endBefore([newestPostWeHave.createdAt]).get();
// add the new posts to the list
var posts = <TimelinePost>[];
for (var doc in snapshot.docs) {
var data = doc.data();
var user = await _userService.getUser(data['creator_id']);
var post = TimelinePost.fromJson(doc.id, data).copyWith(creator: user);
posts.add(post);
}
_posts = [...posts, ..._posts];
notifyListeners();
return posts;
}
@override
TimelinePost? getPost(String postId) =>
(_posts.any((element) => element.id == postId))

View file

@ -14,8 +14,10 @@ abstract class TimelineService with ChangeNotifier {
Future<TimelinePost> createPost(TimelinePost post);
Future<List<TimelinePost>> fetchPosts(String? category);
Future<TimelinePost> fetchPost(TimelinePost post);
Future<List<TimelinePost>> fetchPostsPaginated(String? category, int limit);
TimelinePost? getPost(String postId);
List<TimelinePost> getPosts(String? category);
Future<List<TimelinePost>> refreshPosts(String? category);
Future<TimelinePost> fetchPostDetails(TimelinePost post);
Future<TimelinePost> reactToPost(
TimelinePost post,