lemmur-app-android/lib/pages/search_results.dart

136 lines
4.0 KiB
Dart
Raw Normal View History

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';
import '../hooks/stores.dart';
2021-03-01 14:21:45 +01:00
import '../l10n/l10n.dart';
import '../widgets/comment/comment.dart';
2021-09-11 01:04:15 +02:00
import '../widgets/post/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,
2021-07-08 12:19:26 +02:00
}) : assert(instanceHost.isNotEmpty),
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(
isScrollable: true,
tabs: [
Tab(text: L10n.of(context).posts),
Tab(text: L10n.of(context).comments),
Tab(text: L10n.of(context).users),
Tab(text: L10n.of(context).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,
required this.instanceHost,
});
@override
Widget build(BuildContext context) {
2021-01-09 22:50:14 +01:00
final accStore = useAccountsStore();
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
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-02-09 20:39:31 +01:00
itemBuilder: (data) {
switch (type) {
case SearchType.comments:
2021-02-24 20:52:18 +01:00
return CommentWidget.fromCommentView(data as CommentView);
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-09-11 01:04:15 +02:00
child: PostTile.fromPostView(data as PostView),
);
case SearchType.users:
2021-04-05 20:14:39 +02:00
return UsersListItem(user: data as PersonViewSafe);
default:
2021-01-09 20:06:16 +01:00
throw UnimplementedError();
}
},
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;
}
},
);
}
}