feat: allow chatTitleResolver to return null to fallback to default chatTitles

This commit is contained in:
Freek van de Ven 2025-02-14 10:27:53 +01:00
parent 0adc4f975b
commit 7650872fdb
3 changed files with 20 additions and 10 deletions

View file

@ -12,6 +12,7 @@
- Changed the ChatDetailScreen to use the chatId instead of the ChatModel, the screen will now fetch the chat from the ChatService - Changed the ChatDetailScreen to use the chatId instead of the ChatModel, the screen will now fetch the chat from the ChatService
- Changed baseScreenBuilder to include a chatTitle that can be used to show provide the title logic to apps that use the baseScreenBuilder - Changed baseScreenBuilder to include a chatTitle that can be used to show provide the title logic to apps that use the baseScreenBuilder
- Added loadNewMessagesAfter, loadOldMessagesBefore and removed pagination from getMessages in the ChatRepositoryInterface to change pagination behavior to rely on the stream and two methods indicating that more messages should be added to the stream - Added loadNewMessagesAfter, loadOldMessagesBefore and removed pagination from getMessages in the ChatRepositoryInterface to change pagination behavior to rely on the stream and two methods indicating that more messages should be added to the stream
- Added chatTitleResolver that can be used to resolve the chat title from the chat model or return null to allow for default behavior
## 4.0.0 ## 4.0.0
- Move to the new user story architecture - Move to the new user story architecture

View file

@ -84,7 +84,7 @@ class ChatOptions {
/// Typedef for the chatTitleResolver function that is used to get a title for /// Typedef for the chatTitleResolver function that is used to get a title for
/// a chat. /// a chat.
typedef ChatTitleResolver = String Function(ChatModel chat); typedef ChatTitleResolver = String? Function(ChatModel chat);
/// Typedef for the messageThemeResolver function that is used to get a /// Typedef for the messageThemeResolver function that is used to get a
/// [MessageTheme] for a message. This can return null so you can fall back to /// [MessageTheme] for a message. This can return null so you can fall back to

View file

@ -129,21 +129,30 @@ class ChatDetailScreen extends HookWidget {
required ChatModel chat, required ChatModel chat,
required List<UserModel> allUsers, required List<UserModel> allUsers,
}) { }) {
var options = chatScope.options;
var translations = options.translations;
var title = options.chatTitleResolver?.call(chat);
if (title != null) {
return title;
}
if (chat.isGroupChat) { if (chat.isGroupChat) {
return chatScope.options.translations.groupNameEmpty; if (chat.chatName?.isNotEmpty ?? false) {
return chat.chatName;
}
return translations.groupNameEmpty;
} }
// For one-to-one, pick the 'other' user from the list // For one-to-one, pick the 'other' user from the list
var otherUser = allUsers.firstWhere( var otherUser = allUsers
.where(
(u) => u.id != chatScope.userId, (u) => u.id != chatScope.userId,
orElse: () => const UserModel( )
id: "", .firstOrNull;
),
);
return otherUser.fullname?.isNotEmpty ?? false return otherUser != null && otherUser.fullname != null
? otherUser.fullname ? otherUser.fullname
: chatScope.options.translations.anonymousUser; : translations.anonymousUser;
} }
} }