2021-01-08 17:38:48 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
|
|
|
import 'package:lemmy_api_client/lemmy_api_client.dart';
|
|
|
|
|
|
|
|
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 {
|
|
|
|
final String instance;
|
|
|
|
final String query;
|
|
|
|
|
|
|
|
SearchResultsPage({
|
|
|
|
@required this.instance,
|
|
|
|
@required this.query,
|
|
|
|
}) : assert(instance != null),
|
|
|
|
assert(query != null),
|
|
|
|
assert(instance.isNotEmpty),
|
|
|
|
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-09 20:04:25 +01:00
|
|
|
instanceHost: instance, query: query, type: SearchType.posts),
|
2021-01-08 17:38:48 +01:00
|
|
|
_SearchResultsList(
|
2021-01-09 20:04:25 +01:00
|
|
|
instanceHost: instance,
|
|
|
|
query: query,
|
|
|
|
type: SearchType.comments),
|
2021-01-08 17:38:48 +01:00
|
|
|
_SearchResultsList(
|
2021-01-09 20:04:25 +01:00
|
|
|
instanceHost: instance, query: query, type: SearchType.users),
|
2021-01-08 17:38:48 +01:00
|
|
|
_SearchResultsList(
|
2021-01-09 20:04:25 +01:00
|
|
|
instanceHost: instance,
|
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) {
|
|
|
|
final acs = useAccountsStore();
|
|
|
|
|
|
|
|
return SortableInfiniteList(
|
2021-01-08 17:51:40 +01:00
|
|
|
fetcher: (page, batchSize, sort) async {
|
2021-01-09 20:04:25 +01:00
|
|
|
final s = await LemmyApi(instanceHost).v1.search(
|
2021-01-08 17:38:48 +01:00
|
|
|
q: query,
|
|
|
|
sort: sort,
|
|
|
|
type: type,
|
2021-01-09 20:04:25 +01:00
|
|
|
auth: acs.defaultTokenFor(instanceHost)?.raw,
|
2021-01-08 17:38:48 +01:00
|
|
|
page: page,
|
|
|
|
);
|
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:
|
|
|
|
return Comment(
|
|
|
|
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),
|
|
|
|
child: Post(data as PostView),
|
|
|
|
);
|
|
|
|
case SearchType.users:
|
2021-01-08 18:19:24 +01:00
|
|
|
return UsersListItem(user: data as UserView);
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|