diff --git a/lib/utils/date_time_extension.dart b/lib/utils/date_time_extension.dart index dcd4aa851..65a6246df 100644 --- a/lib/utils/date_time_extension.dart +++ b/lib/utils/date_time_extension.dart @@ -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; + } }