From 4dc5e4602419e8db5981bd7e38d18111c980697f Mon Sep 17 00:00:00 2001 From: ntut-xuan Date: Mon, 12 Feb 2024 17:20:10 +0800 Subject: [PATCH] Chore: Optimize huge function chain when Pre-build member --- lib/src/model/coursetable/course_table.dart | 10 +++++++++ .../coursetable/course_table_control.dart | 22 ++++++------------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/lib/src/model/coursetable/course_table.dart b/lib/src/model/coursetable/course_table.dart index ccfb5b96..7ebf3195 100644 --- a/lib/src/model/coursetable/course_table.dart +++ b/lib/src/model/coursetable/course_table.dart @@ -11,14 +11,24 @@ class CourseTable { int semester; List courses; User user; + late final Set weekdays = {}; + late final Set 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 json) => _$CourseTableFromJson(json); Map toJson() => _$CourseTableToJson(this); } diff --git a/lib/ui/pages/coursetable/course_table_control.dart b/lib/ui/pages/coursetable/course_table_control.dart index cc02333c..53347384 100644 --- a/lib/ui/pages/coursetable/course_table_control.dart +++ b/lib/ui/pages/coursetable/course_table_control.dart @@ -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;