Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding time stamps [Add timestamp to Chat #1275] #1279

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions app/lib/pages/chat/widgets/ai_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ Widget buildMessageWidget(ServerMessage message, Function(String) sendMessage, b
return InitialMessageWidget(
showTypingIndicator: showTypingIndicator, messageText: message.text.decodeString, sendMessage: sendMessage);
} else {
return NormalMessageWidget(showTypingIndicator: showTypingIndicator, messageText: message.text.decodeString);
return NormalMessageWidget(showTypingIndicator: showTypingIndicator, messageText: message.text.decodeString, createdAt: message.createdAt);
}
}

Expand Down Expand Up @@ -272,13 +272,30 @@ class DaySummaryWidget extends StatelessWidget {
class NormalMessageWidget extends StatelessWidget {
final bool showTypingIndicator;
final String messageText;
const NormalMessageWidget({super.key, required this.showTypingIndicator, required this.messageText});
final DateTime createdAt;

const NormalMessageWidget({
super.key,
required this.showTypingIndicator,
required this.messageText,
required this.createdAt,
});

@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(left: 8.0, bottom: 4.0),
child: Text(
formatChatTimestamp(createdAt),
style: TextStyle(
color: Colors.grey.shade500,
fontSize: 12,
),
),
),
SelectionArea(
child: showTypingIndicator
? const Row(
Expand All @@ -293,11 +310,14 @@ class NormalMessageWidget extends StatelessWidget {
)
: AutoSizeText(
messageText,
// : utf8.decode(widget.message.text.codeUnits),
style: TextStyle(fontSize: 15.0, fontWeight: FontWeight.w500, color: Colors.grey.shade300),
style: TextStyle(
fontSize: 15.0,
fontWeight: FontWeight.w500,
color: Colors.grey.shade300
),
),
),
CopyButton(messageText: messageText),
if (!showTypingIndicator) CopyButton(messageText: messageText),
],
);
}
Expand Down
36 changes: 26 additions & 10 deletions app/lib/pages/chat/widgets/user_message.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:friend_private/backend/schema/message.dart';
import 'package:friend_private/widgets/extensions/string.dart';
import 'package:friend_private/utils/other/temp.dart';

class HumanMessage extends StatelessWidget {
final ServerMessage message;
Expand All @@ -11,20 +12,35 @@ class HumanMessage extends StatelessWidget {
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.fromLTRB(20, 20, 0, 20),
child: Wrap(
alignment: WrapAlignment.end,
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Container(
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: const BorderRadius.all(Radius.circular(16.0)),
),
padding: const EdgeInsets.all(16.0),
Padding(
padding: const EdgeInsets.only(right: 8.0, bottom: 4.0),
child: Text(
message.text.decodeString,
style: Theme.of(context).textTheme.bodyMedium,
formatChatTimestamp(message.createdAt),
style: TextStyle(
color: Colors.grey.shade500,
fontSize: 12,
),
),
),
Wrap(
alignment: WrapAlignment.end,
children: [
Container(
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: const BorderRadius.all(Radius.circular(16.0)),
),
padding: const EdgeInsets.all(16.0),
child: Text(
message.text.decodeString,
style: Theme.of(context).textTheme.bodyMedium,
),
),
],
),
],
),
);
Expand Down
17 changes: 17 additions & 0 deletions app/lib/utils/other/temp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,20 @@ Future routeToPage(BuildContext context, Widget page, {bool replace = false}) {
}
return Navigator.of(context).push(route);
}

String formatChatTimestamp(DateTime dateTime) {
final now = DateTime.now();
final today = DateTime(now.year, now.month, now.day);
final messageDate = DateTime(dateTime.year, dateTime.month, dateTime.day);

if (messageDate == today) {
// Today, show time only
return dateTimeFormat('h:mm a', dateTime);
} else if (messageDate == today.subtract(const Duration(days: 1))) {
// Yesterday
return 'Yesterday ${dateTimeFormat('h:mm a', dateTime)}';
} else {
// Other days
return dateTimeFormat('MMM d, h:mm a', dateTime);
}
}
18 changes: 18 additions & 0 deletions app/lib/widgets/transcript.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
import 'package:friend_private/backend/preferences.dart';
import 'package:friend_private/backend/schema/person.dart';
import 'package:friend_private/backend/schema/transcript_segment.dart';
import 'package:friend_private/utils/other/temp.dart';

class TranscriptWidget extends StatefulWidget {
final List<TranscriptSegment> segments;
Expand Down Expand Up @@ -167,3 +168,20 @@ String tryDecodingText(String text) {
return text;
}
}

String formatChatTimestamp(DateTime dateTime) {
final now = DateTime.now();
final today = DateTime(now.year, now.month, now.day);
final messageDate = DateTime(dateTime.year, dateTime.month, dateTime.day);

if (messageDate == today) {
// Today, show time only
return dateTimeFormat('h:mm a', dateTime);
} else if (messageDate == today.subtract(const Duration(days: 1))) {
// Yesterday
return 'Yesterday ${dateTimeFormat('h:mm a', dateTime)}';
} else {
// Other days
return dateTimeFormat('MMM d, h:mm a', dateTime);
}
}