Search filtering by categories

This commit is contained in:
LittleSheep 2024-12-22 15:57:37 +08:00
parent fd2eb5cda6
commit 2375c46852
2 changed files with 17 additions and 2 deletions

View File

@ -118,12 +118,14 @@ class SnPostContentProvider {
int take = 10,
int offset = 0,
Iterable<String>? tags,
Iterable<String>? categories,
}) async {
final resp = await _sn.client.get('/cgi/co/posts/search', queryParameters: {
'take': take,
'offset': offset,
'probe': searchTerm,
if (tags?.isNotEmpty ?? false) 'tags': tags!.join(','),
if (categories?.isNotEmpty ?? false) 'categories': categories!.join(','),
});
final List<SnPost> out = await _preloadRelatedDataInBatch(
List.from(resp.data['data']?.map((e) => SnPost.fromJson(e)) ?? []),

View File

@ -23,6 +23,7 @@ class _PostSearchScreenState extends State<PostSearchScreen> {
bool _isBusy = false;
List<String> _searchTags = List.empty(growable: true);
List<String> _searchCategories = List.empty(growable: true);
final List<SnPost> _posts = List.empty(growable: true);
int? _postCount;
@ -31,7 +32,7 @@ class _PostSearchScreenState extends State<PostSearchScreen> {
Duration? _lastTook;
Future<void> _fetchPosts() async {
if (_searchTerm.isEmpty && _searchTags.isEmpty) return;
if (_searchTerm.isEmpty && _searchCategories.isEmpty && _searchTags.isEmpty) return;
if (_postCount != null && _posts.length >= _postCount!) return;
setState(() => _isBusy = true);
@ -45,6 +46,7 @@ class _PostSearchScreenState extends State<PostSearchScreen> {
take: 10,
offset: _posts.length,
tags: _searchTags,
categories: _searchCategories,
);
final List<SnPost> out = result.$1;
_postCount = result.$2;
@ -73,9 +75,20 @@ class _PostSearchScreenState extends State<PostSearchScreen> {
setState(() => _searchTags = value);
},
),
const Gap(4),
PostCategoriesField(
labelText: 'fieldPostCategories'.tr(),
initialCategories: _searchCategories,
onUpdate: (value) {
setState(() => _searchCategories = value);
},
),
],
).padding(horizontal: 24, vertical: 16),
);
).then((_) {
_posts.clear();
_fetchPosts();
});
}
@override