Skip to content

Commit

Permalink
display date in post
Browse files Browse the repository at this point in the history
  • Loading branch information
leo-lox committed Nov 12, 2024
1 parent 63512f0 commit 9e66d1a
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lib/presentation_layer/components/note_card/name_row.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:camelus/presentation_layer/providers/nip05_provider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:intl/intl.dart';
import 'package:timeago/timeago.dart' as timeago;

class NoteCardNameRow extends ConsumerStatefulWidget {
Expand All @@ -28,11 +29,25 @@ class NoteCardNameRow extends ConsumerStatefulWidget {
class _NoteCardNameRowState extends ConsumerState<NoteCardNameRow> {
String nip05verified = "";
late String npubHrShort;
late String dateText;

@override
void initState() {
super.initState();
_initSequence();

final now = DateTime.now();
final postDateTime =
DateTime.fromMillisecondsSinceEpoch(widget.createdAt * 1000);
final difference = now.difference(postDateTime);

if (difference.inDays < 2) {
// Use timeago for posts less than 2 days old
dateText = timeago.format(postDateTime);
} else {
// Use a human-readable date format for posts 2 days or older
dateText = DateFormat('MMM d, yyyy').format(postDateTime);
}
}

void _initSequence() {
Expand Down Expand Up @@ -79,9 +94,7 @@ class _NoteCardNameRowState extends ConsumerState<NoteCardNameRow> {
const TextSpan(text: ' '),
const TextSpan(text: ' · '),
TextSpan(
text: timeago.format(
DateTime.fromMillisecondsSinceEpoch(
widget.createdAt * 1000)),
text: dateText,
style:
const TextStyle(color: Palette.gray, fontSize: 14),
),
Expand Down

0 comments on commit 9e66d1a

Please sign in to comment.