tweak PMP support

This commit is contained in:
Henry Hiles 2026-11-26 15:50:57 -05:00
commit 4800123044
Signed by: Henry-Hiles
SSH key fingerprint: SHA256:VKQUdS31Q90KvX7EkKMHMBpUspcmItAh86a+v7PGiIs

View file

@ -0,0 +1,59 @@
import "package:collection/collection.dart";
import "package:fast_immutable_collections/fast_immutable_collections.dart";
import "package:flutter/foundation.dart";
import "package:flutter_riverpod/flutter_riverpod.dart";
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<StoredProfile> profiles,
StoredProfile? current,
});
class SubprofilesController(final String roomId)
extends Notifier<RoomSubprofiles> {
@override
RoomSubprofiles build() {
final global = ref.watch(
AccountDataController.provider.select((data) => data.perMessageProfiles),
);
final roomJson = ref.watch(
RoomsController.provider.select(
(rooms) =>
rooms[roomId]?.accountData[AccountData.perMessageProfilesKey],
),
);
PerMessageProfiles? room;
if (roomJson != null) {
try {
room = .fromJson(roomJson);
} catch (error) {
debugPrint("Ignoring invalid room per-message profiles: $error");
}
}
final roomIds = room?.profiles.map((profile) => profile.id).toISet();
final profiles = (room?.profiles ?? const IList<StoredProfile>.empty())
.addAll(
global.profiles.where(
(profile) => !(roomIds?.contains(profile.id) ?? false),
),
);
final currentId = room?.defaultProfileId ?? global.defaultProfileId;
return (
profiles: profiles,
current: currentId == null || currentId.isEmpty
? null
: profiles.firstWhereOrNull((profile) => profile.id == currentId),
);
}
static final provider = NotifierProvider.autoDispose
.family<SubprofilesController, RoomSubprofiles, String>(
SubprofilesController.new,
);
}