Skip to content

Commit 0036539

Browse files
committed
msglist [nfc]: Factor out MessageTimestampDisplay
1 parent 40b7b5e commit 0036539

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

lib/widgets/message_list.dart

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,14 +1885,11 @@ String formatHeaderDate(
18851885
}
18861886
}
18871887

1888-
// TODO(i18n): web seems to ignore locale in formatting time, but we could do better
1889-
final _kMessageTimestampFormat = DateFormat('h:mm aa', 'en_US');
1890-
18911888
class SenderRow extends StatelessWidget {
1892-
const SenderRow({super.key, required this.message, required this.showTimestamp});
1889+
const SenderRow({super.key, required this.message, required this.timestampDisplay});
18931890

18941891
final MessageBase message;
1895-
final bool showTimestamp;
1892+
final MessageTimestampDisplay? timestampDisplay;
18961893

18971894
bool _showAsMuted(BuildContext context, PerAccountStore store) {
18981895
final message = this.message;
@@ -1913,8 +1910,7 @@ class SenderRow extends StatelessWidget {
19131910
final designVariables = DesignVariables.of(context);
19141911

19151912
final sender = store.getUser(message.senderId);
1916-
final time = _kMessageTimestampFormat
1917-
.format(DateTime.fromMillisecondsSinceEpoch(1000 * message.timestamp));
1913+
final timestamp = timestampDisplay?.format(message.timestamp);
19181914

19191915
final showAsMuted = _showAsMuted(context, store);
19201916

@@ -1960,9 +1956,9 @@ class SenderRow extends StatelessWidget {
19601956
),
19611957
],
19621958
]))),
1963-
if (showTimestamp) ...[
1959+
if (timestamp != null) ...[
19641960
const SizedBox(width: 4),
1965-
Text(time,
1961+
Text(timestamp,
19661962
style: TextStyle(
19671963
color: messageListTheme.labelTime,
19681964
fontSize: 16,
@@ -1974,6 +1970,26 @@ class SenderRow extends StatelessWidget {
19741970
}
19751971
}
19761972

1973+
// TODO centralize on this for wherever we show message timestamps
1974+
enum MessageTimestampDisplay {
1975+
timeOnly,
1976+
// TODO more
1977+
;
1978+
1979+
static final _timeOnlyFormat = DateFormat('h:mm aa', 'en_US');
1980+
1981+
/// Format a [Message.timestamp] for this mode.
1982+
// TODO(i18n): locale-specific formatting (see #45 for a plan with ffi)
1983+
String? format(int messageTimestamp) {
1984+
final asDateTime =
1985+
DateTime.fromMillisecondsSinceEpoch(1000 * messageTimestamp);
1986+
1987+
switch (this) {
1988+
case timeOnly: return _timeOnlyFormat.format(asDateTime);
1989+
}
1990+
}
1991+
}
1992+
19771993
/// A Zulip message, showing the sender's name and avatar if specified.
19781994
// Design referenced from:
19791995
// - https://github.com/zulip/zulip-mobile/issues/5511
@@ -2064,7 +2080,8 @@ class MessageWithPossibleSender extends StatelessWidget {
20642080
padding: const EdgeInsets.only(top: 4),
20652081
child: Column(children: [
20662082
if (item.showSender)
2067-
SenderRow(message: message, showTimestamp: true),
2083+
SenderRow(message: message,
2084+
timestampDisplay: MessageTimestampDisplay.timeOnly),
20682085
Row(
20692086
crossAxisAlignment: CrossAxisAlignment.baseline,
20702087
textBaseline: localizedTextBaseline(context),
@@ -2227,7 +2244,7 @@ class OutboxMessageWithPossibleSender extends StatelessWidget {
22272244
padding: const EdgeInsets.only(top: 4),
22282245
child: Column(children: [
22292246
if (item.showSender)
2230-
SenderRow(message: message, showTimestamp: false),
2247+
SenderRow(message: message, timestampDisplay: null),
22312248
Padding(
22322249
padding: const EdgeInsets.symmetric(horizontal: 16),
22332250
child: Column(crossAxisAlignment: CrossAxisAlignment.stretch,

0 commit comments

Comments
 (0)