forked from Nexus/nexus
added subprofile search
This commit is contained in:
parent
7435ecd5f0
commit
d5f04122eb
2 changed files with 105 additions and 38 deletions
|
|
@ -2,12 +2,11 @@ import "package:flutter_hooks/flutter_hooks.dart";
|
|||
import "package:hooks_riverpod/hooks_riverpod.dart";
|
||||
import "package:material_adaptive_sheets/material_adaptive_sheets.dart";
|
||||
import "package:material_ui/material_ui.dart";
|
||||
import "package:nexus/controllers/account_data.dart";
|
||||
import "package:nexus/controllers/subprofiles.dart";
|
||||
import "package:nexus/controllers/settings.dart";
|
||||
import "package:nexus/main.dart";
|
||||
import "package:nexus/models/room_subprofiles.dart";
|
||||
import "package:nexus/widgets/avatars/avatar_or_hash.dart";
|
||||
import "package:nexus/widgets/composer/subprofile_picker.dart";
|
||||
|
||||
/// Chooses the subprofile messages are sent as in [roomId], if subprofiles are
|
||||
/// on. It shows the one the text in [controller] would trigger, if any.
|
||||
|
|
@ -32,29 +31,7 @@ class const SubprofileButton(
|
|||
if (!enabled || profiles.isEmpty) return const SizedBox.shrink();
|
||||
final shown = triggered ?? current;
|
||||
|
||||
Future<void> choose(String profileId) => ref
|
||||
.read(AccountDataController.provider.notifier)
|
||||
.setRoomProfile(roomId, profileId)
|
||||
.onError(showError);
|
||||
|
||||
PopupMenuItem<void> item({
|
||||
required String id,
|
||||
required Widget leading,
|
||||
required String title,
|
||||
}) {
|
||||
final selected = (current?.id ?? "") == id;
|
||||
return PopupMenuItem(
|
||||
onTap: () => choose(id),
|
||||
child: ListTile(
|
||||
selected: selected,
|
||||
leading: leading,
|
||||
title: Text(title),
|
||||
trailing: selected ? Icon(Icons.check) : null,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return AdaptiveMenuButton<void>(
|
||||
return IconButton(
|
||||
tooltip: switch ((triggered, current)) {
|
||||
(final triggered?, _) =>
|
||||
"Sending this message as ${triggered.displayName ?? triggered.id}",
|
||||
|
|
@ -69,19 +46,8 @@ class const SubprofileButton(
|
|||
),
|
||||
null => Icon(Icons.person),
|
||||
},
|
||||
itemBuilder: (_) => [
|
||||
item(id: "", leading: Icon(Icons.person), title: "Account Profile"),
|
||||
...profiles.map(
|
||||
(profile) => item(
|
||||
id: profile.id,
|
||||
leading: AvatarOrHash(
|
||||
profile.avatarUrl,
|
||||
profile.displayName ?? profile.id,
|
||||
),
|
||||
title: profile.displayName ?? profile.id,
|
||||
),
|
||||
),
|
||||
],
|
||||
onPressed: () =>
|
||||
context.showAdaptivePopover((_) => SubprofilePicker(roomId)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
101
lib/widgets/composer/subprofile_picker.dart
Normal file
101
lib/widgets/composer/subprofile_picker.dart
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
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/account_data.dart";
|
||||
import "package:nexus/controllers/subprofiles.dart";
|
||||
import "package:nexus/main.dart";
|
||||
import "package:nexus/widgets/avatars/avatar_or_hash.dart";
|
||||
|
||||
/// Searches the subprofiles usable in [roomId], to choose which messages are
|
||||
/// sent as there.
|
||||
class const SubprofilePicker(final String roomId, {super.key})
|
||||
extends HookConsumerWidget {
|
||||
static const accountProfile = "Account Profile";
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final subprofiles = ref.watch(SubprofilesController.provider(roomId));
|
||||
final searchController = useTextEditingController();
|
||||
final query = useValueListenable(searchController).text
|
||||
.trim()
|
||||
.toLowerCase();
|
||||
bool found(String text) => text.toLowerCase().contains(query);
|
||||
|
||||
// An empty ID is the account profile
|
||||
final results = <({String id, String name, Uri? avatar})>[
|
||||
if (found(accountProfile)) (id: "", name: accountProfile, avatar: null),
|
||||
...subprofiles.profiles
|
||||
.where(
|
||||
(profile) =>
|
||||
found(profile.displayName ?? profile.id) ||
|
||||
profile.triggers.any(
|
||||
(trigger) => [
|
||||
trigger.prefix,
|
||||
trigger.suffix,
|
||||
].any((part) => part.isNotEmpty && found(part)),
|
||||
),
|
||||
)
|
||||
.map(
|
||||
(profile) => (
|
||||
id: profile.id,
|
||||
name: profile.displayName ?? profile.id,
|
||||
avatar: profile.avatarUrl,
|
||||
),
|
||||
),
|
||||
];
|
||||
|
||||
void choose(String id) {
|
||||
Navigator.of(context).pop();
|
||||
if (id == (subprofiles.current?.id ?? "")) return;
|
||||
ref
|
||||
.read(AccountDataController.provider.notifier)
|
||||
.setRoomProfile(roomId, id)
|
||||
.onError(showError);
|
||||
}
|
||||
|
||||
return Column(
|
||||
mainAxisSize: .min,
|
||||
crossAxisAlignment: .stretch,
|
||||
children: [
|
||||
Padding(
|
||||
padding: .all(16),
|
||||
child: TextField(
|
||||
controller: searchController,
|
||||
autofocus: true,
|
||||
onSubmitted: (_) {
|
||||
if (results.firstOrNull case final first?) choose(first.id);
|
||||
},
|
||||
decoration: const .new(
|
||||
prefixIcon: Icon(Icons.search),
|
||||
hintText: "Search subprofiles",
|
||||
),
|
||||
),
|
||||
),
|
||||
Flexible(
|
||||
child: results.isEmpty
|
||||
? Padding(
|
||||
padding: .only(bottom: 16),
|
||||
child: Text("No subprofiles found", textAlign: .center),
|
||||
)
|
||||
: ListView(
|
||||
shrinkWrap: true,
|
||||
padding: .only(bottom: 8),
|
||||
children: results.map((result) {
|
||||
final selected =
|
||||
(subprofiles.current?.id ?? "") == result.id;
|
||||
return ListTile(
|
||||
selected: selected,
|
||||
leading: result.id.isEmpty
|
||||
? Icon(Icons.person)
|
||||
: AvatarOrHash(result.avatar, result.name),
|
||||
title: Text(result.name),
|
||||
trailing: selected ? Icon(Icons.check) : null,
|
||||
onTap: () => choose(result.id),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue