Surface/lib/widgets/post/post_comment_list.dart

228 lines
6.8 KiB
Dart
Raw Normal View History

2024-11-11 20:06:00 +08:00
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:go_router/go_router.dart';
import 'package:material_symbols_icons/symbols.dart';
import 'package:provider/provider.dart';
import 'package:responsive_framework/responsive_framework.dart';
2024-11-11 20:06:00 +08:00
import 'package:styled_widget/styled_widget.dart';
2024-11-26 00:00:09 +08:00
import 'package:surface/providers/post.dart';
2024-11-14 22:49:17 +08:00
import 'package:surface/providers/userinfo.dart';
2024-11-11 20:06:00 +08:00
import 'package:surface/types/post.dart';
2025-02-08 13:27:53 +08:00
import 'package:surface/widgets/dialog.dart';
2024-11-11 20:06:00 +08:00
import 'package:surface/widgets/post/post_item.dart';
2024-11-11 22:43:09 +08:00
import 'package:surface/widgets/post/post_mini_editor.dart';
2024-11-11 20:06:00 +08:00
import 'package:very_good_infinite_list/very_good_infinite_list.dart';
2025-02-08 13:27:53 +08:00
import '../../providers/sn_network.dart';
class PostCommentQuickAction extends StatelessWidget {
final double? maxWidth;
final SnPost parentPost;
final Function? onPosted;
const PostCommentQuickAction({super.key, this.maxWidth, required this.parentPost, this.onPosted});
@override
Widget build(BuildContext context) {
final devicePixelRatio = MediaQuery.of(context).devicePixelRatio;
return Container(
height: 240,
constraints: BoxConstraints(maxWidth: maxWidth ?? double.infinity),
margin: ResponsiveBreakpoints.of(context).largerThan(MOBILE) ? const EdgeInsets.symmetric(vertical: 8) : EdgeInsets.zero,
decoration: BoxDecoration(
borderRadius: ResponsiveBreakpoints.of(context).largerThan(MOBILE)
? const BorderRadius.all(Radius.circular(8))
: BorderRadius.zero,
border: ResponsiveBreakpoints.of(context).largerThan(MOBILE)
? Border.all(
color: Theme.of(context).dividerColor,
width: 1 / devicePixelRatio,
)
: Border.symmetric(
horizontal: BorderSide(
color: Theme.of(context).dividerColor,
width: 1 / devicePixelRatio,
),
),
),
child: PostMiniEditor(
postReplyId: parentPost.id,
onPost: () {
onPosted?.call();
},
),
);
}
}
2024-11-11 20:06:00 +08:00
class PostCommentSliverList extends StatefulWidget {
2025-02-08 13:27:53 +08:00
final SnPost parentPost;
2024-11-14 22:49:17 +08:00
final double? maxWidth;
2025-02-08 13:27:53 +08:00
final Function(SnPost)? onSelectAnswer;
2024-11-14 22:49:17 +08:00
const PostCommentSliverList({
super.key,
2025-02-08 13:27:53 +08:00
required this.parentPost,
2024-11-14 22:49:17 +08:00
this.maxWidth,
2025-02-08 13:27:53 +08:00
this.onSelectAnswer,
2024-11-14 22:49:17 +08:00
});
2024-11-11 20:06:00 +08:00
@override
2024-11-11 22:43:09 +08:00
State<PostCommentSliverList> createState() => PostCommentSliverListState();
2024-11-11 20:06:00 +08:00
}
2024-11-11 22:43:09 +08:00
class PostCommentSliverListState extends State<PostCommentSliverList> {
2024-11-11 20:06:00 +08:00
bool _isBusy = true;
final List<SnPost> _posts = List.empty(growable: true);
int? _postCount;
Future<void> _fetchPosts() async {
if (_postCount != null && _posts.length >= _postCount!) return;
setState(() => _isBusy = true);
2024-11-26 00:00:09 +08:00
final pt = context.read<SnPostContentProvider>();
2025-02-08 13:27:53 +08:00
final result = await pt.listPostReplies(widget.parentPost.id);
2024-11-26 00:00:09 +08:00
final List<SnPost> out = result.$1;
2024-11-11 20:06:00 +08:00
if (!mounted) return;
2024-11-26 00:00:09 +08:00
_postCount = result.$2;
2024-11-11 20:06:00 +08:00
_posts.addAll(out);
if (mounted) setState(() => _isBusy = false);
}
2025-02-08 13:27:53 +08:00
Future<void> _selectAnswer(SnPost answer) async {
try {
final sn = context.read<SnNetworkProvider>();
await sn.client.put('/cgi/co/questions/${widget.parentPost.id}/answer', data: {
'publisher': answer.publisherId,
'answer_id': answer.id,
});
if (!mounted) return;
await refresh();
} catch (err) {
if (!mounted) return;
context.showErrorDialog(err);
}
}
2024-11-11 22:43:09 +08:00
Future<void> refresh() async {
_posts.clear();
_postCount = null;
2024-11-11 22:43:09 +08:00
_fetchPosts();
}
2024-11-11 20:06:00 +08:00
@override
void initState() {
super.initState();
_fetchPosts();
}
@override
Widget build(BuildContext context) {
return SliverInfiniteList(
itemCount: _posts.length,
isLoading: _isBusy,
hasReachedMax: _postCount != null && _posts.length >= _postCount!,
onFetchData: _fetchPosts,
itemBuilder: (context, idx) {
return GestureDetector(
child: PostItem(
data: _posts[idx],
maxWidth: widget.maxWidth,
2025-02-08 13:27:53 +08:00
onSelectAnswer: widget.parentPost.type == 'question' ? () => _selectAnswer(_posts[idx]) : null,
2024-11-23 18:04:30 +08:00
onChanged: (data) {
setState(() => _posts[idx] = data);
},
2024-11-28 22:04:38 +08:00
onDeleted: () {
_posts.clear();
_fetchPosts();
},
),
2024-11-11 20:06:00 +08:00
onTap: () {
GoRouter.of(context).pushNamed(
'postDetail',
pathParameters: {'slug': _posts[idx].id.toString()},
extra: _posts[idx],
);
},
);
},
separatorBuilder: (context, index) => const Divider(height: 1),
);
}
}
2024-11-11 22:43:09 +08:00
class PostCommentListPopup extends StatefulWidget {
2025-02-08 13:27:53 +08:00
final SnPost post;
2024-11-11 20:06:00 +08:00
final int commentCount;
2025-02-08 13:27:53 +08:00
2024-11-11 20:06:00 +08:00
const PostCommentListPopup({
super.key,
2025-02-08 13:27:53 +08:00
required this.post,
2024-11-11 20:06:00 +08:00
this.commentCount = 0,
});
2024-11-11 22:43:09 +08:00
@override
State<PostCommentListPopup> createState() => _PostCommentListPopupState();
}
class _PostCommentListPopupState extends State<PostCommentListPopup> {
final GlobalKey<PostCommentSliverListState> _childListKey = GlobalKey();
2024-11-11 20:06:00 +08:00
@override
Widget build(BuildContext context) {
2024-11-14 22:49:17 +08:00
final ua = context.watch<UserProvider>();
2024-11-11 22:43:09 +08:00
final devicePixelRatio = MediaQuery.of(context).devicePixelRatio;
2024-11-11 20:06:00 +08:00
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Icon(Symbols.comment, size: 24),
const Gap(16),
2025-02-08 13:27:53 +08:00
Text('postCommentsDetailed').plural(widget.commentCount).textStyle(Theme.of(context).textTheme.titleLarge!),
2024-11-11 20:06:00 +08:00
],
).padding(horizontal: 20, top: 16, bottom: 12),
Expanded(
child: CustomScrollView(
slivers: [
2024-11-14 22:49:17 +08:00
if (ua.isAuthorized)
SliverToBoxAdapter(
child: Container(
height: 240,
decoration: BoxDecoration(
border: Border.symmetric(
horizontal: BorderSide(
color: Theme.of(context).dividerColor,
width: 1 / devicePixelRatio,
),
2024-11-11 22:43:09 +08:00
),
),
2024-11-14 22:49:17 +08:00
child: PostMiniEditor(
2025-02-08 13:27:53 +08:00
postReplyId: widget.post.id,
2024-11-14 22:49:17 +08:00
onPost: () {
_childListKey.currentState!.refresh();
},
),
2024-11-11 22:43:09 +08:00
),
),
PostCommentSliverList(
2025-02-08 13:27:53 +08:00
parentPost: widget.post,
2024-11-11 22:43:09 +08:00
key: _childListKey,
),
2024-11-11 20:06:00 +08:00
],
),
),
],
);
}
}