Merge pull request #2179 from kaanelloed/24h

chore: use time format based on system settings
This commit is contained in:
Krille-chan 2025-09-09 14:27:42 +02:00 committed by GitHub
commit 6bea0faf84
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:fluffychat/l10n/l10n.dart';
import 'package:fluffychat/utils/platform_infos.dart';
/// Provides extra functionality for formatting the time.
extension DateTimeExtension on DateTime {
@ -28,10 +29,9 @@ extension DateTimeExtension on DateTime {
difference(prevTime) < const Duration(hours: 1);
/// Returns a simple time String.
String localizedTimeOfDay(BuildContext context) =>
L10n.of(context).alwaysUse24HourFormat == 'true'
? DateFormat('HH:mm', L10n.of(context).localeName).format(this)
: DateFormat('h:mm a', L10n.of(context).localeName).format(this);
String localizedTimeOfDay(BuildContext context) => use24HourFormat(context)
? DateFormat('HH:mm', L10n.of(context).localeName).format(this)
: DateFormat('h:mm a', L10n.of(context).localeName).format(this);
/// Returns [localizedTimeOfDay()] if the ChatTime is today, the name of the week
/// day if the ChatTime is this week and a date string else.
@ -76,4 +76,20 @@ extension DateTimeExtension on DateTime {
localizedTimeOfDay(context),
);
}
/// Check if time needs to be in 24h format
bool use24HourFormat(BuildContext context) {
final mediaQuery24h = MediaQuery.alwaysUse24HourFormatOf(context);
final l10n24h = L10n.of(context).alwaysUse24HourFormat == 'true';
// https://github.com/krille-chan/fluffychat/pull/1457#discussion_r1836817914
if (PlatformInfos.isAndroid) {
return mediaQuery24h;
} else if (PlatformInfos.isIOS) {
return mediaQuery24h || l10n24h;
}
return l10n24h;
}
}