forked from Nexus/nexus
add dynamic hinting and latching for PMPs
This commit is contained in:
parent
225497030e
commit
c786344bf2
9 changed files with 120 additions and 37 deletions
|
|
@ -87,26 +87,22 @@ class AccountDataController extends Notifier<AccountData> {
|
||||||
|
|
||||||
Future<void> setRoomProfile(String roomId, String profileId) async {
|
Future<void> setRoomProfile(String roomId, String profileId) async {
|
||||||
const type = AccountData.perMessageProfilesKey;
|
const type = AccountData.perMessageProfilesKey;
|
||||||
final current = ref
|
final rooms = ref.read(RoomsController.provider.notifier);
|
||||||
|
final previous = ref
|
||||||
.read(RoomsController.provider)[roomId]
|
.read(RoomsController.provider)[roomId]
|
||||||
?.accountData[type];
|
?.accountData[type];
|
||||||
|
final content = {...?previous, "default_profile_id": profileId};
|
||||||
|
|
||||||
await ref
|
// Shown straight away, rather than once it comes down sync
|
||||||
.read(ClientController.provider.notifier)
|
rooms.setAccountData(roomId, type, content);
|
||||||
.setAccountData(
|
try {
|
||||||
.new(
|
await ref
|
||||||
type: type,
|
.read(ClientController.provider.notifier)
|
||||||
roomId: roomId,
|
.setAccountData(.new(type: type, roomId: roomId, content: content));
|
||||||
content: {...?current, "default_profile_id": profileId},
|
} catch (_) {
|
||||||
),
|
rooms.setAccountData(roomId, type, previous);
|
||||||
);
|
rethrow;
|
||||||
await ref
|
}
|
||||||
.read(RoomsController.provider.notifier)
|
|
||||||
.awaitRooms(
|
|
||||||
(rooms) =>
|
|
||||||
rooms[roomId]?.accountData[type]?["default_profile_id"] ==
|
|
||||||
profileId,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Saves [rule] of [kind], creating it if it's new, or otherwise changing only
|
/// Saves [rule] of [kind], creating it if it's new, or otherwise changing only
|
||||||
|
|
|
||||||
|
|
@ -136,6 +136,24 @@ class RoomsController extends Notifier<IMap<String, Room>> {
|
||||||
.add(event.eventId, event.rowId);
|
.add(event.eventId, event.rowId);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
void setAccountData(
|
||||||
|
String roomId,
|
||||||
|
String type,
|
||||||
|
Map<String, dynamic>? 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(
|
void update(
|
||||||
IMap<String, Room> rooms, [
|
IMap<String, Room> rooms, [
|
||||||
ISet<String> leftRooms = const .empty(),
|
ISet<String> leftRooms = const .empty(),
|
||||||
|
|
|
||||||
|
|
@ -467,6 +467,21 @@ class SettingsSectionsController
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
if (settings.subprofiles) ...[
|
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)
|
if (subprofiles.profiles.isNotEmpty)
|
||||||
.new(
|
.new(
|
||||||
title: "Default Subprofile",
|
title: "Default Subprofile",
|
||||||
|
|
|
||||||
|
|
@ -6,11 +6,7 @@ import "package:nexus/controllers/account_data.dart";
|
||||||
import "package:nexus/controllers/rooms.dart";
|
import "package:nexus/controllers/rooms.dart";
|
||||||
import "package:nexus/models/account_data.dart";
|
import "package:nexus/models/account_data.dart";
|
||||||
import "package:nexus/models/per_message_profiles.dart";
|
import "package:nexus/models/per_message_profiles.dart";
|
||||||
|
import "package:nexus/models/room_subprofiles.dart";
|
||||||
typedef RoomSubprofiles = ({
|
|
||||||
IList<StoredProfile> profiles,
|
|
||||||
StoredProfile? current,
|
|
||||||
});
|
|
||||||
|
|
||||||
class SubprofilesController(final String roomId)
|
class SubprofilesController(final String roomId)
|
||||||
extends Notifier<RoomSubprofiles> {
|
extends Notifier<RoomSubprofiles> {
|
||||||
|
|
@ -44,8 +40,10 @@ class SubprofilesController(final String roomId)
|
||||||
);
|
);
|
||||||
|
|
||||||
final currentId = room?.defaultProfileId ?? global.defaultProfileId;
|
final currentId = room?.defaultProfileId ?? global.defaultProfileId;
|
||||||
return (
|
return .new(
|
||||||
profiles: profiles,
|
profiles: profiles,
|
||||||
|
triggerOrder: (room?.profiles ?? const IList<StoredProfile>.empty())
|
||||||
|
.addAll(global.profiles),
|
||||||
current: currentId == null || currentId.isEmpty
|
current: currentId == null || currentId.isEmpty
|
||||||
? null
|
? null
|
||||||
: profiles.firstWhereOrNull((profile) => profile.id == currentId),
|
: profiles.firstWhereOrNull((profile) => profile.id == currentId),
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,11 @@ class const ProfileTrigger({
|
||||||
final String suffix = "",
|
final String suffix = "",
|
||||||
final bool keepTrigger = false,
|
final bool keepTrigger = false,
|
||||||
}) with _$ProfileTrigger {
|
}) with _$ProfileTrigger {
|
||||||
|
bool matches(String text) =>
|
||||||
|
text.length >= prefix.length + suffix.length &&
|
||||||
|
text.startsWith(prefix) &&
|
||||||
|
text.endsWith(suffix);
|
||||||
|
|
||||||
Map<String, Object?> toJson() => _$ProfileTriggerToJson(this);
|
Map<String, Object?> toJson() => _$ProfileTriggerToJson(this);
|
||||||
|
|
||||||
factory ProfileTrigger.fromJson(Map<String, Object?> json) =>
|
factory ProfileTrigger.fromJson(Map<String, Object?> json) =>
|
||||||
|
|
|
||||||
20
lib/models/room_subprofiles.dart
Normal file
20
lib/models/room_subprofiles.dart
Normal file
|
|
@ -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<StoredProfile> profiles,
|
||||||
|
// Each trigger is tried in this order, as gomuks does when sending
|
||||||
|
required final IList<StoredProfile> triggerOrder,
|
||||||
|
final StoredProfile? current,
|
||||||
|
}) with _$RoomSubprofiles {
|
||||||
|
StoredProfile? triggeredBy(String text) => triggerOrder.firstWhereOrNull(
|
||||||
|
(profile) =>
|
||||||
|
profile.id.isNotEmpty &&
|
||||||
|
profile.triggers.any((trigger) => trigger.matches(text)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -24,6 +24,7 @@ class const Settings({
|
||||||
final NameColor nameColor = .powerLevel,
|
final NameColor nameColor = .powerLevel,
|
||||||
final ShowAndSend typingIndicators = .showAndSend,
|
final ShowAndSend typingIndicators = .showAndSend,
|
||||||
final bool subprofiles = false,
|
final bool subprofiles = false,
|
||||||
|
final bool latchSubprofiles = false,
|
||||||
|
|
||||||
/// When not sending, read receipts are still sent privately, so that the
|
/// When not sending, read receipts are still sent privately, so that the
|
||||||
/// user's other devices know what has been read.
|
/// user's other devices know what has been read.
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,9 @@ import "package:hooks_riverpod/hooks_riverpod.dart";
|
||||||
import "package:nexus/controllers/attachment.dart";
|
import "package:nexus/controllers/attachment.dart";
|
||||||
import "package:nexus/controllers/client.dart";
|
import "package:nexus/controllers/client.dart";
|
||||||
import "package:nexus/controllers/settings.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/controllers/image_picker.dart";
|
||||||
import "package:nexus/helpers/file_dialog_helper.dart";
|
import "package:nexus/helpers/file_dialog_helper.dart";
|
||||||
import "package:image_picker/image_picker.dart";
|
import "package:image_picker/image_picker.dart";
|
||||||
|
|
@ -433,6 +436,20 @@ class const Composer(
|
||||||
attachment != null && attachment.$2 == null) {
|
attachment != null && attachment.$2 == null) {
|
||||||
return;
|
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(
|
onSend(
|
||||||
"${TextCommand.prefixOf(textCommands)}${controller.formattedText}",
|
"${TextCommand.prefixOf(textCommands)}${controller.formattedText}",
|
||||||
shouldMention: shouldMention.value,
|
shouldMention: shouldMention.value,
|
||||||
|
|
@ -566,7 +583,7 @@ class const Composer(
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
? [
|
? [
|
||||||
SubprofileButton(roomId),
|
SubprofileButton(roomId, controller: controller),
|
||||||
IconButton(
|
IconButton(
|
||||||
onPressed: hasReplacement
|
onPressed: hasReplacement
|
||||||
? null
|
? null
|
||||||
|
|
|
||||||
|
|
@ -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_adaptive_sheets/material_adaptive_sheets.dart";
|
||||||
import "package:material_ui/material_ui.dart";
|
import "package:material_ui/material_ui.dart";
|
||||||
import "package:nexus/controllers/account_data.dart";
|
import "package:nexus/controllers/account_data.dart";
|
||||||
import "package:nexus/controllers/subprofiles.dart";
|
import "package:nexus/controllers/subprofiles.dart";
|
||||||
import "package:nexus/controllers/settings.dart";
|
import "package:nexus/controllers/settings.dart";
|
||||||
import "package:nexus/main.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/avatars/avatar_or_hash.dart";
|
||||||
|
|
||||||
/// Chooses the subprofile messages are sent as in [roomId], if subprofiles are on.
|
/// Chooses the subprofile messages are sent as in [roomId], if subprofiles are
|
||||||
class const SubprofileButton(final String roomId, {super.key})
|
/// on. It shows the one the text in [controller] would trigger, if any.
|
||||||
extends ConsumerWidget {
|
class const SubprofileButton(
|
||||||
|
final String roomId, {
|
||||||
|
required final TextEditingController controller,
|
||||||
|
super.key,
|
||||||
|
}) extends HookConsumerWidget {
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context, WidgetRef ref) {
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
final enabled = ref.watch(
|
final enabled = ref.watch(
|
||||||
|
|
@ -17,10 +23,14 @@ class const SubprofileButton(final String roomId, {super.key})
|
||||||
(settings) => settings.value?.subprofiles ?? false,
|
(settings) => settings.value?.subprofiles ?? false,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
final (:profiles, :current) = ref.watch(
|
final subprofiles = ref.watch(SubprofilesController.provider(roomId));
|
||||||
SubprofilesController.provider(roomId),
|
final RoomSubprofiles(:profiles, :current) = subprofiles;
|
||||||
|
final triggered = useListenableSelector(
|
||||||
|
controller,
|
||||||
|
() => subprofiles.triggeredBy(controller.text),
|
||||||
);
|
);
|
||||||
if (!enabled || profiles.isEmpty) return const SizedBox.shrink();
|
if (!enabled || profiles.isEmpty) return const SizedBox.shrink();
|
||||||
|
final shown = triggered ?? current;
|
||||||
|
|
||||||
Future<void> choose(String profileId) => ref
|
Future<void> choose(String profileId) => ref
|
||||||
.read(AccountDataController.provider.notifier)
|
.read(AccountDataController.provider.notifier)
|
||||||
|
|
@ -45,14 +55,17 @@ class const SubprofileButton(final String roomId, {super.key})
|
||||||
}
|
}
|
||||||
|
|
||||||
return AdaptiveMenuButton<void>(
|
return AdaptiveMenuButton<void>(
|
||||||
tooltip: switch (current) {
|
tooltip: switch ((triggered, current)) {
|
||||||
final current? => "Sending as ${current.displayName ?? current.id}",
|
(final triggered?, _) =>
|
||||||
null => "Choose subprofile",
|
"Sending this message as ${triggered.displayName ?? triggered.id}",
|
||||||
|
(null, final current?) =>
|
||||||
|
"Sending as ${current.displayName ?? current.id}",
|
||||||
|
(null, null) => "Choose subprofile",
|
||||||
},
|
},
|
||||||
icon: switch (current) {
|
icon: switch (shown) {
|
||||||
final current? => AvatarOrHash(
|
final shown? => AvatarOrHash(
|
||||||
current.avatarUrl,
|
shown.avatarUrl,
|
||||||
current.displayName ?? current.id,
|
shown.displayName ?? shown.id,
|
||||||
),
|
),
|
||||||
null => Icon(Icons.person),
|
null => Icon(Icons.person),
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue