1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 21:09:21 +00:00
comunicmobile/lib/ui/screens/search_screen.dart

118 lines
3.4 KiB
Dart
Raw Normal View History

2020-04-17 08:30:29 +00:00
import 'package:comunic/helpers/groups_helper.dart';
import 'package:comunic/helpers/search_helper.dart';
import 'package:comunic/helpers/users_helper.dart';
import 'package:comunic/lists/groups_list.dart';
import 'package:comunic/lists/search_results_list.dart';
import 'package:comunic/lists/users_list.dart';
import 'package:comunic/models/group.dart';
import 'package:comunic/models/search_result.dart';
import 'package:comunic/models/user.dart';
2020-05-05 11:21:37 +00:00
import 'package:comunic/ui/routes/main_route/main_route.dart';
2020-04-17 08:30:29 +00:00
import 'package:comunic/ui/widgets/account_image_widget.dart';
import 'package:comunic/ui/widgets/group_icon_widget.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
import 'package:flutter/material.dart';
/// Search screen
///
/// @author Pierre Hubert
class SearchScreen extends StatefulWidget {
@override
_SearchScreenState createState() => _SearchScreenState();
}
class _SearchScreenState extends State<SearchScreen> {
SearchResultsList? _searchResultsList;
late UsersList _usersList;
late GroupsList _groupsList;
2020-04-17 08:30:29 +00:00
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
TextField(
2020-04-17 08:32:09 +00:00
decoration: InputDecoration(
icon: Icon(Icons.search),
labelText: tr("Search a user, a group...")),
2020-04-17 08:30:29 +00:00
onChanged: (s) => _newSearch(s),
),
_searchResultsList == null
? Container()
: Expanded(
child: ListView(
children: _searchResultsList!
2020-04-17 08:30:29 +00:00
.map((f) => f.kind == SearchResultKind.USER
? _SearchResultUser(
user: _usersList.getUser(f.id),
)
: _SearchResultGroup(
group: _groupsList[f.id]!,
2020-04-17 08:30:29 +00:00
))
.toList(),
),
)
],
),
);
}
/// Perform a new search
void _newSearch(String s) async {
try {
if (s.length < 2) return;
final list = await SearchHelper().globalSearch(s);
final users = await UsersHelper().getListWithThrow(list.usersId);
final groups = await GroupsHelper().getListOrThrow(list.groupsId);
setState(() {
_searchResultsList = list;
_usersList = users;
_groupsList = groups;
});
} catch (e, stack) {
print(e);
print(stack);
showSimpleSnack(context, tr("Could not peform search!")!);
2020-04-17 08:30:29 +00:00
}
}
}
class _SearchResultUser extends StatelessWidget {
final User user;
2022-03-11 15:40:56 +00:00
const _SearchResultUser({Key? key, required this.user}) : super(key: key);
2020-04-17 08:30:29 +00:00
@override
Widget build(BuildContext context) {
return ListTile(
leading: AccountImageWidget(
user: user,
),
title: Text(user.displayName),
2022-03-11 15:21:35 +00:00
onTap: () => MainController.of(context)!.openUserPage(user.id),
2020-04-17 08:30:29 +00:00
);
}
}
class _SearchResultGroup extends StatelessWidget {
final Group group;
2022-03-11 15:40:56 +00:00
const _SearchResultGroup({Key? key, required this.group}) : super(key: key);
2020-04-17 08:30:29 +00:00
@override
Widget build(BuildContext context) {
return ListTile(
leading: GroupIcon(group: group),
title: Text(group.displayName),
subtitle: Text(tr("Group")!),
onTap: () => MainController.of(context)!.openGroup(group.id),
2020-04-17 08:30:29 +00:00
);
}
}