forked from Nexus/nexus
tweak PMP support
This commit is contained in:
parent
3b7a4c0cd4
commit
4800123044
1 changed files with 69 additions and 61 deletions
59
lib/controllers/subprofiles.dart
Normal file
59
lib/controllers/subprofiles.dart
Normal 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,
|
||||
);
|
||||
}
|
||||
Loading…
Reference in a new issue