-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
104e0da
commit 0513d0a
Showing
17 changed files
with
813 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.