Skip to content

Commit

Permalink
Merge pull request #376 from mateusz-bak/370-bug-date-displayinput-fo…
Browse files Browse the repository at this point in the history
…rmat-mismatch

fix: Displayed dates format will follow device locale
  • Loading branch information
mateusz-bak authored Nov 24, 2023
2 parents e2d4f50 + c01613a commit 661daae
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
8 changes: 6 additions & 2 deletions lib/ui/add_book_screen/widgets/date_row.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class DateRow extends StatelessWidget {

@override
Widget build(BuildContext context) {
final dateFormat = DateFormat.yMd(
'${context.locale.languageCode}-${context.locale.countryCode}',
);

return Padding(
padding: const EdgeInsets.symmetric(horizontal: 10),
child: BlocBuilder<EditBookCubit, Book>(
Expand All @@ -52,7 +56,7 @@ class DateRow extends StatelessWidget {
icon: FontAwesomeIcons.play,
text: (state.startDate == null)
? LocaleKeys.start_date.tr()
: '${state.startDate?.day}/${state.startDate?.month}/${state.startDate?.year}',
: dateFormat.format(state.startDate!),
onPressed: showStartDatePicker,
onClearPressed: clearStartDate,
showClearButton: (state.startDate == null) ? false : true,
Expand All @@ -74,7 +78,7 @@ class DateRow extends StatelessWidget {
icon: FontAwesomeIcons.flagCheckered,
text: (state.finishDate == null)
? LocaleKeys.finish_date.tr()
: '${state.finishDate?.day}/${state.finishDate?.month}/${state.finishDate?.year}',
: dateFormat.format(state.finishDate!),
onPressed: showFinishDatePicker,
onClearPressed: clearFinishDate,
showClearButton:
Expand Down
11 changes: 2 additions & 9 deletions lib/ui/book_screen/book_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,6 @@ class BookScreen extends StatelessWidget {
context.read<CurrentBookCubit>().setBook(book);
}

String? _generateDate(DateTime? date) {
if (date == null) return null;

final DateFormat formatter = DateFormat('dd/MM/yyyy');
return formatter.format(date);
}

String _generateReadingTime({
DateTime? startDate,
DateTime? finishDate,
Expand Down Expand Up @@ -208,8 +201,8 @@ class BookScreen extends StatelessWidget {
context,
),
rating: state.rating,
startDate: _generateDate(state.startDate),
finishDate: _generateDate(state.finishDate),
startDate: state.startDate,
finishDate: state.finishDate,
onLikeTap: () => _onLikeTap(context, state),
isLiked: state.favourite,
showChangeStatus: (state.status == 1 ||
Expand Down
14 changes: 9 additions & 5 deletions lib/ui/book_screen/widgets/book_status_detail.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class BookStatusDetail extends StatelessWidget {

final IconData? statusIcon;
final String statusText;
final String? startDate;
final String? finishDate;
final DateTime? startDate;
final DateTime? finishDate;
final int? rating;
final Function() onLikeTap;
final bool isLiked;
Expand All @@ -36,9 +36,13 @@ class BookStatusDetail extends StatelessWidget {
final bool showRatingAndLike;

Widget _buildStartAndFinishDate(BuildContext context) {
final dateFormat = DateFormat.yMd(
'${context.locale.languageCode}-${context.locale.countryCode}',
);

if (startDate != null && finishDate != null) {
return Text(
'$startDate - $finishDate',
'${dateFormat.format(startDate!)} - ${dateFormat.format(finishDate!)}',
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.normal,
Expand All @@ -48,7 +52,7 @@ class BookStatusDetail extends StatelessWidget {

if (startDate == null && finishDate != null) {
return Text(
'${LocaleKeys.finished_on_date.tr()} $finishDate',
'${LocaleKeys.finished_on_date.tr()} ${dateFormat.format(finishDate!)}',
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.normal,
Expand All @@ -58,7 +62,7 @@ class BookStatusDetail extends StatelessWidget {

if (startDate != null && finishDate == null) {
return Text(
'${LocaleKeys.started_on_date.tr()} $startDate',
'${LocaleKeys.started_on_date.tr()} ${dateFormat.format(startDate!)}',
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.normal,
Expand Down
15 changes: 6 additions & 9 deletions lib/ui/books_screen/widgets/book_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class BookCard extends StatelessWidget {
Widget _buildSortAttribute() {
return BlocBuilder<SortBloc, SortState>(
builder: (context, state) {
final dateFormat = DateFormat.yMd(
'${context.locale.languageCode}-${context.locale.countryCode}',
);

if (state is SetSortState) {
if (state.sortType == SortType.byPages) {
return (book.pages != null)
Expand All @@ -46,7 +50,7 @@ class BookCard extends StatelessWidget {
style: const TextStyle(fontSize: 12),
),
Text(
'${_generateDate(book.startDate)}',
dateFormat.format(book.startDate!),
style: const TextStyle(
fontSize: 13,
fontWeight: FontWeight.bold,
Expand All @@ -65,7 +69,7 @@ class BookCard extends StatelessWidget {
style: const TextStyle(fontSize: 12),
),
Text(
'${_generateDate(book.finishDate)}',
dateFormat.format(book.finishDate!),
style: const TextStyle(
fontSize: 13,
fontWeight: FontWeight.bold,
Expand Down Expand Up @@ -148,13 +152,6 @@ class BookCard extends StatelessWidget {
return chips;
}

String? _generateDate(DateTime? date) {
if (date == null) return null;

final DateFormat formatter = DateFormat('dd/MM/yyyy');
return formatter.format(date);
}

@override
Widget build(BuildContext context) {
final coverFile = book.getCoverFile();
Expand Down

0 comments on commit 661daae

Please sign in to comment.