200 lines
7.3 KiB
Dart
200 lines
7.3 KiB
Dart
import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
|
import "package:material_ui/material_ui.dart";
|
|
import "package:material_adaptive_sheets/material_adaptive_sheets.dart";
|
|
import "package:nexus/helpers/extensions/get_localpart.dart";
|
|
import "package:nexus/helpers/extensions/show_leave_room_sheet.dart";
|
|
import "package:flutter/services.dart";
|
|
import "package:flutter_riverpod/flutter_riverpod.dart";
|
|
import "package:nexus/controllers/account_data.dart";
|
|
import "package:nexus/controllers/client.dart";
|
|
import "package:nexus/models/room_notification_level.dart";
|
|
import "package:nexus/controllers/room_settings_sections.dart";
|
|
import "package:nexus/widgets/settings/settings_layout.dart";
|
|
import "package:nexus/controllers/via.dart";
|
|
import "package:nexus/models/room.dart";
|
|
import "package:flutter_hooks/flutter_hooks.dart";
|
|
import "package:nexus/main.dart";
|
|
|
|
final class const RoomMenuButton(
|
|
final Room? room, {
|
|
final IList<Room> children = const .empty(),
|
|
final VoidCallback? onOpenSearch,
|
|
super.key,
|
|
}) extends ConsumerWidget {
|
|
@override
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
final danger = Theme.of(context).colorScheme.error;
|
|
final client = ref.read(ClientController.provider.notifier);
|
|
|
|
return AdaptiveMenuButton(
|
|
icon: const Icon(Icons.more_vert),
|
|
tooltip: "Room options",
|
|
itemBuilder: (_) => [
|
|
if (onOpenSearch != null)
|
|
PopupMenuItem(
|
|
onTap: onOpenSearch,
|
|
child: ListTile(leading: Icon(Icons.search), title: Text("Search")),
|
|
),
|
|
PopupMenuItem(
|
|
onTap: () async {
|
|
if (room != null) await client.markRead(room!);
|
|
await Future.wait(children.map((child) => client.markRead(child)));
|
|
},
|
|
child: ListTile(
|
|
leading: Icon(Icons.check),
|
|
title: Text("Mark as Read"),
|
|
),
|
|
),
|
|
if (room != null) ...[
|
|
PopupMenuItem(
|
|
onTap: () async {
|
|
final roomId = room!.metadata!.id;
|
|
final accountData = ref.read(
|
|
AccountDataController.provider.notifier,
|
|
);
|
|
final current = RoomNotificationLevel.of(
|
|
ref.read(AccountDataController.provider).pushRules.global,
|
|
roomId,
|
|
);
|
|
|
|
final level = await context.showAdaptiveMenu(
|
|
RoomNotificationLevel.values
|
|
.map(
|
|
(level) => PopupMenuItem(
|
|
value: level,
|
|
child: ListTile(
|
|
selected: level == current,
|
|
leading: Icon(level.icon),
|
|
title: Text(level.title),
|
|
subtitle: Text(level.description),
|
|
trailing: level == current ? Icon(Icons.check) : null,
|
|
),
|
|
),
|
|
)
|
|
.toList(),
|
|
);
|
|
if (level == null) return;
|
|
await accountData
|
|
.setRoomNotificationLevel(roomId, level)
|
|
.onError(showError);
|
|
},
|
|
child: ListTile(
|
|
leading: Icon(Icons.notifications),
|
|
title: Text("Notifications"),
|
|
trailing: Icon(Icons.expand_more),
|
|
),
|
|
),
|
|
PopupMenuItem(
|
|
onTap: () async {
|
|
final vias = ref.read(ViaController.provider(room!)).viaQuery;
|
|
|
|
await Clipboard.setData(
|
|
.new(
|
|
text: "matrix:roomid/${room!.metadata?.id.substring(1)}$vias",
|
|
),
|
|
);
|
|
},
|
|
child: ListTile(
|
|
leading: Icon(Icons.link),
|
|
title: Text("Copy Link"),
|
|
),
|
|
),
|
|
PopupMenuItem(
|
|
onTap: () => context.showAdaptiveSheet(
|
|
(context) => HookBuilder(
|
|
builder: (context) {
|
|
final userIdController = useTextEditingController();
|
|
final reasonController = useTextEditingController();
|
|
final text = useValueListenable(userIdController).text.trim();
|
|
final valid = text.isValidUserId;
|
|
|
|
void submit() {
|
|
if (!valid) return;
|
|
Navigator.of(context).pop();
|
|
final reason = reasonController.text.trim();
|
|
client
|
|
.setMembership(
|
|
.new(
|
|
userId: text,
|
|
roomId: room!.metadata!.id,
|
|
action: .invite,
|
|
reason: reason.isEmpty ? null : reason,
|
|
),
|
|
)
|
|
.onError(showError);
|
|
}
|
|
|
|
return SheetLayout(
|
|
title: Text("Invite User"),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: Navigator.of(context).pop,
|
|
child: Text("Cancel"),
|
|
),
|
|
TextButton(
|
|
onPressed: valid ? submit : null,
|
|
child: Text("Invite"),
|
|
),
|
|
],
|
|
children: [
|
|
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",
|
|
),
|
|
),
|
|
TextField(
|
|
controller: reasonController,
|
|
textCapitalization: .sentences,
|
|
onSubmitted: (_) => submit(),
|
|
decoration: const .new(
|
|
labelText: "Invite reason (optional)",
|
|
),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
),
|
|
),
|
|
child: ListTile(
|
|
leading: Icon(Icons.person_add),
|
|
title: Text("Invite"),
|
|
),
|
|
),
|
|
PopupMenuItem(
|
|
onTap: () => showDialog(
|
|
context: context,
|
|
builder: (_) => SettingsLayout(
|
|
title: "Room Settings",
|
|
sectionsProvider: RoomSettingsSectionsController.provider(
|
|
room!.metadata!.id,
|
|
),
|
|
),
|
|
),
|
|
child: ListTile(
|
|
leading: Icon(Icons.settings),
|
|
title: Text("Settings"),
|
|
),
|
|
),
|
|
PopupMenuItem(
|
|
onTap: () => context.showLeaveRoomSheet(
|
|
room!.metadata!.id,
|
|
name: room!.metadata?.name ?? "Unnamed Room",
|
|
),
|
|
child: ListTile(
|
|
leading: Icon(Icons.logout, color: danger),
|
|
title: Text("Leave", style: .new(color: danger)),
|
|
),
|
|
),
|
|
],
|
|
],
|
|
);
|
|
}
|
|
}
|