From e8b8cb8d4d3e56beac1ebfa25e8ec62071a4da72 Mon Sep 17 00:00:00 2001 From: Jaimin Rana Date: Mon, 28 Aug 2023 16:57:44 +0530 Subject: [PATCH] =?UTF-8?q?feat:=E2=9C=A8Added=20support=20of=20date-time?= =?UTF-8?q?=20format=20for=20the=20groupSeparatorBuilder(#93).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 5 +++ example/lib/data.dart | 11 +++++-- lib/src/extensions/extensions.dart | 4 +-- .../models/message_list_configuration.dart | 2 +- lib/src/utils/constants/constants.dart | 1 + lib/src/values/typedefs.dart | 2 +- lib/src/widgets/chat_groupedlist_widget.dart | 31 ++++++++++++++++--- pubspec.yaml | 2 +- 8 files changed, 45 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec2141f0..fcbd0a26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## [1.4.0] + +* **Feat**: [93](https://github.com/SimformSolutionsPvtLtd/flutter_chatview/issues/93)Added support + of date-time format for the groupSeparatorBuilder of 'ChatBackgroundConfiguration' class. + ## [1.3.1] * **Feat**: [105](https://github.com/SimformSolutionsPvtLtd/flutter_chatview/pull/105) Allow user diff --git a/example/lib/data.dart b/example/lib/data.dart index cf086eae..e2f4fdad 100644 --- a/example/lib/data.dart +++ b/example/lib/data.dart @@ -7,14 +7,19 @@ class Data { Message( id: '1', message: "Hi!", - createdAt: DateTime.now(), - sendBy: '1', // userId of who sends the message + createdAt: DateTime.now().copyWith( + day: DateTime.now().day - 2, + ), + sendBy: '1', + // userId of who sends the message status: MessageStatus.read, ), Message( id: '2', message: "Hi!", - createdAt: DateTime.now(), + createdAt: DateTime.now().copyWith( + day: DateTime.now().day - 1, + ), sendBy: '2', status: MessageStatus.read, ), diff --git a/lib/src/extensions/extensions.dart b/lib/src/extensions/extensions.dart index 666d4021..119a1f1f 100644 --- a/lib/src/extensions/extensions.dart +++ b/lib/src/extensions/extensions.dart @@ -31,14 +31,14 @@ import '../utils/package_strings.dart'; extension TimeDifference on DateTime { String get getDay { final DateTime formattedDate = DateFormat(dateFormat).parse(toString()); - final DateFormat formatter = DateFormat.yMMMMd(enUS); + final DateFormat formatter = DateFormat(dateFormatWithTime,enUS); final differenceInDays = formattedDate.difference(DateTime.now()).inDays; if (differenceInDays == 0) { return PackageStrings.today; } else if (differenceInDays <= 1 && differenceInDays >= -1) { return PackageStrings.yesterday; } else { - return formatter.format(formattedDate); + return formatter.format(this); } } diff --git a/lib/src/models/message_list_configuration.dart b/lib/src/models/message_list_configuration.dart index a1fcfc10..1eedc0d8 100644 --- a/lib/src/models/message_list_configuration.dart +++ b/lib/src/models/message_list_configuration.dart @@ -44,7 +44,7 @@ class ChatBackgroundConfiguration { final double? width; /// Provides configurations of chat separator widget. - final StringWithReturnWidget? groupSeparatorBuilder; + final MessageGroupSeparator? groupSeparatorBuilder; /// Used to define the order of a [GroupedListView] or [SliverGroupedListView]. final GroupedListOrder groupedListOrder; diff --git a/lib/src/utils/constants/constants.dart b/lib/src/utils/constants/constants.dart index ae8e6188..accfb047 100644 --- a/lib/src/utils/constants/constants.dart +++ b/lib/src/utils/constants/constants.dart @@ -32,6 +32,7 @@ const String emojiRegExpression = const String imageUrlRegExpression = r'(http(s?):)([/|.|\w|\s|-])*\.(?:jpg|gif|png|jpeg)'; const String dateFormat = "yyyy-MM-dd"; +const String dateFormatWithTime = "E, d MMM yyyy HH:mm"; const String jpg = ".jpg"; const String png = ".png"; const String jpeg = ".jpeg"; diff --git a/lib/src/values/typedefs.dart b/lib/src/values/typedefs.dart index 43ea2e41..85e414a0 100644 --- a/lib/src/values/typedefs.dart +++ b/lib/src/values/typedefs.dart @@ -33,5 +33,5 @@ typedef DoubleCallBack = void Function(double, double); typedef MessageCallBack = void Function(Message message); typedef VoidCallBackWithFuture = Future Function(); typedef StringsCallBack = void Function(String emoji, String messageId); -typedef StringWithReturnWidget = Widget Function(String separator); +typedef MessageGroupSeparator = Widget Function(DateTime separator); typedef DragUpdateDetailsCallback = void Function(DragUpdateDetails); diff --git a/lib/src/widgets/chat_groupedlist_widget.dart b/lib/src/widgets/chat_groupedlist_widget.dart index 9f8b4da1..0aac464e 100644 --- a/lib/src/widgets/chat_groupedlist_widget.dart +++ b/lib/src/widgets/chat_groupedlist_widget.dart @@ -274,14 +274,35 @@ class _ChatGroupedListWidgetState extends State } Widget get _chatStreamBuilder { + DateTime? lastMatchedDate; + return StreamBuilder>( stream: chatController?.messageStreamController.stream, builder: (context, snapshot) { return snapshot.connectionState.isActive - ? GroupedListView( + ? GroupedListView( shrinkWrap: true, elements: snapshot.data!, - groupBy: (element) => element.createdAt.getDateFromDateTime, + groupBy: (element) { + if (lastMatchedDate == null) { + lastMatchedDate = element.createdAt; + return element.createdAt; + } + + /// when conversation is going-on on same date we return + /// same date [lastMatchedDate] + else if (lastMatchedDate!.getDateFromDateTime == + element.createdAt.getDateFromDateTime) { + return lastMatchedDate!; + } + + /// When conversation start at new date we are assigning new + /// date to [lastMatchedDate] + else { + lastMatchedDate = element.createdAt; + return element.createdAt; + } + }, itemComparator: (message1, message2) => message1.message.compareTo(message2.message), physics: const NeverScrollableScrollPhysics(), @@ -350,8 +371,8 @@ class _GroupSeparatorBuilder extends StatelessWidget { this.groupSeparatorBuilder, this.defaultGroupSeparatorConfig, }) : super(key: key); - final String separator; - final StringWithReturnWidget? groupSeparatorBuilder; + final DateTime separator; + final MessageGroupSeparator? groupSeparatorBuilder; final DefaultGroupSeparatorConfiguration? defaultGroupSeparatorConfig; @override @@ -359,7 +380,7 @@ class _GroupSeparatorBuilder extends StatelessWidget { return groupSeparatorBuilder != null ? groupSeparatorBuilder!(separator) : ChatGroupHeader( - day: DateTime.parse(separator), + day: separator, groupSeparatorConfig: defaultGroupSeparatorConfig, ); } diff --git a/pubspec.yaml b/pubspec.yaml index bf4ee825..1dba6af0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: chatview description: A Flutter package that allows you to integrate Chat View with highly customization options. -version: 1.3.1 +version: 1.4.0 issue_tracker: https://github.com/SimformSolutionsPvtLtd/flutter_chatview/issues repository: https://github.com/SimformSolutionsPvtLtd/flutter_chatview