Skip to content

Commit c69ad61

Browse files
authored
Optimize group image path loading and DM header (#777)
Refactored group image path loading to handle direct messages and groups differently, improving performance and error handling and streamline member data retrieval.
1 parent b6191ff commit c69ad61

File tree

2 files changed

+44
-30
lines changed

2 files changed

+44
-30
lines changed

lib/config/providers/group_provider.dart

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -587,37 +587,17 @@ class GroupsNotifier extends Notifier<GroupsState> {
587587
state.groupImagePaths ?? {},
588588
);
589589

590-
final List<Future<void>> loadTasks = [];
591590
final activePubkey = ref.read(activePubkeyProvider);
592591
if (activePubkey == null || activePubkey.isEmpty) return;
593592

594-
for (final group in groups) {
595-
loadTasks.add(
596-
getGroupImagePath(
597-
accountPubkey: activePubkey,
598-
groupId: group.mlsGroupId,
599-
)
600-
.then((imagePath) {
601-
if (imagePath != null && imagePath.isNotEmpty) {
602-
groupImagePaths[group.mlsGroupId] = imagePath;
603-
}
604-
})
605-
.catchError((e) {
606-
_logErrorSync('Failed to load image path for group ${group.mlsGroupId}', e);
607-
// Skip this group if image loading fails
608-
}),
609-
);
610-
}
611-
612-
// Execute all image path loading in parallel for better performance
613-
await Future.wait(loadTasks);
593+
await Future.wait(
594+
groups.map((group) => _loadImagePathForGroup(group, activePubkey, groupImagePaths)),
595+
);
614596

615-
// Update state with the cached image paths
616597
state = state.copyWith(groupImagePaths: groupImagePaths);
617598

618599
_logger.info('GroupsProvider: Loaded image paths for ${groupImagePaths.length} groups');
619600
} catch (e) {
620-
// Log the full exception details with proper ApiError unpacking
621601
String logMessage = 'GroupsProvider: Error loading group image paths - Exception: ';
622602
if (e is ApiError) {
623603
final errorDetails = await e.messageText();
@@ -626,7 +606,33 @@ class GroupsNotifier extends Notifier<GroupsState> {
626606
logMessage += '$e (Type: ${e.runtimeType})';
627607
}
628608
_logger.severe(logMessage, e);
629-
// Don't throw - we want to continue even if some image loading fails
609+
}
610+
}
611+
612+
Future<void> _loadImagePathForGroup(
613+
Group group,
614+
String activePubkey,
615+
Map<String, String> groupImagePaths,
616+
) async {
617+
try {
618+
final groupType = getCachedGroupType(group.mlsGroupId);
619+
String? imagePath;
620+
621+
if (groupType == GroupType.directMessage) {
622+
final otherMember = getOtherGroupMember(group.mlsGroupId);
623+
imagePath = otherMember?.imagePath;
624+
} else {
625+
imagePath = await getGroupImagePath(
626+
accountPubkey: activePubkey,
627+
groupId: group.mlsGroupId,
628+
);
629+
}
630+
631+
if (imagePath != null && imagePath.isNotEmpty) {
632+
groupImagePaths[group.mlsGroupId] = imagePath;
633+
}
634+
} catch (e) {
635+
_logErrorSync('Failed to load image path for group ${group.mlsGroupId}', e);
630636
}
631637
}
632638

@@ -772,12 +778,22 @@ class GroupsNotifier extends Notifier<GroupsState> {
772778
}
773779

774780
/// Load and cache group image path for a single group
781+
/// For DMs: caches the other member's image path
782+
/// For groups: caches the group's image path from Rust API
775783
Future<void> _loadGroupImagePathForGroup(Group group, String activePubkey) async {
776784
try {
777-
final imagePath = await getGroupImagePath(
778-
accountPubkey: activePubkey,
779-
groupId: group.mlsGroupId,
780-
);
785+
final groupType = getCachedGroupType(group.mlsGroupId);
786+
787+
String? imagePath;
788+
if (groupType == GroupType.directMessage) {
789+
final otherMember = getOtherGroupMember(group.mlsGroupId);
790+
imagePath = otherMember?.imagePath;
791+
} else {
792+
imagePath = await getGroupImagePath(
793+
accountPubkey: activePubkey,
794+
groupId: group.mlsGroupId,
795+
);
796+
}
781797

782798
if (imagePath != null && imagePath.isNotEmpty) {
783799
final groupImagePaths = Map<String, String>.from(state.groupImagePaths ?? {});

lib/ui/chat/widgets/chat_header_widget.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,9 @@ class DirectMessageHeader extends ConsumerWidget {
106106

107107
@override
108108
Widget build(BuildContext context, WidgetRef ref) {
109-
// Get the other member's full data from cached members
110109
final groupsNotifier = ref.watch(groupsProvider.notifier);
111110
final otherMember = groupsNotifier.getOtherGroupMember(group.mlsGroupId);
112111

113-
// Show nothing while waiting for data to load
114112
if (otherMember == null) {
115113
return const SizedBox.shrink();
116114
}

0 commit comments

Comments
 (0)