Browse Source

style(neon): make RelativeTime stateful

pull/482/head
Nikolas Rimikis 2 years ago
parent
commit
a7558139ef
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 35
      packages/neon/neon/lib/src/utils/relative_time.dart
  2. 62
      packages/neon/neon/lib/src/widgets/relative_time.dart

35
packages/neon/neon/lib/src/utils/relative_time.dart

@ -0,0 +1,35 @@
import 'package:meta/meta.dart';
@internal
extension RelativeTimeFormat on DateTime {
/// Format the relative time between this and [to].
///
/// If unspecified [DateTime.now] will be used.
String formatRelative([final DateTime? to]) {
final now = to ?? DateTime.now();
var diff = now.difference(toLocal());
final text = StringBuffer();
// Sometimes something can be messed up...
if (diff.isNegative) {
if (diff.inMinutes >= 1) {
text.write('-');
}
diff = Duration(microseconds: -diff.inMicroseconds);
}
if (diff.inMinutes < 1) {
text.write('now');
} else if (diff.inHours < 1) {
text.write('${diff.inMinutes}m');
} else if (diff.inDays < 1) {
text.write('${diff.inHours}h');
} else if (diff.inDays < 365) {
text.write('${diff.inDays}d');
} else {
text.write('${diff.inDays ~/ 365}y');
}
return text.toString();
}
}

62
packages/neon/neon/lib/src/widgets/relative_time.dart

@ -1,6 +1,9 @@
import 'dart:async';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:neon/src/utils/relative_time.dart';
class RelativeTime extends StatelessWidget { class RelativeTime extends StatefulWidget {
const RelativeTime({ const RelativeTime({
required this.date, required this.date,
this.style, this.style,
@ -10,40 +13,33 @@ class RelativeTime extends StatelessWidget {
final DateTime date; final DateTime date;
final TextStyle? style; final TextStyle? style;
static String format(final DateTime date) { @override
final now = DateTime.now(); State<RelativeTime> createState() => _RelativeTimeState();
var diff = now.difference(date.toLocal()); }
final text = StringBuffer();
class _RelativeTimeState extends State<RelativeTime> {
// Sometimes something can be messed up... late final Timer timer;
if (diff.isNegative) {
if (diff.inMinutes >= 1) { @override
text.write('-'); void initState() {
} timer = Timer.periodic(
diff = Duration(microseconds: -diff.inMicroseconds); const Duration(minutes: 1),
} (final _) => setState(() {}),
);
if (diff.inMinutes < 1) {
text.write('now'); super.initState();
} else if (diff.inHours < 1) { }
text.write('${diff.inMinutes}m');
} else if (diff.inDays < 1) { @override
text.write('${diff.inHours}h'); void dispose() {
} else if (diff.inDays < 365) { timer.cancel();
text.write('${diff.inDays}d');
} else { super.dispose();
text.write('${diff.inDays ~/ 365}y');
}
return text.toString();
} }
@override @override
Widget build(final BuildContext context) => StreamBuilder( Widget build(final BuildContext context) => Text(
stream: Stream.periodic(const Duration(minutes: 1)), widget.date.formatRelative(),
builder: (final context, final _) => Text( style: widget.style,
format(date),
style: style,
),
); );
} }

Loading…
Cancel
Save