parent
df592b3f38
commit
ed84410039
13 changed files with 540 additions and 66 deletions
149
lib/widgets/create_room_sheet.dart
Normal file
149
lib/widgets/create_room_sheet.dart
Normal file
|
|
@ -0,0 +1,149 @@
|
|||
import "package:flutter_hooks/flutter_hooks.dart";
|
||||
import "package:hooks_riverpod/hooks_riverpod.dart";
|
||||
import "package:material_ui/material_ui.dart";
|
||||
import "package:nexus/controllers/client_state.dart";
|
||||
import "package:nexus/helpers/extensions/create_room.dart";
|
||||
import "package:nexus/helpers/extensions/get_localpart.dart";
|
||||
import "package:nexus/widgets/sheet_layout.dart";
|
||||
|
||||
class const CreateRoomSheet({super.key}) extends HookConsumerWidget {
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final serverName = ref
|
||||
.watch(ClientStateController.provider)
|
||||
?.userId
|
||||
?.serverName;
|
||||
final nameController = useTextEditingController();
|
||||
final topicController = useTextEditingController();
|
||||
final aliasController = useTextEditingController();
|
||||
final name = useValueListenable(nameController).text.trim();
|
||||
final isPublic = useState(false);
|
||||
final isPublished = useState(false);
|
||||
final isEncrypted = useState(false);
|
||||
final isSpace = useState(false);
|
||||
|
||||
void submit() {
|
||||
if (name.isEmpty) return;
|
||||
|
||||
final topic = topicController.text.trim();
|
||||
final alias = aliasController.text.trim();
|
||||
|
||||
context.createRoom(
|
||||
.new(
|
||||
name: name,
|
||||
topic: topic.isEmpty ? null : topic,
|
||||
roomAliasName: isPublic.value && alias.isNotEmpty ? alias : null,
|
||||
visibility: isPublic.value && isPublished.value ? .public : .private,
|
||||
preset: isPublic.value ? .publicChat : .privateChat,
|
||||
initialState: isEncrypted.value && !isSpace.value
|
||||
? .new([.encryption])
|
||||
: const .empty(),
|
||||
creationContent: isSpace.value ? .new({"type": "m.space"}) : null,
|
||||
),
|
||||
name: name,
|
||||
);
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
|
||||
AnimatedSize animated({
|
||||
required bool visible,
|
||||
required Widget child,
|
||||
double gap = 12,
|
||||
}) => AnimatedSize(
|
||||
duration: Durations.medium2,
|
||||
curve: Curves.easeInOut,
|
||||
alignment: .topCenter,
|
||||
child: visible
|
||||
? Padding(
|
||||
padding: .only(top: gap),
|
||||
child: child,
|
||||
)
|
||||
: const SizedBox(width: .infinity),
|
||||
);
|
||||
|
||||
return SheetLayout(
|
||||
title: Text("Create a ${isSpace.value ? "Space" : "Room"}"),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: Navigator.of(context).pop,
|
||||
child: const Text("Cancel"),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: name.isEmpty ? null : submit,
|
||||
child: const Text("Create"),
|
||||
),
|
||||
],
|
||||
children: [
|
||||
TextField(
|
||||
controller: nameController,
|
||||
autofocus: true,
|
||||
textCapitalization: .sentences,
|
||||
onSubmitted: (_) => submit(),
|
||||
decoration: const .new(labelText: "Name"),
|
||||
),
|
||||
TextField(
|
||||
controller: topicController,
|
||||
textCapitalization: .sentences,
|
||||
decoration: const .new(labelText: "Topic (optional)"),
|
||||
),
|
||||
Column(
|
||||
children: [
|
||||
SwitchListTile(
|
||||
secondary: const Icon(Icons.public),
|
||||
title: const Text("Public"),
|
||||
subtitle: const Text("Anyone can join"),
|
||||
value: isPublic.value,
|
||||
onChanged: (value) {
|
||||
isPublic.value = value;
|
||||
if (value) isEncrypted.value = false;
|
||||
},
|
||||
),
|
||||
animated(
|
||||
visible: isPublic.value,
|
||||
child: TextField(
|
||||
controller: aliasController,
|
||||
decoration: .new(
|
||||
labelText: "Canonical alias (optional)",
|
||||
prefixText: "# ",
|
||||
suffixText: serverName == null ? null : ":$serverName",
|
||||
hintText: "my-room",
|
||||
),
|
||||
),
|
||||
),
|
||||
animated(
|
||||
visible: isPublic.value,
|
||||
child: SwitchListTile(
|
||||
secondary: const Icon(Icons.travel_explore),
|
||||
title: const Text("Publish to room directory"),
|
||||
subtitle: const Text("List it in your homeserver's directory"),
|
||||
value: isPublished.value,
|
||||
onChanged: (value) => isPublished.value = value,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
Column(
|
||||
children: [
|
||||
SwitchListTile(
|
||||
secondary: const Icon(Icons.workspaces),
|
||||
title: const Text("Space"),
|
||||
subtitle: const Text("A group of rooms"),
|
||||
value: isSpace.value,
|
||||
onChanged: (value) => isSpace.value = value,
|
||||
),
|
||||
animated(
|
||||
visible: !isSpace.value,
|
||||
child: SwitchListTile(
|
||||
secondary: const Icon(Icons.lock),
|
||||
title: const Text("Encrypted"),
|
||||
subtitle: const Text("Can't be turned off later"),
|
||||
value: isEncrypted.value,
|
||||
onChanged: (value) => isEncrypted.value = value,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -5,9 +5,12 @@ import "package:material_ui/material_ui.dart";
|
|||
import "package:navigation_rail_m3e/navigation_rail_m3e.dart";
|
||||
import "package:nexus/helpers/extensions/get_all_child_rooms.dart";
|
||||
import "package:nexus/helpers/extensions/room_destinations.dart";
|
||||
import "package:nexus/helpers/extensions/show_adaptive_sheet.dart";
|
||||
import "package:nexus/widgets/adaptive_menu_button.dart";
|
||||
import "package:nexus/models/nav_page.dart";
|
||||
import "package:nexus/widgets/avatar_or_hash.dart";
|
||||
import "package:nexus/widgets/create_room_sheet.dart";
|
||||
import "package:nexus/widgets/start_dm_sheet.dart";
|
||||
import "package:nexus/widgets/divider_widget.dart";
|
||||
import "package:nexus/widgets/error_dialog.dart";
|
||||
import "package:nexus/widgets/pages/settings.dart";
|
||||
|
|
@ -163,13 +166,24 @@ class const SpacesRail({super.key}) extends ConsumerWidget {
|
|||
leading: Icon(Icons.numbers),
|
||||
),
|
||||
),
|
||||
const PopupMenuItem(
|
||||
onTap: null,
|
||||
child: ListTile(
|
||||
title: Text("Create a new room"),
|
||||
PopupMenuItem(
|
||||
onTap: () => context.showAdaptiveSheet(
|
||||
(_) => const CreateRoomSheet(),
|
||||
),
|
||||
child: const ListTile(
|
||||
title: Text("Create a new room (or space)"),
|
||||
leading: Icon(Icons.add),
|
||||
),
|
||||
),
|
||||
PopupMenuItem(
|
||||
onTap: () => context.showAdaptiveSheet(
|
||||
(_) => const StartDmSheet(),
|
||||
),
|
||||
child: const ListTile(
|
||||
title: Text("Start a direct message"),
|
||||
leading: Icon(Icons.person_add),
|
||||
),
|
||||
),
|
||||
],
|
||||
icon: const Icon(Icons.add),
|
||||
),
|
||||
|
|
|
|||
29
lib/widgets/sheet_layout.dart
Normal file
29
lib/widgets/sheet_layout.dart
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import "package:material_ui/material_ui.dart";
|
||||
|
||||
class const SheetLayout({
|
||||
required final Widget title,
|
||||
required final List<Widget> children,
|
||||
required final List<Widget> actions,
|
||||
super.key,
|
||||
}) extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) => Padding(
|
||||
padding: .only(bottom: MediaQuery.viewInsetsOf(context).bottom),
|
||||
child: SingleChildScrollView(
|
||||
padding: const .all(24),
|
||||
child: Column(
|
||||
mainAxisSize: .min,
|
||||
crossAxisAlignment: .stretch,
|
||||
spacing: 12,
|
||||
children: [
|
||||
DefaultTextStyle(
|
||||
style: Theme.of(context).textTheme.headlineSmall!,
|
||||
child: title,
|
||||
),
|
||||
...children,
|
||||
Row(mainAxisAlignment: .end, spacing: 8, children: actions),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
77
lib/widgets/start_dm_sheet.dart
Normal file
77
lib/widgets/start_dm_sheet.dart
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
import "package:flutter_hooks/flutter_hooks.dart";
|
||||
import "package:material_ui/material_ui.dart";
|
||||
import "package:nexus/helpers/extensions/create_room.dart";
|
||||
import "package:nexus/widgets/sheet_layout.dart";
|
||||
|
||||
/// Starts a DM, asking for a user ID unless [userId] is given. Pops with true
|
||||
/// if a DM was started.
|
||||
class const StartDmSheet({final String? userId, final String? name, super.key})
|
||||
extends HookWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final userIdController = useTextEditingController();
|
||||
final text = useValueListenable(userIdController).text.trim();
|
||||
final target = userId ?? text;
|
||||
final valid = RegExp(r"^@[^:\s]+:[^\s]+$").hasMatch(target);
|
||||
final encrypted = useState(true);
|
||||
final reasonController = useTextEditingController();
|
||||
|
||||
void submit() {
|
||||
if (!valid) return;
|
||||
context.startDirectMessage(
|
||||
target,
|
||||
name: name ?? target,
|
||||
encrypted: encrypted.value,
|
||||
inviteReason: reasonController.text.trim().isEmpty
|
||||
? null
|
||||
: reasonController.text.trim(),
|
||||
);
|
||||
Navigator.of(context).pop(true);
|
||||
}
|
||||
|
||||
return SheetLayout(
|
||||
title: const Text("Start a Direct Message"),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: Navigator.of(context).pop,
|
||||
child: const Text("Cancel"),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: valid ? submit : null,
|
||||
child: const Text("Start"),
|
||||
),
|
||||
],
|
||||
children: [
|
||||
if (userId == null)
|
||||
TextField(
|
||||
controller: userIdController,
|
||||
autofocus: true,
|
||||
keyboardType: .emailAddress,
|
||||
onSubmitted: (_) => submit(),
|
||||
decoration: .new(
|
||||
labelText: "User ID",
|
||||
hintText: "@user:example.com",
|
||||
errorText: text.isEmpty || valid ? null : "Enter a valid user ID",
|
||||
),
|
||||
)
|
||||
else
|
||||
Text(
|
||||
"Are you sure you want to start a direct message with ${name ?? userId}?",
|
||||
),
|
||||
TextField(
|
||||
controller: reasonController,
|
||||
textCapitalization: .sentences,
|
||||
onSubmitted: (_) => submit(),
|
||||
decoration: const .new(labelText: "Invite reason (optional)"),
|
||||
),
|
||||
SwitchListTile(
|
||||
secondary: const Icon(Icons.lock),
|
||||
title: const Text("Encrypted"),
|
||||
subtitle: const Text("Can't be turned off later"),
|
||||
value: encrypted.value,
|
||||
onChanged: (value) => encrypted.value = value,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -8,13 +8,17 @@ import "package:nexus/controllers/client.dart";
|
|||
import "package:nexus/controllers/client_state.dart";
|
||||
import "package:nexus/controllers/power_level.dart";
|
||||
import "package:nexus/controllers/profile.dart";
|
||||
import "package:nexus/controllers/rooms.dart";
|
||||
import "package:nexus/helpers/extensions/better_when.dart";
|
||||
import "package:nexus/helpers/extensions/create_room.dart";
|
||||
import "package:nexus/helpers/extensions/show_adaptive_sheet.dart";
|
||||
import "package:nexus/helpers/extensions/get_localpart.dart";
|
||||
import "package:nexus/models/content/membership.dart";
|
||||
import "package:nexus/models/membership_action.dart";
|
||||
import "package:nexus/widgets/avatar_or_hash.dart";
|
||||
import "package:nexus/main.dart";
|
||||
import "package:nexus/widgets/expandable_image.dart";
|
||||
import "package:nexus/widgets/start_dm_sheet.dart";
|
||||
|
||||
final class const UserBottomSheet(
|
||||
final MembershipContent member,
|
||||
|
|
@ -177,13 +181,30 @@ final class const UserBottomSheet(
|
|||
),
|
||||
|
||||
SizedBox(height: 8),
|
||||
if (userId != ref.watch(ClientStateController.provider)?.userId &&
|
||||
roomId != null) ...[
|
||||
if (userId !=
|
||||
ref.watch(ClientStateController.provider)?.userId) ...[
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: M3EButton.icon(
|
||||
onPressed: null,
|
||||
onPressed: () async {
|
||||
final name = member.displayName ?? userId.localpart;
|
||||
final hasDm = ref
|
||||
.read(RoomsController.provider)
|
||||
.values
|
||||
.any((room) => room.metadata?.dmUserId == userId);
|
||||
|
||||
if (hasDm) {
|
||||
context.startDirectMessage(userId, name: name);
|
||||
} else if (await context.showAdaptiveSheet<bool>(
|
||||
(_) => StartDmSheet(userId: userId, name: name),
|
||||
) !=
|
||||
true) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (context.mounted) Navigator.of(context).pop();
|
||||
},
|
||||
shape: .square,
|
||||
style: .tonal,
|
||||
icon: Icon(Icons.message),
|
||||
|
|
@ -193,15 +214,16 @@ final class const UserBottomSheet(
|
|||
],
|
||||
),
|
||||
|
||||
if (ref.watch(
|
||||
PowerLevelController.provider(
|
||||
.membershipAction(
|
||||
action: .kick,
|
||||
roomId: roomId!,
|
||||
targetUser: userId,
|
||||
),
|
||||
),
|
||||
))
|
||||
if (roomId != null &&
|
||||
ref.watch(
|
||||
PowerLevelController.provider(
|
||||
.membershipAction(
|
||||
action: .kick,
|
||||
roomId: roomId!,
|
||||
targetUser: userId,
|
||||
),
|
||||
),
|
||||
))
|
||||
Padding(
|
||||
padding: .only(top: 4),
|
||||
child: Row(
|
||||
|
|
|
|||
Loading…
Reference in a new issue