Skip to content

Commit

Permalink
Implement- Show upcoming Events
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-sneha-s committed Mar 13, 2024
1 parent 104e0da commit 0513d0a
Show file tree
Hide file tree
Showing 17 changed files with 813 additions and 8 deletions.
11 changes: 10 additions & 1 deletion lib/data/core/extensions/date_formatter.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:projectunity/data/core/extensions/context_extension.dart';

extension DateExtension on DateTime {
String toDate() {
String toDateWithYear() {
return DateFormat("dd MMMM, yyyy").format(this);
}

String toMonthYear() {
return DateFormat("MMMM, yyyy").format(this);
}
String toDateWithoutYear(BuildContext context) {
final today= DateUtils.dateOnly(DateTime.now());
if(isAtSameMomentAs(today)){
return context.l10n.dateFormatter_today;
}
return DateFormat("MMMM d, EEE").format(this);
}
}
18 changes: 18 additions & 0 deletions lib/data/core/extensions/date_time.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ 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 isBeforeOrSame(DateTime date) =>
isBefore(date) || isAtSameMomentAs(date);

Expand All @@ -26,6 +34,16 @@ extension TimestampExtension on DateTime {

DateTime get dateOnly => DateUtils.dateOnly(this);

DateTime convertToUpcomingDay() {
DateTime now = DateUtils.dateOnly(DateTime.now());
DateTime targetDate = DateUtils.dateOnly(DateTime(now.year, month, day));
if (targetDate.areSameOrUpcoming(now)) {
return targetDate;
} else {
return DateUtils.dateOnly(DateTime(now.year + 1, month, day));
}
}

bool get isWeekend =>
weekday == DateTime.sunday || weekday == DateTime.saturday;

Expand Down
18 changes: 18 additions & 0 deletions lib/data/core/utils/date_formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@ class DateFormatter {
}
}

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)}🎉";
}
}

String showAnniversaries({required DateTime dateTime,required String name, int? number}){
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)}🎉";
}
}

