2020-01-09 12:16:34 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2020-10-03 13:11:07 +02:00
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
2020-01-09 12:16:34 +01:00
|
|
|
|
|
|
|
/// Provides extra functionality for formatting the time.
|
|
|
|
extension DateTimeExtension on DateTime {
|
2020-05-13 15:58:59 +02:00
|
|
|
bool operator <(DateTime other) {
|
|
|
|
return millisecondsSinceEpoch < other.millisecondsSinceEpoch;
|
2020-01-09 12:16:34 +01:00
|
|
|
}
|
|
|
|
|
2020-05-13 15:58:59 +02:00
|
|
|
bool operator >(DateTime other) {
|
|
|
|
return millisecondsSinceEpoch > other.millisecondsSinceEpoch;
|
2020-01-09 12:16:34 +01:00
|
|
|
}
|
|
|
|
|
2020-05-13 15:58:59 +02:00
|
|
|
bool operator >=(DateTime other) {
|
|
|
|
return millisecondsSinceEpoch >= other.millisecondsSinceEpoch;
|
2020-01-09 12:16:34 +01:00
|
|
|
}
|
|
|
|
|
2020-05-13 15:58:59 +02:00
|
|
|
bool operator <=(DateTime other) {
|
|
|
|
return millisecondsSinceEpoch <= other.millisecondsSinceEpoch;
|
2020-01-09 12:16:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Two message events can belong to the same environment. That means that they
|
|
|
|
/// don't need to display the time they were sent because they are close
|
|
|
|
/// enaugh.
|
|
|
|
static final minutesBetweenEnvironments = 5;
|
|
|
|
|
|
|
|
/// Checks if two DateTimes are close enough to belong to the same
|
|
|
|
/// environment.
|
|
|
|
bool sameEnvironment(DateTime prevTime) {
|
|
|
|
return millisecondsSinceEpoch - prevTime.millisecondsSinceEpoch <
|
|
|
|
1000 * 60 * minutesBetweenEnvironments;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a simple time String.
|
|
|
|
/// TODO: Add localization
|
|
|
|
String localizedTimeOfDay(BuildContext context) {
|
2020-09-23 18:26:04 +02:00
|
|
|
if (MediaQuery.of(context).alwaysUse24HourFormat) {
|
|
|
|
return '${_z(hour)}:${_z(minute)}';
|
|
|
|
} else {
|
|
|
|
return '${_z(hour % 12 == 0 ? 12 : hour % 12)}:${_z(minute)} ${hour > 11 ? "pm" : "am"}';
|
|
|
|
}
|
2020-01-09 12:16:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns [localizedTimeOfDay()] if the ChatTime is today, the name of the week
|
|
|
|
/// day if the ChatTime is this week and a date string else.
|
|
|
|
String localizedTimeShort(BuildContext context) {
|
2021-04-14 10:37:15 +02:00
|
|
|
final now = DateTime.now();
|
2020-01-09 12:16:34 +01:00
|
|
|
|
2021-04-14 10:37:15 +02:00
|
|
|
final sameYear = now.year == year;
|
2020-01-09 12:16:34 +01:00
|
|
|
|
2021-04-14 10:37:15 +02:00
|
|
|
final sameDay = sameYear && now.month == month && now.day == day;
|
2020-01-09 12:16:34 +01:00
|
|
|
|
2021-04-14 10:37:15 +02:00
|
|
|
final sameWeek = sameYear &&
|
2020-01-09 12:16:34 +01:00
|
|
|
!sameDay &&
|
2020-05-13 15:58:59 +02:00
|
|
|
now.millisecondsSinceEpoch - millisecondsSinceEpoch <
|
2020-01-09 12:16:34 +01:00
|
|
|
1000 * 60 * 60 * 24 * 7;
|
|
|
|
|
|
|
|
if (sameDay) {
|
|
|
|
return localizedTimeOfDay(context);
|
|
|
|
} else if (sameWeek) {
|
2020-05-13 15:58:59 +02:00
|
|
|
switch (weekday) {
|
2020-01-09 12:16:34 +01:00
|
|
|
case 1:
|
2020-05-07 07:52:40 +02:00
|
|
|
return L10n.of(context).monday;
|
2020-01-09 12:16:34 +01:00
|
|
|
case 2:
|
2020-05-07 07:52:40 +02:00
|
|
|
return L10n.of(context).tuesday;
|
2020-01-09 12:16:34 +01:00
|
|
|
case 3:
|
2020-05-07 07:52:40 +02:00
|
|
|
return L10n.of(context).wednesday;
|
2020-01-09 12:16:34 +01:00
|
|
|
case 4:
|
2020-05-07 07:52:40 +02:00
|
|
|
return L10n.of(context).thursday;
|
2020-01-09 12:16:34 +01:00
|
|
|
case 5:
|
2020-05-07 07:52:40 +02:00
|
|
|
return L10n.of(context).friday;
|
2020-01-09 12:16:34 +01:00
|
|
|
case 6:
|
2020-05-07 07:52:40 +02:00
|
|
|
return L10n.of(context).saturday;
|
2020-01-09 12:16:34 +01:00
|
|
|
case 7:
|
2020-05-07 07:52:40 +02:00
|
|
|
return L10n.of(context).sunday;
|
2020-01-09 12:16:34 +01:00
|
|
|
}
|
|
|
|
} else if (sameYear) {
|
2020-05-07 07:52:40 +02:00
|
|
|
return L10n.of(context).dateWithoutYear(
|
2020-05-13 15:58:59 +02:00
|
|
|
month.toString().padLeft(2, '0'), day.toString().padLeft(2, '0'));
|
2020-01-09 12:16:34 +01:00
|
|
|
}
|
2020-05-13 15:58:59 +02:00
|
|
|
return L10n.of(context).dateWithYear(year.toString(),
|
|
|
|
month.toString().padLeft(2, '0'), day.toString().padLeft(2, '0'));
|
2020-01-09 12:16:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// If the DateTime is today, this returns [localizedTimeOfDay()], if not it also
|
|
|
|
/// shows the date.
|
|
|
|
/// TODO: Add localization
|
|
|
|
String localizedTime(BuildContext context) {
|
2021-04-14 10:37:15 +02:00
|
|
|
final now = DateTime.now();
|
2020-01-09 12:16:34 +01:00
|
|
|
|
2021-04-14 10:37:15 +02:00
|
|
|
final sameYear = now.year == year;
|
2020-01-09 12:16:34 +01:00
|
|
|
|
2021-04-14 10:37:15 +02:00
|
|
|
final sameDay = sameYear && now.month == month && now.day == day;
|
2020-01-09 12:16:34 +01:00
|
|
|
|
|
|
|
if (sameDay) return localizedTimeOfDay(context);
|
2020-05-07 07:52:40 +02:00
|
|
|
return L10n.of(context).dateAndTimeOfDay(
|
2020-01-20 13:46:39 +01:00
|
|
|
localizedTimeShort(context), localizedTimeOfDay(context));
|
2020-01-09 12:16:34 +01:00
|
|
|
}
|
|
|
|
|
2020-05-13 15:58:59 +02:00
|
|
|
static String _z(int i) => i < 10 ? '0${i.toString()}' : i.toString();
|
2020-01-09 12:16:34 +01:00
|
|
|
}
|