feat: add imageAuthenticationResolver in the ChatOptions for CachedNetworkImage image requests

This commit is contained in:
Freek van de Ven 2025-02-20 14:35:10 +01:00
parent f93a450259
commit 836686f8d2
4 changed files with 43 additions and 18 deletions

View file

@ -20,6 +20,7 @@
- Changed the ChatBottomInputSection to be multiline and go from 45px to 120px in height depending on how many lines are in the textfield - Changed the ChatBottomInputSection to be multiline and go from 45px to 120px in height depending on how many lines are in the textfield
- Added chatScreenBuilder to the userstory configuration to customize the specific chat screen with a ChatModel as argument - Added chatScreenBuilder to the userstory configuration to customize the specific chat screen with a ChatModel as argument
- Added senderTitleResolver to the ChatOptions to resolve the title of the sender in the chat message - Added senderTitleResolver to the ChatOptions to resolve the title of the sender in the chat message
- Added imageAuthenticationResolver to the ChatOptions to resolve the authentication headers for CachedNetworkImage for all images in the userstory
## 4.0.0 ## 4.0.0
- Move to the new user story architecture - Move to the new user story architecture

View file

@ -23,6 +23,7 @@ class ChatOptions {
this.iconDisabledColor, this.iconDisabledColor,
this.chatAlignment, this.chatAlignment,
this.onNoChats, this.onNoChats,
this.imageAuthenticationResolver,
ChatRepositoryInterface? chatRepository, ChatRepositoryInterface? chatRepository,
UserRepositoryInterface? userRepository, UserRepositoryInterface? userRepository,
}) : chatRepository = chatRepository ?? LocalChatRepository(), }) : chatRepository = chatRepository ?? LocalChatRepository(),
@ -90,6 +91,11 @@ class ChatOptions {
/// [onNoChats] is a function that is triggered when there are no chats. /// [onNoChats] is a function that is triggered when there are no chats.
final Function? onNoChats; final Function? onNoChats;
/// If [imageAuthenticationResolver] is set, it will be used to get the
/// authentication headers for an image. The [imageUrl] is provided to allow
/// for different authentication headers for different images.
final ImageAuthenticationResolver? imageAuthenticationResolver;
} }
/// 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
@ -100,6 +106,12 @@ typedef ChatTitleResolver = String? Function(ChatModel chat);
/// a sender. /// a sender.
typedef SenderTitleResolver = String? Function(UserModel? user); typedef SenderTitleResolver = String? Function(UserModel? user);
/// Typedef for the imageAuthenticationResolver function that is used to get
/// authentication headers for an image.
typedef ImageAuthenticationResolver = Map<String, String>? Function(
String imageUrl,
);
/// 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
/// default values for some messages. /// default values for some messages.

View file

@ -241,7 +241,10 @@ class _DefaultChatImage extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var chatScope = ChatScope.of(context);
var options = chatScope.options;
var textTheme = Theme.of(context).textTheme; var textTheme = Theme.of(context).textTheme;
var imageUrl = message.imageUrl!;
return Padding( return Padding(
padding: const EdgeInsets.symmetric(horizontal: 4), padding: const EdgeInsets.symmetric(horizontal: 4),
child: SizedBox( child: SizedBox(
@ -251,10 +254,11 @@ class _DefaultChatImage extends StatelessWidget {
child: AnimatedSize( child: AnimatedSize(
duration: const Duration(milliseconds: 300), duration: const Duration(milliseconds: 300),
child: CachedNetworkImage( child: CachedNetworkImage(
imageUrl: message.imageUrl!, imageUrl: imageUrl,
fit: BoxFit.fitWidth, fit: BoxFit.fitWidth,
httpHeaders: options.imageAuthenticationResolver?.call(imageUrl),
errorWidget: (context, url, error) => Text( errorWidget: (context, url, error) => Text(
"Something went wrong", "Something went wrong with loading the image",
style: textTheme.bodyLarge?.copyWith( style: textTheme.bodyLarge?.copyWith(
color: messageTheme.textColor, color: messageTheme.textColor,
), ),

View file

@ -161,6 +161,8 @@ class OldChatMessageBuilder extends StatelessWidget {
: message.isImageMessage : message.isImageMessage
? CachedNetworkImage( ? CachedNetworkImage(
imageUrl: message.imageUrl ?? "", imageUrl: message.imageUrl ?? "",
httpHeaders: options.imageAuthenticationResolver
?.call(message.imageUrl ?? ""),
) )
: const SizedBox.shrink(), : const SizedBox.shrink(),
), ),
@ -182,7 +184,11 @@ class _ChatImage extends StatelessWidget {
final String image; final String image;
@override @override
Widget build(BuildContext context) => Container( Widget build(BuildContext context) {
var chatScope = ChatScope.of(context);
var options = chatScope.options;
return Container(
clipBehavior: Clip.hardEdge, clipBehavior: Clip.hardEdge,
decoration: BoxDecoration( decoration: BoxDecoration(
color: Colors.black, color: Colors.black,
@ -194,8 +200,10 @@ class _ChatImage extends StatelessWidget {
? CachedNetworkImage( ? CachedNetworkImage(
fadeInDuration: Duration.zero, fadeInDuration: Duration.zero,
imageUrl: image, imageUrl: image,
httpHeaders: options.imageAuthenticationResolver?.call(image),
fit: BoxFit.cover, fit: BoxFit.cover,
) )
: null, : null,
); );
} }
}