Skip to content

Commit

Permalink
Merge 2795de2 into 1fde056
Browse files Browse the repository at this point in the history
  • Loading branch information
albertms10 authored Jan 27, 2023
2 parents 1fde056 + 2795de2 commit e48bda7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
29 changes: 29 additions & 0 deletions lib/utils/app_localizations_extension.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// ignore: format-comment
// coverage:ignore-file
import 'package:cabin_booking/utils/date_time_extension.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:intl/intl.dart';

extension AppLocalizationsExtension on AppLocalizations {
// Cabin.
Expand All @@ -23,4 +27,29 @@ extension AppLocalizationsExtension on AppLocalizations {
...monthlyPeriodicity,
...annuallyPeriodicity,
];

String _formatRelativeDate(DateTime dateTime) {
if (dateTime.isToday) return today;
if (dateTime.isYesterday) return yesterday;
if (dateTime.isTomorrow) return tomorrow;

return DateFormat.yMMMEd().format(dateTime);
}

/// Returns a relatively formatted [String] from the given [DateTime].
///
/// Example:
/// ```dart
/// final today = DateTime.now();
/// assert(appLocalizations.formatRelative(today) == 'Today, 09:00');
///
/// final yesterday = today.subtract(const Duration(days: 1));
/// assert(appLocalizations.formatRelative(yesterday) == 'Yesterday, 09:00');
/// ```
String formatRelative(DateTime dateTime) {
final date = _formatRelativeDate(dateTime);
final time = DateFormat.Hm().format(dateTime);

return [date, time].join(', ');
}
}
29 changes: 0 additions & 29 deletions lib/utils/date_time_extension.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:intl/intl.dart';

/// DateTime extension.
extension DateTimeExtension on DateTime {
Expand Down Expand Up @@ -77,31 +75,4 @@ extension DateTimeExtension on DateTime {
/// Whether this [DateTime] refers to the same date as tomorrow.
bool get isTomorrow =>
isSameDateAs(DateTime.now().add(const Duration(days: 1)));

String _formatRelativeDate(AppLocalizations appLocalizations) {
if (isToday) return appLocalizations.today;
if (isYesterday) return appLocalizations.yesterday;
if (isTomorrow) return appLocalizations.tomorrow;

return DateFormat.yMMMEd().format(this);
}

String _formatRelativeTime() => DateFormat.Hm().format(this);

/// Returns a relatively formatted [String] from this [DateTime].
///
/// Example:
/// ```dart
/// final today = DateTime.now();
/// assert(today.formatRelative(appLocalizations) == 'Today, 09:00');
///
/// final yesterday = today.subtract(const Duration(days: 1));
/// assert(yesterday.formatRelative(appLocalizations) == 'Yesterday, 09:00');
/// ```
String formatRelative(AppLocalizations appLocalizations) {
final date = _formatRelativeDate(appLocalizations);
final time = _formatRelativeTime();

return [date, time].join(', ');
}
}
7 changes: 4 additions & 3 deletions lib/widgets/jump_bar/booking_search_result.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:cabin_booking/model.dart';
import 'package:cabin_booking/utils/date_time_extension.dart';
import 'package:cabin_booking/utils/app_localizations_extension.dart';
import 'package:cabin_booking/widgets/jump_bar/search_result_label.dart';
import 'package:collection/collection.dart' show IterableNullableExtension;
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -41,8 +41,9 @@ class BookingSearchResult extends StatelessWidget {
)
: null,
formatter: (dateRange) {
final startDate =
dateRange.startDate?.formatRelative(appLocalizations);
final startDate = dateRange.startDate == null
? null
: appLocalizations.formatRelative(dateRange.startDate!);

final endDate = dateRange.endDate != null
? DateFormat.Hm().format(dateRange.endDate!)
Expand Down

0 comments on commit e48bda7

Please sign in to comment.