nexus/lib/widgets/composer/composer.dart

321 lines
13 KiB
Dart

import "dart:io";
import "package:fast_immutable_collections/fast_immutable_collections.dart";
import "package:file_selector/file_selector.dart";
import "package:nexus/controllers/rooms.dart";
import "package:nexus/widgets/emoji_picker.dart";
import "package:material_ui/material_ui.dart";
import "package:flutter/services.dart";
import "package:flutter_hooks/flutter_hooks.dart";
import "package:fluttertagger/fluttertagger.dart";
import "package:hooks_riverpod/hooks_riverpod.dart";
import "package:nexus/controllers/attachment.dart";
import "package:nexus/controllers/image_picker.dart";
import "package:nexus/controllers/power_level.dart";
import "package:nexus/models/content/message.dart";
import "package:nexus/models/relation_type.dart";
import "package:nexus/widgets/composer/overlays/tagger_overlay.dart";
import "package:nexus/widgets/composer/relation_preview.dart";
import "package:nexus/main.dart";
class const Composer(
final String roomId, {
required final int? relatedEvent,
required final RelationType relationType,
required final VoidCallback onDismiss,
required final Future<void> Function(
String text, {
required bool shouldMention,
required IList<Tag> tags,
})
onSend,
final FocusNode? node,
super.key,
}) extends HookConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final theme = Theme.of(context);
final controller = useMemoized(FlutterTaggerController.new);
final triggerCharacter = useState("");
final shouldMention = useState(true);
final query = useState("");
final relatedEvent = this.relatedEvent == null
? null
: ref.watch(
RoomsController.provider.select((rooms) {
final event = rooms[roomId]?.events[this.relatedEvent!];
if (event == null) return null;
final edited = event.lastEditRowId == 0
? null
: rooms[roomId]?.events[event.lastEditRowId];
return edited == null
? event
: event.copyWith(
content: edited.content,
localContent: edited.localContent,
);
}),
);
final textBefore = useState<String?>(null);
useEffect(() {
if (relationType == .edit && relatedEvent != null) {
textBefore.value ??= controller.text;
final text =
relatedEvent.localContent?.editSource ??
switch (relatedEvent.content) {
TextMessageContent(:final body) => body,
_ => "",
};
controller.value = .new(
text: text,
selection: .collapsed(offset: text.length),
);
} else if (textBefore.value case final draft?) {
controller.value = .new(
text: draft,
selection: .collapsed(offset: draft.length),
);
textBefore.value = null;
}
return null;
}, [relationType, relatedEvent == null]);
final attachment = ref.watch(AttachmentController.provider(roomId));
void send() {
if (controller.value.text.isEmpty && attachment == null ||
attachment != null && attachment.$2 == null) {
return;
}
onSend(
controller.formattedText,
shouldMention: shouldMention.value,
tags: .new(controller.tags),
);
onDismiss();
controller.text = "";
}
final style = TextStyle(
color: theme.colorScheme.primary,
fontWeight: .bold,
);
return Padding(
padding: .all(12),
child: Column(
children: [
if (attachment != null)
Card(
margin: .only(bottom: 8),
child: ListTile(
leading: attachment.$2 == null
? SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator(),
)
: Icon(Icons.file_copy),
title: Text(attachment.$1),
trailing: IconButton(
onPressed: () =>
ref.invalidate(AttachmentController.provider(roomId)),
icon: Icon(Icons.close),
),
),
),
ClipRRect(
borderRadius: .all(.circular(12)),
child: Column(
children: [
RelationPreview(
relatedEvent,
shouldMention: shouldMention.value,
toggleShouldMention: () =>
shouldMention.value = !shouldMention.value,
relationType: relationType,
onDismiss: onDismiss,
),
Container(
color: theme.colorScheme.surfaceContainerHighest,
child: Row(
mainAxisAlignment: .center,
children:
ref.watch(
PowerLevelController.provider(
.new(eventType: .message, roomId: roomId),
),
)
? [
IconButton(
onPressed: () => showModalBottomSheet(
isScrollControlled: true,
context: context,
builder: (context) => EmojiPicker(
onSelection: (value) {
Navigator.of(context).pop();
controller.text += value;
node?.requestFocus();
},
),
),
icon: Icon(Icons.emoji_emotions),
),
PopupMenuButton(
tooltip: "Add media",
enabled: attachment == null,
itemBuilder: (context) => [
if (Platform.isAndroid || Platform.isIOS)
PopupMenuItem(
child: ListTile(
title: Text("Camera"),
leading: Icon(Icons.add_a_photo),
),
onTap: () async => ref
.read(
AttachmentController.provider(roomId)
.notifier,
)
.add(
(await ref
.watch(
ImagePickerController.provider,
)
.pickImage(source: .camera))!,
)
.onError(showError),
),
PopupMenuItem(
child: ListTile(
title: Text("Gallery"),
leading: Icon(Icons.add_photo_alternate),
),
onTap: () async => ref
.read(
AttachmentController.provider(roomId)
.notifier,
)
.add(
(await ref
.watch(
ImagePickerController.provider,
)
.pickImage(source: .gallery))!,
)
.onError(showError),
),
PopupMenuItem(
onTap: () async => ref
.watch(
AttachmentController.provider(roomId)
.notifier,
)
.add((await openFile())!)
.onError(showError),
child: ListTile(
title: Text("Files"),
leading: Icon(Icons.attachment),
),
),
],
icon: Icon(Icons.add),
),
SizedBox(width: 8),
Expanded(
child: FlutterTagger(
triggerStrategy: .eager,
overlay: TaggerOverlay(
query.value,
roomId: roomId,
triggerCharacter: triggerCharacter.value,
addTag: ({required id, required name}) {
controller.addTag(id: id, name: name);
node?.requestFocus();
},
),
controller: controller,
onSearch: (newQuery, newTriggerCharacter) {
triggerCharacter.value = newTriggerCharacter;
query.value = newQuery;
},
triggerCharacterAndStyles: {
"@": style,
"#": style,
":": style,
},
builder: (context, key) => Focus(
onKeyEvent: (_, event) {
if (event is KeyDownEvent &&
event.logicalKey ==
LogicalKeyboardKey.enter) {
final shiftPressed = HardwareKeyboard
.instance
.isShiftPressed;
if (!shiftPressed) {
send();
return KeyEventResult.handled;
}
}
return KeyEventResult.ignored;
},
child: TextField(
maxLines: 12,
minLines: 1,
textCapitalization: .sentences,
autofocus:
(Platform.isLinux ||
Platform.isMacOS ||
Platform.isWindows),
decoration: .new(
hintText: "Your message here...",
border: .none,
),
controller: controller,
key: key,
focusNode: node,
),
),
),
),
IconButton(
onPressed:
attachment != null && attachment.$2 == null
? null
: send,
icon: Icon(Icons.send),
tooltip: "Send message",
),
]
: [
Expanded(
child: Padding(
padding: .symmetric(
horizontal: 8,
vertical: 12,
),
child: Text(
"You don't have permission to send messages in this room...",
),
),
),
],
),
),
],
),
),
],
),
);
}
}