Add extension for relative time and fix a bug

the bug was that time from server is in UTC format,
and our time is in local format, so it needed
to be shifted back to UTC.

oddly enough .toUtc() method didn't work
This commit is contained in:
krawieck 2021-02-12 17:53:51 +01:00
parent efb2551f4f
commit 40636246df
4 changed files with 18 additions and 6 deletions

View File

@ -0,0 +1,12 @@
import 'package:timeago/timeago.dart' as timeago;
extension FancyTime on DateTime {
/// returns [this] time as a relative, human-readable string. In short format
String get fancyShort => timeago.format(this,
locale: 'en_short',
clock: DateTime.now().subtract(DateTime.now().timeZoneOffset));
/// returns [this] time as a relative, human-readable string
String get fancy => timeago.format(this,
clock: DateTime.now().subtract(DateTime.now().timeZoneOffset));
}

View File

@ -5,7 +5,6 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:lemmy_api_client/v2.dart';
import 'package:timeago/timeago.dart' as timeago;
import 'package:url_launcher/url_launcher.dart' as ul;
import '../comment_tree.dart';
@ -13,6 +12,7 @@ import '../hooks/delayed_loading.dart';
import '../hooks/logged_in_action.dart';
import '../hooks/stores.dart';
import '../util/extensions/api.dart';
import '../util/extensions/datetime.dart';
import '../util/goto.dart';
import '../util/intl.dart';
import '../util/text_color.dart';
@ -385,7 +385,7 @@ class CommentWidget extends HookWidget {
Text(compactNumber(comment.counts.score +
(wasVoted ? 0 : myVote.value.value))),
const Text(' · '),
Text(timeago.format(comment.comment.published)),
Text(comment.comment.published.fancy),
],
),
)

View File

@ -6,7 +6,6 @@ import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:intl/intl.dart';
import 'package:lemmy_api_client/v2.dart';
import 'package:timeago/timeago.dart' as timeago;
import 'package:url_launcher/url_launcher.dart' as ul;
import '../hooks/delayed_loading.dart';
@ -15,6 +14,7 @@ import '../pages/full_post.dart';
import '../url_launcher.dart';
import '../util/cleanup_url.dart';
import '../util/extensions/api.dart';
import '../util/extensions/datetime.dart';
import '../util/goto.dart';
import '../util/more_icon.dart';
import 'bottom_modal.dart';
@ -216,7 +216,7 @@ class PostWidget extends HookWidget {
),
TextSpan(
text:
' · ${timeago.format(post.post.published, locale: 'en_short')}'),
' · ${post.post.published.fancyShort}'),
if (post.post.locked)
const TextSpan(text: ' · 🔒'),
if (post.post.stickied)

View File

@ -3,12 +3,12 @@ import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:intl/intl.dart';
import 'package:lemmy_api_client/v2.dart';
import 'package:timeago/timeago.dart' as timeago;
import '../hooks/memo_future.dart';
import '../hooks/stores.dart';
import '../pages/manage_account.dart';
import '../util/extensions/api.dart';
import '../util/extensions/datetime.dart';
import '../util/goto.dart';
import '../util/intl.dart';
import '../util/text_color.dart';
@ -282,7 +282,7 @@ class _UserOverview extends HookWidget {
),
const SizedBox(height: 15),
Text(
'Joined ${timeago.format(userView.user.published)}',
'Joined ${userView.user.published.fancy}',
style: theme.textTheme.bodyText1,
),
Row(