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

Replace DateTime.now() with clock.now(). #1314

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion app/lib/blackboard/blackboard_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
// SPDX-License-Identifier: EUPL-1.2

import 'package:clock/clock.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:cloud_firestore_helper/cloud_firestore_helper.dart';

Expand Down Expand Up @@ -64,7 +65,7 @@ class BlackboardItem {
title: "",
text: null,
pictureURL: "",
createdOn: DateTime.now(),
createdOn: clock.now(),
attachments: [],
sendNotification: true,
forUsers: {},
Expand Down
5 changes: 3 additions & 2 deletions app/lib/changelog/change_database_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
//
// SPDX-License-Identifier: EUPL-1.2

import 'package:clock/clock.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:sharezone/changelog/change.dart';
import 'package:cloud_firestore_helper/cloud_firestore_helper.dart';
import 'package:sharezone/changelog/change.dart';

class ChangeDatabaseModel {
final String id;
Expand All @@ -32,7 +33,7 @@ class ChangeDatabaseModel {
return ChangeDatabaseModel._(
id: "",
version: "",
releaseDate: DateTime.now(),
releaseDate: clock.now(),
newFeatures: [],
improvements: [],
fixes: [],
Expand Down
4 changes: 3 additions & 1 deletion app/lib/comments/comment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//
// SPDX-License-Identifier: EUPL-1.2

import 'package:clock/clock.dart';

import 'misc.dart';

class Comment {
Expand Down Expand Up @@ -66,7 +68,7 @@ class CommentAge extends Duration {

int get inApproximateYears => (inDays / approximateDaysPerMonth).round();

DateTime get writtenOnDateTime => DateTime.now().subtract(this);
DateTime get writtenOnDateTime => clock.now().subtract(this);
}

class CommentAuthor {
Expand Down
6 changes: 3 additions & 3 deletions app/lib/comments/comment_data_models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
//
// SPDX-License-Identifier: EUPL-1.2

import 'package:clock/clock.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:sharezone/comments/comment.dart';
import 'package:cloud_firestore_helper/cloud_firestore_helper.dart';
import 'package:sharezone/comments/comment.dart';

class CommentDataModel {
final String? id;
Expand Down Expand Up @@ -73,8 +74,7 @@ class CommentDataModel {
author: author.toModel(),
content: comment,
ratings: _createRatings(),
age: CommentAge.fromDuration(
DateTime.now().difference(writtenOn!.toDate())),
age: CommentAge.fromDuration(clock.now().difference(writtenOn!.toDate())),
id: id,
);
}
Expand Down
7 changes: 4 additions & 3 deletions app/lib/dashboard/bloc/dashboard_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import 'dart:async';

import 'package:bloc_base/bloc_base.dart';
import 'package:clock/clock.dart';
import 'package:date/date.dart';
import 'package:design/design.dart';
import 'package:firebase_hausaufgabenheft_logik/firebase_hausaufgabenheft_logik.dart';
Expand Down Expand Up @@ -52,7 +53,7 @@ extension RepeatEveryExtension<T> on Stream<T> {
class DashboardBloc extends BlocBase {
final String _uid;
final todayDateTimeWithoutTime =
DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day);
DateTime(clock.now().year, clock.now().month, clock.now().day);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The calculation of todayDateTimeWithoutTime using clock.now() is correct and aligns with the objective of making the code more testable. However, consider simplifying the expression by using a single call to clock.now() to improve readability and performance.

- DateTime(clock.now().year, clock.now().month, clock.now().day);
+ final now = clock.now(); // Consider moving this to the class level if used in multiple places
+ DateTime(now.year, now.month, now.day);

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
DateTime(clock.now().year, clock.now().month, clock.now().day);
final now = clock.now(); // Consider moving this to the class level if used in multiple places
DateTime(now.year, now.month, now.day);


final _unreadBlackboardViewsSubject = BehaviorSubject<List<BlackboardView>>();
final _unreadBlackboardItemsEmptySubject = BehaviorSubject<bool>();
Expand Down Expand Up @@ -152,7 +153,7 @@ class DashboardBloc extends BlocBase {

List<HomeworkDto> _filterUrgentHomeworks(
List<HomeworkDto> allHomeworks, TypeOfUser typeOfUser) {
final now = DateTime.now();
final now = clock.now();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of clock.now() for determining now and dayAfterTomorrow is correct and supports the testability improvements. However, the comment regarding the behavior of adding days to a DateTime object and the suggestion to use now.add(Duration(days:2)) for clarity and correctness is valuable. Consider adopting this suggestion to improve code readability and maintainability.

- final dayAfterTomorrow = DateTime(now.year, now.month, now.day + 2);
+ final dayAfterTomorrow = now.add(Duration(days: 2));

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
final now = clock.now();

final dayAfterTomorrow = DateTime(now.year, now.month, now.day + 2);
// Was passiert, wenn der 30. Oktober ist und 2 Tage dazu gezählt werden? Springt es dann auf den 1. November um?
// Antwort: Springt um auf den 1.November, gechekt, auf wenn der Code unschön ist, besser wäre dayAfterTomorrow = now.add(Duration(days:2))
Expand Down Expand Up @@ -286,7 +287,7 @@ extension on DateTime {
bool get isDayAfterTomorrow => dayAfterTomorrow == withoutTime;

static DateTime get today =>
DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day);
DateTime(clock.now().year, clock.now().month, clock.now().day);
static DateTime get tomorrow =>
DateTime(today.year, today.month, today.day + 1);
static DateTime get dayAfterTomorrow =>
Expand Down
1 change: 1 addition & 0 deletions app/lib/dashboard/dashboard_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'dart:developer';

import 'package:analytics/analytics.dart';
import 'package:bloc_provider/bloc_provider.dart';
import 'package:clock/clock.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
Expand Down
7 changes: 4 additions & 3 deletions app/lib/dashboard/models/homework_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
// SPDX-License-Identifier: EUPL-1.2

import 'package:clock/clock.dart';
import 'package:firebase_hausaufgabenheft_logik/firebase_hausaufgabenheft_logik.dart';
import 'package:flutter/material.dart';
import 'package:group_domain_models/group_domain_models.dart';
Expand Down Expand Up @@ -39,7 +40,7 @@ class HomeworkView {
}

final todayDateTimeWithoutTime =
DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day);
DateTime(clock.now().year, clock.now().month, clock.now().day);
if (dateTime.isBefore(todayDateTimeWithoutTime)) {
return "Überfällig!";
} else if (dateTime.isAtSameMomentAs(todayDateTimeWithoutTime)) {
Expand All @@ -62,8 +63,8 @@ class HomeworkView {
final defaultColor = Colors.grey[400]!;
if (!withUrgentColor) return defaultColor;

final dayAfterTomorrow = DateTime(
DateTime.now().year, DateTime.now().month, DateTime.now().day + 2);
final dayAfterTomorrow =
DateTime(clock.now().year, clock.now().month, clock.now().day + 2);
return dateTime.isBefore(dayAfterTomorrow)
? Colors.redAccent
: defaultColor;
Expand Down
5 changes: 2 additions & 3 deletions app/lib/dashboard/sections/holiday_countdown_section.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ class _HolidayText extends StatelessWidget {
}
// For each Holiday create a Widget and add to the list.
for (var holiday in holidayList) {
int daysTillHolidayBeginn =
holiday!.start.difference(DateTime.now()).inDays;
int daysTillHolidayBeginn = holiday!.start.difference(clock.now()).inDays;
String holidayTitle = capitalize(holiday.name);

String emoji;
Expand All @@ -139,7 +138,7 @@ class _HolidayText extends StatelessWidget {
emoji = "🎉🎉🙌";
textWidget = Text("$holidayTitle: JETZT, WOOOOOOO! $emoji");
} else {
int daysTillHolidayEnd = holiday.end.difference(DateTime.now()).inDays;
int daysTillHolidayEnd = holiday.end.difference(clock.now()).inDays;
if (daysTillHolidayEnd == 0) {
textWidget = Text("$holidayTitle: Letzer Tag 😱");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// SPDX-License-Identifier: EUPL-1.2

import 'package:bloc_base/bloc_base.dart';
import 'package:clock/clock.dart';
import 'package:crash_analytics/crash_analytics.dart';
import 'package:meta/meta.dart';
import 'package:sharezone/changelog/change.dart';
Expand Down Expand Up @@ -41,7 +42,7 @@ class UpdateReminderBloc extends BlocBase {
getCurrentVersion: () => platformInformationRetriever.init().then(
(_) => Version.parse(name: platformInformationRetriever.version)),
updateGracePeriod: updateGracePeriod,
getCurrentDateTime: () => DateTime.now(),
getCurrentDateTime: () => clock.now(),
crashAnalytics: crashAnalytics,
);
}
Expand Down
5 changes: 3 additions & 2 deletions app/lib/feedback/src/cache/feedback_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import 'dart:developer';

import 'package:clock/clock.dart';
import 'package:key_value_store/key_value_store.dart';
import 'package:meta/meta.dart';

Expand Down Expand Up @@ -37,13 +38,13 @@ class FeedbackCache {
}

final durationPassedFromLastSubmit =
lastSubmit.difference(DateTime.now()).abs();
lastSubmit.difference(clock.now()).abs();
if (durationPassedFromLastSubmit < feedbackCooldown) return true;
return false;
}

Future<void> setLastSubmit() async {
_cache.setString(lastSubmitCacheKey, DateTime.now().toIso8601String());
_cache.setString(lastSubmitCacheKey, clock.now().toIso8601String());
}

Future<DateTime?> _getLastSubmitTime() async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
// SPDX-License-Identifier: EUPL-1.2

import 'package:clock/clock.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:files_basics/files_models.dart';
import 'package:files_basics/local_file.dart';
Expand Down Expand Up @@ -45,7 +46,7 @@ class FirebaseFileUploaderImplementation {
ChangeActivity(
authorID: creatorID,
authorName: creatorName,
changedOn: DateTime.now(),
changedOn: clock.now(),
)
],
name: localFile.getName(),
Expand Down
3 changes: 2 additions & 1 deletion app/lib/holidays/holiday_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import 'dart:async';

import 'package:bloc_base/bloc_base.dart';
import 'package:clock/clock.dart';
import 'package:holidays/holidays.dart';
import 'package:rxdart/subjects.dart';
import 'package:sharezone/util/api/user_api.dart';
Expand All @@ -34,7 +35,7 @@ class HolidayBloc extends BlocBase {
{required this.holidayManager,
required this.stateGateway,
this.getCurrentTime}) {
getCurrentTime ??= () => DateTime.now();
getCurrentTime ??= () => clock.now();

final holidaysStream = userState
// StateEnum.notSelected would result into an UnsupportedStateException
Expand Down
17 changes: 9 additions & 8 deletions app/lib/homework/parent/homework_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ import 'dart:developer';

import 'package:analytics/analytics.dart';
import 'package:bloc_provider/bloc_provider.dart';
import 'package:clock/clock.dart';
import 'package:common_domain_models/common_domain_models.dart';
import 'package:firebase_hausaufgabenheft_logik/firebase_hausaufgabenheft_logik.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_animations/flutter_staggered_animations.dart';
import 'package:sharezone/main/application_bloc.dart';
import 'package:sharezone/homework/parent/src/homework_page_bloc.dart';
import 'package:sharezone/homework/homework_dialog/homework_dialog.dart';
import 'package:sharezone/homework/homework_page_new.dart';
import 'package:sharezone/homework/parent/src/homework_page_bloc.dart';
import 'package:sharezone/homework/shared/homework_archived.dart';
import 'package:sharezone/homework/shared/homework_card.dart';
import 'package:sharezone/main/application_bloc.dart';
import 'package:sharezone/navigation/logic/navigation_bloc.dart';
import 'package:sharezone/navigation/models/navigation_item.dart';
import 'package:sharezone/navigation/scaffold/app_bar_configuration.dart';
import 'package:sharezone/navigation/scaffold/sharezone_main_scaffold.dart';
import 'package:sharezone/homework/shared/homework_archived.dart';
import 'package:sharezone/homework/homework_dialog/homework_dialog.dart';
import 'package:sharezone/homework/shared/homework_card.dart';
import 'package:sharezone_common/translations.dart';
import 'package:sharezone_widgets/sharezone_widgets.dart';
import 'package:user/user.dart';
Expand Down Expand Up @@ -59,7 +60,7 @@ Future<void> openHomeworkDialogAndShowConfirmationIfSuccessful(
List<HomeworkDto> getNotArchived(List<HomeworkDto> homeworkList) {
final List<HomeworkDto> notArchivedHomeworks = <HomeworkDto>[];
for (var homework in homeworkList) {
final int dif = homework.todoUntil.difference(DateTime.now()).inDays;
final int dif = homework.todoUntil.difference(clock.now()).inDays;
if (dif > -1) {
notArchivedHomeworks.add(homework);
}
Expand Down Expand Up @@ -572,8 +573,8 @@ class _HomeworkListWithCards extends StatelessWidget {
final List<HomeworkDto> dayAfterTomorrowList = <HomeworkDto>[];
final List<HomeworkDto> severalDaysList = <HomeworkDto>[];

DateTime today = DateTime(
DateTime.now().year, DateTime.now().month, DateTime.now().day);
DateTime today =
DateTime(clock.now().year, clock.now().month, clock.now().day);
for (var homework in homeworkList!) {
DateTime homeworkDate = DateTime(homework.todoUntil.year,
homework.todoUntil.month, homework.todoUntil.day);
Expand Down
5 changes: 3 additions & 2 deletions app/lib/homework/parent/src/homework_page_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'dart:async';
import 'dart:developer';

import 'package:bloc_base/bloc_base.dart';
import 'package:clock/clock.dart';
import 'package:firebase_hausaufgabenheft_logik/firebase_hausaufgabenheft_logik.dart';
import 'package:rxdart/subjects.dart';
import 'package:sharezone/util/api.dart';
Expand Down Expand Up @@ -116,8 +117,8 @@ class HomeworkPageBloc extends BlocBase {
for (HomeworkDto homework in homeworkNotDone) {
final DateTime homeworkDateTime = DateTime(homework.todoUntil.year,
homework.todoUntil.month, homework.todoUntil.day);
final DateTime todayDateTime = DateTime(
DateTime.now().year, DateTime.now().month, DateTime.now().day);
final DateTime todayDateTime =
DateTime(clock.now().year, clock.now().month, clock.now().day);
if (homeworkDateTime.isBefore(todayDateTime)) {
api.homework.changeIsHomeworkDoneTo(homework.id, true);
}
Expand Down
9 changes: 5 additions & 4 deletions app/lib/homework/shared/homework_archived.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
import 'dart:developer';

import 'package:bloc_provider/bloc_provider.dart';
import 'package:clock/clock.dart';
import 'package:firebase_hausaufgabenheft_logik/firebase_hausaufgabenheft_logik.dart';
import 'package:flutter/material.dart';
import 'package:sharezone/main/application_bloc.dart';
import 'package:sharezone/homework/parent/src/homework_page_bloc.dart';
import 'package:sharezone/homework/parent/homework_page.dart';
import 'package:sharezone/homework/parent/src/homework_page_bloc.dart';
import 'package:sharezone/homework/shared/homework_card.dart';
import 'package:sharezone/main/application_bloc.dart';
import 'package:sharezone_widgets/sharezone_widgets.dart';
import 'package:user/user.dart';

Expand Down Expand Up @@ -63,8 +64,8 @@ class _HomeworkArchivedPageState extends State<HomeworkArchivedPage> {
return ShowCenteredError(error: snapshot.error.toString());
}

DateTime today = DateTime(DateTime.now().year,
DateTime.now().month, DateTime.now().day);
DateTime today = DateTime(
clock.now().year, clock.now().month, clock.now().day);
List<HomeworkDto> homeworkList =
snapshot.data!.where((HomeworkDto homework) {
DateTime todoUntil = DateTime(homework.todoUntil.year,
Expand Down
15 changes: 8 additions & 7 deletions app/lib/homework/shared/homework_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,28 @@

import 'package:analytics/analytics.dart';
import 'package:bloc_provider/bloc_provider.dart';
import 'package:clock/clock.dart';
import 'package:common_domain_models/common_domain_models.dart';
import 'package:firebase_hausaufgabenheft_logik/firebase_hausaufgabenheft_logik.dart';
import 'package:flutter/material.dart';
import 'package:group_domain_models/group_domain_models.dart';
import 'package:intl/intl.dart';
import 'package:sharezone/main/application_bloc.dart';
import 'package:sharezone/homework/parent/src/homework_card_bloc.dart';
import 'package:sharezone/dashboard/models/homework_view.dart';
import 'package:sharezone/groups/src/pages/course/course_card.dart';
import 'package:sharezone/homework/teacher/homework_done_by_users_list/homework_completion_user_list_page.dart';
import 'package:sharezone/homework/homework_details/homework_details.dart';
import 'package:sharezone/homework/homework_details/homework_details_view_factory.dart';
import 'package:sharezone/homework/shared/homework_permissions.dart';
import 'package:sharezone/homework/parent/homework_page.dart';
import 'package:sharezone/homework/parent/src/homework_card_bloc.dart';
import 'package:sharezone/homework/shared/delete_homework.dart';
import 'package:sharezone/homework/shared/homework_permissions.dart';
import 'package:sharezone/homework/teacher/homework_done_by_users_list/homework_completion_user_list_page.dart';
import 'package:sharezone/main/application_bloc.dart';
import 'package:sharezone/report/page/report_page.dart';
import 'package:sharezone/report/report_icon.dart';
import 'package:sharezone/report/report_item.dart';
import 'package:sharezone/submissions/homework_list_submissions_page.dart';
import 'package:sharezone/util/api/connections_gateway.dart';
import 'package:sharezone/util/navigation_service.dart';
import 'package:sharezone/homework/shared/delete_homework.dart';
import 'package:sharezone_widgets/sharezone_widgets.dart';
import 'package:user/user.dart';

Expand All @@ -52,8 +53,8 @@ class HomeworkCard extends StatelessWidget {
final bloc = HomeworkCardBloc(api, homework!);
final analytics = BlocProvider.of<SharezoneContext>(context).analytics;

DateTime tomorrowWithoutTime = DateTime(
DateTime.now().year, DateTime.now().month, DateTime.now().day + 1);
DateTime tomorrowWithoutTime =
DateTime(clock.now().year, clock.now().month, clock.now().day + 1);
Comment on lines +56 to +57
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The replacement of DateTime.now() with clock.now() in the initialization of tomorrowWithoutTime is correctly implemented. This change is crucial for enhancing the testability of the code by allowing the use of the withClock function in tests to manipulate the current time. However, it's worth noting that calling clock.now() multiple times within the same expression could lead to slight differences in the returned time if the calls span a change in the current millisecond. To ensure consistency, consider storing the result of clock.now() in a local variable and using that for all subsequent operations within this scope.

- DateTime tomorrowWithoutTime =
-     DateTime(clock.now().year, clock.now().month, clock.now().day + 1);
+ final now = clock.now();
+ DateTime tomorrowWithoutTime = DateTime(now.year, now.month, now.day + 1);

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
DateTime tomorrowWithoutTime =
DateTime(clock.now().year, clock.now().month, clock.now().day + 1);
final now = clock.now();
DateTime tomorrowWithoutTime = DateTime(now.year, now.month, now.day + 1);

DateTime todoUntilWithoutTime = DateTime(homework!.todoUntil.year,
homework!.todoUntil.month, homework!.todoUntil.day);

Expand Down
Loading
Loading