| 1 | import 'package:cake_wallet/generated/i18n.dart'; |
| 2 | import 'package:intl/intl.dart'; |
| 3 | import 'package:cake_wallet/di.dart'; |
| 4 | import 'package:cake_wallet/store/settings_store.dart'; |
| 5 | |
| 6 | class DateFormatter { |
| 7 | static String currentLocalFormat({bool hasTime = true, bool reverse = false}) { |
| 8 | // final isUSA = getIt.get<SettingsStore>().languageCode.toLowerCase() == 'en'; |
| 9 | // final format = isUSA ? usaStyleFormat(hasTime, reverse) : regularStyleFormat(hasTime, reverse); |
| 10 | |
| 11 | return hasTime ? (reverse ? "HH:mm MMMM d, yyyy" : "MMMM d, yyyy, HH:mm") : "MMMM d, yyyy"; |
| 12 | } |
| 13 | |
| 14 | static DateFormat withCurrentLocal({bool hasTime = true, bool reverse = false}) => DateFormat( |
| 15 | currentLocalFormat(hasTime: hasTime, reverse: reverse), |
| 16 | getIt.get<SettingsStore>().languageCode); |
| 17 | |
| 18 | static String usaStyleFormat(bool hasTime, bool reverse) => |
| 19 | hasTime ? (reverse ? 'HH:mm yyyy.MM.dd' : 'yyyy.MM.dd, HH:mm') : 'yyyy.MM.dd'; |
| 20 | |
| 21 | static String regularStyleFormat(bool hasTime, bool reverse) => |
| 22 | hasTime ? (reverse ? 'HH:mm dd.MM.yyyy' : 'dd.MM.yyyy, HH:mm') : 'dd.MM.yyyy'; |
| 23 | |
| 24 | static String convertDateTimeToReadableString(DateTime date) { |
| 25 | final nowDate = DateTime.now(); |
| 26 | final diffDays = date.difference(nowDate).inDays; |
| 27 | final isToday = |
| 28 | nowDate.day == date.day && nowDate.month == date.month && nowDate.year == date.year; |
| 29 | final dateSectionDateFormat = withCurrentLocal(hasTime: false); |
| 30 | var title = ""; |
| 31 | |
| 32 | if (isToday) { |
| 33 | title = S.current.today; |
| 34 | } else if (diffDays == 0) { |
| 35 | title = S.current.yesterday; |
| 36 | } else if (diffDays > -7 && diffDays < 0) { |
| 37 | final dateFormat = DateFormat.EEEE(); |
| 38 | title = dateFormat.format(date); |
| 39 | } else { |
| 40 | title = dateSectionDateFormat.format(date); |
| 41 | } |
| 42 | |
| 43 | return title; |
| 44 | } |
| 45 | } |