chore: use time format based on system settings

This commit is contained in:
kaanelloed 2025-09-01 17:03:00 -04:00 committed by David
parent 4b2611e892
commit e584c22c8b

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,19 @@ 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';
if (PlatformInfos.isAndroid) {
return mediaQuery24h;
} else if (PlatformInfos.isIOS) {
return mediaQuery24h || l10n24h;
}
return l10n24h;
}
}