Skip to content

Commit

Permalink
feat:✨Added support of date-time format for the groupSeparatorBuilder(#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jaiminrana05 committed Aug 28, 2023
1 parent 31eedc2 commit e8b8cb8
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 8 additions & 3 deletions example/lib/data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
Expand Down
4 changes: 2 additions & 2 deletions lib/src/extensions/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/models/message_list_configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions lib/src/utils/constants/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion lib/src/values/typedefs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ typedef DoubleCallBack = void Function(double, double);
typedef MessageCallBack = void Function(Message message);
typedef VoidCallBackWithFuture = Future<void> 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);
31 changes: 26 additions & 5 deletions lib/src/widgets/chat_groupedlist_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,35 @@ class _ChatGroupedListWidgetState extends State<ChatGroupedListWidget>
}

Widget get _chatStreamBuilder {
DateTime? lastMatchedDate;

return StreamBuilder<List<Message>>(
stream: chatController?.messageStreamController.stream,
builder: (context, snapshot) {
return snapshot.connectionState.isActive
? GroupedListView<Message, String>(
? GroupedListView<Message, DateTime>(
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(),
Expand Down Expand Up @@ -350,16 +371,16 @@ 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
Widget build(BuildContext context) {
return groupSeparatorBuilder != null
? groupSeparatorBuilder!(separator)
: ChatGroupHeader(
day: DateTime.parse(separator),
day: separator,
groupSeparatorConfig: defaultGroupSeparatorConfig,
);
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit e8b8cb8

Please sign in to comment.