diff --git a/lib/main.dart b/lib/main.dart index 852b033..09a4d2a 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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'; @@ -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 { diff --git a/lib/pages/search_page.dart b/lib/pages/search_page.dart index 03b2240..7d749aa 100644 --- a/lib/pages/search_page.dart +++ b/lib/pages/search_page.dart @@ -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, @@ -94,14 +95,34 @@ class _MessageSearchPageState extends State { 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), ); diff --git a/lib/pages/whisper_page.dart b/lib/pages/whisper_page.dart index 443e658..58a9d13 100644 --- a/lib/pages/whisper_page.dart +++ b/lib/pages/whisper_page.dart @@ -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( diff --git a/lib/utils/chat_date.dart b/lib/utils/chat_date.dart new file mode 100644 index 0000000..768a8e0 --- /dev/null +++ b/lib/utils/chat_date.dart @@ -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); +} diff --git a/lib/widgets/chat_list_item.dart b/lib/widgets/chat_list_item.dart index 87c5269..f548b95 100644 --- a/lib/widgets/chat_list_item.dart +++ b/lib/widgets/chat_list_item.dart @@ -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, @@ -22,35 +23,6 @@ class ChatListItem extends StatefulWidget { } class _ChatListItemState extends State { - 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; @@ -133,7 +105,11 @@ class _ChatListItemState extends State { mainAxisSize: MainAxisSize.min, children: [ Text( - formatDate(), + chatDateLabel( + widget.lastMessage.receivedAt, + todayLabel: 'todayText'.tr(), + yesterdayLabel: 'yesterdayText'.tr(), + ), style: unread ? TextStyle( fontWeight: FontWeight.bold, diff --git a/test/chat_date_test.dart b/test/chat_date_test.dart new file mode 100644 index 0000000..14928ee --- /dev/null +++ b/test/chat_date_test.dart @@ -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); + }); +}