From c786344bf26810d54654dd198ca2e375b1153c63 Mon Sep 17 00:00:00 2001 From: Henry-Hiles Date: Thu, 26 Nov 2026 16:06:08 -0500 Subject: [PATCH] add dynamic hinting and latching for PMPs --- lib/controllers/account_data.dart | 30 +++++++--------- lib/controllers/rooms.dart | 18 ++++++++++ lib/controllers/settings_sections.dart | 15 ++++++++ lib/controllers/subprofiles.dart | 10 +++--- lib/models/per_message_profiles.dart | 5 +++ lib/models/room_subprofiles.dart | 20 +++++++++++ lib/models/settings.dart | 1 + lib/widgets/composer/composer.dart | 19 +++++++++- lib/widgets/composer/subprofile_button.dart | 39 ++++++++++++++------- 9 files changed, 120 insertions(+), 37 deletions(-) create mode 100644 lib/models/room_subprofiles.dart diff --git a/lib/controllers/account_data.dart b/lib/controllers/account_data.dart index 13b5d81..8c68209 100644 --- a/lib/controllers/account_data.dart +++ b/lib/controllers/account_data.dart @@ -87,26 +87,22 @@ class AccountDataController extends Notifier { Future setRoomProfile(String roomId, String profileId) async { const type = AccountData.perMessageProfilesKey; - final current = ref + final rooms = ref.read(RoomsController.provider.notifier); + final previous = ref .read(RoomsController.provider)[roomId] ?.accountData[type]; + final content = {...?previous, "default_profile_id": profileId}; - await ref - .read(ClientController.provider.notifier) - .setAccountData( - .new( - type: type, - roomId: roomId, - content: {...?current, "default_profile_id": profileId}, - ), - ); - await ref - .read(RoomsController.provider.notifier) - .awaitRooms( - (rooms) => - rooms[roomId]?.accountData[type]?["default_profile_id"] == - profileId, - ); + // Shown straight away, rather than once it comes down sync + rooms.setAccountData(roomId, type, content); + try { + await ref + .read(ClientController.provider.notifier) + .setAccountData(.new(type: type, roomId: roomId, content: content)); + } catch (_) { + rooms.setAccountData(roomId, type, previous); + rethrow; + } } /// Saves [rule] of [kind], creating it if it's new, or otherwise changing only diff --git a/lib/controllers/rooms.dart b/lib/controllers/rooms.dart index 91d8093..bd162dd 100644 --- a/lib/controllers/rooms.dart +++ b/lib/controllers/rooms.dart @@ -136,6 +136,24 @@ class RoomsController extends Notifier> { .add(event.eventId, event.rowId); }); + void setAccountData( + String roomId, + String type, + Map? content, + ) { + if (state[roomId] case final room?) { + state = state.add( + roomId, + room.copyWith( + accountData: switch (content) { + final content? => room.accountData.add(type, content), + null => room.accountData.remove(type), + }, + ), + ); + } + } + void update( IMap rooms, [ ISet leftRooms = const .empty(), diff --git a/lib/controllers/settings_sections.dart b/lib/controllers/settings_sections.dart index c084b9b..50a344b 100644 --- a/lib/controllers/settings_sections.dart +++ b/lib/controllers/settings_sections.dart @@ -467,6 +467,21 @@ class SettingsSectionsController ), ), if (settings.subprofiles) ...[ + .new( + title: "Latch Triggered Subprofiles", + description: "After you send a message with a subprofile's trigger, keep sending as it in that room until you choose another.", + icon: Icons.push_pin, + builder: (title, description, icon) => SwitchListTile( + title: Text(title), + subtitle: Text(description), + secondary: Icon(icon), + value: settings.latchSubprofiles, + onChanged: (value) => ref + .read(SettingsController.provider.notifier) + .set(settings.copyWith(latchSubprofiles: value)) + .onError(showError), + ), + ), if (subprofiles.profiles.isNotEmpty) .new( title: "Default Subprofile", diff --git a/lib/controllers/subprofiles.dart b/lib/controllers/subprofiles.dart index 70f6e05..98d5f3e 100644 --- a/lib/controllers/subprofiles.dart +++ b/lib/controllers/subprofiles.dart @@ -6,11 +6,7 @@ import "package:nexus/controllers/account_data.dart"; import "package:nexus/controllers/rooms.dart"; import "package:nexus/models/account_data.dart"; import "package:nexus/models/per_message_profiles.dart"; - -typedef RoomSubprofiles = ({ - IList profiles, - StoredProfile? current, -}); +import "package:nexus/models/room_subprofiles.dart"; class SubprofilesController(final String roomId) extends Notifier { @@ -44,8 +40,10 @@ class SubprofilesController(final String roomId) ); final currentId = room?.defaultProfileId ?? global.defaultProfileId; - return ( + return .new( profiles: profiles, + triggerOrder: (room?.profiles ?? const IList.empty()) + .addAll(global.profiles), current: currentId == null || currentId.isEmpty ? null : profiles.firstWhereOrNull((profile) => profile.id == currentId), diff --git a/lib/models/per_message_profiles.dart b/lib/models/per_message_profiles.dart index 7de8398..9d4c386 100644 --- a/lib/models/per_message_profiles.dart +++ b/lib/models/per_message_profiles.dart @@ -41,6 +41,11 @@ class const ProfileTrigger({ final String suffix = "", final bool keepTrigger = false, }) with _$ProfileTrigger { + bool matches(String text) => + text.length >= prefix.length + suffix.length && + text.startsWith(prefix) && + text.endsWith(suffix); + Map toJson() => _$ProfileTriggerToJson(this); factory ProfileTrigger.fromJson(Map json) => diff --git a/lib/models/room_subprofiles.dart b/lib/models/room_subprofiles.dart new file mode 100644 index 0000000..abf72ac --- /dev/null +++ b/lib/models/room_subprofiles.dart @@ -0,0 +1,20 @@ +import "package:collection/collection.dart"; +import "package:fast_immutable_collections/fast_immutable_collections.dart"; +import "package:freezed_annotation/freezed_annotation.dart"; +import "package:nexus/models/per_message_profiles.dart"; + +part "room_subprofiles.freezed.dart"; + +@freezed +class const RoomSubprofiles({ + required final IList profiles, + // Each trigger is tried in this order, as gomuks does when sending + required final IList triggerOrder, + final StoredProfile? current, +}) with _$RoomSubprofiles { + StoredProfile? triggeredBy(String text) => triggerOrder.firstWhereOrNull( + (profile) => + profile.id.isNotEmpty && + profile.triggers.any((trigger) => trigger.matches(text)), + ); +} diff --git a/lib/models/settings.dart b/lib/models/settings.dart index ffad23b..fa5c97a 100644 --- a/lib/models/settings.dart +++ b/lib/models/settings.dart @@ -24,6 +24,7 @@ class const Settings({ final NameColor nameColor = .powerLevel, final ShowAndSend typingIndicators = .showAndSend, final bool subprofiles = false, + final bool latchSubprofiles = false, /// When not sending, read receipts are still sent privately, so that the /// user's other devices know what has been read. diff --git a/lib/widgets/composer/composer.dart b/lib/widgets/composer/composer.dart index bd4a3de..1baf18e 100644 --- a/lib/widgets/composer/composer.dart +++ b/lib/widgets/composer/composer.dart @@ -27,6 +27,9 @@ import "package:hooks_riverpod/hooks_riverpod.dart"; import "package:nexus/controllers/attachment.dart"; import "package:nexus/controllers/client.dart"; import "package:nexus/controllers/settings.dart"; +import "package:nexus/controllers/account_data.dart"; +import "package:nexus/controllers/subprofiles.dart"; +import "package:nexus/models/settings.dart"; import "package:nexus/controllers/image_picker.dart"; import "package:nexus/helpers/file_dialog_helper.dart"; import "package:image_picker/image_picker.dart"; @@ -433,6 +436,20 @@ class const Composer( attachment != null && attachment.$2 == null) { return; } + if (ref.read(SettingsController.provider).value case Settings( + subprofiles: true, + latchSubprofiles: true, + )) { + final subprofiles = ref.read(SubprofilesController.provider(roomId)); + if (subprofiles.triggeredBy(controller.text) case final triggered? + when triggered.id != subprofiles.current?.id) { + ref + .read(AccountDataController.provider.notifier) + .setRoomProfile(roomId, triggered.id) + .onError(showError); + } + } + onSend( "${TextCommand.prefixOf(textCommands)}${controller.formattedText}", shouldMention: shouldMention.value, @@ -566,7 +583,7 @@ class const Composer( ), ) ? [ - SubprofileButton(roomId), + SubprofileButton(roomId, controller: controller), IconButton( onPressed: hasReplacement ? null diff --git a/lib/widgets/composer/subprofile_button.dart b/lib/widgets/composer/subprofile_button.dart index d6a52c2..145e932 100644 --- a/lib/widgets/composer/subprofile_button.dart +++ b/lib/widgets/composer/subprofile_button.dart @@ -1,15 +1,21 @@ -import "package:flutter_riverpod/flutter_riverpod.dart"; +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"; -/// Chooses the subprofile messages are sent as in [roomId], if subprofiles are on. -class const SubprofileButton(final String roomId, {super.key}) - extends ConsumerWidget { +/// 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. +class const SubprofileButton( + final String roomId, { + required final TextEditingController controller, + super.key, +}) extends HookConsumerWidget { @override Widget build(BuildContext context, WidgetRef ref) { final enabled = ref.watch( @@ -17,10 +23,14 @@ class const SubprofileButton(final String roomId, {super.key}) (settings) => settings.value?.subprofiles ?? false, ), ); - final (:profiles, :current) = ref.watch( - SubprofilesController.provider(roomId), + final subprofiles = ref.watch(SubprofilesController.provider(roomId)); + final RoomSubprofiles(:profiles, :current) = subprofiles; + final triggered = useListenableSelector( + controller, + () => subprofiles.triggeredBy(controller.text), ); if (!enabled || profiles.isEmpty) return const SizedBox.shrink(); + final shown = triggered ?? current; Future choose(String profileId) => ref .read(AccountDataController.provider.notifier) @@ -45,14 +55,17 @@ class const SubprofileButton(final String roomId, {super.key}) } return AdaptiveMenuButton( - tooltip: switch (current) { - final current? => "Sending as ${current.displayName ?? current.id}", - null => "Choose subprofile", + tooltip: switch ((triggered, current)) { + (final triggered?, _) => + "Sending this message as ${triggered.displayName ?? triggered.id}", + (null, final current?) => + "Sending as ${current.displayName ?? current.id}", + (null, null) => "Choose subprofile", }, - icon: switch (current) { - final current? => AvatarOrHash( - current.avatarUrl, - current.displayName ?? current.id, + icon: switch (shown) { + final shown? => AvatarOrHash( + shown.avatarUrl, + shown.displayName ?? shown.id, ), null => Icon(Icons.person), },