From 63575ac8fe685e9e6b841ac7859d6a46d61bde20 Mon Sep 17 00:00:00 2001 From: Freek van de Ven Date: Fri, 14 Feb 2025 10:27:53 +0100 Subject: [PATCH] feat: allow chatTitleResolver to return null to fallback to default chatTitles --- CHANGELOG.md | 1 + .../lib/src/config/chat_options.dart | 2 +- .../chat_detail/chat_detail_screen.dart | 27 ++++++++++++------- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f609645..ba96bd2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 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 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 - Move to the new user story architecture diff --git a/packages/flutter_chat/lib/src/config/chat_options.dart b/packages/flutter_chat/lib/src/config/chat_options.dart index ac86c0f..4930431 100644 --- a/packages/flutter_chat/lib/src/config/chat_options.dart +++ b/packages/flutter_chat/lib/src/config/chat_options.dart @@ -84,7 +84,7 @@ class ChatOptions { /// Typedef for the chatTitleResolver function that is used to get a title for /// 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 /// [MessageTheme] for a message. This can return null so you can fall back to diff --git a/packages/flutter_chat/lib/src/screens/chat_detail/chat_detail_screen.dart b/packages/flutter_chat/lib/src/screens/chat_detail/chat_detail_screen.dart index 68a0646..9adf9b9 100644 --- a/packages/flutter_chat/lib/src/screens/chat_detail/chat_detail_screen.dart +++ b/packages/flutter_chat/lib/src/screens/chat_detail/chat_detail_screen.dart @@ -129,21 +129,30 @@ class ChatDetailScreen extends HookWidget { required ChatModel chat, required List allUsers, }) { + var options = chatScope.options; + var translations = options.translations; + var title = options.chatTitleResolver?.call(chat); + if (title != null) { + return title; + } + 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 - var otherUser = allUsers.firstWhere( - (u) => u.id != chatScope.userId, - orElse: () => const UserModel( - id: "", - ), - ); + var otherUser = allUsers + .where( + (u) => u.id != chatScope.userId, + ) + .firstOrNull; - return otherUser.fullname?.isNotEmpty ?? false + return otherUser != null && otherUser.fullname != null ? otherUser.fullname - : chatScope.options.translations.anonymousUser; + : translations.anonymousUser; } }