Skip to content

Commit

Permalink
Chore: Optimize huge function chain when Pre-build member
Browse files Browse the repository at this point in the history
  • Loading branch information
ntut-xuan committed Feb 12, 2024
1 parent b4b19a3 commit 4dc5e46
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
10 changes: 10 additions & 0 deletions lib/src/model/coursetable/course_table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,24 @@ class CourseTable {
int semester;
List<Course> courses;
User user;
late final Set<int> weekdays = {};
late final Set<String> periods = {};

CourseTable({required this.year, required this.semester, required this.courses, required this.user}) {
year = year;
semester = semester;
courses = courses;
user = user;
weekdays.addAll(courses.map((course) => course.coursePeriods.map((coursePeriod) => coursePeriod.weekday))
.expand((element) => element));
periods.addAll(courses.map((course) => course.coursePeriods.map((coursePeriod) => coursePeriod.period))
.expand((element) => element));
}

bool isPeriodInCourseTable(String period) => periods.contains(period);

bool isWeekdayInCourseTable(int weekday) => weekdays.contains(weekday);

factory CourseTable.fromJson(Map<String, dynamic> json) => _$CourseTableFromJson(json);
Map<String, dynamic> toJson() => _$CourseTableToJson(this);
}
22 changes: 7 additions & 15 deletions lib/ui/pages/coursetable/course_table_control.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,13 @@ class CourseTableControl {

void set(CourseTable value) {
courseTable = value;
isHideSaturday =
!courseTable.courses.any((course) => course.coursePeriods.any((coursePeriod) => coursePeriod.weekday == 6));
isHideSunday =
!courseTable.courses.any((course) => course.coursePeriods.any((coursePeriod) => coursePeriod.weekday == 0));
isHideUnKnown = !courseTable.courses.any((course) => course.coursePeriods.isEmpty);
isHideN =
!courseTable.courses.any((course) => course.coursePeriods.any((coursePeriod) => coursePeriod.period == "N"));
isHideA =
!courseTable.courses.any((course) => course.coursePeriods.any((coursePeriod) => coursePeriod.period == "A"));
isHideB =
!courseTable.courses.any((course) => course.coursePeriods.any((coursePeriod) => coursePeriod.period == "B"));
isHideC =
!courseTable.courses.any((course) => course.coursePeriods.any((coursePeriod) => coursePeriod.period == "C"));
isHideD =
!courseTable.courses.any((course) => course.coursePeriods.any((coursePeriod) => coursePeriod.period == "D"));
isHideSaturday = !courseTable.isWeekdayInCourseTable(6);
isHideSunday = !courseTable.isWeekdayInCourseTable(0);
isHideN = !courseTable.isPeriodInCourseTable("N");
isHideA = !courseTable.isPeriodInCourseTable("A");
isHideB = !courseTable.isPeriodInCourseTable("B");
isHideC = !courseTable.isPeriodInCourseTable("C");
isHideD = !courseTable.isPeriodInCourseTable("D");
isHideA &= (isHideB & isHideC & isHideD);
isHideB &= (isHideC & isHideD);
isHideC &= isHideD;
Expand Down

0 comments on commit 4dc5e46

Please sign in to comment.