renderCourseTable method

List<Widget> renderCourseTable()

Implementation

List<Widget> renderCourseTable() {
  final List<TimeCode> timeCodes = widget.courseData.timeCodes;
  final int maxTimeCode = widget.courseData.maxTimeCodeIndex;
  final int minTimeCode = widget.courseData.minTimeCodeIndex;
  final bool hasHoliday = widget.courseData.hasHoliday;
  final List<Column> columns = <Column>[
    //TODO 可讀性改善
    //ignore: prefer_const_constructors
    Column(
      //ignore: prefer_const_literals_to_create_immutables
      children: <Widget>[],
    ),
  ];
  columns[0].children.add(
        _weekBorder(''),
      );
  for (int i = minTimeCode; i < maxTimeCode + 1; i++) {
    columns[0].children.add(
          TimeCodeBorder(
            timeCode: timeCodes[i],
            hasHoliday: hasHoliday,
          ),
        );
  }
  final List<List<CourseBorder>> courseBorderCollection =
      <List<CourseBorder>>[
    <CourseBorder>[],
    <CourseBorder>[],
    <CourseBorder>[],
    <CourseBorder>[],
    <CourseBorder>[],
    if (hasHoliday) ...<List<CourseBorder>>[
      <CourseBorder>[],
      <CourseBorder>[],
    ],
  ];
  for (int i = 0; i < courseBorderCollection.length; i++) {
    for (int j = 0; j < maxTimeCode - minTimeCode + 1; j++) {
      courseBorderCollection[i].add(
        const CourseBorder(),
      );
    }
  }
  for (int i = 0; i < widget.courseData.courses.length; i++) {
    final Course course = widget.courseData.courses[i];
    if (invisibleCourseCodes.contains(course.code)) continue;
    for (int j = 0; j < (course.times.length); j++) {
      final SectionTime time = course.times[j];
      final int timeCodeIndex = time.index;
      final int courseBorderIndex = timeCodeIndex - minTimeCode;
      final int len = ApColors.colors.length;
      final ui.Color? color =
          ApColors.colors[i % len][300 + 100 * (i ~/ len)];
      courseBorderCollection[time.weekDayIndex][courseBorderIndex] =
          CourseBorder(
        sectionTime: time,
        timeCode: widget.courseData.timeCodes[timeCodeIndex],
        course: course,
        color: color,
        onPressed: _onPressed,
      );
    }
  }
  for (int i = 0; i < courseBorderCollection.length; i++) {
    final List<CourseBorder> courseBorders = courseBorderCollection[i];
    for (int j = 0; j < courseBorders.length; j++) {
      int repeat = 0;
      if (courseBorders[j].course != null) {
        for (int k = j + 1; k < courseBorders.length; k++) {
          if (courseBorders[k].course != null &&
              courseBorders[j].course!.title ==
                  courseBorders[k].course!.title) {
            repeat++;
          } else {
            break;
          }
        }
      }
      if (repeat != 0) {
        courseBorders[j] = CourseBorder(
          timeCode: courseBorders[j].timeCode!.copyWith(
                endTime: courseBorders[j + repeat].timeCode!.endTime,
              ),
          sectionTime: courseBorders[j].sectionTime,
          course: courseBorders[j].course,
          height: _courseHeight * (repeat + 1),
          color: courseBorders[j].color,
          border: (j + repeat > courseBorders.length)
              ? Border(
                  left: _innerBorderSide,
                  right: (i == courseBorderCollection.length - 1)
                      ? BorderSide.none
                      : _innerBorderSide,
                  top: _innerBorderSide,
                )
              : null,
          onPressed: _onPressed,
        );
        for (int k = j + 1; k < j + repeat + 1; k++) {
          courseBorders[k] = CourseBorder(
            course: courseBorders[k].course,
            height: 0.0,
            width: 0.0,
          );
        }
        j += repeat;
        repeat = 0;
      } else if (j == courseBorders.length - 1) {
        courseBorders[j] = CourseBorder(
          course: courseBorders[j].course,
          color: courseBorders[j].color,
          border: Border(
            left: _innerBorderSide,
            right: _innerBorderSide,
            top: _innerBorderSide,
            bottom: _innerBorderSide,
          ),
          onPressed: _onPressed,
          sectionTime: courseBorders[j].sectionTime,
          timeCode: courseBorders[j].timeCode,
        );
      }
    }
    columns.add(
      Column(
        children: <Widget>[
          _weekBorder(app.weekdaysCourse[i]),
          ...courseBorderCollection[i],
        ],
      ),
    );
  }
  return <Widget>[
    columns[0],
    for (int i = 1; i < columns.length; i++)
      Expanded(
        child: columns[i],
      ),
  ];
}