Refactor dialog stuff
This commit is contained in:
parent
185ee37f04
commit
a8383951ba
6 changed files with 83 additions and 90 deletions
27
lib/helpers/extensions/focus_room.dart
Normal file
27
lib/helpers/extensions/focus_room.dart
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import "package:collection/collection.dart";
|
||||
import "package:flutter_riverpod/flutter_riverpod.dart";
|
||||
import "package:nexus/controllers/key_controller.dart";
|
||||
import "package:nexus/controllers/spaces_controller.dart";
|
||||
|
||||
extension FocusRoom on WidgetRef {
|
||||
Future<void> focusRoom(String id) async {
|
||||
final spaces = watch(SpacesController.provider);
|
||||
final space = spaces.firstWhereOrNull((space) => space.id == id);
|
||||
|
||||
await watch(KeyController.provider(KeyController.spaceKey).notifier).set(
|
||||
space?.id ??
|
||||
spaces
|
||||
.firstWhere(
|
||||
(space) =>
|
||||
space.children.any((child) => child.metadata?.id == id),
|
||||
)
|
||||
.id,
|
||||
);
|
||||
|
||||
if (space == null) {
|
||||
await watch(
|
||||
KeyController.provider(KeyController.roomKey).notifier,
|
||||
).set(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,10 +1,8 @@
|
|||
import "package:collection/collection.dart";
|
||||
import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
||||
import "package:flutter/material.dart";
|
||||
import "package:hooks_riverpod/hooks_riverpod.dart";
|
||||
import "package:nexus/controllers/client_controller.dart";
|
||||
import "package:nexus/controllers/key_controller.dart";
|
||||
import "package:nexus/controllers/spaces_controller.dart";
|
||||
import "package:nexus/helpers/extensions/focus_room.dart";
|
||||
import "package:nexus/helpers/extensions/link_to_mention.dart";
|
||||
import "package:nexus/models/requests/join_room_request.dart";
|
||||
|
||||
|
|
@ -14,7 +12,7 @@ extension JoinRoomWithSnackbars on ClientController {
|
|||
String roomAlias,
|
||||
WidgetRef ref,
|
||||
) async {
|
||||
final roomIdOrAlias = roomAlias.mention ?? roomAlias;
|
||||
final roomIdOrAlias = roomAlias.mention;
|
||||
// TODO: Parse vias properly
|
||||
|
||||
final scaffoldMessenger = ScaffoldMessenger.of(context);
|
||||
|
|
@ -41,33 +39,7 @@ extension JoinRoomWithSnackbars on ClientController {
|
|||
content: Text("Room $roomIdOrAlias successfully joined."),
|
||||
action: SnackBarAction(
|
||||
label: "Open",
|
||||
onPressed: () async {
|
||||
final spaces = ref.watch(SpacesController.provider);
|
||||
final space = spaces.firstWhereOrNull((space) => space.id == id);
|
||||
|
||||
await ref
|
||||
.watch(
|
||||
KeyController.provider(KeyController.spaceKey).notifier,
|
||||
)
|
||||
.set(
|
||||
space?.id ??
|
||||
spaces
|
||||
.firstWhere(
|
||||
(space) => space.children.any(
|
||||
(child) => child.metadata?.id == id,
|
||||
),
|
||||
)
|
||||
.id,
|
||||
);
|
||||
|
||||
if (space == null) {
|
||||
await ref
|
||||
.watch(
|
||||
KeyController.provider(KeyController.roomKey).notifier,
|
||||
)
|
||||
.set(id);
|
||||
}
|
||||
},
|
||||
onPressed: () => ref.focusRoom(id),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ extension LinkToMention on String {
|
|||
///
|
||||
/// Returns the decoded identifier (e.g. "#room:matrix.org")
|
||||
/// or null if this is not a Matrix link.
|
||||
String? get mention {
|
||||
String get mention {
|
||||
final trimmed = trim();
|
||||
|
||||
final matrixTo = RegExp(
|
||||
|
|
@ -39,6 +39,6 @@ extension LinkToMention on String {
|
|||
} catch (_) {}
|
||||
}
|
||||
|
||||
return null;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,11 +10,9 @@ class MentionChip extends ConsumerWidget {
|
|||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final mention = label.mention;
|
||||
final membership =
|
||||
mention?.startsWith("@") == true || label.startsWith("@") == true
|
||||
final membership = label.mention.startsWith("@") == true
|
||||
? ref
|
||||
.watch(UserController.provider(mention ?? label))
|
||||
.watch(UserController.provider(label.mention))
|
||||
.whenOrNull(data: (data) => data)
|
||||
: null;
|
||||
|
||||
|
|
@ -31,8 +29,7 @@ class MentionChip extends ConsumerWidget {
|
|||
child: Chip(
|
||||
label: Text(
|
||||
(membership == null ? null : "@${membership.displayName}") ??
|
||||
mention ??
|
||||
label,
|
||||
label.mention,
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Theme.of(context).colorScheme.onPrimary,
|
||||
|
|
|
|||
46
lib/widgets/chat_page/join_dialog.dart
Normal file
46
lib/widgets/chat_page/join_dialog.dart
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
import "package:flutter/material.dart";
|
||||
import "package:flutter_hooks/flutter_hooks.dart";
|
||||
import "package:hooks_riverpod/hooks_riverpod.dart";
|
||||
import "package:nexus/controllers/client_controller.dart";
|
||||
import "package:nexus/helpers/extensions/join_room_with_snackbars.dart";
|
||||
import "package:nexus/widgets/form_text_input.dart";
|
||||
|
||||
class JoinDialog extends HookConsumerWidget {
|
||||
const JoinDialog({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final roomAlias = useTextEditingController();
|
||||
return AlertDialog(
|
||||
title: Text("Join a Room"),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text("Enter the room alias, ID, or a Matrix.to link."),
|
||||
SizedBox(height: 12),
|
||||
FormTextInput(
|
||||
required: false,
|
||||
capitalize: true,
|
||||
controller: roomAlias,
|
||||
title: "#room:server",
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(onPressed: Navigator.of(context).pop, child: Text("Cancel")),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
Navigator.of(context).pop();
|
||||
|
||||
final client = ref.watch(ClientController.provider.notifier);
|
||||
if (context.mounted) {
|
||||
client.joinRoomWithSnackBars(context, roomAlias.text, ref);
|
||||
}
|
||||
},
|
||||
child: Text("Join"),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,14 +1,11 @@
|
|||
import "package:flutter/material.dart";
|
||||
import "package:flutter_hooks/flutter_hooks.dart";
|
||||
import "package:hooks_riverpod/hooks_riverpod.dart";
|
||||
import "package:nexus/controllers/client_controller.dart";
|
||||
import "package:nexus/controllers/key_controller.dart";
|
||||
import "package:nexus/controllers/selected_space_controller.dart";
|
||||
import "package:nexus/controllers/spaces_controller.dart";
|
||||
import "package:nexus/helpers/extensions/join_room_with_snackbars.dart";
|
||||
import "package:nexus/widgets/avatar_or_hash.dart";
|
||||
import "package:nexus/widgets/chat_page/join_dialog.dart";
|
||||
import "package:nexus/widgets/chat_page/room_menu.dart";
|
||||
import "package:nexus/widgets/form_text_input.dart";
|
||||
|
||||
class Sidebar extends HookConsumerWidget {
|
||||
final bool isDesktop;
|
||||
|
|
@ -91,53 +88,7 @@ class Sidebar extends HookConsumerWidget {
|
|||
PopupMenuItem(
|
||||
onTap: () => showDialog(
|
||||
context: context,
|
||||
builder: (alertContext) => HookBuilder(
|
||||
builder: (_) {
|
||||
final roomAlias = useTextEditingController();
|
||||
return AlertDialog(
|
||||
title: Text("Join a Room"),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
"Enter the room alias, ID, or a Matrix.to link.",
|
||||
),
|
||||
SizedBox(height: 12),
|
||||
FormTextInput(
|
||||
required: false,
|
||||
capitalize: true,
|
||||
controller: roomAlias,
|
||||
title: "#room:server",
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: Navigator.of(context).pop,
|
||||
child: Text("Cancel"),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
Navigator.of(alertContext).pop();
|
||||
|
||||
final client = ref.watch(
|
||||
ClientController.provider.notifier,
|
||||
);
|
||||
if (context.mounted) {
|
||||
client.joinRoomWithSnackBars(
|
||||
context,
|
||||
roomAlias.text,
|
||||
ref,
|
||||
);
|
||||
}
|
||||
},
|
||||
child: Text("Join"),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
builder: (_) => JoinDialog(),
|
||||
),
|
||||
child: ListTile(
|
||||
title: Text("Join an existing room (or space)"),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue