Skip to content

Commit 50a0e8b

Browse files
committed
refactor: 🔨 dart format and timgeago package update
1 parent 34ddf97 commit 50a0e8b

File tree

9 files changed

+244
-7
lines changed

9 files changed

+244
-7
lines changed

lib/src/extensions/extensions.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,5 +173,6 @@ extension BuildContextExtension on BuildContext {
173173
child: SizedBox.shrink(),
174174
);
175175

176-
ChatBubbleConfiguration? get chatBubbleConfig => chatListConfig.chatBubbleConfig;
176+
ChatBubbleConfiguration? get chatBubbleConfig =>
177+
chatListConfig.chatBubbleConfig;
177178
}

lib/src/models/config_models/chat_bubble_configuration.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,6 @@ class ChatBubbleConfiguration {
6767
this.outgoingChatBubbleConfig,
6868
this.onDoubleTap,
6969
this.receiptsWidgetConfig,
70-
this.disableLinkPreview = false
70+
this.disableLinkPreview = false,
7171
});
7272
}

lib/src/utils/constants/constants.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
*/
2222
import 'package:flutter/material.dart';
2323
import 'package:intl/intl.dart';
24-
import 'package:timeago/timeago.dart' as timeago;
2524

2625
import '../../../chatview.dart';
2726
import '../../widgets/chat_message_sending_to_sent_animation.dart';
27+
import '../timeago/timeago.dart' as timeago;
2828

