2021-11-14 13:56:36 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
2023-05-17 12:26:59 +02:00
|
|
|
import 'package:flutter_linkify/flutter_linkify.dart';
|
2021-11-14 13:56:36 +01:00
|
|
|
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
|
|
|
import 'package:matrix/matrix.dart';
|
|
|
|
import 'package:vrouter/vrouter.dart';
|
|
|
|
|
2022-07-24 19:02:14 +02:00
|
|
|
import 'package:fluffychat/utils/url_launcher.dart';
|
2023-01-08 11:41:42 +01:00
|
|
|
import 'package:fluffychat/widgets/avatar.dart';
|
2021-11-14 13:56:36 +01:00
|
|
|
import 'package:fluffychat/widgets/matrix.dart';
|
|
|
|
import '../utils/localized_exception_extension.dart';
|
|
|
|
|
|
|
|
class PublicRoomBottomSheet extends StatelessWidget {
|
2022-07-24 19:02:14 +02:00
|
|
|
final String? roomAlias;
|
2021-11-14 13:56:36 +01:00
|
|
|
final BuildContext outerContext;
|
2022-01-28 18:21:20 +01:00
|
|
|
final PublicRoomsChunk? chunk;
|
2022-07-24 19:02:14 +02:00
|
|
|
final VoidCallback? onRoomJoined;
|
|
|
|
|
|
|
|
PublicRoomBottomSheet({
|
|
|
|
this.roomAlias,
|
2022-01-28 18:21:20 +01:00
|
|
|
required this.outerContext,
|
2021-11-14 13:56:36 +01:00
|
|
|
this.chunk,
|
2022-07-24 19:02:14 +02:00
|
|
|
this.onRoomJoined,
|
2022-01-28 18:21:20 +01:00
|
|
|
Key? key,
|
2022-07-24 19:02:14 +02:00
|
|
|
}) : super(key: key) {
|
|
|
|
assert(roomAlias != null || chunk != null);
|
|
|
|
}
|
2021-11-14 13:56:36 +01:00
|
|
|
|
|
|
|
void _joinRoom(BuildContext context) async {
|
|
|
|
final client = Matrix.of(context).client;
|
2023-03-15 19:39:05 +01:00
|
|
|
final chunk = this.chunk;
|
|
|
|
final navigator = Navigator.of(context);
|
|
|
|
final router = VRouter.of(context);
|
2021-11-14 13:56:36 +01:00
|
|
|
final result = await showFutureLoadingDialog<String>(
|
|
|
|
context: context,
|
2023-03-15 19:39:05 +01:00
|
|
|
future: () async {
|
|
|
|
if (chunk != null && client.getRoomById(chunk.roomId) != null) {
|
|
|
|
return chunk.roomId;
|
|
|
|
}
|
|
|
|
final roomId = chunk != null && chunk.joinRule == 'knock'
|
|
|
|
? await client.knockRoom(chunk.roomId)
|
|
|
|
: await client.joinRoom(roomAlias ?? chunk!.roomId);
|
|
|
|
|
|
|
|
if (client.getRoomById(roomId) == null) {
|
|
|
|
await client.onSync.stream.firstWhere(
|
|
|
|
(sync) => sync.rooms?.join?.containsKey(roomId) ?? false,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return roomId;
|
|
|
|
},
|
2021-11-14 13:56:36 +01:00
|
|
|
);
|
|
|
|
if (result.error == null) {
|
2023-03-15 19:39:05 +01:00
|
|
|
navigator.pop();
|
2022-07-24 19:02:14 +02:00
|
|
|
// don't open the room if the joined room is a space
|
|
|
|
if (!client.getRoomById(result.result!)!.isSpace) {
|
2023-03-15 19:39:05 +01:00
|
|
|
router.toSegments(['rooms', result.result!]);
|
2022-07-24 19:02:14 +02:00
|
|
|
}
|
2021-11-14 13:56:36 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-30 13:44:05 +02:00
|
|
|
bool _testRoom(PublicRoomsChunk r) => r.canonicalAlias == roomAlias;
|
2021-11-14 13:56:36 +01:00
|
|
|
|
|
|
|
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(
|
2022-07-24 19:02:14 +02:00
|
|
|
server: roomAlias!.domain,
|
2021-11-14 13:56:36 +01:00
|
|
|
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;
|
2023-01-07 10:29:34 +01:00
|
|
|
return SafeArea(
|
|
|
|
child: Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: Text(
|
|
|
|
roomAlias ?? chunk!.name ?? chunk!.roomId,
|
|
|
|
overflow: TextOverflow.fade,
|
|
|
|
),
|
|
|
|
leading: IconButton(
|
|
|
|
icon: const Icon(Icons.arrow_downward_outlined),
|
|
|
|
onPressed: Navigator.of(context, rootNavigator: false).pop,
|
|
|
|
tooltip: L10n.of(context)!.close,
|
2021-11-14 13:56:36 +01:00
|
|
|
),
|
2023-01-07 10:29:34 +01:00
|
|
|
actions: [
|
2023-01-08 11:41:42 +01:00
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: OutlinedButton.icon(
|
|
|
|
onPressed: () => _joinRoom(context),
|
|
|
|
label: Text(L10n.of(context)!.joinRoom),
|
|
|
|
icon: const Icon(Icons.login_outlined),
|
|
|
|
),
|
2023-01-07 10:29:34 +01:00
|
|
|
),
|
|
|
|
],
|
2021-11-14 13:56:36 +01:00
|
|
|
),
|
2023-01-07 10:29:34 +01:00
|
|
|
body: FutureBuilder<PublicRoomsChunk>(
|
2023-03-02 10:57:52 +01:00
|
|
|
future: _search(context),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
final profile = snapshot.data;
|
|
|
|
return ListView(
|
|
|
|
padding: EdgeInsets.zero,
|
|
|
|
children: [
|
|
|
|
if (profile == null)
|
|
|
|
Container(
|
|
|
|
height: 156,
|
|
|
|
alignment: Alignment.center,
|
|
|
|
color: Theme.of(context).secondaryHeaderColor,
|
|
|
|
child: snapshot.hasError
|
|
|
|
? Text(snapshot.error!.toLocalizedString(context))
|
|
|
|
: const CircularProgressIndicator.adaptive(
|
|
|
|
strokeWidth: 2,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
else
|
|
|
|
Center(
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.all(16.0),
|
|
|
|
child: Avatar(
|
|
|
|
mxContent: profile.avatarUrl,
|
|
|
|
name: profile.name ?? roomAlias,
|
|
|
|
size: Avatar.defaultSize * 3,
|
|
|
|
fontSize: 36,
|
2023-01-08 11:41:42 +01:00
|
|
|
),
|
2023-01-07 10:29:34 +01:00
|
|
|
),
|
2023-03-02 10:57:52 +01:00
|
|
|
),
|
|
|
|
ListTile(
|
|
|
|
title: Text(
|
|
|
|
profile?.name ??
|
2023-01-07 10:29:34 +01:00
|
|
|
roomAlias?.localpart ??
|
|
|
|
chunk!.roomId.localpart ??
|
2023-03-02 10:57:52 +01:00
|
|
|
'',
|
|
|
|
),
|
|
|
|
subtitle: Text(
|
|
|
|
'${L10n.of(context)!.participant}: ${profile?.numJoinedMembers ?? 0}',
|
2023-01-07 10:29:34 +01:00
|
|
|
),
|
2023-03-02 10:57:52 +01:00
|
|
|
trailing: const Icon(Icons.account_box_outlined),
|
|
|
|
),
|
|
|
|
if (profile?.topic?.isNotEmpty ?? false)
|
|
|
|
ListTile(
|
|
|
|
title: Text(
|
|
|
|
L10n.of(context)!.groupDescription,
|
|
|
|
style: TextStyle(
|
|
|
|
color: Theme.of(context).colorScheme.secondary,
|
2023-01-07 10:29:34 +01:00
|
|
|
),
|
2023-03-02 10:57:52 +01:00
|
|
|
),
|
2023-05-17 12:26:59 +02:00
|
|
|
subtitle: Linkify(
|
2023-03-02 10:57:52 +01:00
|
|
|
text: profile!.topic!,
|
|
|
|
linkStyle: const TextStyle(color: Colors.blueAccent),
|
2023-05-17 12:26:59 +02:00
|
|
|
style: TextStyle(
|
2023-03-02 10:57:52 +01:00
|
|
|
fontSize: 14,
|
|
|
|
color: Theme.of(context).textTheme.bodyMedium!.color,
|
2023-01-07 10:29:34 +01:00
|
|
|
),
|
2023-05-23 08:22:34 +02:00
|
|
|
options: const LinkifyOptions(humanize: false),
|
2023-05-17 12:26:59 +02:00
|
|
|
onOpen: (url) =>
|
|
|
|
UrlLauncher(context, url.url).launchUrl(),
|
2023-01-07 10:29:34 +01:00
|
|
|
),
|
2023-03-02 10:57:52 +01:00
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2021-11-14 13:56:36 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|