Skip to content

Commit

Permalink
Merge pull request #2287 from nextcloud/feat/nextcloud/spreed-timesta…
Browse files Browse the repository at this point in the history
…mp-helpers
  • Loading branch information
provokateurin authored Jul 18, 2024
2 parents db6e5a9 + 4c6fe24 commit 3efd5dc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
8 changes: 2 additions & 6 deletions packages/neon/neon_talk/lib/src/pages/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ import 'package:neon_talk/src/blocs/room.dart';
import 'package:neon_talk/src/blocs/talk.dart';
import 'package:neon_talk/src/dialogs/create_room.dart';
import 'package:neon_talk/src/pages/room.dart';
import 'package:neon_talk/src/utils/helpers.dart';
import 'package:neon_talk/src/widgets/message.dart';
import 'package:neon_talk/src/widgets/read_indicator.dart';
import 'package:neon_talk/src/widgets/room_avatar.dart';
import 'package:neon_talk/src/widgets/unread_indicator.dart';
import 'package:nextcloud/spreed.dart' as spreed;
import 'package:nextcloud/utils.dart';
import 'package:timezone/timezone.dart' as tz;

/// The main page displaying the chat list.
class TalkMainPage extends StatefulWidget {
Expand Down Expand Up @@ -113,10 +112,7 @@ class _TalkMainPageState extends State<TalkMainPage> {
);
}

final timestamp = DateTimeUtils.fromSecondsSinceEpoch(
tz.local,
lastChatMessage.timestamp,
);
final timestamp = lastChatMessage.parsedTimestamp.toLocal();

final time = Tooltip(
message: DateFormat.yMd().add_jm().format(timestamp),
Expand Down
6 changes: 2 additions & 4 deletions packages/neon/neon_talk/lib/src/pages/room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import 'package:neon_framework/utils.dart';
import 'package:neon_framework/widgets.dart';
import 'package:neon_talk/src/blocs/room.dart';
import 'package:neon_talk/src/theme.dart';
import 'package:neon_talk/src/utils/helpers.dart';
import 'package:neon_talk/src/widgets/message.dart';
import 'package:neon_talk/src/widgets/message_input.dart';
import 'package:neon_talk/src/widgets/room_avatar.dart';
import 'package:nextcloud/utils.dart';
import 'package:timezone/timezone.dart' as tz;

const _millisecondsPerDay = 24 * 60 * 60 * 1000;
Expand Down Expand Up @@ -121,8 +121,6 @@ class _TalkRoomPageState extends State<TalkRoomPage> {
if (previousMessage == null ||
(tz.local.translate(previousMessage.timestamp * 1000) ~/ _millisecondsPerDay) !=
(tz.local.translate(message.timestamp * 1000) ~/ _millisecondsPerDay)) {
final date = DateTimeUtils.fromSecondsSinceEpoch(tz.local, message.timestamp);

child = Column(
children: [
Padding(
Expand All @@ -135,7 +133,7 @@ class _TalkRoomPageState extends State<TalkRoomPage> {
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(50)),
),
label: Text(DateFormat.yMd().format(date)),
label: Text(DateFormat.yMd().format(message.parsedTimestamp.toLocal())),
),
),
],
Expand Down
30 changes: 30 additions & 0 deletions packages/neon/neon_talk/lib/src/utils/helpers.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:nextcloud/spreed.dart' as spreed;
import 'package:nextcloud/utils.dart';
import 'package:timezone/timezone.dart' as tz;

/// `systemMessage` of chat messages that are hidden and only need to be processed internally.
/// They modify the state of other messages, but should not be displayed to the user.
Expand All @@ -17,3 +19,31 @@ extension ChatMessageHidden on spreed.$ChatMessageInterface {
/// Chat message is hidden for the user and only needs to be processed internally.
bool get isHidden => _hiddenMessages.contains(systemMessage);
}

/// Helper extension for [spreed.$BaseMessageInterface]
extension $BaseMessageInterfaceHelpers on spreed.$BaseMessageInterface {
/// Parsed equivalent of [expirationTimestamp].
tz.TZDateTime get parsedExpirationTimestamp => DateTimeUtils.fromSecondsSinceEpoch(tz.UTC, expirationTimestamp);
}

/// Helper extension for [spreed.$ChatMessageInterface]
extension $ChatMessageInterfaceHelpers on spreed.$ChatMessageInterface {
/// Parsed equivalent of [timestamp].
tz.TZDateTime get parsedTimestamp => DateTimeUtils.fromSecondsSinceEpoch(tz.UTC, timestamp);

/// Parsed equivalent of [lastEditTimestamp].
tz.TZDateTime? get parsedLastEditTimestamp =>
lastEditTimestamp != null ? DateTimeUtils.fromSecondsSinceEpoch(tz.UTC, lastEditTimestamp!) : null;
}

/// Helper extension for [spreed.$ChatReminderInterface]
extension $ChatReminderInterfaceHelpers on spreed.$ChatReminderInterface {
/// Parsed equivalent of [timestamp].
tz.TZDateTime get parsedTimestamp => DateTimeUtils.fromSecondsSinceEpoch(tz.UTC, timestamp);
}

/// Helper extension for [spreed.$ReactionInterface]
extension $ReactionInterfaceHelpers on spreed.$ReactionInterface {
/// Parsed equivalent of [timestamp].
tz.TZDateTime get parsedTimestamp => DateTimeUtils.fromSecondsSinceEpoch(tz.UTC, timestamp);
}
12 changes: 3 additions & 9 deletions packages/neon/neon_talk/lib/src/widgets/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:neon_framework/widgets.dart';
import 'package:neon_talk/l10n/localizations.dart';
import 'package:neon_talk/src/blocs/message_bloc.dart';
import 'package:neon_talk/src/blocs/room.dart';
import 'package:neon_talk/src/utils/helpers.dart';
import 'package:neon_talk/src/widgets/actor_avatar.dart';
import 'package:neon_talk/src/widgets/reactions.dart';
import 'package:neon_talk/src/widgets/read_indicator.dart';
Expand All @@ -20,7 +21,6 @@ import 'package:neon_talk/src/widgets/rich_object/fallback.dart';
import 'package:neon_talk/src/widgets/rich_object/file.dart';
import 'package:neon_talk/src/widgets/rich_object/mention.dart';
import 'package:nextcloud/spreed.dart' as spreed;
import 'package:nextcloud/utils.dart';
import 'package:timezone/timezone.dart' as tz;

final _timeFormat = DateFormat.jm();
Expand Down Expand Up @@ -450,16 +450,10 @@ class _TalkCommentMessageState extends State<TalkCommentMessage> {
final textTheme = Theme.of(context).textTheme;
final labelColor = Theme.of(context).colorScheme.inverseSurface.withOpacity(0.7);

final date = DateTimeUtils.fromSecondsSinceEpoch(
tz.UTC,
widget.chatMessage.timestamp,
);
final date = widget.chatMessage.parsedTimestamp.toLocal();
tz.TZDateTime? previousDate;
if (widget.previousChatMessage != null) {
previousDate = DateTimeUtils.fromSecondsSinceEpoch(
tz.UTC,
widget.previousChatMessage!.timestamp,
);
previousDate = widget.previousChatMessage!.parsedTimestamp.toLocal();
}

final separateMessages = widget.chatMessage.actorId != widget.previousChatMessage?.actorId ||
Expand Down

0 comments on commit 3efd5dc

Please sign in to comment.