2021-01-08 17:38:48 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
2021-01-24 20:01:55 +01:00
|
|
|
import 'package:lemmy_api_client/v2.dart';
|
2021-01-08 17:38:48 +01:00
|
|
|
|
|
|
|
import '../comment_tree.dart';
|
|
|
|
import '../hooks/stores.dart';
|
|
|
|
import '../widgets/comment.dart';
|
|
|
|
import '../widgets/post.dart';
|
|
|
|
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-01-10 01:51:26 +01:00
|
|
|
@required this.instanceHost,
|
2021-01-08 17:38:48 +01:00
|
|
|
@required this.query,
|
2021-01-10 01:51:26 +01:00
|
|
|
}) : assert(instanceHost != null),
|
2021-01-08 17:38:48 +01:00
|
|
|
assert(query != null),
|
2021-01-10 01:51:26 +01: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(
|
|
|
|
centerTitle: true,
|
|
|
|
title: Text('Looking for "$query"'),
|
|
|
|
bottom: const TabBar(
|
|
|
|
isScrollable: true,
|
|
|
|
tabs: [
|
|
|
|
Tab(text: 'Posts'),
|
|
|
|
Tab(text: 'Comments'),
|
|
|
|
Tab(text: 'Users'),
|
|
|
|
Tab(text: 'Communities'),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
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({
|
|
|
|
@required this.type,
|
|
|
|
@required this.query,
|
2021-01-09 20:04:25 +01:00
|
|
|
@required this.instanceHost,
|
2021-01-08 17:38:48 +01:00
|
|
|
}) : assert(type != null),
|
|
|
|
assert(query != null),
|
2021-01-09 20:04:25 +01:00
|
|
|
assert(instanceHost != null);
|
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
|
|
|
|
|
|
|
return SortableInfiniteList(
|
2021-01-08 17:51:40 +01:00
|
|
|
fetcher: (page, batchSize, sort) async {
|
2021-01-24 20:01:55 +01:00
|
|
|
final s = await LemmyApiV2(instanceHost).run(Search(
|
|
|
|
q: query,
|
|
|
|
sort: sort,
|
|
|
|
type: type,
|
|
|
|
auth: accStore.defaultTokenFor(instanceHost)?.raw,
|
|
|
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
builder: (data) {
|
|
|
|
switch (type) {
|
|
|
|
case SearchType.comments:
|
2021-01-24 20:01:55 +01:00
|
|
|
return CommentWidget(
|
2021-01-08 17:38:48 +01:00
|
|
|
CommentTree(data as CommentView),
|
|
|
|
postCreatorId: null,
|
|
|
|
);
|
|
|
|
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-01-24 20:01:55 +01:00
|
|
|
child: PostWidget(data as PostView),
|
2021-01-08 17:38:48 +01:00
|
|
|
);
|
|
|
|
case SearchType.users:
|
2021-01-24 20:01:55 +01:00
|
|
|
return UsersListItem(user: data as UserViewSafe);
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|