Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hello - Handle different start/end date scenarios #934

Merged
merged 5 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(),
};
}
}
46 changes: 34 additions & 12 deletions lib/ui/views/news_details_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,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 @@ -178,14 +181,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 @@ -216,8 +236,10 @@ class _NewsDetailsViewState extends State<NewsDetailsView> {
const Icon(Icons.event, size: 20.0, color: Colors.white),
),
Flexible(
child: Padding(
padding: const EdgeInsets.only(left: 8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
AppIntl.of(context)!.news_event_date,
Expand All @@ -227,11 +249,11 @@ class _NewsDetailsViewState extends State<NewsDetailsView> {
Text(
formattedEventDate,
style: const TextStyle(color: Colors.white),
textAlign: TextAlign.right,
textAlign: TextAlign.left,
),
],
),
),
)),
],
),
),
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: The 4th generation of ÉTSMobile, the main gateway between the Éco
# pub.dev using `pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 4.34.0+1
version: 4.33.3+1

environment:
sdk: ">=2.12.0 <3.0.0"
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
Loading