String timeAgoPresentation(DateTime date) {
Duration difference = today.difference(date);
if (difference.inDays > 365) {
Expand Down
12 changes: 8 additions & 4 deletions lib/data/di/service_locator.config.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions lib/data/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"all_tag": "All",
"ok_tag": "OK",
"profile_tag": "Profile",
"view_all_tag":"View All",


"spaces_title": "Spaces",
Expand Down Expand Up @@ -254,6 +255,52 @@
"admin_home_add_member_blood_group_hint_text": "B+",
"admin_home_add_member_address_hint_text": "552_554, Laxmi Enclave-2, Katargam, surat-395004",

"event_card_title":"Celebrations of the Week",
"birthdays_tag":"Birthdays",
"work_anniversaries_tag":"Work Anniversary",
"no_event_text":"No festivities on the calendar this week",
"upcoming_birthday_text":"{name}'s birthday is coming up on ",
"@upcoming_birthday_text":{
"placeholders":{
"name":{
"type":"String"
}
}
},
"present_birthday_text":"Today is {name}'s birthday 🥳🥳! Let's make it sparkle with cheerful wishes! ",
"@present_birthday_text":{
"placeholders":{
"name":{
"type":"String"
}
}
},

"upcoming_anniversary_text":"{name} will have successfully completed {years} years with us on ",
"@upcoming_anniversary_text":{
"placeholders":{
"name":{
"type":"String"
},
"years":{
"type":"int"
}
}
},
"present_anniversary_text":"Congratulations on {years} with us {name}! Keep up the good work! ",
"@present_anniversary_text":{
"placeholders":{
"name":{
"type":"String"
},
"years":{
"type":"int"
}
}
},



"search_employee_tag": "Search employee",
"empty_leaves_message": "There is no any leaves, yet",

Expand Down
3 changes: 2 additions & 1 deletion lib/data/model/employee/employee.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions lib/gen/assets.gen.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions lib/ui/admin/home/home_screen/admin_home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import 'package:projectunity/data/core/extensions/context_extension.dart';
import 'package:projectunity/data/core/utils/bloc_status.dart';
import 'package:projectunity/style/app_page.dart';
import 'package:projectunity/ui/admin/home/home_screen/widget/request_list.dart';
import 'package:projectunity/ui/shared/events/bloc/celebrations_bloc.dart';
import 'package:projectunity/ui/shared/events/bloc/celebrations_event.dart';
import 'package:projectunity/ui/shared/who_is_out_card/bloc/who_is_out_card_event.dart';
import '../../../../data/di/service_locator.dart';
import '../../../../data/provider/user_state.dart';
import '../../../../style/app_text_style.dart';
import '../../../shared/appbar_drawer/appbar/space_notifier_widget.dart';
import '../../../shared/appbar_drawer/drawer/bloc/app_drawer_bloc.dart';
import '../../../shared/appbar_drawer/drawer/bloc/app_drawer_event.dart';
import '../../../shared/events/celebrations_event_card.dart';
import '../../../shared/who_is_out_card/bloc/who_is_out_card_bloc.dart';
import '../../../shared/who_is_out_card/who_is_out_card.dart';
import '../../../widget/circular_progress_indicator.dart';
Expand All @@ -35,6 +38,9 @@ class AdminHomeScreenPage extends StatelessWidget {
BlocProvider(
create: (context) =>
getIt<WhoIsOutCardBloc>()..add(FetchWhoIsOutCardLeaves())),
BlocProvider(
create: (context) => getIt<CelebrationsBloc>()
..add(FetchCelebrations(DateTime.now()))),
],
child: const AdminHomeScreen(),
);
Expand Down Expand Up @@ -77,6 +83,8 @@ class _AdminHomeScreenState extends State<AdminHomeScreen> {
body: ListView(
children: [
const WhoIsOutCard(),
const EventCard(),

const SizedBox(
height: 20,
),
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/admin/home/home_screen/widget/request_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class LeaveRequestList extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
DateFormatter(AppLocalizations.of(context))
DateFormatter(context.l10n)
.getDateRepresentation(mapEntry.key),
style: AppTextStyle.style20.copyWith(
color: context.colorScheme.textPrimary,
Expand Down
106 changes: 106 additions & 0 deletions lib/ui/shared/events/bloc/celebrations_bloc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:injectable/injectable.dart';
import 'package:projectunity/data/core/exception/error_const.dart';
import 'package:projectunity/data/core/extensions/date_time.dart';
import 'package:projectunity/data/model/employee/employee.dart';
import 'package:projectunity/ui/shared/events/bloc/celebrations_state.dart';

import '../../../../data/core/utils/bloc_status.dart';
import '../../../../data/repo/employee_repo.dart';
import '../model/event.dart';
import 'celebrations_event.dart';

@Injectable()
class CelebrationsBloc extends Bloc<CelebrationEvent, CelebrationsState> {
final EmployeeRepo _employeeRepo;
List<Employee> employees = [];
List<Event> allBirthdayEvents = [];
List<Event> allAnniversaryEvents = [];
List<Event> currentWeekBday=[];
List<Event> currentWeekAnniversaries=[];

CelebrationsBloc(this._employeeRepo)
: super(CelebrationsState(currentWeek: DateTime.now())) {
on<FetchCelebrations>(_fetchEvent);
on<ShowBirthdaysEvent>(_showAllBirthdays);
on<ShowAnniversariesEvent>(_showAllAnniversaries);
}

Future<void> _fetchEvent(FetchCelebrations event,
Emitter<CelebrationsState> emit) async {
try {
emit(state.copyWith(status: Status.loading));
employees = _employeeRepo.allEmployees
.where((employee) => employee.status == EmployeeStatus.active)
.toList();
employees = employees.map((e) {
if (e.dateOfBirth != null) {
final birthdate = e.dateOfBirth!.convertToUpcomingDay();
final Event event =
Event(name: e.name, dateTime: birthdate, imageUrl: e.imageUrl);
allBirthdayEvents.add(event);
}
final joiningDate = e.dateOfJoining.convertToUpcomingDay();
final Event event =
Event(name: e.name, dateTime: joiningDate, imageUrl: e.imageUrl);
allAnniversaryEvents.add(event);
return e;
}).toList();
allBirthdayEvents.sort((a, b) => a.dateTime.compareTo(b.dateTime));
allAnniversaryEvents.sort((a, b) => a.dateTime.compareTo(b.dateTime));


currentWeekBday = _getBirthdays();
currentWeekAnniversaries = _getAnniversaries();
emit(state
.copyWith(status: Status.success, birthdays: currentWeekBday,
anniversaries:currentWeekAnniversaries));
} on Exception {
emit(
state.copyWith(status: Status.error, error: firestoreFetchDataError));
}
}

void _showAllBirthdays(ShowBirthdaysEvent event,
Emitter<CelebrationsState> emit) {
bool showAllBirthdays = !state.showAllBdays;
if (showAllBirthdays) {
emit(state.copyWith(
showAllBdays: showAllBirthdays, birthdays: allBirthdayEvents));
} else {
emit(state.copyWith(
showAllBdays: showAllBirthdays, birthdays: currentWeekBday));
}
}

void _showAllAnniversaries(ShowAnniversariesEvent event,
Emitter<CelebrationsState> emit) {
bool allAnniversaries = !state.showAllAnniversaries;
if (allAnniversaries) {
emit(state.copyWith(
showAllAnniversaries: allAnniversaries,
anniversaries: allAnniversaryEvents));
} else {
emit(state.copyWith(
showAllAnniversaries: allAnniversaries,
anniversaries: currentWeekAnniversaries));
}
}

List<Event> _getBirthdays() {
final List<Event> birthdays = allBirthdayEvents.where((event) {
return event.dateTime.isDateInCurrentWeek(
DateUtils.dateOnly(DateTime.now()));
}).toList();
return birthdays;
}

List<Event> _getAnniversaries() {
final List<Event> anniversaries = allAnniversaryEvents.where((event) {
return event.dateTime.isDateInCurrentWeek(
DateUtils.dateOnly(DateTime.now()));
}).toList();
return anniversaries;
}
}
9 changes: 9 additions & 0 deletions lib/ui/shared/events/bloc/celebrations_event.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
abstract class CelebrationEvent{}

class FetchCelebrations extends CelebrationEvent{
final DateTime dateTime;
FetchCelebrations( this.dateTime);
}

class ShowBirthdaysEvent extends CelebrationEvent{}
class ShowAnniversariesEvent extends CelebrationEvent{}
19 changes: 19 additions & 0 deletions lib/ui/shared/events/bloc/celebrations_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:freezed_annotation/freezed_annotation.dart';

import '../../../../data/core/utils/bloc_status.dart';
import '../../../../data/model/employee/employee.dart';
import '../model/event.dart';
part 'celebrations_state.freezed.dart';

@freezed
class CelebrationsState with _$CelebrationsState{
const factory CelebrationsState({
@Default(Status.initial) Status status,
required DateTime currentWeek,
@Default(false) bool showAllBdays,
@Default(false) bool showAllAnniversaries,
@Default([]) List<Event> birthdays,
@Default([]) List<Event> anniversaries,
String? error
})= _CelebrationsState;
}
Loading

0 comments on commit 0513d0a

Please sign in to comment.