forked from Nexus/nexus
101 lines
3.4 KiB
Dart
101 lines
3.4 KiB
Dart
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(),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|