Skip to content

Commit 2e0cceb

Browse files
committed
msglist [nfc]: Factor out MessageTimestampDisplay
1 parent 0be1938 commit 2e0cceb

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

lib/widgets/message_list.dart

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,14 +1880,11 @@ String formatHeaderDate(
18801880
}
18811881
}
18821882

1883-
// TODO(i18n): web seems to ignore locale in formatting time, but we could do better
1884-
final _kMessageTimestampFormat = DateFormat('h:mm aa', 'en_US');
1885-
18861883
class SenderRow extends StatelessWidget {
1887-
const SenderRow({super.key, required this.message, required this.showTimestamp});
1884+
const SenderRow({super.key, required this.message, required this.timestampStyle});
18881885

18891886
final MessageBase message;
1890-
final bool showTimestamp;
1887+
final MessageTimestampStyle timestampStyle;
18911888

18921889
bool _showAsMuted(BuildContext context, PerAccountStore store) {
18931890
final message = this.message;
@@ -1908,8 +1905,7 @@ class SenderRow extends StatelessWidget {
19081905
final designVariables = DesignVariables.of(context);
19091906

19101907
final sender = store.getUser(message.senderId);
1911-
final time = _kMessageTimestampFormat
1912-
.format(DateTime.fromMillisecondsSinceEpoch(1000 * message.timestamp));
1908+
final timestamp = timestampStyle.format(message.timestamp);
19131909

19141910
final showAsMuted = _showAsMuted(context, store);
19151911

@@ -1955,9 +1951,9 @@ class SenderRow extends StatelessWidget {
19551951
),
19561952
],
19571953
]))),
1958-
if (showTimestamp) ...[
1954+
if (timestamp != null) ...[
19591955
const SizedBox(width: 4),
1960-
Text(time,
1956+
Text(timestamp,
19611957
style: TextStyle(
19621958
color: messageListTheme.labelTime,
19631959
fontSize: 16,
@@ -1969,6 +1965,27 @@ class SenderRow extends StatelessWidget {
19691965
}
19701966
}
19711967

1968+
// TODO centralize on this for wherever we show message timestamps
1969+
enum MessageTimestampStyle {
1970+
none,
1971+
timeOnly,
1972+
;
1973+
1974+
static final _timeOnlyFormat = DateFormat('h:mm aa', 'en_US');
1975+
1976+
/// Format a [Message.timestamp] for this mode.
1977+
// TODO(i18n): locale-specific formatting (see #45 for a plan with ffi)
1978+
String? format(int messageTimestamp) {
1979+
final asDateTime =
1980+
DateTime.fromMillisecondsSinceEpoch(1000 * messageTimestamp);
1981+
1982+
switch (this) {
1983+
case none: return null;
1984+
case timeOnly: return _timeOnlyFormat.format(asDateTime);
1985+
}
1986+
}
1987+
}
1988+
19721989
/// A Zulip message, showing the sender's name and avatar if specified.
19731990
// Design referenced from:
19741991
// - https://github.com/zulip/zulip-mobile/issues/5511
@@ -2059,7 +2076,8 @@ class MessageWithPossibleSender extends StatelessWidget {
20592076
padding: const EdgeInsets.only(top: 4),
20602077
child: Column(children: [
20612078
if (item.showSender)
2062-
SenderRow(message: message, showTimestamp: true),
2079+
SenderRow(message: message,
2080+
timestampStyle: MessageTimestampStyle.timeOnly),
20632081
Row(
20642082
crossAxisAlignment: CrossAxisAlignment.baseline,
20652083
textBaseline: localizedTextBaseline(context),
@@ -2222,7 +2240,7 @@ class OutboxMessageWithPossibleSender extends StatelessWidget {
22222240
padding: const EdgeInsets.only(top: 4),
22232241
child: Column(children: [
22242242
if (item.showSender)
2225-
SenderRow(message: message, showTimestamp: false),
2243+
SenderRow(message: message, timestampStyle: MessageTimestampStyle.none),
22262244
Padding(
22272245
padding: const EdgeInsets.symmetric(horizontal: 16),
22282246
child: Column(crossAxisAlignment: CrossAxisAlignment.stretch,

0 commit comments

Comments
 (0)