Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lightbox: Use a friendlier format for the date #987

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions lib/widgets/lightbox.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,28 @@ class _LightboxPageLayoutState extends State<_LightboxPageLayout> {
});
}

String _formatDate(DateTime messageDate) {
final now = DateTime.now();
final difference = now.difference(messageDate);
final locale = Localizations.localeOf(context).toString();

if (difference.inSeconds < 60) {
return "A few seconds ago";
} else if (difference.inMinutes < 60) {
return "${difference.inMinutes} ${difference.inMinutes == 1 ? 'minute' : 'minutes'} ago";
} else if (difference.inHours < 24) {
final time = DateFormat.jm(locale).format(messageDate);
return "Today at $time";
} else if (difference.inHours < 48) {
final time = DateFormat.jm(locale).format(messageDate);
return "Yesterday at $time";
} else {
final date = DateFormat('MMM d, yyyy', locale).format(messageDate);
final time = DateFormat('hh:mm a', locale).format(messageDate);
return "$date at $time";
}
Comment on lines +150 to +164
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are untranslated English strings. Instead, we should show strings in the user's chosen language.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the feedback, @chrisbobbe.
Please can you provide a suggestion on how i should achieve that?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not able to prioritize that right now; this is a post-launch issue. Maybe there's a library out there that can handle this for us?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay thank you, I will look that up.

}

@override
Widget build(BuildContext context) {
final themeData = Theme.of(context);
Expand All @@ -152,11 +174,8 @@ class _LightboxPageLayoutState extends State<_LightboxPageLayout> {

PreferredSizeWidget? appBar;
if (_headerFooterVisible) {
// TODO(#45): Format with e.g. "Yesterday at 4:47 PM"
final timestampText = DateFormat
.yMMMd(/* TODO(#278): Pass selected language here, I think? */)
.add_Hms()
.format(DateTime.fromMillisecondsSinceEpoch(widget.message.timestamp * 1000));
final messageDate = DateTime.fromMillisecondsSinceEpoch(widget.message.timestamp * 1000);
final timestampText = _formatDate(messageDate);

// We use plain [AppBar] instead of [ZulipAppBar], even though this page
// has a [PerAccountStore], because:
Expand Down
2 changes: 1 addition & 1 deletion test/widgets/lightbox_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ void main() {
matching: find.textContaining(findRichText: true,
eg.otherUser.fullName)));
check(labelTextWidget.text.toPlainText())
.contains('Jul 23, 2024 23:12:24');
.contains('Jul 23, 2024 at 11:12 PM');

debugNetworkImageHttpClientProvider = null;
});
Expand Down