fluffychat/lib/widgets/public_room_bottom_sheet.dart

145 lines
5.1 KiB
Dart
Raw Normal View History

2022-01-28 18:21:20 +01:00
//@dart=2.12
2021-11-14 13:56:36 +01:00
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:flutter_matrix_html/flutter_html.dart';
import 'package:future_loading_dialog/future_loading_dialog.dart';
import 'package:matrix/matrix.dart';
import 'package:vrouter/vrouter.dart';
import 'package:fluffychat/config/themes.dart';
import 'package:fluffychat/widgets/content_banner.dart';
import 'package:fluffychat/widgets/matrix.dart';
import '../utils/localized_exception_extension.dart';
class PublicRoomBottomSheet extends StatelessWidget {
final String roomAlias;
final BuildContext outerContext;
2022-01-28 18:21:20 +01:00
final PublicRoomsChunk? chunk;
2021-11-14 13:56:36 +01:00
const PublicRoomBottomSheet({
2022-01-28 18:21:20 +01:00
required this.roomAlias,
required this.outerContext,
2021-11-14 13:56:36 +01:00
this.chunk,
2022-01-28 18:21:20 +01:00
Key? key,
2021-11-14 13:56:36 +01:00
}) : super(key: key);
void _joinRoom(BuildContext context) async {
final client = Matrix.of(context).client;
final result = await showFutureLoadingDialog<String>(
context: context,
future: () => client.joinRoom(roomAlias),
);
if (result.error == null) {
2022-01-28 18:21:20 +01:00
if (client.getRoomById(result.result!) == null) {
2021-11-14 13:56:36 +01:00
await client.onSync.stream.firstWhere(
(sync) => sync.rooms?.join?.containsKey(result.result) ?? false);
}
2022-01-28 18:21:20 +01:00
VRouter.of(context).toSegments(['rooms', result.result!]);
2021-11-14 13:56:36 +01:00
Navigator.of(context, rootNavigator: false).pop();
return;
}
}
bool _testRoom(PublicRoomsChunk r) =>
r.canonicalAlias == roomAlias ||
(r.aliases?.contains(roomAlias) ?? false);
Future<PublicRoomsChunk> _search(BuildContext context) async {
2022-01-28 18:21:20 +01:00
final chunk = this.chunk;
2021-11-14 13:56:36 +01:00
if (chunk != null) return chunk;
final query = await Matrix.of(context).client.queryPublicRooms(
server: roomAlias.domain,
filter: PublicRoomQueryFilter(
genericSearchTerm: roomAlias,
),
);
2022-01-28 18:21:20 +01:00
if (!query.chunk.any(_testRoom)) {
throw (L10n.of(context)!.noRoomsFound);
2021-11-14 13:56:36 +01:00
}
return query.chunk.firstWhere(_testRoom);
}
@override
Widget build(BuildContext context) {
2022-01-28 18:21:20 +01:00
final roomAlias = this.roomAlias;
2021-11-14 13:56:36 +01:00
return Center(
child: SizedBox(
width: min(
MediaQuery.of(context).size.width, FluffyThemes.columnWidth * 1.5),
child: Material(
elevation: 4,
child: SafeArea(
child: Scaffold(
extendBodyBehindAppBar: true,
appBar: AppBar(
elevation: 0,
2021-11-19 18:33:09 +01:00
titleSpacing: 0,
2021-11-14 13:56:36 +01:00
backgroundColor:
Theme.of(context).scaffoldBackgroundColor.withOpacity(0.5),
2021-11-19 18:33:09 +01:00
title: Text(
roomAlias,
overflow: TextOverflow.fade,
),
2021-11-14 13:56:36 +01:00
leading: IconButton(
icon: const Icon(Icons.arrow_downward_outlined),
onPressed: Navigator.of(context, rootNavigator: false).pop,
2022-01-28 18:21:20 +01:00
tooltip: L10n.of(context)!.close,
2021-11-14 13:56:36 +01:00
),
2021-11-19 18:33:09 +01:00
actions: [
TextButton.icon(
onPressed: () => _joinRoom(context),
2022-01-28 18:21:20 +01:00
label: Text(L10n.of(context)!.joinRoom),
2021-11-19 18:33:09 +01:00
icon: const Icon(Icons.login_outlined),
),
],
2021-11-14 13:56:36 +01:00
),
body: FutureBuilder<PublicRoomsChunk>(
future: _search(context),
builder: (context, snapshot) {
final profile = snapshot.data;
2021-11-19 18:33:09 +01:00
return ListView(
padding: EdgeInsets.zero,
2021-11-14 13:56:36 +01:00
children: [
2021-11-19 18:33:09 +01:00
if (profile == null)
Container(
2021-11-20 08:56:40 +01:00
height: 156,
2021-11-19 18:33:09 +01:00
alignment: Alignment.center,
color: Theme.of(context).secondaryHeaderColor,
child: snapshot.hasError
? Text(
2022-01-28 18:21:20 +01:00
snapshot.error!.toLocalizedString(context))
2021-11-19 18:33:09 +01:00
: const CircularProgressIndicator.adaptive(
strokeWidth: 2),
)
else
ContentBanner(
2022-01-28 18:21:20 +01:00
profile.avatarUrl!,
2021-11-19 18:33:09 +01:00
height: 156,
defaultIcon: Icons.person_outline,
client: Matrix.of(context).client,
),
2021-11-14 13:56:36 +01:00
ListTile(
2022-01-28 18:21:20 +01:00
title:
Text(profile?.name ?? roomAlias.localpart ?? ''),
2021-11-14 13:56:36 +01:00
subtitle: Text(
2022-01-28 18:21:20 +01:00
'${L10n.of(context)!.participant}: ${profile?.numJoinedMembers ?? 0}'),
2021-11-14 13:56:36 +01:00
trailing: const Icon(Icons.account_box_outlined),
),
2022-01-28 18:21:20 +01:00
if (profile?.topic?.isNotEmpty ?? false)
2021-11-14 13:56:36 +01:00
ListTile(
2022-01-28 18:21:20 +01:00
subtitle: Html(data: profile!.topic!),
2021-11-14 13:56:36 +01:00
),
],
);
}),
),
),
),
),
);
}
}