1
0
mirror of https://github.com/krawieck/lemmur/ synced 2024-12-16 02:19:06 +01:00
lemmur-app-android/lib/pages/search_results.dart

130 lines
3.7 KiB
Dart
Raw Normal View History

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';
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';
class SearchResultsPage extends HookWidget {
final String instanceHost;
final String query;
SearchResultsPage({
@required this.instanceHost,
@required this.query,
}) : assert(instanceHost != null),
assert(query != null),
assert(instanceHost.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(
instanceHost: instanceHost,
query: query,
type: SearchType.posts),
_SearchResultsList(
instanceHost: instanceHost,
2021-01-09 20:04:25 +01:00
query: query,
type: SearchType.comments),
_SearchResultsList(
instanceHost: instanceHost,
query: query,
type: SearchType.users),
_SearchResultsList(
instanceHost: instanceHost,
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;
const _SearchResultsList({
@required this.type,
@required this.query,
2021-01-09 20:04:25 +01:00
@required this.instanceHost,
}) : assert(type != null),
assert(query != null),
2021-01-09 20:04:25 +01:00
assert(instanceHost != null);
@override
Widget build(BuildContext context) {
2021-01-09 22:50:14 +01:00
final accStore = useAccountsStore();
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
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();
}
},
builder: (data) {
switch (type) {
case SearchType.comments:
2021-01-24 20:01:55 +01:00
return CommentWidget(
CommentTree(data as CommentView),
postCreatorId: null,
);
case SearchType.communities:
2021-01-08 18:07:58 +01:00
return CommunitiesListItem(community: data as CommunityView);
case SearchType.posts:
return Padding(
padding: const EdgeInsets.only(bottom: 20),
2021-01-24 20:01:55 +01:00
child: PostWidget(data as PostView),
);
case SearchType.users:
2021-01-24 20:01:55 +01:00
return UsersListItem(user: data as UserViewSafe);
default:
2021-01-09 20:06:16 +01:00
throw UnimplementedError();
}
},
);
}
}