fluffychat/lib/pages/views/search_view.dart

272 lines
11 KiB
Dart
Raw Normal View History

2021-03-27 18:04:31 +01:00
import 'package:famedlysdk/famedlysdk.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/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 18:04:31 +01: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 13:11:55 +02:00
import 'package:vrouter/vrouter.dart';
import '../../utils/localized_exception_extension.dart';
2021-04-17 11:24:00 +02: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
2021-05-22 09:13:47 +02: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
? controller.genericSearchTerm.domain
: 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,
2021-04-17 11:24:00 +02:00
genericSearchTerm: controller.genericSearchTerm,
2021-03-27 18:04:31 +01:00
)
.catchError((error) {
2021-05-23 13:11:55 +02:00
if (controller.alias == null) {
2021-03-27 18:04:31 +01:00
throw error;
}
return PublicRoomsResponse.fromJson({
'chunk': [],
});
}).then((PublicRoomsResponse res) {
2021-05-23 13:11:55 +02:00
if (controller.alias != null &&
2021-03-27 18:04:31 +01:00
!res.chunk.any((room) =>
2021-05-23 13:11:55 +02:00
(room.aliases?.contains(controller.alias) ?? false) ||
room.canonicalAlias == controller.alias)) {
2021-03-27 18:04:31 +01:00
// we have to tack on the original alias
res.chunk.add(PublicRoom.fromJson(<String, dynamic>{
2021-05-23 13:11:55 +02:00
'aliases': [controller.alias],
'name': controller.alias,
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 ||
!room.displayname
.toLowerCase()
2021-04-17 11:24:00 +02:00
.contains(controller.controller.text.toLowerCase()),
2021-03-27 18:04:31 +01:00
);
return DefaultTabController(
length: 3,
initialIndex: 1,
child: Scaffold(
appBar: AppBar(
leading: BackButton(),
titleSpacing: 0,
title: DefaultAppBarSearchField(
autofocus: true,
hintText: L10n.of(context).search,
2021-04-17 11:24:00 +02:00
searchController: controller.controller,
2021-03-27 18:04:31 +01:00
suffix: Icon(Icons.search_outlined),
2021-04-17 11:24:00 +02:00
onChanged: controller.search,
2021-03-27 18:04:31 +01:00
),
bottom: TabBar(
indicatorColor: Theme.of(context).accentColor,
labelColor: Theme.of(context).accentColor,
unselectedLabelColor: Theme.of(context).textTheme.bodyText1.color,
labelStyle: TextStyle(fontSize: 16),
labelPadding: EdgeInsets.symmetric(
horizontal: 8,
vertical: 0,
),
tabs: [
2021-04-03 12:50:04 +02:00
Tab(child: Text(L10n.of(context).discover, maxLines: 1)),
2021-03-27 18:04:31 +01:00
Tab(child: Text(L10n.of(context).chats, maxLines: 1)),
Tab(child: Text(L10n.of(context).people, maxLines: 1)),
],
),
),
body: TabBarView(
children: [
ListView(
children: [
SizedBox(height: 12),
ListTile(
leading: CircleAvatar(
foregroundColor: Theme.of(context).accentColor,
backgroundColor: Theme.of(context).secondaryHeaderColor,
child: Icon(Icons.edit_outlined),
),
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<PublicRoomsResponse>(
2021-04-17 11:24:00 +02:00
future: controller.publicRoomsResponse,
2021-03-27 18:04:31 +01:00
builder: (BuildContext context,
AsyncSnapshot<PublicRoomsResponse> snapshot) {
if (snapshot.hasError) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(height: 32),
Icon(
Icons.error_outlined,
size: 80,
color: Colors.grey,
),
Center(
child: Text(
snapshot.error.toLocalizedString(context),
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.grey,
fontSize: 16,
),
),
),
],
);
}
if (snapshot.connectionState != ConnectionState.done) {
return Center(child: CircularProgressIndicator());
}
final publicRoomsResponse = snapshot.data;
if (publicRoomsResponse.chunk.isEmpty) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(height: 32),
Icon(
Icons.search_outlined,
size: 80,
color: Colors.grey,
),
Center(
child: Text(
L10n.of(context).noPublicRoomsFound,
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.grey,
fontSize: 16,
),
),
),
],
);
}
return GridView.builder(
shrinkWrap: true,
padding: EdgeInsets.all(12),
physics: NeverScrollableScrollPhysics(),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
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: [
Avatar(
Uri.parse(publicRoomsResponse
.chunk[i].avatarUrl ??
''),
publicRoomsResponse.chunk[i].name),
Text(
publicRoomsResponse.chunk[i].name,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
maxLines: 1,
textAlign: TextAlign.center,
),
Text(
L10n.of(context).countParticipants(
publicRoomsResponse
.chunk[i].numJoinedMembers ??
0),
style: TextStyle(fontSize: 10.5),
maxLines: 1,
textAlign: TextAlign.center,
),
Text(
publicRoomsResponse.chunk[i].topic ??
L10n.of(context).noDescription,
maxLines: 4,
textAlign: TextAlign.center,
),
],
),
),
),
),
);
}),
],
),
ListView.builder(
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(
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: () => Matrix.of(context)
.client
.startDirectChat(foundProfile.userId),
);
if (roomID.error == null) {
2021-05-23 13:11:55 +02:00
VRouter.of(context).push('/rooms/${roomID.result}');
2021-03-27 18:55:23 +01:00
}
2021-03-27 18:04:31 +01:00
},
leading: Avatar(
foundProfile.avatarUrl,
foundProfile.displayname ?? foundProfile.userId,
//size: 24,
),
title: Text(
foundProfile.displayname ??
foundProfile.userId.localpart,
style: TextStyle(),
maxLines: 1,
),
subtitle: Text(
foundProfile.userId,
maxLines: 1,
style: TextStyle(
fontSize: 12,
),
),
);
},
)
2021-04-17 11:24:00 +02:00
: ContactsList(searchController: controller.controller),
2021-03-27 18:04:31 +01:00
],
),
),
);
}
}