fluffychat/lib/pages/chat_details/chat_details.dart

346 lines
11 KiB
Dart
Raw Normal View History

2021-10-26 18:50:34 +02:00
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
2021-04-03 13:09:20 +02:00
2021-10-26 18:50:34 +02:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:file_picker_cross/file_picker_cross.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:future_loading_dialog/future_loading_dialog.dart';
import 'package:image_picker/image_picker.dart';
import 'package:matrix/matrix.dart';
2021-10-26 18:50:34 +02:00
import 'package:vrouter/vrouter.dart';
2020-10-04 17:01:54 +02:00
2021-11-09 21:32:16 +01:00
import 'package:fluffychat/pages/chat_details/chat_details_view.dart';
2021-11-15 07:43:19 +01:00
import 'package:fluffychat/pages/settings/settings.dart';
2022-12-30 17:54:01 +01:00
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart';
import 'package:fluffychat/utils/platform_infos.dart';
2021-10-26 18:50:34 +02:00
import 'package:fluffychat/widgets/matrix.dart';
2021-05-01 09:58:09 +02:00
enum AliasActions { copy, delete, setCanonical }
class ChatDetails extends StatefulWidget {
2022-01-29 12:35:03 +01:00
const ChatDetails({Key? key}) : super(key: key);
2020-01-01 19:10:13 +01:00
@override
ChatDetailsController createState() => ChatDetailsController();
}
2020-01-01 19:10:13 +01:00
class ChatDetailsController extends State<ChatDetails> {
2022-01-29 12:35:03 +01:00
List<User>? members;
2021-11-13 21:42:35 +01:00
bool displaySettings = false;
void toggleDisplaySettings() =>
setState(() => displaySettings = !displaySettings);
2020-01-01 19:10:13 +01:00
2022-01-29 12:35:03 +01:00
String? get roomId => VRouter.of(context).pathParameters['roomid'];
void setDisplaynameAction() async {
2022-01-29 12:35:03 +01:00
final room = Matrix.of(context).client.getRoomById(roomId!)!;
final input = await showTextInputDialog(
2021-05-23 15:02:36 +02:00
useRootNavigator: false,
context: context,
2022-01-29 12:35:03 +01:00
title: L10n.of(context)!.changeTheNameOfTheGroup,
okLabel: L10n.of(context)!.ok,
cancelLabel: L10n.of(context)!.cancel,
textFields: [
DialogTextField(
initialText: room.getLocalizedDisplayname(
MatrixLocals(
2022-01-29 12:35:03 +01:00
L10n.of(context)!,
),
),
)
],
);
if (input == null) return;
final success = await showFutureLoadingDialog(
context: context,
future: () => room.setName(input.single),
);
if (success.error == null) {
2021-05-23 13:11:55 +02:00
ScaffoldMessenger.of(context).showSnackBar(
2022-01-29 12:35:03 +01:00
SnackBar(content: Text(L10n.of(context)!.displaynameHasBeenChanged)));
}
}
2021-05-01 09:58:09 +02:00
void editAliases() async {
2022-01-29 12:35:03 +01:00
final room = Matrix.of(context).client.getRoomById(roomId!);
2021-05-01 09:58:09 +02:00
// The current endpoint doesnt seem to be implemented in Synapse. This may
// change in the future and then we just need to switch to this api call:
//
// final aliases = await showFutureLoadingDialog(
// context: context,
// future: () => room.client.requestRoomAliases(room.id),
// );
//
// While this is not working we use the unstable api:
final aliases = await showFutureLoadingDialog(
context: context,
2022-01-29 12:35:03 +01:00
future: () => room!.client
2021-05-01 09:58:09 +02:00
.request(
RequestType.GET,
'/client/unstable/org.matrix.msc2432/rooms/${Uri.encodeComponent(room.id)}/aliases',
)
.then((response) => List<String>.from(response['aliases'])),
);
// Switch to the stable api once it is implemented.
if (aliases.error != null) return;
2022-01-29 12:35:03 +01:00
final adminMode = room!.canSendEvent('m.room.canonical_alias');
if (aliases.result!.isEmpty && (room.canonicalAlias.isNotEmpty)) {
aliases.result!.add(room.canonicalAlias);
2021-05-01 09:58:09 +02:00
}
2022-01-29 12:35:03 +01:00
if (aliases.result!.isEmpty && adminMode) {
2021-05-01 09:58:09 +02:00
return setAliasAction();
}
final select = await showConfirmationDialog(
2021-05-23 15:02:36 +02:00
useRootNavigator: false,
2021-05-01 09:58:09 +02:00
context: context,
2022-01-29 12:35:03 +01:00
title: L10n.of(context)!.editRoomAliases,
2021-05-01 09:58:09 +02:00
actions: [
if (adminMode)
2022-01-29 12:35:03 +01:00
AlertDialogAction(label: L10n.of(context)!.create, key: 'new'),
...aliases.result!
2021-05-01 09:58:09 +02:00
.map((alias) => AlertDialogAction(key: alias, label: alias))
.toList(),
],
);
if (select == null) return;
if (select == 'new') {
return setAliasAction();
}
final option = await showConfirmationDialog<AliasActions>(
context: context,
title: select,
actions: [
AlertDialogAction(
2022-01-29 12:35:03 +01:00
label: L10n.of(context)!.copyToClipboard,
2021-05-01 09:58:09 +02:00
key: AliasActions.copy,
isDefaultAction: true,
),
if (adminMode) ...{
AlertDialogAction(
2022-01-29 12:35:03 +01:00
label: L10n.of(context)!.setAsCanonicalAlias,
2021-05-01 09:58:09 +02:00
key: AliasActions.setCanonical,
isDestructiveAction: true,
),
AlertDialogAction(
2022-01-29 12:35:03 +01:00
label: L10n.of(context)!.delete,
2021-05-01 09:58:09 +02:00
key: AliasActions.delete,
isDestructiveAction: true,
),
},
],
);
2022-01-29 12:35:03 +01:00
if (option == null) return;
2021-05-01 09:58:09 +02:00
switch (option) {
case AliasActions.copy:
await Clipboard.setData(ClipboardData(text: select));
2021-05-23 13:11:55 +02:00
ScaffoldMessenger.of(context).showSnackBar(
2022-01-29 12:35:03 +01:00
SnackBar(content: Text(L10n.of(context)!.copiedToClipboard)),
2021-05-01 09:58:09 +02:00
);
break;
case AliasActions.delete:
await showFutureLoadingDialog(
context: context,
2021-05-20 13:59:55 +02:00
future: () => room.client.deleteRoomAlias(select),
2021-05-01 09:58:09 +02:00
);
break;
case AliasActions.setCanonical:
await showFutureLoadingDialog(
context: context,
future: () => room.client.setRoomStateWithKey(
room.id,
EventTypes.RoomCanonicalAlias,
'',
{
'alias': select,
},
),
2021-05-01 09:58:09 +02:00
);
break;
}
}
void setAliasAction() async {
2022-01-29 12:35:03 +01:00
final room = Matrix.of(context).client.getRoomById(roomId!)!;
final domain = room.client.userID!.domain;
2021-05-01 09:58:09 +02:00
final input = await showTextInputDialog(
2021-05-23 15:02:36 +02:00
useRootNavigator: false,
context: context,
2022-01-29 12:35:03 +01:00
title: L10n.of(context)!.setInvitationLink,
okLabel: L10n.of(context)!.ok,
cancelLabel: L10n.of(context)!.cancel,
textFields: [
DialogTextField(
2021-05-01 09:58:09 +02:00
prefixText: '#',
suffixText: domain,
2022-01-29 12:35:03 +01:00
hintText: L10n.of(context)!.alias,
initialText: room.canonicalAlias.localpart,
)
],
);
if (input == null) return;
await showFutureLoadingDialog(
context: context,
2021-05-20 13:59:55 +02:00
future: () =>
2022-08-14 16:59:21 +02:00
room.client.setRoomAlias('#${input.single}:${domain!}', room.id),
);
}
2021-04-14 13:58:40 +02:00
void setTopicAction() async {
2022-01-29 12:35:03 +01:00
final room = Matrix.of(context).client.getRoomById(roomId!)!;
final input = await showTextInputDialog(
2021-05-23 15:02:36 +02:00
useRootNavigator: false,
context: context,
2022-01-29 12:35:03 +01:00
title: L10n.of(context)!.setGroupDescription,
okLabel: L10n.of(context)!.ok,
cancelLabel: L10n.of(context)!.cancel,
textFields: [
DialogTextField(
2022-01-29 12:35:03 +01:00
hintText: L10n.of(context)!.setGroupDescription,
initialText: room.topic,
minLines: 1,
maxLines: 4,
)
],
);
if (input == null) return;
final success = await showFutureLoadingDialog(
context: context,
future: () => room.setDescription(input.single),
);
if (success.error == null) {
2021-05-23 13:11:55 +02:00
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
2022-01-29 12:35:03 +01:00
content: Text(L10n.of(context)!.groupDescriptionHasBeenChanged)));
}
}
void setGuestAccessAction(GuestAccess guestAccess) => showFutureLoadingDialog(
context: context,
future: () => Matrix.of(context)
.client
2022-01-29 12:35:03 +01:00
.getRoomById(roomId!)!
.setGuestAccess(guestAccess),
);
void setHistoryVisibilityAction(HistoryVisibility historyVisibility) =>
showFutureLoadingDialog(
context: context,
future: () => Matrix.of(context)
.client
2022-01-29 12:35:03 +01:00
.getRoomById(roomId!)!
.setHistoryVisibility(historyVisibility),
);
void setJoinRulesAction(JoinRules joinRule) => showFutureLoadingDialog(
context: context,
future: () => Matrix.of(context)
.client
2022-01-29 12:35:03 +01:00
.getRoomById(roomId!)!
.setJoinRules(joinRule),
);
void goToEmoteSettings() async {
2022-01-29 12:35:03 +01:00
final room = Matrix.of(context).client.getRoomById(roomId!)!;
// okay, we need to test if there are any emote state events other than the default one
// if so, we need to be directed to a selection screen for which pack we want to look at
// otherwise, we just open the normal one.
if ((room.states['im.ponies.room_emotes'] ?? <String, Event>{})
.keys
.any((String s) => s.isNotEmpty)) {
2021-08-04 10:15:42 +02:00
VRouter.of(context).to('multiple_emotes');
} else {
2021-08-04 10:15:42 +02:00
VRouter.of(context).to('emotes');
}
}
void setAvatarAction() async {
2022-01-29 12:35:03 +01:00
final room = Matrix.of(context).client.getRoomById(roomId!);
2021-11-15 07:43:19 +01:00
final actions = [
if (PlatformInfos.isMobile)
SheetAction(
key: AvatarAction.camera,
2022-01-29 12:35:03 +01:00
label: L10n.of(context)!.openCamera,
2021-11-15 07:43:19 +01:00
isDefaultAction: true,
icon: Icons.camera_alt_outlined,
),
SheetAction(
key: AvatarAction.file,
2022-01-29 12:35:03 +01:00
label: L10n.of(context)!.openGallery,
2021-11-15 07:43:19 +01:00
icon: Icons.photo_outlined,
),
if (room?.avatar != null)
SheetAction(
key: AvatarAction.remove,
2022-01-29 12:35:03 +01:00
label: L10n.of(context)!.delete,
2021-11-15 07:43:19 +01:00
isDestructiveAction: true,
icon: Icons.delete_outlined,
),
];
final action = actions.length == 1
? actions.single.key
2021-11-15 07:43:19 +01:00
: await showModalActionSheet<AvatarAction>(
context: context,
2022-01-29 12:35:03 +01:00
title: L10n.of(context)!.editRoomAvatar,
2021-11-15 07:43:19 +01:00
actions: actions,
);
if (action == null) return;
if (action == AvatarAction.remove) {
await showFutureLoadingDialog(
context: context,
2022-01-29 12:35:03 +01:00
future: () => room!.setAvatar(null),
2021-11-15 07:43:19 +01:00
);
return;
}
MatrixFile file;
if (PlatformInfos.isMobile) {
final result = await ImagePicker().pickImage(
source: action == AvatarAction.camera
? ImageSource.camera
: ImageSource.gallery,
imageQuality: 50,
);
if (result == null) return;
file = MatrixFile(
bytes: await result.readAsBytes(),
name: result.path,
);
} else {
2021-11-15 07:43:19 +01:00
final result =
await FilePickerCross.importFromStorage(type: FileTypeCross.image);
2022-01-29 12:35:03 +01:00
if (result.fileName == null) return;
file = MatrixFile(
bytes: result.toUint8List(),
2022-01-29 12:35:03 +01:00
name: result.fileName!,
);
}
2021-11-15 07:43:19 +01:00
await showFutureLoadingDialog(
context: context,
2022-01-29 12:35:03 +01:00
future: () => room!.setAvatar(file),
);
2020-01-01 19:10:13 +01:00
}
void requestMoreMembersAction() async {
2022-01-29 12:35:03 +01:00
final room = Matrix.of(context).client.getRoomById(roomId!);
final participants = await showFutureLoadingDialog(
2022-01-29 12:35:03 +01:00
context: context, future: () => room!.requestParticipants());
if (participants.error == null) {
setState(() => members = participants.result);
}
}
2021-10-26 18:47:05 +02:00
static const fixedWidth = 360.0;
@override
2021-05-23 13:11:55 +02:00
Widget build(BuildContext context) {
2022-01-29 12:35:03 +01:00
members ??=
Matrix.of(context).client.getRoomById(roomId!)!.getParticipants();
2021-10-14 18:09:30 +02:00
return SizedBox(
2021-10-26 18:47:05 +02:00
width: fixedWidth,
2021-05-23 13:11:55 +02:00
child: ChatDetailsView(this),
);
}
2020-01-01 19:10:13 +01:00
}