-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor!: initial attachments refactor.
Signed-off-by: xsahil03x <[email protected]>
- Loading branch information
Showing
61 changed files
with
3,613 additions
and
1,696 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
69 changes: 69 additions & 0 deletions
69
packages/stream_chat/lib/src/core/models/attachment_giphy_info.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import 'package:stream_chat/src/core/models/attachment.dart'; | ||
|
||
/// {@template giphy_info_type} | ||
/// The different types of quality for a Giphy attachment. | ||
/// {@endtemplate} | ||
enum GiphyInfoType { | ||
/// Original quality giphy, the largest size to load. | ||
original('original'), | ||
|
||
/// Lower quality with a fixed height, adjusts width according to the | ||
/// Giphy aspect ratio. Lower size than [original]. | ||
fixedHeight('fixed_height'), | ||
|
||
/// Still image of the [fixedHeight] giphy. | ||
fixedHeightStill('fixed_height_still'), | ||
|
||
/// Lower quality with a fixed height with width adjusted according to the | ||
/// aspect ratio and played at a lower frame rate. Significantly lower size, | ||
/// but visually less appealing. | ||
fixedHeightDownsampled('fixed_height_downsampled'); | ||
|
||
/// {@macro giphy_info_type} | ||
const GiphyInfoType(this.value); | ||
|
||
/// The value of the [GiphyInfoType]. | ||
final String value; | ||
} | ||
|
||
/// {@template giphy_info} | ||
/// A class that contains extra information about a Giphy attachment. | ||
/// {@endtemplate} | ||
class GiphyInfo { | ||
/// {@macro giphy_info} | ||
const GiphyInfo({ | ||
required this.url, | ||
required this.width, | ||
required this.height, | ||
}); | ||
|
||
/// The url for the Giphy image. | ||
final String url; | ||
|
||
/// The width of the Giphy image. | ||
final double width; | ||
|
||
/// The height of the Giphy image. | ||
final double height; | ||
|
||
@override | ||
String toString() => 'GiphyInfo{url: $url, width: $width, height: $height}'; | ||
} | ||
|
||
/// GiphyInfo extension on [Attachment] class. | ||
extension GiphyInfoX on Attachment { | ||
/// Returns the [GiphyInfo] for the given [type]. | ||
GiphyInfo? giphyInfo(GiphyInfoType type) { | ||
final giphy = extraData['giphy'] as Map<String, Object?>?; | ||
if (giphy == null) return null; | ||
|
||
final info = giphy[type.value] as Map<String, Object?>?; | ||
if (info == null) return null; | ||
|
||
return GiphyInfo( | ||
url: info['url']! as String, | ||
width: double.parse(info['width']! as String), | ||
height: double.parse(info['height']! as String), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
packages/stream_chat_flutter/lib/src/attachment/attachment.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 0 additions & 57 deletions
57
packages/stream_chat_flutter/lib/src/attachment/attachment_widget.dart
This file was deleted.
Oops, something went wrong.
62 changes: 62 additions & 0 deletions
62
packages/stream_chat_flutter/lib/src/attachment/attachment_widget_catalog.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import 'package:collection/collection.dart'; | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:stream_chat_flutter/src/attachment/builder/attachment_widget_builder.dart'; | ||
import 'package:stream_chat_flutter_core/stream_chat_flutter_core.dart'; | ||
|
||
/// {@template attachmentWidgetCatalog} | ||
/// A widget catalog which determines which attachment widget should be build | ||
/// for a given [Message] and [Attachment] based on the list of [builders]. | ||
/// | ||
/// This is used by the [MessageWidget] to build the widget for the | ||
/// [Message.attachments]. If you want to customize the widget used to show | ||
/// attachments, you can use this to add your own attachment builder. | ||
/// {@endtemplate} | ||
/// | ||
/// See also: | ||
/// | ||
/// * [StreamAttachmentWidgetBuilder], which is used to build a widget for a | ||
/// given [Message] and [Attachment]. | ||
/// * [MessageWidget] which uses the [AttachmentWidgetCatalog] to build the | ||
/// widget for the [Message.attachments]. | ||
class AttachmentWidgetCatalog { | ||
/// {@macro attachmentWidgetCatalog} | ||
const AttachmentWidgetCatalog({required this.builders}); | ||
|
||
/// The list of builders to use to build the widget. | ||
/// | ||
/// The order of the builders is important. The first builder that can handle | ||
/// the message and attachments will be used to build the widget. | ||
final List<StreamAttachmentWidgetBuilder> builders; | ||
|
||
/// Builds a widget for the given [message] and [attachments]. | ||
/// | ||
/// It iterates through the list of builders and uses the first builder | ||
/// that can handle the message and attachments. | ||
/// | ||
/// Throws an [Exception] if no builder is found for the message. | ||
Widget build(BuildContext context, Message message) { | ||
assert(!message.isDeleted, 'Cannot build attachment for deleted message'); | ||
|
||
assert( | ||
message.attachments.isNotEmpty, | ||
'Cannot build attachment for message without attachments', | ||
); | ||
|
||
// The list of attachments to build the widget for. | ||
final attachments = message.attachments.grouped; | ||
for (final builder in builders) { | ||
if (builder.canHandle(message, attachments)) { | ||
return builder.build(context, message, attachments); | ||
} | ||
} | ||
|
||
throw Exception('No builder found for $message and $attachments'); | ||
} | ||
} | ||
|
||
extension on List<Attachment> { | ||
/// Groups the attachments by their type. | ||
Map<String, List<Attachment>> get grouped { | ||
return groupBy(this, (attachment) => attachment.type!); | ||
} | ||
} |
Oops, something went wrong.