mirror of
https://github.com/Iconica-Development/flutter_timeline.git
synced 2025-05-19 10:33:44 +02:00
feat: add more methods to the timeline interface
This commit is contained in:
parent
cf129c05c0
commit
06df2b7649
2 changed files with 83 additions and 0 deletions
|
@ -122,6 +122,50 @@ class FirebaseTimelineService with ChangeNotifier implements TimelineService {
|
||||||
return posts;
|
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
|
@override
|
||||||
Future<TimelinePost> fetchPost(TimelinePost post) async {
|
Future<TimelinePost> fetchPost(TimelinePost post) async {
|
||||||
var doc = await _db
|
var doc = await _db
|
||||||
|
@ -139,6 +183,43 @@ class FirebaseTimelineService with ChangeNotifier implements TimelineService {
|
||||||
return updatedPost;
|
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
|
@override
|
||||||
TimelinePost? getPost(String postId) =>
|
TimelinePost? getPost(String postId) =>
|
||||||
(_posts.any((element) => element.id == postId))
|
(_posts.any((element) => element.id == postId))
|
||||||
|
|
|
@ -14,8 +14,10 @@ abstract class TimelineService with ChangeNotifier {
|
||||||
Future<TimelinePost> createPost(TimelinePost post);
|
Future<TimelinePost> createPost(TimelinePost post);
|
||||||
Future<List<TimelinePost>> fetchPosts(String? category);
|
Future<List<TimelinePost>> fetchPosts(String? category);
|
||||||
Future<TimelinePost> fetchPost(TimelinePost post);
|
Future<TimelinePost> fetchPost(TimelinePost post);
|
||||||
|
Future<List<TimelinePost>> fetchPostsPaginated(String? category, int limit);
|
||||||
TimelinePost? getPost(String postId);
|
TimelinePost? getPost(String postId);
|
||||||
List<TimelinePost> getPosts(String? category);
|
List<TimelinePost> getPosts(String? category);
|
||||||
|
Future<List<TimelinePost>> refreshPosts(String? category);
|
||||||
Future<TimelinePost> fetchPostDetails(TimelinePost post);
|
Future<TimelinePost> fetchPostDetails(TimelinePost post);
|
||||||
Future<TimelinePost> reactToPost(
|
Future<TimelinePost> reactToPost(
|
||||||
TimelinePost post,
|
TimelinePost post,
|
||||||
|
|
Loading…
Reference in a new issue