Skip to content

Commit

Permalink
feat: add relative timestamp
Browse files Browse the repository at this point in the history
This is related to #334
  • Loading branch information
Julian KOUNE authored and hoangdat committed Jul 21, 2023
1 parent a06dfb3 commit 6b20c1a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
8 changes: 1 addition & 7 deletions lib/pages/chat/others_group_chat_empty_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:fluffychat/resource/image_paths.dart';
import 'package:intl/intl.dart';
import 'package:linagora_design_flutter/colors/linagora_ref_colors.dart';
import 'package:matrix/matrix.dart';

Expand Down Expand Up @@ -67,14 +66,9 @@ class OthersGroupChatEmptyView extends StatelessWidget {

Widget _getDateWidget(BuildContext context, Event firstEvent) {
final eventDateTime = firstEvent.originServerTs;
final dateString = eventDateTime.isToday()
? L10n.of(context)!.today
: eventDateTime.isYesterday()
? L10n.of(context)!.yesterday
: DateFormat("dd MMM").format(eventDateTime);

return Text(
dateString,
eventDateTime.relativeTime(context),
style: Theme.of(context).textTheme.bodySmall?.copyWith(
fontWeight: FontWeight.w500,
fontSize: 12,
Expand Down
18 changes: 15 additions & 3 deletions lib/utils/date_time_extension.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';

import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:intl/intl.dart';

/// Provides extra functionality for formatting the time.
extension DateTimeExtension on DateTime {
Expand Down Expand Up @@ -108,12 +109,23 @@ extension DateTimeExtension on DateTime {
static String _z(int i) => i < 10 ? '0${i.toString()}' : i.toString();

bool isToday() {
final now = DateTime.now();
return now.day == day && now.month == month && now.year == year;
return DateUtils.isSameDay(this, DateTime.now());
}

bool isYesterday() {
final yesterday = DateTime.now().subtract(const Duration(days: 1));
return yesterday.day == day && yesterday.month == month && yesterday.year == year;
return DateUtils.isSameDay(this, yesterday);
}

String relativeTime(BuildContext context) {
if (isToday()) {
return L10n.of(context)!.today;
} else if (isYesterday()) {
return L10n.of(context)!.yesterday;
} else if (year == DateTime.now().year) {
return DateFormat("MMMM d").format(this);
} else {
return DateFormat("MMMM d, y").format(this);
}
}
}

0 comments on commit 6b20c1a

Please sign in to comment.