Skip to content
Merged
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
9 changes: 9 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import 'dart:io';

import 'package:easy_localization/easy_localization.dart';
// intl is pinned through easy_localization; declaring it a second time would
// only duplicate that pin.
// ignore: depend_on_referenced_packages
import 'package:intl/date_symbol_data_local.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -48,6 +52,11 @@ void main() async {

// Load locale (the notification service needs it for the channel texts)
await EasyLocalization.ensureInitialized();
// Date patterns and month names for the languages the app ships with. Without
// them DateFormat falls back to English for everything.
for (final locale in ['en', 'cs']) {
await initializeDateFormatting(locale);
}

// Local notifications. A failure here must not block the app.
try {
Expand Down
31 changes: 26 additions & 5 deletions lib/pages/search_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter/material.dart';

import '../models/user.dart';
import '../utils/cache_utils.dart';
import '../utils/chat_date.dart';
import '../utils/message_search.dart';

/// Searches one conversation's local history. Messages are stored decrypted,
Expand Down Expand Up @@ -94,14 +95,34 @@ class _MessageSearchPageState extends State<MessageSearchPage> {

Widget _resultTile(MessageHit hit) {
final message = hit.message;
final time = DateFormat.MMMd().add_Hm().format(message.sentAt.toLocal());
return ListTile(
leading: CircleAvatar(
radius: 20,
backgroundColor: widget.peer.avatarColor,
child: Text(
widget.peer.username.isNotEmpty
? widget.peer.username[0].toUpperCase()
: '?',
style: TextStyle(
fontSize: 16,
color: widget.peer.avatarTextColor,
),
),
),
title: _snippetText(message.preview),
trailing: Text(
time,
style: TextStyle(
subtitle: Padding(
padding: const EdgeInsets.only(top: 2),
child: Text(
chatDateLabel(
message.sentAt,
todayLabel: 'todayText'.tr(),
yesterdayLabel: 'yesterdayText'.tr(),
),
style: TextStyle(
fontSize: 12,
color: Theme.of(context).colorScheme.onSurfaceVariant),
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
),
onTap: () => Navigator.pop(context, message.sentAt),
);
Expand Down
6 changes: 6 additions & 0 deletions lib/pages/whisper_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ class WhisperPage extends StatelessWidget {
return MaterialApp(
navigatorObservers: [PageNavigatorObserver()],
debugShowCheckedModeBanner: true,
// Every DateFormat in the app (chat rows, bubbles, day headers, search)
// follows the selected language instead of the platform default.
builder: (context, child) {
Intl.defaultLocale = context.locale.toString();
return child ?? const SizedBox.shrink();
},
// Light theme
theme: ThemeData.from(
colorScheme: ColorScheme.fromSeed(
Expand Down
36 changes: 36 additions & 0 deletions lib/utils/chat_date.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:easy_localization/easy_localization.dart';

/// Date label of a conversation row and of a search result: today and
/// yesterday by name, then the weekday for the last week, then the date.
///
/// [todayLabel] and [yesterdayLabel] are passed in because a pure helper
/// cannot reach the localization; [now] is injectable for tests. Patterns and
/// month names follow `Intl.defaultLocale`, which the app keeps in sync with
/// the selected language.
String chatDateLabel(
DateTime when, {
required String todayLabel,
required String yesterdayLabel,
DateTime? now,
}) {
final current = (now ?? DateTime.now()).toLocal();
final date = when.toLocal();
final time = DateFormat.Hm().format(date);
final days = DateTime(date.year, date.month, date.day)
.difference(DateTime(current.year, current.month, current.day))
.inDays;

if (days == 0) {
return '$todayLabel $time';
}
if (days == -1) {
return '$yesterdayLabel $time';
}
if (days >= -7) {
return '${DateFormat.EEEE().format(date)} $time';
}
if (date.year == current.year) {
return DateFormat.MMMMd().format(date);
}
return DateFormat.yMMMMd().format(date);
}
36 changes: 6 additions & 30 deletions lib/widgets/chat_list_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import '../models/private_message.dart';
import '../models/user.dart';
import '../utils/chat_date.dart';

class ChatListItem extends StatefulWidget {
const ChatListItem(this.user, this.lastMessage, this.onPressed,
Expand All @@ -22,35 +23,6 @@ class ChatListItem extends StatefulWidget {
}

class _ChatListItemState extends State<ChatListItem> {
String formatDate() {
final DateTime now = DateTime.now().toLocal();
final DateTime messageDate = widget.lastMessage.receivedAt.toLocal();
final int daysDifference = messageDate.difference(now).inDays;

// Today
if (messageDate.day == now.day &&
messageDate.month == now.month &&
messageDate.year == now.year) {
return '${'todayText'.tr()} ${DateFormat.Hm().format(messageDate)}';
}
// Yesterday
else if (daysDifference == -1) {
return '${'yesterdayText'.tr()} ${DateFormat.Hm().format(messageDate)}';
}
// Last week
else if (daysDifference >= -7) {
return '${DateFormat.EEEE().format(messageDate)} ${DateFormat.Hm().format(messageDate)}';
}
// This year
else if (messageDate.year == now.year) {
return DateFormat.MMMMd().format(messageDate);
}
// Different year
else {
return DateFormat.yMMMMd().format(messageDate);
}
}

/// Rounded counter, capped the way every messenger does it.
Widget _unreadBadge() {
final scheme = Theme.of(context).colorScheme;
Expand Down Expand Up @@ -133,7 +105,11 @@ class _ChatListItemState extends State<ChatListItem> {
mainAxisSize: MainAxisSize.min,
children: [
Text(
formatDate(),
chatDateLabel(
widget.lastMessage.receivedAt,
todayLabel: 'todayText'.tr(),
yesterdayLabel: 'yesterdayText'.tr(),
),
style: unread
? TextStyle(
fontWeight: FontWeight.bold,
Expand Down
43 changes: 43 additions & 0 deletions test/chat_date_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import 'package:easy_localization/easy_localization.dart';
// intl is pinned through easy_localization; same import as the app itself.
// ignore: depend_on_referenced_packages
import 'package:intl/date_symbol_data_local.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:whisper/utils/chat_date.dart';

void main() {
final now = DateTime(2026, 6, 10, 12);

setUpAll(() async {
await initializeDateFormatting('en');
Intl.defaultLocale = 'en';
});

String label(DateTime when) => chatDateLabel(when,
todayLabel: 'Today', yesterdayLabel: 'Yesterday', now: now);

test('today carries the localized label and the time', () {
expect(label(DateTime(2026, 6, 10, 9, 5)), 'Today 09:05');
});

test('yesterday carries its own label', () {
expect(label(DateTime(2026, 6, 9, 23, 59)), 'Yesterday 23:59');
});

test('the last week shows the weekday name', () {
final when = DateTime(2026, 6, 7, 8, 30);
expect(label(when), '${DateFormat.EEEE().format(when)} 08:30');
});

test('an older message this year shows the date without the year', () {
final when = DateTime(2026, 5, 3, 8);
expect(label(when), DateFormat.MMMMd().format(when));
expect(label(when).contains('2026'), isFalse);
});

test('a message from another year shows the year', () {
final when = DateTime(2025, 12, 31, 8);
expect(label(when), DateFormat.yMMMMd().format(when));
expect(label(when).contains('2025'), isTrue);
});
}
Loading