feat: add refreshindicator for postdetail

This commit is contained in:
Freek van de Ven 2023-11-21 23:04:56 +01:00
parent 753ecc039e
commit f587e81a4b
3 changed files with 253 additions and 221 deletions

View file

@ -94,6 +94,23 @@ class FirebaseTimelineService with ChangeNotifier implements TimelineService {
return posts;
}
@override
Future<TimelinePost> fetchPost(TimelinePost post) async {
var doc = await _db
.collection(_options.timelineCollectionName)
.doc(post.id)
.get();
var data = doc.data();
if (data == null) return post;
var user = await _userService.getUser(data['creator_id']);
var updatedPost = TimelinePost.fromJson(doc.id, data).copyWith(
creator: user,
);
_posts = _posts.map((p) => (p.id == post.id) ? updatedPost : p).toList();
notifyListeners();
return updatedPost;
}
@override
List<TimelinePost> getPosts(String? category) => _posts
.where((element) => category == null || element.category == category)

View file

@ -12,6 +12,7 @@ abstract class TimelineService with ChangeNotifier {
Future<void> deletePost(TimelinePost post);
Future<TimelinePost> createPost(TimelinePost post);
Future<List<TimelinePost>> fetchPosts(String? category);
Future<TimelinePost> fetchPost(TimelinePost post);
List<TimelinePost> getPosts(String? category);
Future<TimelinePost> fetchPostDetails(TimelinePost post);
Future<TimelinePost> reactToPost(

View file

@ -111,7 +111,17 @@ class _TimelinePostScreenState extends State<TimelinePostScreen> {
return Stack(
children: [
SingleChildScrollView(
RefreshIndicator(
onRefresh: () async {
updatePost(
await widget.service.fetchPostDetails(
await widget.service.fetchPost(
post,
),
),
);
},
child: SingleChildScrollView(
child: Padding(
padding: widget.padding,
child: Column(
@ -122,7 +132,8 @@ class _TimelinePostScreenState extends State<TimelinePostScreen> {
if (post.creator != null)
InkWell(
onTap: widget.onUserTap != null
? () => widget.onUserTap?.call(post.creator!.userId)
? () =>
widget.onUserTap?.call(post.creator!.userId)
: null,
child: Row(
children: [
@ -133,7 +144,8 @@ class _TimelinePostScreenState extends State<TimelinePostScreen> {
) ??
CircleAvatar(
radius: 20,
backgroundImage: CachedNetworkImageProvider(
backgroundImage:
CachedNetworkImageProvider(
post.creator!.imageUrl!,
),
),
@ -307,7 +319,8 @@ class _TimelinePostScreenState extends State<TimelinePostScreen> {
children: [
Text(
reaction.creator?.fullName ??
widget.options.translations.anonymousUser,
widget
.options.translations.anonymousUser,
style: theme.textTheme.titleSmall,
),
Padding(
@ -348,6 +361,7 @@ class _TimelinePostScreenState extends State<TimelinePostScreen> {
),
),
),
),
if (post.reactionEnabled)
Align(
alignment: Alignment.bottomCenter,