2929
const String emojiRegExpression =
3030
r'(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])';
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// This code is sourced from the timeago package available on pub.dev. link : https://pub.dev/packages/timeago
2+
3+
import 'package:chatview/src/utils/timeago/lookupmessages.dart';
4+
5+
/// English Messages
6+
class EnMessages implements LookupMessages {
7+
@override
8+
String prefixAgo() => '';
9+
@override
10+
String prefixFromNow() => '';
11+
@override
12+
String suffixAgo() => 'ago';
13+
@override
14+
String suffixFromNow() => 'from now';
15+
@override
16+
String lessThanOneMinute(int seconds) => 'a moment';
17+
@override
18+
String aboutAMinute(int minutes) => 'a minute';
19+
@override
20+
String minutes(int minutes) => '$minutes minutes';
21+
@override
22+
String aboutAnHour(int minutes) => 'about an hour';
23+
@override
24+
String hours(int hours) => '$hours hours';
25+
@override
26+
String aDay(int hours) => 'a day';
27+
@override
28+
String days(int days) => '$days days';
29+
@override
30+
String aboutAMonth(int days) => 'about a month';
31+
@override
32+
String months(int months) => '$months months';
33+
@override
34+
String aboutAYear(int year) => 'about a year';
35+
@override
36+
String years(int years) => '$years years';
37+
@override
38+
String wordSeparator() => ' ';
39+
}
40+
41+
/// English short Messages
42+
class EnShortMessages implements LookupMessages {
43+
@override
44+
String prefixAgo() => '';
45+
@override
46+
String prefixFromNow() => '';
47+
@override
48+
String suffixAgo() => '';
49+
@override
50+
String suffixFromNow() => '';
51+
@override
52+
String lessThanOneMinute(int seconds) => 'now';
53+
@override
54+
String aboutAMinute(int minutes) => '1m';
55+
@override
56+
String minutes(int minutes) => '${minutes}m';
57+
@override
58+
String aboutAnHour(int minutes) => '~1h';
59+
@override
60+
String hours(int hours) => '${hours}h';
61+
@override
62+
String aDay(int hours) => '~1d';
63+
@override
64+
String days(int days) => '${days}d';
65+
@override
66+
String aboutAMonth(int days) => '~1mo';
67+
@override
68+
String months(int months) => '${months}mo';
69+
@override
70+
String aboutAYear(int year) => '~1y';
71+
@override
72+
String years(int years) => '${years}y';
73+
@override
74+
String wordSeparator() => ' ';
75+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// This code is sourced from the timeago package available on pub.dev. link: https://pub.dev/packages/timeago
2+
3+
/// [LookupMessages] template for any language
4+
///
5+
///
6+
abstract class LookupMessages {
7+
/// Example: `prefixAgo()` 1 min `suffixAgo()`
8+
String prefixAgo();
9+
10+
/// Example: `prefixFromNow()` 1 min `suffixFromNow()`
11+
String prefixFromNow();
12+
13+
/// Example: `prefixAgo()` 1 min `suffixAgo()`
14+
String suffixAgo();
15+
16+
/// Example: `prefixFromNow()` 1 min `suffixFromNow()`
17+
String suffixFromNow();
18+
19+
/// Format when time is less than a minute
20+
String lessThanOneMinute(int seconds);
21+
22+
/// Format when time is about a minute
23+
String aboutAMinute(int minutes);
24+
25+
/// Format when time is in minutes
26+
String minutes(int minutes);
27+
28+
/// Format when time is about an hour
29+
String aboutAnHour(int minutes);
30+
31+
/// Format when time is in hours
32+
String hours(int hours);
33+
34+
/// Format when time is a day
35+
String aDay(int hours);
36+
37+
/// Format when time is in days
38+
String days(int days);
39+
40+
/// Format when time is about a month
41+
String aboutAMonth(int days);
42+
43+
/// Format when time is in months
44+
String months(int months);
45+
46+
/// Format when time is about a year
47+
String aboutAYear(int year);
48+
49+
/// Format when time is about a year
50+
String years(int years);
51+
52+
/// word separator when words are concatenated
53+
String wordSeparator() => ' ';
54+
}

lib/src/utils/timeago/timeago.dart

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// This code is sourced from the timeago package available on pub.dev. link: https://pub.dev/packages/timeago
2+
3+
import 'package:chatview/src/utils/timeago/en_messages.dart';
4+
import 'package:chatview/src/utils/timeago/lookupmessages.dart';
5+
import 'package:flutter/foundation.dart';
6+
7+
String _default = 'en';
8+
9+
Map<String, LookupMessages> _lookupMessagesMap = {
10+
'en': EnMessages(),
11+
'en_short': EnShortMessages(),
12+
};
13+
14+
/// Sets the default [locale]. By default it is `en`.
15+
///
16+
/// Example
17+
/// ```
18+
/// setLocaleMessages('fr', FrMessages());
19+
/// setDefaultLocale('fr');
20+
/// ```
21+
void setDefaultLocale(String locale) {
22+
assert(_lookupMessagesMap.containsKey(locale),
23+
'[locale] must be a registered locale');
24+
_default = locale;
25+
}
26+
27+
/// Sets a [locale] with the provided [lookupMessages] to be available when
28+
/// using the [format] function.
29+
///
30+
/// Example:
31+
/// ```dart
32+
/// setLocaleMessages('fr', FrMessages());
33+
/// ```
34+
///
35+
/// If you want to define locale message implement [LookupMessages] interface
36+
/// with the desired messages
37+
///
38+
void setLocaleMessages(String locale, LookupMessages lookupMessages) {
39+
_lookupMessagesMap[locale] = lookupMessages;
40+
}
41+
42+
/// Formats provided [date] to a fuzzy time like 'a moment ago'
43+
///
44+
/// - If [locale] is passed will look for message for that locale, if you want
45+
/// to add or override locales use [setLocaleMessages]. Defaults to 'en'
46+
/// - If [clock] is passed this will be the point of reference for calculating
47+
/// the elapsed time. Defaults to DateTime.now()
48+
/// - If [allowFromNow] is passed, format will use the From prefix, ie. a date
49+
/// 5 minutes from now in 'en' locale will display as "5 minutes from now"
50+
String format(DateTime date,
51+
{String? locale, DateTime? clock, bool allowFromNow = false}) {
52+
final locale0 = locale ?? _default;
53+
if (_lookupMessagesMap[locale0] == null) {
54+
debugPrint(
55+
"Locale [$locale0] has not been added, using [$_default] as fallback. To add a locale use [setLocaleMessages]");
56+
}
57+
final allowFromNow0 = allowFromNow;
58+
final messages = _lookupMessagesMap[locale0] ?? EnMessages();
59+
final clock0 = clock ?? DateTime.now();
60+
var elapsed = clock0.millisecondsSinceEpoch - date.millisecondsSinceEpoch;
61+
62+
String prefix, suffix;
63+
64+
if (allowFromNow0 && elapsed < 0) {
65+
elapsed = date.isBefore(clock0) ? elapsed : elapsed.abs();
66+
prefix = messages.prefixFromNow();
67+
suffix = messages.suffixFromNow();
68+
} else {
69+
prefix = messages.prefixAgo();
70+
suffix = messages.suffixAgo();
71+
}
72+
73+
final num seconds = elapsed / 1000;
74+
final num minutes = seconds / 60;
75+
final num hours = minutes / 60;
76+
final num days = hours / 24;
77+
final num months = days / 30;
78+
final num years = days / 365;
79+
80+
String result;
81+
if (seconds < 45) {
82+
result = messages.lessThanOneMinute(seconds.round());
83+
} else if (seconds < 90) {
84+
result = messages.aboutAMinute(minutes.round());
85+
} else if (minutes < 45) {
86+
result = messages.minutes(minutes.round());
87+
} else if (minutes < 90) {
88+
result = messages.aboutAnHour(minutes.round());
89+
} else if (hours < 24) {
90+
result = messages.hours(hours.round());
91+
} else if (hours < 48) {
92+
result = messages.aDay(hours.round());
93+
} else if (days < 30) {
94+
result = messages.days(days.round());
95+
} else if (days < 60) {
96+
result = messages.aboutAMonth(days.round());
97+
} else if (days < 365) {
98+
result = messages.months(months.round());
99+
} else if (years < 2) {
100+
result = messages.aboutAYear(months.round());
101+
} else {
102+
result = messages.years(years.round());
103+
}
104+
105+
return [prefix, result, suffix]
106+
.where((str) => str.isNotEmpty)
107+
.join(messages.wordSeparator());
108+
}

lib/src/values/custom_time_messages.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import 'package:timeago/timeago.dart';
1+
import '../utils/timeago/lookupmessages.dart';
22

33
// Override "en" locale messages with custom messages that are more precise and short
44
// setLocaleMessages('en', ReceiptsCustomMessages())

lib/src/widgets/chat_view.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import 'package:chatview/src/widgets/reaction_popup.dart';
3131
import 'package:chatview/src/widgets/suggestions/suggestions_config_inherited_widget.dart';
3232
import 'package:flutter/foundation.dart';
3333
import 'package:flutter/material.dart';
34-
import 'package:timeago/timeago.dart';
34+
import '../utils/timeago/timeago.dart';
3535
import '../values/custom_time_messages.dart';
3636
import 'send_message_widget.dart';
3737

pubspec.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@ environment:
1818
dependencies:
1919
flutter:
2020
sdk: flutter
21-
intl: ^0.19.0
21+
intl: ^0.20.0
2222
url_launcher: ^6.3.0
2323
emoji_picker_flutter: ^3.0.0
2424
any_link_preview: ^3.0.2
2525
image_picker: '>=0.8.9 <2.0.0'
2626
audio_waveforms: 1.2.0
2727
# For formatting time locale in message receipts
28-
timeago: ^3.7.0
2928
cached_network_image: ^3.4.1
3029

3130
dev_dependencies:

0 commit comments

Comments
 (0)