fluffychat/lib/pages/chat_details/chat_details.dart

192 lines
5.7 KiB
Dart
Raw Normal View History

2021-10-26 16:50:34 +00:00
import 'package:flutter/material.dart';
2021-04-03 11:09:20 +00:00
2021-10-26 16:50:34 +00:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:collection/collection.dart';
import 'package:file_picker/file_picker.dart';
2021-10-26 16:50:34 +00:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:future_loading_dialog/future_loading_dialog.dart';
import 'package:go_router/go_router.dart';
2021-10-26 16:50:34 +00:00
import 'package:image_picker/image_picker.dart';
import 'package:matrix/matrix.dart';
2020-10-04 15:01:54 +00:00
2021-11-09 20:32:16 +00:00
import 'package:fluffychat/pages/chat_details/chat_details_view.dart';
2021-11-15 06:43:19 +00:00
import 'package:fluffychat/pages/settings/settings.dart';
2022-12-30 16:54:01 +00:00
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/widgets/app_lock.dart';
2021-10-26 16:50:34 +00:00
import 'package:fluffychat/widgets/matrix.dart';
2021-05-01 07:58:09 +00:00
enum AliasActions { copy, delete, setCanonical }
class ChatDetails extends StatefulWidget {
2023-08-07 16:40:02 +00:00
final String roomId;
final Widget? embeddedCloseButton;
2023-08-07 16:40:02 +00:00
const ChatDetails({
super.key,
2023-08-07 16:40:02 +00:00
required this.roomId,
this.embeddedCloseButton,
});
2020-01-01 18:10:13 +00:00
@override
ChatDetailsController createState() => ChatDetailsController();
}
2020-01-01 18:10:13 +00:00
class ChatDetailsController extends State<ChatDetails> {
2021-11-13 20:42:35 +00:00
bool displaySettings = false;
void toggleDisplaySettings() =>
setState(() => displaySettings = !displaySettings);
2020-01-01 18:10:13 +00:00
2023-08-07 16:40:02 +00:00
String? get roomId => widget.roomId;
void setDisplaynameAction() async {
2022-01-29 11:35:03 +00:00
final room = Matrix.of(context).client.getRoomById(roomId!)!;
final input = await showTextInputDialog(
context: context,
2022-01-29 11:35:03 +00: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 11:35:03 +00:00
L10n.of(context)!,
),
),
2023-08-18 05:24:31 +00:00
),
],
);
if (input == null) return;
final success = await showFutureLoadingDialog(
context: context,
future: () => room.setName(input.single),
);
if (success.error == null) {
2021-05-23 11:11:55 +00:00
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(L10n.of(context)!.displaynameHasBeenChanged)),
);
}
}
void setTopicAction() async {
2022-01-29 11:35:03 +00:00
final room = Matrix.of(context).client.getRoomById(roomId!)!;
final input = await showTextInputDialog(
context: context,
title: L10n.of(context)!.setChatDescription,
2022-01-29 11:35:03 +00:00
okLabel: L10n.of(context)!.ok,
cancelLabel: L10n.of(context)!.cancel,
textFields: [
DialogTextField(
hintText: L10n.of(context)!.noChatDescriptionYet,
initialText: room.topic,
minLines: 4,
maxLines: 8,
2023-08-18 05:24:31 +00:00
),
],
);
if (input == null) return;
final success = await showFutureLoadingDialog(
context: context,
future: () => room.setDescription(input.single),
);
if (success.error == null) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(L10n.of(context)!.chatDescriptionHasBeenChanged),
),
);
}
}
void goToEmoteSettings() async {
2022-01-29 11:35:03 +00: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)) {
context.push('/rooms/${room.id}/details/multiple_emotes');
} else {
context.push('/rooms/${room.id}/details/emotes');
}
}
void setAvatarAction() async {
2022-01-29 11:35:03 +00:00
final room = Matrix.of(context).client.getRoomById(roomId!);
2021-11-15 06:43:19 +00:00
final actions = [
if (PlatformInfos.isMobile)
SheetAction(
key: AvatarAction.camera,
2022-01-29 11:35:03 +00:00
label: L10n.of(context)!.openCamera,
2021-11-15 06:43:19 +00:00
isDefaultAction: true,
icon: Icons.camera_alt_outlined,
),
SheetAction(
key: AvatarAction.file,
2022-01-29 11:35:03 +00:00
label: L10n.of(context)!.openGallery,
2021-11-15 06:43:19 +00:00
icon: Icons.photo_outlined,
),
if (room?.avatar != null)
SheetAction(
key: AvatarAction.remove,
2022-01-29 11:35:03 +00:00
label: L10n.of(context)!.delete,
2021-11-15 06:43:19 +00:00
isDestructiveAction: true,
icon: Icons.delete_outlined,
),
];
final action = actions.length == 1
? actions.single.key
2021-11-15 06:43:19 +00:00
: await showModalActionSheet<AvatarAction>(
context: context,
2022-01-29 11:35:03 +00:00
title: L10n.of(context)!.editRoomAvatar,
2021-11-15 06:43:19 +00:00
actions: actions,
);
if (action == null) return;
if (action == AvatarAction.remove) {
await showFutureLoadingDialog(
context: context,
2022-01-29 11:35:03 +00:00
future: () => room!.setAvatar(null),
2021-11-15 06:43:19 +00: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 {
final picked = await AppLock.of(context).pauseWhile(
FilePicker.platform.pickFiles(
type: FileType.image,
withData: true,
),
);
final pickedFile = picked?.files.firstOrNull;
if (pickedFile == null) return;
file = MatrixFile(
bytes: pickedFile.bytes!,
name: pickedFile.name,
);
}
2021-11-15 06:43:19 +00:00
await showFutureLoadingDialog(
context: context,
2022-01-29 11:35:03 +00:00
future: () => room!.setAvatar(file),
);
2020-01-01 18:10:13 +00:00
}
2021-10-26 16:47:05 +00:00
static const fixedWidth = 360.0;
@override
Widget build(BuildContext context) => ChatDetailsView(this);
2020-01-01 18:10:13 +00:00
}