fluffychat/lib/pages/search/search_view.dart

302 lines
13 KiB
Dart
Raw Normal View History

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