diff --git a/lib/setup/repo_selector.dart b/lib/setup/repo_selector.dart index 69648b34..c9e95a70 100644 --- a/lib/setup/repo_selector.dart +++ b/lib/setup/repo_selector.dart @@ -3,7 +3,7 @@ import 'package:flutter/material.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:function_types/function_types.dart'; import 'package:intl/intl.dart'; -//import 'package:font_awesome_flutter/font_awesome_flutter.dart'; +import 'package:timeago/timeago.dart' as timeago; import 'package:gitjournal/analytics.dart'; import 'package:gitjournal/apis/githost_factory.dart'; @@ -13,6 +13,8 @@ import 'package:gitjournal/setup/error.dart'; import 'package:gitjournal/setup/loading.dart'; import 'package:gitjournal/utils/logger.dart'; +//import 'package:font_awesome_flutter/font_awesome_flutter.dart'; + class GitHostSetupRepoSelector extends StatefulWidget { final GitHost gitHost; final Func1 onDone; @@ -229,16 +231,8 @@ class _RepoTile extends StatelessWidget { @override Widget build(BuildContext context) { var theme = Theme.of(context); - final _dateFormat = DateFormat('dd MMM, yyyy'); var textTheme = theme.textTheme; - Widget trailing = Container(); - if (repo.updatedAt != null) { - var dateStr = _dateFormat.format(repo.updatedAt); - - trailing = Text(dateStr, style: textTheme.caption); - } - /* var iconsRow = Row( children: [ @@ -277,7 +271,7 @@ class _RepoTile extends StatelessWidget { return ListTile( title: Text(repo.fullName), - trailing: trailing, + trailing: _SmartDateTime(repo.updatedAt, textTheme.caption), selected: selected, contentPadding: const EdgeInsets.all(0.0), onTap: onTap, @@ -311,3 +305,35 @@ class _IconText extends StatelessWidget { } } */ + +class _SmartDateTime extends StatelessWidget { + final DateTime dt; + final TextStyle style; + + _SmartDateTime(this.dt, this.style); + + static final _dateFormat = DateFormat('d MMM yyyy'); + static final _dateFormatWithoutYear = DateFormat('d MMM'); + static final thirtyDaysAgo = + DateTime.now().subtract(const Duration(days: 30)); + + @override + Widget build(BuildContext context) { + if (dt == null) { + return Container(); + } + + String text; + + if (dt.isAfter(thirtyDaysAgo)) { + Locale locale = Localizations.localeOf(context); + text = timeago.format(dt, locale: locale.languageCode); + } else if (dt.year == DateTime.now().year) { + text = _dateFormatWithoutYear.format(dt); + } else { + text = _dateFormat.format(dt); + } + + return Text(text, style: style); + } +} diff --git a/pubspec.lock b/pubspec.lock index 4f658f43..792a5ffe 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -910,6 +910,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.3.10" + timeago: + dependency: "direct main" + description: + name: timeago + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.27" tool_base: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index cb72066e..1e034acd 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -56,6 +56,7 @@ dependencies: receive_sharing_intent: ^1.4.0+2 in_app_purchase: ^0.3.4+5 flutter_plugin_android_lifecycle: ^1.0.8 # for fixing the build + timeago: ^2.0.27 dev_dependencies: flutter_launcher_icons: "^0.7.2"