Skip to content

Commit

Permalink
Fix Upcoming events are noor showing in dashboard (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-sneha-s authored Apr 30, 2024
1 parent 1658e67 commit 1781de2
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 14 deletions.
25 changes: 17 additions & 8 deletions lib/data/core/extensions/date_time.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@ extension DateExtention on int {
extension TimestampExtension on DateTime {
int get futureDateSelectionYear => year + 2;

bool isDateInCurrentWeek(DateTime currentDate) =>
month == currentDate.month &&
day >= currentDate.day &&
day <=
currentDate
.add(Duration(days: DateTime.daysPerWeek - currentDate.weekday))
.day;
bool isDateInCurrentWeek(DateTime currentDate) {
DateTime now = DateTime.now().dateOnly;
DateTime startOfWeek =
now.dateOnly.subtract(Duration(days: now.weekday - 1));
DateTime endOfWeek = startOfWeek.dateOnly
.add(const Duration(days: DateTime.daysPerWeek - 1));

DateTime birthdayThisYear = DateTime(currentDate.year, month, day).dateOnly;
DateTime birthdayNextYear =
DateTime(currentDate.year + 1, month, day).dateOnly;

return (birthdayThisYear.isAfterOrSame(now) &&
birthdayThisYear.isBefore(endOfWeek)) ||
(birthdayNextYear.isAfterOrSame(now) &&
birthdayNextYear.isBefore(endOfWeek));
}

int calculateDifferenceInYears(DateTime currentDate) {
int yearDifference = year - currentDate.year;
Expand All @@ -36,7 +45,7 @@ extension TimestampExtension on DateTime {
bool isBeforeOrSame(DateTime date) =>
isBefore(date) || isAtSameMomentAs(date);

bool isAfterOrSame(DateTime date) => isBefore(date) || isAtSameMomentAs(date);
bool isAfterOrSame(DateTime date) => isAfter(date) || isAtSameMomentAs(date);

int get timeStampToInt => millisecondsSinceEpoch;

Expand Down
14 changes: 12 additions & 2 deletions lib/data/core/utils/date_formatter.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter_gen/gen_l10n/app_localization.dart';
import 'package:intl/intl.dart';
import 'package:projectunity/data/core/extensions/date_time.dart';
import 'package:projectunity/data/core/extensions/string_extension.dart';
import 'package:projectunity/data/model/leave/leave.dart';

class DateFormatter {
Expand Down Expand Up @@ -86,12 +87,21 @@ class DateFormatter {
}
}

String getEventDateRepresentation(DateTime dt) {
if (today.dateOnly.isAtSameMomentAs(dt.dateOnly)) {
return _localization.dateFormatter_today.toLowerCase();
} else if (today.add(oneDay).dateOnly.isAtSameMomentAs(dt.dateOnly)) {
return _localization.dateFormatter_tomorrow.toLowerCase();
}
return _localization.date_format_MMMd(dt).capitalize();
}

String showBirthdays({required DateTime dateTime, required String name}) {
final today = DateTime.now().dateOnly;
if (dateTime.dateOnly.isAtSameMomentAs(today)) {
return _localization.present_birthday_text(name);
} else {
return "${_localization.upcoming_birthday_text(name)} ${getDateRepresentation(dateTime).toLowerCase()}!🎂🎁";
return "${_localization.upcoming_birthday_text(name)} ${getEventDateRepresentation(dateTime)}!🎂🎁";
}
}

Expand All @@ -105,7 +115,7 @@ class DateFormatter {
if (upcomingDate.isAtSameMomentAs(today)) {
return _localization.present_anniversary_text(name, yearDifference);
} else {
return "${_localization.upcoming_anniversary_text(name, yearDifference)} ${getDateRepresentation(upcomingDate).toLowerCase()}!🎉";
return "${_localization.upcoming_anniversary_text(name, yearDifference)} ${getEventDateRepresentation(upcomingDate)}!🎉";
}
}

Expand Down
12 changes: 12 additions & 0 deletions lib/data/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,18 @@
}
},


"date_format_MMMd": "{date}",
"@date_format_MMMd": {
"placeholders": {
"date": {
"type": "DateTime",
"format": "MMMMd"
}
}
},


"date_format_yMMMM": "{date}",
"@date_format_yMMMM": {
"placeholders": {
Expand Down
9 changes: 7 additions & 2 deletions lib/ui/shared/events/celebrations_event_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,18 @@ class CurrentWeekEventCard extends StatelessWidget {
),
const SizedBox(width: 10),
Expanded(
child: Text(isAnniversary
child: Text(
isAnniversary
? DateFormatter(context.l10n).showAnniversaries(
dateOfJoining: event.dateTime,
upcomingDate: event.upcomingDate,
name: event.name)
: DateFormatter(context.l10n).showBirthdays(
dateTime: event.dateTime, name: event.name))),
dateTime: event.dateTime, name: event.name),
style: AppTextStyle.style14
.copyWith(color: context.colorScheme.textPrimary),
),
),
],
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ void main() {

test('Add applied leaves on leave list test', () {
when(leaveRepo.fetchLeave(leaveId: casualLeave.leaveId))
.thenAnswer((realInvocation) async => casualLeave);
.thenAnswer((_) async => casualLeave);
bloc.add(UpdateLeave(leaveId: casualLeave.leaveId));
expectLater(
bloc.stream,
emits(UserLeaveState(
casualLeaves: [casualLeave]
.groupByMonth((element) => element.appliedOn))));
.groupByMonth((element) => element.startDate))));
});
});

Expand Down

0 comments on commit 1781de2

Please sign in to comment.