From 941980171b8dbf021a1a3c111673c5c5e1d2145b Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Thu, 9 Jan 2020 12:16:34 +0100 Subject: [PATCH] Add DateTime extension --- lib/components/list_items/chat_list_item.dart | 4 +- lib/components/list_items/message.dart | 4 +- lib/utils/chat_time.dart | 132 ------------------ lib/utils/date_time_extension.dart | 101 ++++++++++++++ pubspec.yaml | 2 +- 5 files changed, 106 insertions(+), 137 deletions(-) delete mode 100644 lib/utils/chat_time.dart create mode 100644 lib/utils/date_time_extension.dart diff --git a/lib/components/list_items/chat_list_item.dart b/lib/components/list_items/chat_list_item.dart index 38c4cef..1965d99 100644 --- a/lib/components/list_items/chat_list_item.dart +++ b/lib/components/list_items/chat_list_item.dart @@ -1,6 +1,6 @@ import 'package:famedlysdk/famedlysdk.dart'; import 'package:fluffychat/components/message_content.dart'; -import 'package:fluffychat/utils/chat_time.dart'; +import 'package:fluffychat/utils/date_time_extension.dart'; import 'package:fluffychat/utils/app_route.dart'; import 'package:fluffychat/utils/room_name_calculator.dart'; import 'package:fluffychat/views/chat.dart'; @@ -93,7 +93,7 @@ class ChatListItem extends StatelessWidget { ), SizedBox(width: 16), Text( - ChatTime(room.timeCreated).toEventTimeString(), + room.timeCreated.localizedTimeShort(context), style: TextStyle( color: Color(0xFF555555), fontSize: 13, diff --git a/lib/components/list_items/message.dart b/lib/components/list_items/message.dart index 1672707..f6b6c7a 100644 --- a/lib/components/list_items/message.dart +++ b/lib/components/list_items/message.dart @@ -2,7 +2,7 @@ import 'package:bubble/bubble.dart'; import 'package:famedlysdk/famedlysdk.dart'; import 'package:fluffychat/components/dialogs/redact_message_dialog.dart'; import 'package:fluffychat/components/message_content.dart'; -import 'package:fluffychat/utils/chat_time.dart'; +import 'package:fluffychat/utils/date_time_extension.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -126,7 +126,7 @@ class Message extends StatelessWidget { ), SizedBox(width: 4), Text( - ChatTime(event.time).toEventTimeString(), + event.time.localizedTime(context), style: TextStyle( color: textColor.withAlpha(200), ), diff --git a/lib/utils/chat_time.dart b/lib/utils/chat_time.dart deleted file mode 100644 index 2364030..0000000 --- a/lib/utils/chat_time.dart +++ /dev/null @@ -1,132 +0,0 @@ -/* - * Copyright (c) 2019 Zender & Kurtz GbR. - * - * Authors: - * Christian Pauly - * Marcel Radzio - * - * This file is part of famedlysdk. - * - * famedlysdk is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * famedlysdk is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with famedlysdk. If not, see . - */ - -/// Used to localize and present time in a chat application manner. -class ChatTime { - DateTime dateTime = DateTime.now(); - - /// Insert with a timestamp [ts] which represents the milliseconds since - /// the Unix epoch. - ChatTime(DateTime ts) { - if (ts != null) dateTime = ts; - } - - /// Returns a ChatTime object which represents the current time. - ChatTime.now() { - dateTime = DateTime.now(); - } - - /// Returns [toTimeString()] if the ChatTime is today, the name of the week - /// day if the ChatTime is this week and a date string else. - String toString() { - DateTime now = DateTime.now(); - - bool sameYear = now.year == dateTime.year; - - bool sameDay = - sameYear && now.month == dateTime.month && now.day == dateTime.day; - - bool sameWeek = sameYear && - !sameDay && - now.millisecondsSinceEpoch - dateTime.millisecondsSinceEpoch < - 1000 * 60 * 60 * 24 * 7; - - if (sameDay) { - return toTimeString(); - } else if (sameWeek) { - switch (dateTime.weekday) { - case 1: - return "Monday"; - case 2: - return "Tuesday"; - case 3: - return "Wednesday"; - case 4: - return "Thursday"; - case 5: - return "Friday"; - case 6: - return "Saturday"; - case 7: - return "Sunday"; - } - } else if (sameYear) { - return "${dateTime.month.toString().padLeft(2, '0')}-${dateTime.day.toString().padLeft(2, '0')}"; - } - return "${dateTime.year.toString()}-${dateTime.month.toString().padLeft(2, '0')}-${dateTime.day.toString().padLeft(2, '0')}"; - } - - /// Returns the milliseconds since the Unix epoch. - num toTimeStamp() { - return dateTime.millisecondsSinceEpoch; - } - - operator <(ChatTime other) { - return this.toTimeStamp() < other.toTimeStamp(); - } - - operator >(ChatTime other) { - return this.toTimeStamp() > other.toTimeStamp(); - } - - operator >=(ChatTime other) { - return this.toTimeStamp() >= other.toTimeStamp(); - } - - operator <=(ChatTime other) { - return this.toTimeStamp() <= other.toTimeStamp(); - } - - /// 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 ChatTimes are close enough to belong to the same - /// environment. - bool sameEnvironment(ChatTime prevTime) { - return toTimeStamp() - prevTime.toTimeStamp() < - 1000 * 60 * minutesBetweenEnvironments; - } - - /// Returns a simple time String. - String toTimeString() { - return "${_z(dateTime.hour % 12)}:${_z(dateTime.minute)} ${dateTime.hour > 11 ? 'pm' : 'am'}"; - } - - /// If the ChatTime is today, this returns [toTimeString()], if not it also - /// shows the date. - String toEventTimeString() { - DateTime now = DateTime.now(); - - bool sameYear = now.year == dateTime.year; - - bool sameDay = - sameYear && now.month == dateTime.month && now.day == dateTime.day; - - if (sameDay) return toTimeString(); - return "${toString()}, ${toTimeString()}"; - } - - static String _z(int i) => i < 10 ? "0${i.toString()}" : i.toString(); -} diff --git a/lib/utils/date_time_extension.dart b/lib/utils/date_time_extension.dart new file mode 100644 index 0000000..978b5eb --- /dev/null +++ b/lib/utils/date_time_extension.dart @@ -0,0 +1,101 @@ +import 'package:flutter/material.dart'; + +/// Provides extra functionality for formatting the time. +extension DateTimeExtension on DateTime { + @Deprecated('Use [millisecondsSinceEpoch] instead.') + num toTimeStamp() => this.millisecondsSinceEpoch; + + @deprecated + String toTimeString() => localizedTimeOfDay(null); + @deprecated + String toEventTimeString() => localizedTime(null); + + operator <(DateTime other) { + return this.millisecondsSinceEpoch < other.millisecondsSinceEpoch; + } + + operator >(DateTime other) { + return this.millisecondsSinceEpoch > other.millisecondsSinceEpoch; + } + + operator >=(DateTime other) { + return this.millisecondsSinceEpoch >= other.millisecondsSinceEpoch; + } + + operator <=(DateTime other) { + return this.millisecondsSinceEpoch <= other.millisecondsSinceEpoch; + } + + /// 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) { + return "${_z(this.hour % 12)}:${_z(this.minute)} ${this.hour > 11 ? 'pm' : 'am'}"; + } + + /// 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) { + DateTime now = DateTime.now(); + + bool sameYear = now.year == this.year; + + bool sameDay = sameYear && now.month == this.month && now.day == this.day; + + bool sameWeek = sameYear && + !sameDay && + now.millisecondsSinceEpoch - this.millisecondsSinceEpoch < + 1000 * 60 * 60 * 24 * 7; + + if (sameDay) { + return localizedTimeOfDay(context); + } else if (sameWeek) { + switch (this.weekday) { + case 1: + return "Monday"; + case 2: + return "Tuesday"; + case 3: + return "Wednesday"; + case 4: + return "Thursday"; + case 5: + return "Friday"; + case 6: + return "Saturday"; + case 7: + return "Sunday"; + } + } else if (sameYear) { + return "${this.month.toString().padLeft(2, '0')}-${this.day.toString().padLeft(2, '0')}"; + } + return "${this.year.toString()}-${this.month.toString().padLeft(2, '0')}-${this.day.toString().padLeft(2, '0')}"; + } + + /// If the DateTime is today, this returns [localizedTimeOfDay()], if not it also + /// shows the date. + /// TODO: Add localization + String localizedTime(BuildContext context) { + DateTime now = DateTime.now(); + + bool sameYear = now.year == this.year; + + bool sameDay = sameYear && now.month == this.month && now.day == this.day; + + if (sameDay) return localizedTimeOfDay(context); + return "${localizedTimeShort(context)}, ${localizedTimeOfDay(context)}"; + } + + static String _z(int i) => i < 10 ? "0${i.toString()}" : i.toString(); +} diff --git a/pubspec.yaml b/pubspec.yaml index fdd206f..0f8575e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -14,7 +14,7 @@ description: Chat with your friends. version: 0.2.3+13 environment: - sdk: ">=2.1.0 <3.0.0" + sdk: ">=2.6.0 <3.0.0" dependencies: flutter: