cleanup PinnedEventsController
This commit is contained in:
parent
5c78cc85f7
commit
177c057337
2 changed files with 14 additions and 36 deletions
|
|
@ -1,7 +1,7 @@
|
|||
import "package:collection/collection.dart";
|
||||
import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
||||
import "package:hooks_riverpod/hooks_riverpod.dart";
|
||||
import "package:nexus/controllers/client_controller.dart";
|
||||
import "package:nexus/controllers/event_controller.dart";
|
||||
import "package:nexus/controllers/rooms_controller.dart";
|
||||
import "package:nexus/models/content/content.dart";
|
||||
import "package:nexus/models/content/pinned_events.dart";
|
||||
|
|
@ -26,42 +26,19 @@ class PinnedEventsController extends AsyncNotifier<IList<Event>> {
|
|||
|
||||
if (pinnedStateEvent == null) return .new();
|
||||
|
||||
final pinnedContent = switch (pinnedStateEvent.content) {
|
||||
PinnedEventsContent content => content,
|
||||
_ => null,
|
||||
};
|
||||
|
||||
if (pinnedContent == null || pinnedContent.pinned.isEmpty) {
|
||||
return .new();
|
||||
}
|
||||
|
||||
final localEvents = <Event>[];
|
||||
final missingIds = <String>[];
|
||||
|
||||
for (final eventId in pinnedContent.pinned) {
|
||||
final localEvent = room.events.values.firstWhereOrNull(
|
||||
(e) => e.eventId == eventId,
|
||||
);
|
||||
|
||||
if (localEvent != null) {
|
||||
localEvents.add(localEvent);
|
||||
} else {
|
||||
missingIds.add(eventId);
|
||||
}
|
||||
}
|
||||
|
||||
if (missingIds.isNotEmpty) {
|
||||
final client = ref.read(ClientController.provider.notifier);
|
||||
final fetched = await Future.wait(
|
||||
missingIds.map(
|
||||
(id) => client.getEvent(.new(roomId: roomId, eventId: id)),
|
||||
if (pinnedStateEvent.content case PinnedEventsContent content) {
|
||||
return (await Future.wait(
|
||||
content.pinnedEvents.map(
|
||||
(eventId) => ref.watch(
|
||||
EventController.provider(
|
||||
.new(eventId: eventId, roomId: roomId),
|
||||
).future,
|
||||
),
|
||||
);
|
||||
|
||||
localEvents.addAll(fetched.whereType<Event>());
|
||||
),
|
||||
)).nonNulls.toIList();
|
||||
}
|
||||
|
||||
return localEvents.toIList();
|
||||
return .new();
|
||||
}
|
||||
|
||||
Future<void> togglePin(Event event) async {
|
||||
|
|
|
|||
|
|
@ -7,8 +7,9 @@ part "pinned_events.g.dart";
|
|||
@freezed
|
||||
abstract class PinnedEventsContent extends Content with _$PinnedEventsContent {
|
||||
PinnedEventsContent._();
|
||||
factory PinnedEventsContent({@Default(IList.empty()) IList<String> pinned}) =
|
||||
_PinnedEventsContent;
|
||||
factory PinnedEventsContent({
|
||||
@Default(IList.empty()) @JsonKey(name: "pinned") IList<String> pinnedEvents,
|
||||
}) = _PinnedEventsContent;
|
||||
|
||||
factory PinnedEventsContent.fromJson(Map<String, Object?> json) =>
|
||||
_$PinnedEventsContentFromJson(json);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue