Skip to content

Commit

Permalink
Hello - Handle different start/end date scenarios (#934)
Browse files Browse the repository at this point in the history
* Added end event date

* Golden skip test

* Update news_details_view.dart

* [BOT] Applying version.

---------

Co-authored-by: Udito3 <Udito3@users.noreply.github.com>
  • Loading branch information
Udito3 and Udito3 authored Mar 16, 2024
1 parent dbe5a1a commit 08e2180
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 43 deletions.
8 changes: 5 additions & 3 deletions lib/core/managers/news_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class NewsRepository {
avatar: "https://picsum.photos/200/200",
activity: "Club scientifique",
publishedDate: DateTime.now(),
eventDate: DateTime.now().add(const Duration(days: 2)),
eventStartDate: DateTime.now().add(const Duration(days: 2)),
eventEndDate: DateTime.now().add(const Duration(days: 5)),
image: "https://picsum.photos/400/200",
tags: [
"Robotique",
Expand All @@ -58,7 +59,8 @@ class NewsRepository {
avatar: "https://picsum.photos/200/200",
activity: "Club scientifique",
publishedDate: DateTime.now(),
eventDate: DateTime.now().add(const Duration(days: 10)),
eventStartDate: DateTime.now().add(const Duration(days: 10)),
eventEndDate: DateTime.now().add(const Duration(days: 11)),
image: "https://picsum.photos/400/200",
tags: [
"Compétition",
Expand All @@ -73,7 +75,7 @@ class NewsRepository {
avatar: "https://picsum.photos/200/200",
activity: "Service à la vie étudiante",
publishedDate: DateTime.now(),
eventDate: DateTime.now().add(const Duration(days: 3)),
eventStartDate: DateTime.now().add(const Duration(days: 3)),
image: "https://picsum.photos/400/200",
tags: ["5 @ 7", "Vie étudiante", "Activité"],
),
Expand Down
33 changes: 18 additions & 15 deletions lib/core/models/news.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@ class News {
final String image;
final List<String> tags;
final DateTime publishedDate;
final DateTime eventDate;
final DateTime eventStartDate;
final DateTime? eventEndDate;

News({
required this.id,
required this.title,
required this.description,
required this.author,
required this.avatar,
required this.activity,
required this.image,
required this.tags,
required this.publishedDate,
required this.eventDate,
});
News(
{required this.id,
required this.title,
required this.description,
required this.author,
required this.avatar,
required this.activity,
required this.image,
required this.tags,
required this.publishedDate,
required this.eventStartDate,
this.eventEndDate});

/// Used to create [News] instance from a JSON file
factory News.fromJson(Map<String, dynamic> map) {
Expand All @@ -35,7 +36,8 @@ class News {
image: map['image'] as String,
tags: map['tags'] as List<String>,
publishedDate: DateTime.parse(map['publishedDate'] as String),
eventDate: DateTime.parse(map['eventDate'] as String),
eventStartDate: DateTime.parse(map['eventStartDate'] as String),
eventEndDate: DateTime.parse(map['eventEndDate'] as String),
);
}

Expand All @@ -50,7 +52,8 @@ class News {
'image': image,
'tags': tags.toList(),
'publishedDate': publishedDate.toString(),
'eventDate': eventDate.toString(),
'eventStartDate': eventStartDate.toString(),
'eventEndDate': eventEndDate.toString(),
};
}
}
40 changes: 30 additions & 10 deletions lib/ui/views/news_details_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,11 @@ class _NewsDetailsViewState extends State<NewsDetailsView> {
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
_buildTitle(widget.news.title),
_buildDate(context, widget.news.publishedDate,
widget.news.eventDate),
_buildDate(
context,
widget.news.publishedDate,
widget.news.eventStartDate,
widget.news.eventEndDate),
_buildImage(widget.news.image),
_buildAuthor(widget.news.avatar, widget.news.author,
widget.news.activity),
Expand Down Expand Up @@ -187,14 +190,31 @@ class _NewsDetailsViewState extends State<NewsDetailsView> {
);
}

Widget _buildDate(
BuildContext context, DateTime publishedDate, DateTime eventDate) {
Widget _buildDate(BuildContext context, DateTime publishedDate,
DateTime eventStartDate, DateTime? eventEndDate) {
final String locale = Localizations.localeOf(context).toString();
final String formattedPublishedDate =
DateFormat('d MMMM yyyy', Localizations.localeOf(context).toString())
.format(publishedDate);
final String formattedEventDate =
DateFormat('d MMMM yyyy', Localizations.localeOf(context).toString())
.format(eventDate);
DateFormat('d MMMM yyyy', locale).format(publishedDate);

late String formattedEventDate;

final bool sameMonthAndYear = eventEndDate?.month == eventStartDate.month &&
eventEndDate?.year == eventStartDate.year;
final bool sameDayMonthAndYear =
eventEndDate?.day == eventStartDate.day && sameMonthAndYear;

if (eventEndDate == null || sameDayMonthAndYear) {
formattedEventDate =
DateFormat('d MMMM yyyy', locale).format(eventStartDate);
} else {
if (sameMonthAndYear) {
formattedEventDate =
'${DateFormat('d', locale).format(eventStartDate)} - ${DateFormat('d MMMM yyyy', locale).format(eventEndDate)}';
} else {
formattedEventDate =
'${DateFormat('d MMMM yyyy', locale).format(eventStartDate)} -\n${DateFormat('d MMMM yyyy', locale).format(eventEndDate)}';
}
}

return Padding(
padding: const EdgeInsets.fromLTRB(16.0, 5.0, 16.0, 8.0),
Expand Down Expand Up @@ -231,7 +251,7 @@ class _NewsDetailsViewState extends State<NewsDetailsView> {
child: Padding(
padding: const EdgeInsets.only(left: 4),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
AppIntl.of(context)!.news_event_date,
Expand Down
13 changes: 9 additions & 4 deletions test/models/news_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ void main() {
'image': 'https://example.com/image.jpg',
'tags': ['Tag 1', 'Tag 2'],
'publishedDate': '2022-01-01T12:00:00Z',
'eventDate': '2022-01-01T12:00:00Z',
'eventStartDate': '2022-01-01T12:00:00Z',
'eventEndDate': '2022-01-01T12:00:00Z',
};

final news = News.fromJson(json);
Expand All @@ -35,7 +36,9 @@ void main() {
expect(news.tags[1], equals('Tag 2'));
expect(
news.publishedDate, equals(DateTime.parse('2022-01-01T12:00:00Z')));
expect(news.eventDate, equals(DateTime.parse('2022-01-01T12:00:00Z')));
expect(
news.eventStartDate, equals(DateTime.parse('2022-01-01T12:00:00Z')));
expect(news.eventEndDate, equals(DateTime.parse('2022-01-01T12:00:00Z')));
});

test('toJson() should convert News to JSON correctly', () {
Expand All @@ -49,7 +52,8 @@ void main() {
image: 'https://example.com/image.jpg',
tags: ['Tag 1', 'Tag 2'],
publishedDate: DateTime.parse('2022-01-01T12:00:00.000Z'),
eventDate: DateTime.parse('2022-01-01T12:00:00.000Z'),
eventStartDate: DateTime.parse('2022-01-01T12:00:00.000Z'),
eventEndDate: DateTime.parse('2022-01-01T12:00:00.000Z'),
);

final json = news.toJson();
Expand All @@ -65,7 +69,8 @@ void main() {
expect(json['tags'][0], equals('Tag 1'));
expect(json['tags'][1], equals('Tag 2'));
expect(json['publishedDate'], equals('2022-01-01 12:00:00.000Z'));
expect(json['eventDate'], equals('2022-01-01 12:00:00.000Z'));
expect(json['eventStartDate'], equals('2022-01-01 12:00:00.000Z'));
expect(json['eventEndDate'], equals('2022-01-01 12:00:00.000Z'));
});
});
}
9 changes: 6 additions & 3 deletions test/ui/views/ets_view_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ void main() {
image: "",
tags: ["tag1", "tag2"],
publishedDate: DateTime.parse('2022-01-01T12:00:00Z'),
eventDate: DateTime.parse('2022-01-02T12:00:00Z'),
eventStartDate: DateTime.parse('2022-01-02T12:00:00Z'),
eventEndDate: DateTime.parse('2022-01-02T12:00:00Z'),
),
News(
id: 2,
Expand All @@ -48,7 +49,8 @@ void main() {
image: "",
tags: ["tag3", "tag4"],
publishedDate: DateTime.parse('2022-02-01T12:00:00Z'),
eventDate: DateTime.parse('2022-02-02T12:00:00Z'),
eventStartDate: DateTime.parse('2022-02-02T12:00:00Z'),
eventEndDate: DateTime.parse('2022-02-02T12:00:00Z'),
),
News(
id: 3,
Expand All @@ -60,7 +62,8 @@ void main() {
image: "",
tags: ["tag5", "tag6"],
publishedDate: DateTime.parse('2022-02-01T12:00:00Z'),
eventDate: DateTime.parse('2022-02-02T12:00:00Z'),
eventStartDate: DateTime.parse('2022-02-02T12:00:00Z'),
eventEndDate: DateTime.parse('2022-02-02T12:00:00Z'),
),
];

Expand Down
Binary file modified test/ui/views/goldenFiles/newsDetailsView_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/ui/views/goldenFiles/newsView_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion test/ui/views/news_details_view_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ void main() {
image: '',
tags: ['sampleTag1', 'sampleTag2'],
publishedDate: DateTime.parse('2022-01-01T12:00:00Z'),
eventDate: DateTime.parse('2022-01-02T12:00:00Z'),
eventStartDate: DateTime.parse('2022-02-02T12:00:00Z'),
eventEndDate: DateTime.parse('2022-02-02T12:00:00Z'),
);
});

Expand Down
9 changes: 6 additions & 3 deletions test/ui/views/news_view_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ void main() {
image: "",
tags: ["tag1", "tag2"],
publishedDate: DateTime.parse('2022-01-01T12:00:00Z'),
eventDate: DateTime.parse('2022-01-02T12:00:00Z'),
eventStartDate: DateTime.parse('2022-02-02T12:00:00Z'),
eventEndDate: DateTime.parse('2022-02-02T12:00:00Z'),
),
News(
id: 2,
Expand All @@ -46,7 +47,8 @@ void main() {
image: "",
tags: ["tag3", "tag4"],
publishedDate: DateTime.parse('2022-02-01T12:00:00Z'),
eventDate: DateTime.parse('2022-02-02T12:00:00Z'),
eventStartDate: DateTime.parse('2022-02-02T12:00:00Z'),
eventEndDate: DateTime.parse('2022-02-02T12:00:00Z'),
),
News(
id: 3,
Expand All @@ -58,7 +60,8 @@ void main() {
image: "",
tags: ["tag5", "tag6"],
publishedDate: DateTime.parse('2022-02-01T12:00:00Z'),
eventDate: DateTime.parse('2022-02-02T12:00:00Z'),
eventStartDate: DateTime.parse('2022-02-02T12:00:00Z'),
eventEndDate: DateTime.parse('2022-02-02T12:00:00Z'),
),
];

Expand Down
3 changes: 2 additions & 1 deletion test/ui/widgets/news_card_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ void main() {
image: "",
tags: ["tag5", "tag6"],
publishedDate: DateTime.parse('2022-02-01T12:00:00Z'),
eventDate: DateTime.parse('2022-02-02T12:00:00Z'),
eventStartDate: DateTime.parse('2022-02-02T12:00:00Z'),
eventEndDate: DateTime.parse('2022-02-02T12:00:00Z'),
);

group('News card Tests', () {
Expand Down
3 changes: 2 additions & 1 deletion test/viewmodels/news_details_viewmodel_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ void main() {
image: "https://example.com/image.jpg",
tags: ["tag1", "tag2"],
publishedDate: DateTime.parse('2022-01-01T12:00:00Z'),
eventDate: DateTime.parse('2022-01-02T12:00:00Z'),
eventStartDate: DateTime.parse('2022-02-02T12:00:00Z'),
eventEndDate: DateTime.parse('2022-02-02T12:00:00Z'),
);

group('NewsDetailsViewModel Tests', () {
Expand Down
6 changes: 4 additions & 2 deletions test/viewmodels/news_viewmodel_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ void main() {
image: "",
tags: ["tag1", "tag2"],
publishedDate: DateTime.parse('2022-01-01T12:00:00Z'),
eventDate: DateTime.parse('2022-01-02T12:00:00Z'),
eventStartDate: DateTime.parse('2022-02-02T12:00:00Z'),
eventEndDate: DateTime.parse('2022-02-02T12:00:00Z'),
),
News(
id: 2,
Expand All @@ -40,7 +41,8 @@ void main() {
image: "",
tags: ["tag3", "tag4"],
publishedDate: DateTime.parse('2022-02-01T12:00:00Z'),
eventDate: DateTime.parse('2022-02-02T12:00:00Z'),
eventStartDate: DateTime.parse('2022-02-02T12:00:00Z'),
eventEndDate: DateTime.parse('2022-02-02T12:00:00Z'),
)
];

Expand Down

0 comments on commit 08e2180

Please sign in to comment.