fluffychat/lib/pages/views/search_view.dart

287 lines
12 KiB
Dart
Raw Normal View History

import 'package:matrix/matrix.dart';
2021-05-22 06:53:52 +00:00
import 'package:fluffychat/widgets/avatar.dart';
import 'package:fluffychat/widgets/contacts_list.dart';
import 'package:fluffychat/widgets/default_app_bar_search_field.dart';
import 'package:fluffychat/widgets/list_items/chat_list_item.dart';
import 'package:fluffychat/widgets/matrix.dart';
2021-03-27 17:04:31 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:future_loading_dialog/future_loading_dialog.dart';
2021-05-23 11:11:55 +00:00
import 'package:vrouter/vrouter.dart';
import '../../utils/localized_exception_extension.dart';
import '../../utils/platform_infos.dart';
2021-04-17 09:24:00 +00:00
import '../search.dart';
2021-03-27 17:04:31 +00:00
2021-05-22 07:13:47 +00:00
class SearchView extends StatelessWidget {
2021-04-17 09:24:00 +00:00
final SearchController controller;
2021-03-27 17:04:31 +00:00
2021-05-22 07:13:47 +00:00
const SearchView(this.controller, {Key key}) : super(key: key);
2021-03-27 17:04:31 +00:00
@override
Widget build(BuildContext context) {
2021-04-17 09:24:00 +00:00
final server = controller.genericSearchTerm?.isValidMatrixId ?? false
? controller.genericSearchTerm.domain
: controller.server;
if (controller.lastServer != server) {
controller.lastServer = server;
controller.publicRoomsResponse = null;
2021-03-27 17:04:31 +00:00
}
2021-04-17 09:24:00 +00:00
controller.publicRoomsResponse ??= Matrix.of(context)
2021-03-27 17:04:31 +00:00
.client
2021-05-20 11:59:55 +00:00
.queryPublicRooms(
2021-03-27 17:04:31 +00:00
server: server,
filter: PublicRoomQueryFilter(
genericSearchTerm: controller.genericSearchTerm,
),
2021-03-27 17:04:31 +00:00
)
.catchError((error) {
if (!(controller.genericSearchTerm?.isValidMatrixId ?? false)) {
2021-03-27 17:04:31 +00:00
throw error;
}
return QueryPublicRoomsResponse.fromJson({
2021-03-27 17:04:31 +00:00
'chunk': [],
});
}).then((QueryPublicRoomsResponse res) {
if (controller.genericSearchTerm != null &&
2021-03-27 17:04:31 +00:00
!res.chunk.any((room) =>
(room.aliases?.contains(controller.genericSearchTerm) ?? false) ||
room.canonicalAlias == controller.genericSearchTerm)) {
2021-03-27 17:04:31 +00:00
// we have to tack on the original alias
res.chunk.add(PublicRoomsChunk.fromJson(<String, dynamic>{
'aliases': [controller.genericSearchTerm],
'name': controller.genericSearchTerm,
2021-03-27 17:04:31 +00:00
}));
}
return res;
});
final rooms = List<Room>.from(Matrix.of(context).client.rooms);
rooms.removeWhere(
(room) =>
room.lastEvent == null ||
!room.displayname
.toLowerCase()
2021-04-17 09:24:00 +00:00
.contains(controller.controller.text.toLowerCase()),
2021-03-27 17:04:31 +00:00
);
2021-10-16 07:59:38 +00:00
const tabCount = 3;
2021-03-27 17:04:31 +00:00
return DefaultTabController(
2021-10-16 07:59:38 +00:00
length: tabCount,
2021-05-24 09:07:02 +00:00
initialIndex:
controller.controller.text?.startsWith('#') ?? false ? 0 : 1,
2021-03-27 17:04:31 +00:00
child: Scaffold(
appBar: AppBar(
2021-10-14 16:09:30 +00:00
leading: const BackButton(),
2021-03-27 17:04:31 +00:00
titleSpacing: 0,
title: DefaultAppBarSearchField(
autofocus: true,
hintText: L10n.of(context).search,
2021-04-17 09:24:00 +00:00
searchController: controller.controller,
2021-10-14 16:09:30 +00:00
suffix: const Icon(Icons.search_outlined),
2021-04-17 09:24:00 +00:00
onChanged: controller.search,
2021-03-27 17:04:31 +00:00
),
bottom: TabBar(
2021-05-24 08:59:00 +00:00
indicatorColor: Theme.of(context).colorScheme.secondary,
labelColor: Theme.of(context).colorScheme.secondary,
2021-03-27 17:04:31 +00:00
unselectedLabelColor: Theme.of(context).textTheme.bodyText1.color,
2021-10-14 16:09:30 +00:00
labelStyle: const TextStyle(fontSize: 16),
labelPadding: const EdgeInsets.symmetric(
2021-03-27 17:04:31 +00:00
horizontal: 8,
vertical: 0,
),
tabs: [
2021-04-03 10:50:04 +00:00
Tab(child: Text(L10n.of(context).discover, maxLines: 1)),
2021-03-27 17:04:31 +00:00
Tab(child: Text(L10n.of(context).chats, maxLines: 1)),
Tab(child: Text(L10n.of(context).people, maxLines: 1)),
],
),
),
body: TabBarView(
children: [
ListView(
keyboardDismissBehavior: PlatformInfos.isIOS
? ScrollViewKeyboardDismissBehavior.onDrag
: ScrollViewKeyboardDismissBehavior.manual,
2021-03-27 17:04:31 +00:00
children: [
2021-10-14 16:09:30 +00:00
const SizedBox(height: 12),
2021-03-27 17:04:31 +00:00
ListTile(
leading: CircleAvatar(
2021-05-24 08:59:00 +00:00
foregroundColor: Theme.of(context).colorScheme.secondary,
2021-03-27 17:04:31 +00:00
backgroundColor: Theme.of(context).secondaryHeaderColor,
2021-10-14 16:09:30 +00:00
child: const Icon(Icons.edit_outlined),
2021-03-27 17:04:31 +00:00
),
title: Text(L10n.of(context).changeTheServer),
2021-04-17 09:24:00 +00:00
onTap: controller.setServer,
2021-03-27 17:04:31 +00:00
),
FutureBuilder<QueryPublicRoomsResponse>(
2021-04-17 09:24:00 +00:00
future: controller.publicRoomsResponse,
2021-03-27 17:04:31 +00:00
builder: (BuildContext context,
AsyncSnapshot<QueryPublicRoomsResponse> snapshot) {
2021-03-27 17:04:31 +00:00
if (snapshot.hasError) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
2021-10-14 16:09:30 +00:00
const SizedBox(height: 32),
const Icon(
2021-03-27 17:04:31 +00:00
Icons.error_outlined,
size: 80,
color: Colors.grey,
),
Center(
child: Text(
snapshot.error.toLocalizedString(context),
textAlign: TextAlign.center,
2021-10-14 16:09:30 +00:00
style: const TextStyle(
2021-03-27 17:04:31 +00:00
color: Colors.grey,
fontSize: 16,
),
),
),
],
);
}
if (snapshot.connectionState != ConnectionState.done) {
2021-10-14 16:09:30 +00:00
return const Center(
2021-10-10 11:38:06 +00:00
child: CircularProgressIndicator.adaptive(
strokeWidth: 2));
2021-03-27 17:04:31 +00:00
}
final publicRoomsResponse = snapshot.data;
if (publicRoomsResponse.chunk.isEmpty) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
2021-10-14 16:09:30 +00:00
const SizedBox(height: 32),
const Icon(
2021-03-27 17:04:31 +00:00
Icons.search_outlined,
size: 80,
color: Colors.grey,
),
Center(
child: Text(
L10n.of(context).noPublicRoomsFound,
textAlign: TextAlign.center,
2021-10-14 16:09:30 +00:00
style: const TextStyle(
2021-03-27 17:04:31 +00:00
color: Colors.grey,
fontSize: 16,
),
),
),
],
);
}
return GridView.builder(
shrinkWrap: true,
2021-10-14 16:09:30 +00:00
padding: const EdgeInsets.all(12),
physics: const NeverScrollableScrollPhysics(),
2021-10-16 07:59:38 +00:00
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(
2021-03-27 17:04:31 +00:00
crossAxisCount: 2,
childAspectRatio: 1,
crossAxisSpacing: 16,
mainAxisSpacing: 16,
),
itemCount: publicRoomsResponse.chunk.length,
itemBuilder: (BuildContext context, int i) => Material(
elevation: 2,
borderRadius: BorderRadius.circular(16),
child: InkWell(
2021-04-17 09:24:00 +00:00
onTap: () => controller.joinGroupAction(
2021-03-27 17:04:31 +00:00
publicRoomsResponse.chunk[i],
),
borderRadius: BorderRadius.circular(16),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Avatar(publicRoomsResponse.chunk[i].avatarUrl,
2021-03-27 17:04:31 +00:00
publicRoomsResponse.chunk[i].name),
Text(
publicRoomsResponse.chunk[i].name,
2021-10-14 16:09:30 +00:00
style: const TextStyle(
2021-03-27 17:04:31 +00:00
fontSize: 16,
fontWeight: FontWeight.bold,
),
maxLines: 1,
textAlign: TextAlign.center,
),
Text(
L10n.of(context).countParticipants(
publicRoomsResponse
.chunk[i].numJoinedMembers ??
0),
2021-10-14 16:09:30 +00:00
style: const TextStyle(fontSize: 10.5),
2021-03-27 17:04:31 +00:00
maxLines: 1,
textAlign: TextAlign.center,
),
Text(
publicRoomsResponse.chunk[i].topic ??
L10n.of(context).noDescription,
maxLines: 4,
textAlign: TextAlign.center,
),
],
),
),
),
),
);
}),
],
),
ListView.builder(
keyboardDismissBehavior: PlatformInfos.isIOS
? ScrollViewKeyboardDismissBehavior.onDrag
: ScrollViewKeyboardDismissBehavior.manual,
2021-03-27 17:04:31 +00:00
itemCount: rooms.length,
itemBuilder: (_, i) => ChatListItem(rooms[i]),
),
2021-04-17 09:24:00 +00:00
controller.foundProfiles.isNotEmpty
2021-03-27 17:04:31 +00:00
? ListView.builder(
keyboardDismissBehavior: PlatformInfos.isIOS
? ScrollViewKeyboardDismissBehavior.onDrag
: ScrollViewKeyboardDismissBehavior.manual,
2021-04-17 09:24:00 +00:00
itemCount: controller.foundProfiles.length,
2021-03-27 17:04:31 +00:00
itemBuilder: (BuildContext context, int i) {
2021-04-17 09:24:00 +00:00
final foundProfile = controller.foundProfiles[i];
2021-03-27 17:04:31 +00:00
return ListTile(
2021-03-27 17:55:23 +00:00
onTap: () async {
final roomID = await showFutureLoadingDialog(
context: context,
future: () => Matrix.of(context)
.client
.startDirectChat(foundProfile.userId),
);
if (roomID.error == null) {
2021-08-15 11:26:16 +00:00
VRouter.of(context)
.toSegments(['rooms', roomID.result]);
2021-03-27 17:55:23 +00:00
}
2021-03-27 17:04:31 +00:00
},
leading: Avatar(
foundProfile.avatarUrl,
foundProfile.displayName ?? foundProfile.userId,
2021-03-27 17:04:31 +00:00
//size: 24,
),
title: Text(
foundProfile.displayName ??
2021-03-27 17:04:31 +00:00
foundProfile.userId.localpart,
2021-10-14 16:09:30 +00:00
style: const TextStyle(),
2021-03-27 17:04:31 +00:00
maxLines: 1,
),
subtitle: Text(
foundProfile.userId,
maxLines: 1,
2021-10-14 16:09:30 +00:00
style: const TextStyle(
2021-03-27 17:04:31 +00:00
fontSize: 12,
),
),
);
},
)
2021-04-17 09:24:00 +00:00
: ContactsList(searchController: controller.controller),
2021-03-27 17:04:31 +00:00
],
),
),
);
}
}