2021-01-08 17:38:48 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
2021-04-05 20:14:39 +02:00
|
|
|
import 'package:lemmy_api_client/v3.dart';
|
2021-01-08 17:38:48 +01:00
|
|
|
|
|
|
|
import '../hooks/stores.dart';
|
2021-03-01 14:21:45 +01:00
|
|
|
import '../l10n/l10n.dart';
|
2021-09-08 14:38:26 +02:00
|
|
|
import '../widgets/comment/comment.dart';
|
2021-09-11 01:04:15 +02:00
|
|
|
import '../widgets/post/post.dart';
|
2021-01-08 17:38:48 +01:00
|
|
|
import '../widgets/sortable_infinite_list.dart';
|
2021-01-08 18:07:58 +01:00
|
|
|
import 'communities_list.dart';
|
2021-01-08 18:19:24 +01:00
|
|
|
import 'users_list.dart';
|
2021-01-08 17:38:48 +01:00
|
|
|
|
|
|
|
class SearchResultsPage extends HookWidget {
|
2021-01-10 01:51:26 +01:00
|
|
|
final String instanceHost;
|
2021-01-08 17:38:48 +01:00
|
|
|
final String query;
|
|
|
|
|
|
|
|
SearchResultsPage({
|
2021-04-09 00:11:44 +02:00
|
|
|
required this.instanceHost,
|
|
|
|
required this.query,
|
2021-07-08 12:19:26 +02:00
|
|
|
}) : assert(instanceHost.isNotEmpty),
|
2021-01-08 17:38:48 +01:00
|
|
|
assert(query.isNotEmpty);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) => DefaultTabController(
|
|
|
|
length: 4,
|
|
|
|
child: Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: Text('Looking for "$query"'),
|
2021-03-01 14:21:45 +01:00
|
|
|
bottom: TabBar(
|
2021-01-08 17:38:48 +01:00
|
|
|
isScrollable: true,
|
|
|
|
tabs: [
|
2021-11-05 21:37:27 +01:00
|
|
|
Tab(text: L10n.of(context).posts),
|
|
|
|
Tab(text: L10n.of(context).comments),
|
|
|
|
Tab(text: L10n.of(context).users),
|
|
|
|
Tab(text: L10n.of(context).communities),
|
2021-01-08 17:38:48 +01:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
body: TabBarView(
|
|
|
|
children: [
|
|
|
|
_SearchResultsList(
|
2021-01-10 01:51:26 +01:00
|
|
|
instanceHost: instanceHost,
|
|
|
|
query: query,
|
|
|
|
type: SearchType.posts),
|
2021-01-08 17:38:48 +01:00
|
|
|
_SearchResultsList(
|
2021-01-10 01:51:26 +01:00
|
|
|
instanceHost: instanceHost,
|
2021-01-09 20:04:25 +01:00
|
|
|
query: query,
|
|
|
|
type: SearchType.comments),
|
2021-01-08 17:38:48 +01:00
|
|
|
_SearchResultsList(
|
2021-01-10 01:51:26 +01:00
|
|
|
instanceHost: instanceHost,
|
|
|
|
query: query,
|
|
|
|
type: SearchType.users),
|
2021-01-08 17:38:48 +01:00
|
|
|
_SearchResultsList(
|
2021-01-10 01:51:26 +01:00
|
|
|
instanceHost: instanceHost,
|
2021-01-08 17:38:48 +01:00
|
|
|
query: query,
|
|
|
|
type: SearchType.communities),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
class _SearchResultsList extends HookWidget {
|
|
|
|
final SearchType type;
|
|
|
|
final String query;
|
2021-01-09 20:04:25 +01:00
|
|
|
final String instanceHost;
|
2021-01-08 17:38:48 +01:00
|
|
|
|
|
|
|
const _SearchResultsList({
|
2021-04-09 00:11:44 +02:00
|
|
|
required this.type,
|
|
|
|
required this.query,
|
|
|
|
required this.instanceHost,
|
|
|
|
});
|
2021-01-08 17:38:48 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2021-01-09 22:50:14 +01:00
|
|
|
final accStore = useAccountsStore();
|
2021-01-08 17:38:48 +01:00
|
|
|
|
2021-04-09 00:11:44 +02:00
|
|
|
return SortableInfiniteList<Object>(
|
2021-01-08 17:51:40 +01:00
|
|
|
fetcher: (page, batchSize, sort) async {
|
2021-04-05 20:14:39 +02:00
|
|
|
final s = await LemmyApiV3(instanceHost).run(Search(
|
2021-01-24 20:01:55 +01:00
|
|
|
q: query,
|
|
|
|
sort: sort,
|
|
|
|
type: type,
|
2021-04-11 18:27:22 +02:00
|
|
|
auth: accStore.defaultUserDataFor(instanceHost)?.jwt.raw,
|
2021-01-24 20:01:55 +01:00
|
|
|
page: page,
|
|
|
|
limit: batchSize,
|
|
|
|
));
|
2021-01-08 17:51:40 +01:00
|
|
|
|
2021-01-08 17:38:48 +01:00
|
|
|
switch (s.type) {
|
|
|
|
case SearchType.comments:
|
|
|
|
return s.comments;
|
|
|
|
case SearchType.communities:
|
|
|
|
return s.communities;
|
|
|
|
case SearchType.posts:
|
|
|
|
return s.posts;
|
|
|
|
case SearchType.users:
|
|
|
|
return s.users;
|
|
|
|
default:
|
2021-01-09 20:06:16 +01:00
|
|
|
throw UnimplementedError();
|
2021-01-08 17:38:48 +01:00
|
|
|
}
|
|
|
|
},
|
2021-02-09 20:39:31 +01:00
|
|
|
itemBuilder: (data) {
|
2021-01-08 17:38:48 +01:00
|
|
|
switch (type) {
|
|
|
|
case SearchType.comments:
|
2021-02-24 20:52:18 +01:00
|
|
|
return CommentWidget.fromCommentView(data as CommentView);
|
2021-01-08 17:38:48 +01:00
|
|
|
case SearchType.communities:
|
2021-01-08 18:07:58 +01:00
|
|
|
return CommunitiesListItem(community: data as CommunityView);
|
2021-01-08 17:38:48 +01:00
|
|
|
case SearchType.posts:
|
|
|
|
return Padding(
|
|
|
|
padding: const EdgeInsets.only(bottom: 20),
|
2021-09-11 01:04:15 +02:00
|
|
|
child: PostTile.fromPostView(data as PostView),
|
2021-01-08 17:38:48 +01:00
|
|
|
);
|
|
|
|
case SearchType.users:
|
2021-04-05 20:14:39 +02:00
|
|
|
return UsersListItem(user: data as PersonViewSafe);
|
2021-01-08 17:38:48 +01:00
|
|
|
default:
|
2021-01-09 20:06:16 +01:00
|
|
|
throw UnimplementedError();
|
2021-01-08 17:38:48 +01:00
|
|
|
}
|
|
|
|
},
|
2021-04-06 17:52:10 +02:00
|
|
|
uniqueProp: (item) {
|
|
|
|
switch (type) {
|
|
|
|
case SearchType.comments:
|
2021-04-22 21:08:30 +02:00
|
|
|
return (item as CommentView).comment.apId;
|
2021-04-06 17:52:10 +02:00
|
|
|
case SearchType.communities:
|
2021-04-22 21:08:30 +02:00
|
|
|
return (item as CommunityView).community.actorId;
|
2021-04-06 17:52:10 +02:00
|
|
|
case SearchType.posts:
|
2021-04-22 21:08:30 +02:00
|
|
|
return (item as PostView).post.apId;
|
2021-04-06 17:52:10 +02:00
|
|
|
case SearchType.users:
|
2021-04-22 21:08:30 +02:00
|
|
|
return (item as PersonViewSafe).person.actorId;
|
2021-04-06 17:52:10 +02:00
|
|
|
default:
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
},
|
2021-01-08 17:38:48 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|