From 72d3a394ad9f185e7424a90f2dce69d015a93fd7 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Tue, 11 Oct 2022 18:27:28 +0200 Subject: [PATCH] neon: Implement self updating relative times --- .../src/apps/files/widgets/browser_view.dart | 4 +++- .../src/apps/news/widgets/articles_view.dart | 6 ++--- .../src/apps/notes/widgets/notes_view.dart | 6 ++--- .../src/apps/notifications/pages/main.dart | 4 +++- packages/neon/lib/src/neon.dart | 2 +- .../relative_time.dart} | 22 +++++++++++++++++-- 6 files changed, 31 insertions(+), 13 deletions(-) rename packages/neon/lib/src/{utils/custom_timeago.dart => widgets/relative_time.dart} (59%) diff --git a/packages/neon/lib/src/apps/files/widgets/browser_view.dart b/packages/neon/lib/src/apps/files/widgets/browser_view.dart index 3e13af79..78001e53 100644 --- a/packages/neon/lib/src/apps/files/widgets/browser_view.dart +++ b/packages/neon/lib/src/apps/files/widgets/browser_view.dart @@ -244,7 +244,9 @@ class _FilesBrowserViewState extends State { ), subtitle: Row( children: [ - Text(CustomTimeAgo.format(details.lastModified)), + RelativeTime( + date: details.lastModified, + ), if (details.size > 0) ...[ const SizedBox( width: 10, diff --git a/packages/neon/lib/src/apps/news/widgets/articles_view.dart b/packages/neon/lib/src/apps/news/widgets/articles_view.dart index c138206c..27466c16 100644 --- a/packages/neon/lib/src/apps/news/widgets/articles_view.dart +++ b/packages/neon/lib/src/apps/news/widgets/articles_view.dart @@ -169,10 +169,8 @@ class _NewsArticlesViewState extends State { borderRadius: const BorderRadius.all(Radius.circular(2)), ), ), - Text( - CustomTimeAgo.format( - DateTime.fromMillisecondsSinceEpoch(article.pubDate! * 1000), - ), + RelativeTime( + date: DateTime.fromMillisecondsSinceEpoch(article.pubDate! * 1000), style: const TextStyle( fontWeight: FontWeight.w300, fontSize: 12, diff --git a/packages/neon/lib/src/apps/notes/widgets/notes_view.dart b/packages/neon/lib/src/apps/notes/widgets/notes_view.dart index 5c2b3d4f..3a8d755e 100644 --- a/packages/neon/lib/src/apps/notes/widgets/notes_view.dart +++ b/packages/neon/lib/src/apps/notes/widgets/notes_view.dart @@ -89,10 +89,8 @@ class NotesView extends StatelessWidget { title: Text(note.title!), subtitle: Row( children: [ - Text( - CustomTimeAgo.format( - DateTime.fromMillisecondsSinceEpoch(note.modified! * 1000), - ), + RelativeTime( + date: DateTime.fromMillisecondsSinceEpoch(note.modified! * 1000), ), if (note.category! != '') ...[ const SizedBox( diff --git a/packages/neon/lib/src/apps/notifications/pages/main.dart b/packages/neon/lib/src/apps/notifications/pages/main.dart index e7785286..84051302 100644 --- a/packages/neon/lib/src/apps/notifications/pages/main.dart +++ b/packages/neon/lib/src/apps/notifications/pages/main.dart @@ -81,7 +81,9 @@ class _NotificationsMainPageState extends State { height: 5, ), ], - Text(CustomTimeAgo.format(DateTime.parse(notification.datetime!))), + RelativeTime( + date: DateTime.parse(notification.datetime!), + ), ], ), leading: matchingAppImplementations.isNotEmpty diff --git a/packages/neon/lib/src/neon.dart b/packages/neon/lib/src/neon.dart index c2a0bb66..fd61ee30 100644 --- a/packages/neon/lib/src/neon.dart +++ b/packages/neon/lib/src/neon.dart @@ -69,7 +69,6 @@ part 'platform/platform.dart'; part 'utils/account_options.dart'; part 'utils/app_implementation.dart'; part 'utils/confirmation_dialog.dart'; -part 'utils/custom_timeago.dart'; part 'utils/env.dart'; part 'utils/global.dart'; part 'utils/global_options.dart'; @@ -99,5 +98,6 @@ part 'widgets/image_wrapper.dart'; part 'widgets/neon_logo.dart'; part 'widgets/nextcloud_logo.dart'; part 'widgets/no_animation_page_route.dart'; +part 'widgets/relative_time.dart'; part 'widgets/result_stream_builder.dart'; part 'widgets/standard_rx_result_builder.dart'; diff --git a/packages/neon/lib/src/utils/custom_timeago.dart b/packages/neon/lib/src/widgets/relative_time.dart similarity index 59% rename from packages/neon/lib/src/utils/custom_timeago.dart rename to packages/neon/lib/src/widgets/relative_time.dart index 983ffd69..f42042b8 100644 --- a/packages/neon/lib/src/utils/custom_timeago.dart +++ b/packages/neon/lib/src/widgets/relative_time.dart @@ -1,9 +1,18 @@ part of '../neon.dart'; -class CustomTimeAgo { +class RelativeTime extends StatelessWidget { + const RelativeTime({ + required this.date, + this.style, + super.key, + }); + + final DateTime date; + final TextStyle? style; + static String format(final DateTime date) { final now = DateTime.now(); - var diff = now.difference(date); + var diff = now.difference(date.toLocal()); final text = StringBuffer(); // Sometimes something can be messed up... @@ -28,4 +37,13 @@ class CustomTimeAgo { return text.toString(); } + + @override + Widget build(final BuildContext context) => StreamBuilder( + stream: Stream.periodic(const Duration(minutes: 1)), + builder: (final context, final _) => Text( + format(date), + style: style, + ), + ); }