Search advance filter with tags

This commit is contained in:
LittleSheep 2024-12-08 14:19:18 +08:00
parent 669107a99f
commit e6a9185d11
4 changed files with 44 additions and 40 deletions

View File

@ -162,7 +162,6 @@ class ChatMessageController extends ChangeNotifier {
} }
Future<void> _applyMessage(SnChatMessage message) async { Future<void> _applyMessage(SnChatMessage message) async {
print(message.toJson());
if (message.channelId != channel?.id) return; if (message.channelId != channel?.id) return;
switch (message.type) { switch (message.type) {

View File

@ -29,22 +29,14 @@ class SnPostContentProvider {
for (var i = 0; i < out.length; i++) { for (var i = 0; i < out.length; i++) {
out[i] = out[i].copyWith( out[i] = out[i].copyWith(
preload: SnPostPreload( preload: SnPostPreload(
thumbnail: attachments thumbnail: attachments.where((ele) => ele?.rid == out[i].body['thumbnail']).firstOrNull,
.where((ele) => ele?.rid == out[i].body['thumbnail']) attachments: attachments.where((ele) => out[i].body['attachments']?.contains(ele?.rid) ?? false).toList(),
.firstOrNull,
attachments: attachments
.where((ele) =>
out[i].body['attachments']?.contains(ele?.rid) ?? false)
.toList(),
), ),
); );
} }
await _ud.listAccount( await _ud.listAccount(
attachments attachments.where((ele) => ele != null).map((ele) => ele!.accountId).toSet(),
.where((ele) => ele != null)
.map((ele) => ele!.accountId)
.toSet(),
); );
return out; return out;
@ -60,13 +52,8 @@ class SnPostContentProvider {
final attachments = await _attach.getMultiple(rids.toList()); final attachments = await _attach.getMultiple(rids.toList());
out = out.copyWith( out = out.copyWith(
preload: SnPostPreload( preload: SnPostPreload(
thumbnail: attachments thumbnail: attachments.where((ele) => ele?.rid == out.body['thumbnail']).firstOrNull,
.where((ele) => ele?.rid == out.body['thumbnail']) attachments: attachments.where((ele) => out.body['attachments']?.contains(ele?.rid) ?? false).toList(),
.firstOrNull,
attachments: attachments
.where(
(ele) => out.body['attachments']?.contains(ele?.rid) ?? false)
.toList(),
), ),
); );
@ -97,8 +84,7 @@ class SnPostContentProvider {
int take = 10, int take = 10,
int offset = 0, int offset = 0,
}) async { }) async {
final resp = await _sn.client final resp = await _sn.client.get('/cgi/co/posts/$parentId/replies', queryParameters: {
.get('/cgi/co/posts/$parentId/replies', queryParameters: {
'take': take, 'take': take,
'offset': offset, 'offset': offset,
}); });
@ -113,11 +99,13 @@ class SnPostContentProvider {
String searchTerm, { String searchTerm, {
int take = 10, int take = 10,
int offset = 0, int offset = 0,
Iterable<String>? tags,
}) async { }) async {
final resp = await _sn.client.get('/cgi/co/posts/search', queryParameters: { final resp = await _sn.client.get('/cgi/co/posts/search', queryParameters: {
'take': take, 'take': take,
'offset': offset, 'offset': offset,
'probe': searchTerm, 'probe': searchTerm,
if (tags?.isNotEmpty ?? false) 'tags': tags!.join(','),
}); });
final List<SnPost> out = await _preloadRelatedDataInBatch( final List<SnPost> out = await _preloadRelatedDataInBatch(
List.from(resp.data['data']?.map((e) => SnPost.fromJson(e)) ?? []), List.from(resp.data['data']?.map((e) => SnPost.fromJson(e)) ?? []),

View File

@ -87,7 +87,7 @@ class _ChatScreenState extends State<ChatScreen> {
const uuid = Uuid(); const uuid = Uuid();
final sn = context.read<SnNetworkProvider>(); final sn = context.read<SnNetworkProvider>();
final ua = context.read<UserProvider>(); final ua = context.read<UserProvider>();
final resp = await sn.client.post('/cgi/im/channels/global/dm', data: { await sn.client.post('/cgi/im/channels/global/dm', data: {
'alias': uuid.v4().replaceAll('-', '').substring(0, 12), 'alias': uuid.v4().replaceAll('-', '').substring(0, 12),
'name': 'DM', 'name': 'DM',
'description': 'A direct message channel between @${ua.user?.name} and @${user.name}', 'description': 'A direct message channel between @${ua.user?.name} and @${user.name}',

View File

@ -5,9 +5,12 @@ import 'package:go_router/go_router.dart';
import 'package:material_symbols_icons/symbols.dart'; import 'package:material_symbols_icons/symbols.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:styled_widget/styled_widget.dart'; import 'package:styled_widget/styled_widget.dart';
import 'package:surface/controllers/post_write_controller.dart';
import 'package:surface/providers/post.dart'; import 'package:surface/providers/post.dart';
import 'package:surface/types/post.dart'; import 'package:surface/types/post.dart';
import 'package:surface/widgets/dialog.dart';
import 'package:surface/widgets/post/post_item.dart'; import 'package:surface/widgets/post/post_item.dart';
import 'package:surface/widgets/post/post_tags_field.dart';
import 'package:very_good_infinite_list/very_good_infinite_list.dart'; import 'package:very_good_infinite_list/very_good_infinite_list.dart';
class PostSearchScreen extends StatefulWidget { class PostSearchScreen extends StatefulWidget {
@ -20,6 +23,8 @@ class PostSearchScreen extends StatefulWidget {
class _PostSearchScreenState extends State<PostSearchScreen> { class _PostSearchScreenState extends State<PostSearchScreen> {
bool _isBusy = false; bool _isBusy = false;
List<String> _searchTags = List.empty(growable: true);
final List<SnPost> _posts = List.empty(growable: true); final List<SnPost> _posts = List.empty(growable: true);
int? _postCount; int? _postCount;
@ -27,28 +32,32 @@ class _PostSearchScreenState extends State<PostSearchScreen> {
Duration? _lastTook; Duration? _lastTook;
Future<void> _fetchPosts() async { Future<void> _fetchPosts() async {
if (_searchTerm.isEmpty) return; if (_searchTerm.isEmpty && _searchTags.isEmpty) return;
if (_postCount != null && _posts.length >= _postCount!) return; if (_postCount != null && _posts.length >= _postCount!) return;
setState(() => _isBusy = true); setState(() => _isBusy = true);
final stopwatch = Stopwatch()..start(); final stopwatch = Stopwatch()..start();
final pt = context.read<SnPostContentProvider>(); try {
final result = await pt.searchPosts( final pt = context.read<SnPostContentProvider>();
_searchTerm, final result = await pt.searchPosts(
take: 10, _searchTerm,
offset: _posts.length, take: 10,
); offset: _posts.length,
final List<SnPost> out = result.$1; tags: _searchTags,
);
if (!mounted) return; final List<SnPost> out = result.$1;
_postCount = result.$2;
stopwatch.stop(); _posts.addAll(out);
} catch (err) {
_lastTook = stopwatch.elapsed; if (!mounted) return;
_postCount = result.$2; context.showErrorDialog(err);
_posts.addAll(out); return;
} finally {
stopwatch.stop();
_lastTook = stopwatch.elapsed;
}
if (mounted) setState(() => _isBusy = false); if (mounted) setState(() => _isBusy = false);
} }
@ -57,8 +66,16 @@ class _PostSearchScreenState extends State<PostSearchScreen> {
showModalBottomSheet( showModalBottomSheet(
context: context, context: context,
builder: (context) => Column( builder: (context) => Column(
children: [], children: [
), PostTagsField(
labelText: 'fieldPostTags'.tr(),
initialTags: _searchTags,
onUpdate: (value) {
setState(() => _searchTags = value);
},
),
],
).padding(horizontal: 24, vertical: 16),
); );
} }